/*
 *
 * Кастомный шейк - с затуханием - нужен для события "перемещения в корзину"
 *
 */
(function( $, undefined ) {

$.effects.c_shake = function(o) {

	return this.queue(function() {

		// Create element
		var el = $(this), props = ['position','top','bottom','left','right'];

		// Set options
		var mode = $.effects.setMode(el, o.options.mode || 'effect'); // Set Mode
		var direction = o.options.direction || 'left'; // Default direction
		var distance = o.options.distance || 20; // Default distance
		var times = o.options.times || 3; // Default # of times
		var speed = o.duration || o.options.duration || 140; // Default speed per shake

		// Adjust
		$.effects.save(el, props); el.show(); // Save & Show
		$.effects.createWrapper(el); // Create Wrapper
		var ref = (direction == 'up' || direction == 'down') ? 'top' : 'left';
		var motion = (direction == 'up' || direction == 'left') ? 'pos' : 'neg';

		// Animation
		var animation = {}, animation1 = {}, animation2 = {};
		animation[ref] = (motion == 'pos' ? '-=' : '+=')  + distance;
		animation1[ref] = (motion == 'pos' ? '+=' : '-=')  + distance * 2;
		animation2[ref] = (motion == 'pos' ? '-=' : '+=')  + distance * 2;

		// Animate
		/*el.animate(animation, speed, 'easeOutQuart');
		for (var i = 1; i < times; i++) { // Shakes
			el.animate(animation1, speed, 'easeOutQuart').animate(animation2, speed, 'easeOutQuart');
		};
		el.animate(animation1, speed, 'easeOutQuart' ).
		animate(animation, speed / 2, 'easeOutQuart', function(){ // Last shake
			$.effects.restore(el, props); $.effects.removeWrapper(el); // Restore
			if(o.callback) o.callback.apply(this, arguments); // Callback
		});*/


        el.find('a').css({ color:'#E41B49'}); //подсвечиваем ссылку "Ваша корзина"
        
		el.animate({ left: '+='+(1*distance)}, speed, 'linear');
		el.animate({ left: '-='+(2*distance)}, speed, 'linear');
		el.animate({ left: '+='+(1.75*distance)}, speed, 'linear');
		el.animate({ left: '-='+(1.25*distance)}, speed, 'linear');
		el.animate({ left: '+='+(0.75*distance)}, speed, 'linear');
		el.animate({ left: '-='+(0.25*distance)}, speed, 'linear',
            function(){
                el.find('a').animate({ color:'#4D4D4D' }); //тушим ссылку "Ваша корзина"
            }
        );

		el.queue('fx', function() { el.dequeue(); });
		el.dequeue();
	});

};

})(jQuery);



/*
 *
 * Кастомный трансфер - сместе с содержимым - нужен для события "перемещения в корзину"
 *
 */
(function( $, undefined ) {

$.effects.c_transfer = function(o) {
	return this.queue(function() {
		var elem = $(this),
			target = $(o.options.to),
			endPosition = target.offset(),
			animation = {
				top: endPosition.top,
				left: endPosition.left,
				height: elem.innerHeight()/2,
				width: elem.innerWidth()/2
			},
			startPosition = elem.offset(),
			//transfer = $('<div class="ui-effects-transfer">'+elem.parent().html()+'</div>')
			transfer = $('<img src="'+elem.attr('src')+'" alt="" style="box-shadow: 1px 8px 20px #444444; border-radius: 3px 3px 3px 3px;" />')
				.appendTo(document.body)
				.addClass(o.options.className)
				.css({
					top: startPosition.top,
					left: startPosition.left,
					height: elem.innerHeight(),
					width: elem.innerWidth(),
					position: 'absolute'
				})
				.animate(animation, o.duration, o.options.easing, function() {
					transfer.remove();
					(o.callback && o.callback.apply(elem[0], arguments));
					elem.dequeue();
				});
	});
};

})(jQuery);

/*
 *
 * эффект перемещения в корзину
 *
 */
(function( $, undefined ) {
        $.effects.jump_to_basket = function(o) {

                $(this).effect( 'c_transfer', o.options, 1000, function(){

                        $( o.options.shake_container ).effect( 'c_shake', {}, 100 );

                });

            	return this;
        };
})(jQuery);




/*
 *
 * эффект накручиваничя счётчика
 *
 */
(function($) {
        // color - свойство с которым мы будем работать
        $.fx.step.value_to = function(fx){
            if (fx.state == 0 ) {
                // получаем текущее значение элемента
                //console.log(fx.elem);

                fx.start = parseInt( $(fx.elem).html() );
                fx.start = (!fx.start ? 0 : fx.start );
                //console.log(fx.start);
            }

            // настройки анимации
            fx.options;

            // свойство которое изменяем
            fx.prop;

            // начальные значения
            fx.start;

            // результирующие значения
            fx.end;

            // коэффициент, изменяется от 0 до 1
            fx.pos;

            // еденицы измерения
            fx.unit;

            fx.start = parseInt(fx.start);
            fx.end = parseInt(fx.end);
            $(fx.elem).html(parseInt( fx.start + (fx.end-fx.start)*fx.pos ));

            //console.log(fx.options, fx.prop, fx.start, fx.end, fx.pos );
            //console.log( fx.pos );

        };
})(jQuery);

(function($) {
        // color - свойство с которым мы будем работать
        $.fx.step.value_to_with_cent = function(fx){
            if (fx.state == 0 ) {

                fx.start = parseInt( $(fx.elem).html() );
                fx.start = (!fx.start ? 0 : fx.start );
            }

            fx.start = parseInt(fx.start);
            fx.end = parseInt(fx.end);
            $(fx.elem).html(parseInt( fx.start + (fx.end-fx.start)*fx.pos )+'.&ndash;');

        };
})(jQuery);

