$(document).ready(function(){ 


        $(".buyProduct").click(function() {
        
		var productIDValSplitter = (this.id).split("_");
		var productIDVal 	 = productIDValSplitter[1];
		var variants = new Array();
			/*var name = new Array();*/
        if($(this).attr('rel') == 'variant'){
		   var pDiv = "#variant_" + productIDVal;
	     // var i = 0;
		/*  $('#variant_'+productIDVal +' .variant').each(function(){
		          var variant = $(this).attr('name');
				  var variantID = $(':selected',this).val();			
				  name[variant] = variantID;
		//i++;		   	       
		  }) */
		   variants 	= $('#variant_'+productIDVal +' .variant').serializeArray();
		   //variantsID = serializaArray(name); 
		   $('#variant_'+productIDVal).dialog("close");
		   
		}else{
		
		   var pDiv = "#featuredProduct_" + productIDVal; 
        $(this).parent().parent().css({'z-index':'300'}); 
		$(this).parent().parent().prev().css({'z-index':'200'});  
		$(this).parent().parent().next().css({'z-index':'200'});
		
		if($('#variant_'+productIDVal).length == 1) {
		var dialogTitle = "";
		
		     $('#variant_'+productIDVal).dialog({
     				show: "blind", // animace při zobrazování
     				hide: "explode", // animace při zavírání
     				title: dialogTitle, 
     				height: 'auto',
     				width: 'auto',
     				modal: true  //zamezi manipulaci s obsahem mimo dialog
				});	
				return false;
		}
		 
		}      
		
	
		
		
		var productX 		= $("#productImageWrapID_" + productIDVal).offset().left;
		var productY 		= $("#productImageWrapID_" + productIDVal).offset().top;
	        
        var basketX 		= $("#basketTitleWrap").offset().left;
		var basketY 		= $("#basketTitleWrap").offset().top;
               
        var gotoX 		= basketX - productX;
		var gotoY 		= basketY - productY;
		
		var newImageWidth 	= $("#productImageWrapID_" + productIDVal).width() / 3;
		var newImageHeight	= $("#productImageWrapID_" + productIDVal).height() / 3;
 

		$("#productImageWrapID_" + productIDVal + " img")

		.clone()
		.prependTo(pDiv)
		.css({'position' : 'absolute','z-index' : '1500'})
            
		.animate({opacity: 0.5, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {
         $(this).animate({opacity: 0}, 100 ).remove();
         
                $.ajax({
				type: "POST",  
		        	url: "/kosik/addToBasket/"+productIDVal+"/",
				data: { productID: productIDVal, action: "addToBasket",variants:variants},  
				success: function(theResponse) {
                      //aktualizace stavu v headeru
					  $("#resultKosik").html(theResponse);
                            
				}

				});

               // alert("Položka byla přidána do košíku");
          //info o vlozeni vlozeno do samostatne funkce
          returnInfoBasket(productIDVal);
     	});
	   return false;
	});

    
		
		//vlozeni do kosiku
		$('form#productBuy').submit(function() {
		
		var productIDVal 	 = $("[name=productID]").val();
		
		var product = $(this).serializeArray(); //posbira udaje formulare a sestavi pole
		 
		var productX 		= $("#detail-image").offset().left;
		var productY 		= $("#detail-image").offset().top;
	        
        var basketX 		= $("#resultKosik").offset().left;
		var basketY 		= $("#resultKosik").offset().top;
               
        var gotoX 		= basketX - productX;
		var gotoY 		= basketY - productY;
		
		var newImageWidth 	= $("#detail-image").width() / 10;
		var newImageHeight	= $("#detail-image").height() / 10;
 

		$("#detail-image a img")
		.clone()
		.prependTo("#detail-image")
		.css({'position' : 'absolute','border' : 'solid 1px #ccc'})
            
		//.animate({opacity: 0.5}, 100 )
		.animate({opacity: 0.5, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {
         $(this).animate({opacity: 0}, 100 ).remove();
		  
		  $.ajax({
				type: "POST",  
		        	url: "/kosik/addProductToBasket/"+productIDVal+"/",
				data: { productID: productIDVal, product: product, action: "addToBasket"},  
				success: function(theResponse) {
                     //aktualizace stavu v headeru
					 $("#resultKosik").html(theResponse);
                            
				}

				});
               returnInfoBasket(productIDVal)
            
          	});
          
                
		  return false;
		});

});
function returnInfoBasket(productIDVal){
          $("#odpovedProduct_" + productIDVal)
		//.clone()
		//.prependTo("#featuredProduct_" + productIDVal)
        .html("Položka byla přídana do košíku")
		.css({'position' : 'absolute','left' : '-100px','width' : '200px','height' : 'auto','padding' : '10px','border' : '2px solid #ffffff','font-size' : '14px','background-color' : '#9B1132','color' : 'white'})
		.show()
		.animate({opacity: 1}, 1000,function(){
		$(this).animate({opacity: 0}, 2000 ).hide();
		
		} ); 
		}
		
   		
