$(document).ready(function(){
	$('.add-cart .button').click(function(){
		qty = $('#product-quantity').attr('value');
		if (isNaN(qty) || qty==0 || qty==''){
			alert('Please enter number for Quantity.');
			$('#product-quantity').focus();
			return false;
		}
		id = $('#product-id').attr('value');
		if (isNaN(id)){
			alert('Unable to define product item.');
			return false;
		}
		product_options = new Array();
		$("input[@type='radio']:checked").each(function(){
			product_options.push(this.value);
		})
		$("input[@type='checkbox']:checked").each(function(){
			product_options.push(this.value);
		})
		doPageLoading();
		$.ajax({
			type: "POST",
			url: "/php2xml/addItemToCart.php",
			data: "product_id="+id+"["+product_options.toString()+"]&qty="+qty,
			success: function(msg){
				endPageLoading();
				alert('Item has been added.');
				$('#cart-info').attr('innerHTML',msg);
			}
		});
	});
	
	if ($("#product-image-1") && $("#product-image-2")){
		var tmpSizeScale = 0.30;
		var pic1 = ({'width':$("#product-image-1 img").width(),
					'height':$("#product-image-1 img").height()});
		var pic2 = ({'width':$("#product-image-2 img").width(),
					'height':$("#product-image-2 img").height()});
		$("#product-image-1").css({'z-index':10});
		$("#product-image-2").css({'z-index':11,
								  'left': Math.round(pic1.width - pic2.width*tmpSizeScale + 5)+'px',
								  'top': Math.round(pic1.height - pic2.height*tmpSizeScale + 5)+'px'
								  });
		$("#product-image-2 img").attr({
								  'width': Math.round(pic2.width*tmpSizeScale),
								  'height': Math.round(pic2.height*tmpSizeScale)
								  });
		image1Click = function(){
			$("#product-image-1").unbind("click").bind("click",image2Click);
			$("#product-image-2").unbind("click").bind("click",image2Click);
			$("#product-image-1").css({'z-index':10});
			$("#product-image-2").css({'z-index':11});
			$("#product-image-1 img").animate({
										  'width': pic1.width,
										  'height':pic1.height
											  });
			$("#product-image-1").animate({
										  'z-index':10,
										  'left': '0px',
										  'top': '0px'
										  });
			$("#product-image-2").animate({'z-index':11,
									  'left': Math.round(pic1.width - pic2.width*tmpSizeScale + 5)+'px',
									  'top': Math.round(pic1.height - pic2.height*tmpSizeScale + 15)+'px'
									  });
			$("#product-image-2 img").animate({
									  'width': Math.round(pic2.width*tmpSizeScale),
									  'height': Math.round(pic2.height*tmpSizeScale)
									  });
		}
		image2Click = function(){
			$("#product-image-1").unbind("click").bind("click",image1Click);
			$("#product-image-2").unbind("click").bind("click",image1Click);
			$("#product-image-1").css({'z-index':11});
			$("#product-image-2").css({'z-index':10});
			$("#product-image-2 img").animate({
										  'width': pic2.width,
										  'height':pic2.height
											  });
			$("#product-image-2").animate({
										  'left': '0px',
										  'top': '0px'
										  });
			
			$("#product-image-1").animate({
									  'left': Math.round(pic1.width - pic2.width*tmpSizeScale + 5)+'px',
									  'top': Math.round(pic1.height - pic2.height*tmpSizeScale + 15)+'px'
									  });
			$("#product-image-1 img").animate({
									  'width': Math.round(pic2.width*tmpSizeScale),
									  'height': Math.round(pic2.height*tmpSizeScale)
									  });
		}
		$("#product-image-1").bind("click",image2Click);
		$("#product-image-2").bind("click",image2Click);
		$("#product-info-image").css({'height': ((pic2.height > pic1.height ? pic2.height : pic1.height)+25)+"px"});
	}
})