/**
 * @author Sergey Chikuyonok (sc@design.ru)
 * @copyright Art.Lebedev Studio (http://www.artlebedev.ru)
 * @include "/rit-demo/js/excavator.js"
 * @include "/rit-demo/js/jTweener.js"
 */

$(function(){
	var ns = 'excavator';

	jTweener.addNSAction({
		onUpdate: function(){
			excavator.draw();
		}
	}, ns);

	/**
	 * Приезжаем экскаватор
	 */
	function getIn(callback){
		jTweener.addTween($('#excavator'), {
			left: 0,
			time: 3,
			transition: 'easeoutcubic',
			onComplete: callback,
			delay: 1
		});
	}

	/**
	 * Хватаем землю
	 */
	function grab(callback){
		jTweener.addTween(excavator, {
			namespace: ns,
			rotateHand1: 10,
			rotateHand2: 5,
			rotateHand3: 5,
			rotateBucket: 15,
			time: 1,
			transition: 'easeinoutcubic',
			delay: 1
		});

		jTweener.addTween(excavator, {
			namespace: ns,
			rotateHand1: -45,
			rotateHand2: -25,
			rotateHand3: -30,
			rotateBucket: 10,
			time: 0.6,
			transition: 'easeinexpo',
			delay: 2.02
		});

		jTweener.addTween(excavator, {
			namespace: ns,
			rotateHand1: -47,
			rotateHand2: -30,
			rotateHand3: -32,
			rotateBucket: 5,
			time: 0.6,
			transition: 'easeoutelastic',
			delay: 2.65,
			onComplete: callback
		});

	}

	/**
	 * Тащим землю
	 */
	function pull(callback){
		var h1 = 70, h2 = 50, h3 = 60, b = 25;
		var t = 0.7;
		jTweener.addTween(excavator, {
			namespace: ns,
			rotateHand1: '+=' + h1,
			rotateHand2: '-=' + h2,
			rotateHand3: '-=' + h3,
			rotateBucket: '+=' + b,
			time: t,
			transition: 'easeinoutcubic'
		});

		jTweener.addTween($('#dirt'), {
			left: '+=170',
			time: t,
			transition: 'easeinoutcubic'
		});

		jTweener.addTween(excavator, {
			namespace: ns,
			rotateHand1: '-=' + h1,
			rotateHand2: '+=' + h2,
			rotateHand3: '+=' + h3,
			rotateBucket: '-=' + b,
			time: t,
			transition: 'easeinoutcubic',
			delay: t
		});

		jTweener.addTween($('#excavator'), {
			left: '+=170',
			time: t,
			transition: 'easeinoutcubic',
			delay: t,
			onComplete: callback
		});

	}

	/**
	 * Уезжаем экскаватор
	 */
	function getOut(){
		if ($('#dirt')[0].offsetLeft < $(window).width()) {
			pull(getOut);
		}
	}

	$(function(){
		var ex = $('#excavator');
		ex.css({
			left: ex[0].offsetLeft
		});

		getIn(function(){
			grab(getOut);
		})
	});


});
