$(document).ready(function(){	$(document).click(function(){		cart.hideCountForm();	});	$('#countForm').click(function(){		return false;	});	$('.addToCart').click(function(){  		cart.showCountForm(this);		return false;	});	cart.setDeleteEvents();});var cart = {	clickedItem: null,	moveToPoint: null,	flyingItem: null,	maxDistance: 15,	flyMs: 10,	showCountForm: function (item) {		this.clickedItem = $('#'+$(item).attr('rel')).get(0);		//show or hide color selector		if ($(this.clickedItem).find('select').get(0)) {			$('#priceCont').html($(this.clickedItem).find('td.price h2').html());			$('#colorSelectCont').html('');			var colorSelect = $(this.clickedItem).find('select').clone();			colorSelect.change(function(){				var price = $(cart.clickedItem).find('td.price span.num').get(this.selectedIndex+1); 				$('#priceCont').html(					'<span class="num">'+					$(price).html()+					'</span>&nbsp;<span>'+					$(this.clickedItem).find('td.price span.unit').html()+					'</span>'				);			});			var price = $(this.clickedItem).find('td.price span.num').get(1);			$('#priceCont').html(				'<span class="num">'+				$(price).html()+				'</span>&nbsp;<span>'+				$(this.clickedItem).find('td.price span.unit').html()+				'</span>'			);			$('#colorSelectCont').append(colorSelect.get(0));        	$('#colorSelect').show();		} else {			$('#priceCont').html(				'<span class="num">'+				$(this.clickedItem).find('td.price span.num').html()+				'</span>&nbsp;<span>'+				$(this.clickedItem).find('td.price span.unit').html()+				'</span>'			);        	$('#colorSelectCont').html('');        	$('#colorSelect').hide();		}		$('#countForm').css('left',$(item).offset().left+$(item).width()-$('#countForm').width()+'px');		$('#countForm').css('top',$(item).offset().top+'px');		$('#countField').attr('value',1);	   	$('#countForm').show();	},	hideCountForm: function () {		this.clickedItem = null;	   	$('#countForm').hide();	},	moveToCart: function () {		$('#countForm').hide();    	this.moveToPoint = {        	left: $('#order').offset().left + $('#order').width() - 45,        	top: $('#order').offset().top    	}    	this.flyingItem = document.createElement('IMG');    	this.flyingItem.style.border = '1px solid';    	$('body').append(this.flyingItem);    	this.flyingItem.src = $(this.clickedItem).find('img.mainImage').attr('src');    	this.flyingItem.style.position = 'absolute';    	this.flyingItem.style.left = $(this.clickedItem).find('img.mainImage').offset().left + 'px';     	this.flyingItem.style.top = $(this.clickedItem).find('img.mainImage').offset().top + 'px';     	this.flyInterval = setInterval(this.moveToCartIteration,this.flyMs);	},	moveToCartIteration: function () {    	var flyX = cart.moveToPoint.left - $(cart.flyingItem).offset().left;    	var flyY = cart.moveToPoint.top - $(cart.flyingItem).offset().top;    	var flyRatio = Math.abs(flyX/flyY);    	var stepX, stepY;    	if (flyRatio > 1) {        	stepX = cart.maxDistance;        	stepY = stepX / flyRatio;    	} else {        	stepY = cart.maxDistance;        	stepX = stepY * flyRatio;    	}    	if (Math.abs(flyX) >= cart.maxDistance && Math.abs(flyY) >= cart.maxDistance) {	    	stepX = stepX * (flyX > 0 ? 1 : -1);	    	stepY = stepY * (flyY > 0 ? 1 : -1);	        $(cart.flyingItem).css('left',$(cart.flyingItem).offset().left + stepX);	        $(cart.flyingItem).css('top',$(cart.flyingItem).offset().top + stepY);	  	} else {        	$(cart.flyingItem).css('left',cart.moveToPoint.left);	        $(cart.flyingItem).css('top',cart.moveToPoint.top);	        clearInterval(cart.flyInterval);	        $(cart.flyingItem).remove();	        var itemCount = parseInt($('#countField').attr('value'));			if (isNaN(itemCount)) itemCount = 1;			if (itemCount == 0) itemCount = 1;			if (itemCount < 0) itemCount = -itemCount;	        $.post(            	'/prAddToCart.php',            	{            		action: 'add',            		item_id: cart.clickedItem.id,            		color_id: $('#colorSelectCont select').get(0) ? $('#colorSelectCont select').attr('value') : 0,            		count: itemCount            	},            	function(data){                	$('#order').html(data);                	cart.setDeleteEvents();            	},            	'text'	        );	  	}	},	setDeleteEvents: function() {    	$('.deleteFromOrder').click(function(){			$.post(	            '/prAddToCart.php',	            {	            	action: 'delete',	            	item_id: $(this).attr('rel')	            },	            function(data){	               	$('#order').html(data);	               	cart.setDeleteEvents();	            },	            'text'		    );		});	}}