/*
ImageThumb ->  Animation Start
CartCover ->  Animation End	
*/

(function( $ ){
 	$.fn.AddProduct2 = function(options) {
		var settings = {
			add2 : '',
			ImageThumb : '',
			CartCover : 'ul.cart-navi',
			Type : 'Catalogue'
		};


		if ( options ) { 
			$.extend( settings, options );
		};
		
		// add to cart catalogue by press enter
		$(".count input").live("keydown", function(e) {
			var code = (e.keyCode ? e.keyCode : e.which);
			
			if (code == 13) {
			//	alert(code);
			//	var thisValue = this;
			//	CatalogueEnter(thisValue);
				return false;
			};
			
		});
		
		$(this).live("click", function() {
			var thisValue = this;
			AddToCart(settings.Type, thisValue);
			return false;
		});

		
		/*FUNCTIONS*/
		
		function AddToCart(options, thisValue) {
			var animation = true;
			if (options == "Catalogue") {
				animation = Catalogue(thisValue);
			};
			if (options == "UserSelection") {
				Selection(thisValue);
			};
			if (options == "Product") {
				Product(thisValue);
			};
			
			if (animation) {
				RunAnimation(thisValue);
			};
			
			
		};
		
		// animation
		function RunAnimation(object) {
			// Product image selection
			var img = false;
			if (settings.Type == 'Catalogue') {
				var img = $(object).parent().parent().parent().find("img", settings.ImageThumb);
			};
			if (settings.Type == 'UserSelection') {
				var img = $(object).parent().parent().find("img", settings.ImageThumb);
			};
			if (settings.Type == 'Product') {
				var img = $("img", settings.ImageThumb);
			};
			if (img == false) {
				alert("failed to load product img");
			};
			// Image position
			var imgPostion = img.offset();
			// Image Source
			var imageSrc = img.attr("src");
			// Creating a copy of Product Image 
			img.before('<img src="' + imageSrc+ '" id="temp" style="position: absolute; top: ' + imgPostion.top + 'px; left: ' + imgPostion.left + 'px;" />');
			// Cart Cover Position
			var cart  = $(settings.CartCover).offset();
			// Cart Params
			params = {
						top : cart.top + 'px',
						left : cart.left + 'px',
						opacity : 0.0,
						width : $(settings.CartCover).width(),  
						height : $(settings.CartCover).height()
					};
			// Animation it self
			$('#temp').animate(params, 'slow', false, function () {
				$('#temp').remove();
			});	
			
			
		};
		
		function  Catalogue(thisValue) {
			var pro_id = $(thisValue).attr("id");
			var pcf = "pcf_"+pro_id;
			
			if ($(thisValue).hasClass('sestava')) {
				var url = $(thisValue).attr("name");
				window.location.href = url;
				return false;
			}
			else{
				var count = $("#proId"+pro_id).find("#"+pcf).val();
				if (count == undefined) {count = 1};
				var data = 'action=addtocart&'+pcf+'='+count;


				$.ajax({
				  type: 'POST',
				  url: '/inshop/scripts/shop.aspx',
			      beforeSend : function() {
				  	$(".product-list-container").addAjaxLoadScreen({show:true});
				  },
				  data: data,
				  success: function() {
					CallCartUpdate();
				  },
				 complete: function(jqXHR, textStatus) {
				  	$(".product-list-container").addAjaxLoadScreen({hide:true});
						if (textStatus != 'success') {
						alert(textStatus);
						};
				  }
				});
				return true;
			};

			
		};
		
		function Selection(thisValue) {
		
			$.ajax({
				url: $("a",thisValue).attr("href"),
				type: 'GET',
				beforeSend: function() {},
				error:  function() {},
				success:  function() {
					CallCartUpdate();
				}
				});
		};
		
		
		function Product(thisValue) {
			$.ajax({
			  type: 'POST',
			  url: '/inshop/scripts/shop.aspx',
		      beforeSend : function() {
			  	$(".product-headline").addAjaxLoadScreen({show:true});
			  },
			  data: $(".FormaddProduct2Cart").serialize(),
			  success: function() {
				CallCartUpdate();
			  },
			 complete: function(jqXHR, textStatus) {
			  	$(".product-headline").addAjaxLoadScreen({hide:true});
					if (textStatus != 'success') {
					alert(textStatus);
					};
			  }
			});
		}
		
		
		function CatalogueEnter(thisValue) {
			var pcf = $(thisValue).attr("id");
			
			var count =  $(thisValue).val();
			
			if (count == undefined) {count = 1};
			var data = 'action=addtocart&'+pcf+'='+count;
			
			
			$.ajax({
			  type: 'POST',
			  url: '/inshop/scripts/shop.aspx',
		      beforeSend : function() {
			  	$(".product-list-container").addAjaxLoadScreen({show:true});
			  },
			  data: data,
			  success: function() {
				CallCartUpdate();
			  },
			 complete: function(jqXHR, textStatus) {
			  	$(".product-list-container").addAjaxLoadScreen({hide:true});
					if (textStatus != 'success') {
					alert(textStatus);
					};
			  }
			});
			
			
		};
		
		
		
		function CallCartUpdate() {
			$(".block-container.Cart").addAjaxLoadScreen({show:true});
			$('.shopping-cart-container').load('/inshop/scripts/shop.aspx?action=ViewTemplate&Path=AjaxHeaderCart&IncludeLayout=0&LayoutNotIncluded=1');
			$(".cart-sidebar").load('/inshop/scripts/shop.aspx?action=ViewTemplate&Path=/inshop/Layout/Templates/Robin/Blocks/CartAjax&IncludeLayout=0&LayoutNotIncluded=1');
			if ($(".cart-sidebar").length>0) {
				$.scrollTo(".cart-sidebar", 500);
			};
			if ($(".ajax-cart").length>0) {
				$(".ajax-cart").load('/inshop/scripts/shop.aspx?action=ViewTemplate&Path=CartAjax&IncludeLayout=0&LayoutNotIncluded=1');
				$.scrollTo(".ajax-cart", 500)
			};
		}


		
	};	
})( jQuery );
