var $j = jQuery.noConflict();

$j(document).ready(function() 
{
	$j(".purchase").click(function(){addToBasket(this);return false;})
	$j("#basket .remove").click(function(){removeFromBasket(this);return false;})

	function addToBasket(sourceElement)
	{
		var product = $j(sourceElement).parent();
		var product_id = $j(sourceElement).attr('id');
		
		
		var product_price = product.find('.product_price').val();
		var product_name = product.find('.product_name').val();
		var product_quantity = product.find('.people').val();
		
		var product_nights = product.find('.nights').val();
		
		if(0 == product_nights)
		{
			alert('Minimum is 1 night');
			return false;
		}
		
		if(null == product_nights)
		{
			//product_nights = 1;
			updatet_sum_price = parseInt($j('#purchased_'+product_id).find('.product_sum_price').val())+parseInt(product_price*product_quantity);
		  product_nights = null;
    }
		else
		{
      updatet_sum_price = parseInt($j('#purchased_'+product_id).find('.product_sum_price').val())+parseInt(product_price*product_quantity*product_nights);
    }
		
		
		
		var basket = $j('#basket');

		//najprv zistim ci existuje
		if (basket.find('#purchased_'+product_id).is('#purchased_'+product_id)) 
		{
			updatet_quantity = parseInt($j('#purchased_'+product_id).find('.product_quantity').val())+parseInt(product_quantity);
			//updatet_sum_price = parseInt($j('#purchased_'+product_id).find('.product_sum_price').val())+parseInt(product_price*product_quantity*product_nights);
			product_remove = '<a class="remove" href="">Remove from Cart</a>';
			
			updatet_state = '<input type="hidden" name="product_quantity" class="product_quantity" value="'+updatet_quantity+'" />'+
							'<input type="hidden" name="product_sum_price" class="product_sum_price" value="'+updatet_sum_price+'" />';
			
			updatet_text = updatet_state+updatet_quantity+' x '+product_name+'<strong>Price:'+updatet_sum_price+'</strong>'+product_remove;
			
			$j('#purchased_'+product_id).html(updatet_text);
			
			$j("#basket .remove").unbind();
			$j("#basket .remove").click(function(){removeFromBasket(this);return false;})
			
			product_quantity = updatet_quantity;
			

			$j('#purchased_'+product_id).fadeOut(200);
			
			
			$j('#purchased_'+product_id).fadeIn(500);

		}
		else 
		{
			var product_offset = product.offset();
			current_x = product_offset.left;
			current_y = product_offset.top;
			
			//vytvorim element
			
			new_elem = $j("<div>element</div>");
			new_elem.css('position', 'absolute');
			new_elem.css('left', current_x);
			new_elem.css('top', current_y);
			new_elem.css('width', '400px');
			new_elem.css('height', '50px');
			new_elem.css('border', '5px dotted #ef8301');
			new_elem.appendTo("body");
			
			product_remove = '<a class="remove" href="">Remove from Cart</a>';
			

			//purchased_product_quantity = '<input class="quantity" type="text" value="'+product_quantity+'" disabled="true" />';
			//purchased_product_price = '<span class="price">'+product_price+'</span>';
			if(null == product_nights)
  		{
  			//product_nights = 1;
  			sum_price = product_quantity*product_price;
  			product_nights = null;
  		}
  		else
  		{
        sum_price = product_quantity*product_price*product_nights;
      }
			
			//sum_price = product_quantity*product_price*product_nights;
			
			product_state = '<input type="hidden" name="product_quantity" class="product_quantity" value="'+product_quantity+'" />'+
							'<input type="hidden" name="product_sum_price" class="product_sum_price" value="'+sum_price+'" />';
			purchased_product = $j('<li id="purchased_' + product_id + '">'+product_state+product_quantity+' x '+product_name+'<strong>Price:'+sum_price+'</strong>'+product_remove+'</li>');
			purchased_product.css('display', 'none');
			
			purchased_product.prependTo("#basket");
			$j("#basket .remove").unbind();
			//$j("#basket .increase").unbind();
			//$j("#basket .decrease").unbind();
			$j("#basket .remove").click(function(){removeFromBasket(this);return false;})
			//$j("#basket .increase").click(function(){increase(this);return false;})
			//$j("#basket .decrease").click(function(){decrease(this);return false;})
			
			//basket
			var basket_offset = basket.offset();
			target_x = basket_offset.left;
			target_y = basket_offset.top;
			
			new_elem.animate({
				top: target_y + 'px',
				left: target_x + 'px'
			}, 400, 'linear', function(){
				showPurchasedProduct()
			});
		}
		
		$j.ajax({
				   type: "GET",
				   url: "/inc2/product/ajax/addProductToBasket.php",
				   data: "product_id="+product_id+"&quantity="+product_quantity+"&price="+product_price+'&name=' + product_name+'&nights='+product_nights,
				   success: function(msg)
				   {
						$j('#result').html(msg);
				   }
			   });	
		//update sum
		//increaseSum(sum_price);
	}

	function showPurchasedProduct()
	{
		new_elem.remove();
		purchased_product.slideDown()
	}
	
	function removeFromBasket(sourceElement)
	{
		var purchased_product = $j(sourceElement).parent();
		
		//update sum
		var product_price = parseInt(purchased_product.find('.price').text());
		var product_count = parseInt(purchased_product.find('.quantity').val());
		decreaseSum(product_count*product_price);

		purchased_product.remove();
		
		$j.ajax({
				   type: "GET",
				   url: "/inc2/product/ajax/removeProductFromBasket.php",
				   data: "product_id="+purchased_product.attr('id'),
				   success: function(msg)
				   {
						$j('#result').html(msg);
				   }
			   });		
	}
	
	function increaseSum(sum_value)
	{
		current_sum = parseInt($j('#sum').text());
		new_sum = current_sum + sum_value;
		$j('#sum').text(new_sum);
	}
	
	function decreaseSum(sum_value)
	{
		current_sum = parseInt($j('#sum').text());
		new_sum = current_sum - sum_value;
		$j('#sum').text(new_sum);
	}
	
	function increase(sourceElement)
	{
		
		purchased_product = $j(sourceElement).parent();
		
		product_id = purchased_product.attr('id').substring(10); 
		product_name = purchased_product.find('h2').text();
		product_quantity = parseInt(purchased_product.find('.quantity').val());
		purchased_product.find('.quantity').val(product_quantity+1);
		
		product_price = parseInt(purchased_product.find('.price').text());
		increaseSum(product_price);
		
		$j.ajax({
				   type: "GET",
				   url: "/stProduct/addProductToBasket",
				   data: "product_id="+product_id+"&quantity="+(product_quantity+1)+"&price="+product_price+"&name="+product_name
			   });	
	}
	
	function decrease(sourceElement)
	{
		purchased_product = $j(sourceElement).parent();
		product_id = purchased_product.attr('id').substring(10); 
		product_name = purchased_product.find('h2').text();
		product_quantity = parseInt(purchased_product.find('.quantity').val());
		product_price = parseInt(purchased_product.find('.price').text());
		
		if (1 == product_quantity) 
		{
			removeFromBasket(sourceElement);
		}
		else 
		{
			purchased_product.find('.quantity').val(product_quantity - 1);
			decreaseSum(product_price);
			$j.ajax({
				   type: "GET",
				   url: "/stProduct/addProductToBasket",
				   data: "product_id="+product_id+"&quantity="+(product_quantity-1)+"&price="+product_price+"&name="+product_name
			   });	
		}
	}
	
	function purchase()
	{
		$j('.sIFR-replaced').hide();
		
		$j('#basket').animate({
				position:  'absolute',
				width: '800px',
				height: '600px',
				border: '1px solid black',
				zIndex: 100
			}, 200, 'linear', function(){
				//showPurchasedProduct()
			});
	}
	
	$j("#purchased_products .remove").click(function(){removeFromPurchase(this);return false;})
	$j("#purchased_products .quantity").keyup(function(){updateSum(this);updatelibSum(this);})
	$j("#purchased_products .nights").keyup(function(){updateSum(this);updatelibSum(this);})

	function removeFromPurchase(elem)
	{
    table_row = $j(elem).parent().parent();
    
    decrease_sum = parseInt(table_row.find('.sum').text());
    decrease_sum_lib = parseInt(table_row.find('.sum_lib').text());
    
    
    old_sum = parseInt(table_row.parent().find('.total_price_text').text());
    new_sum = old_sum - decrease_sum;
    
    old_sum_lib = parseInt(table_row.parent().find('.total_price_lib_text').text());
    new_sum_lib = old_sum_lib - decrease_sum_lib;
    
    table_row.parent().find('.total_price_text').text(new_sum);
    table_row.parent().find('#purchased_products_total_price').val(new_sum);
    
    table_row.parent().find('.total_price_lib_text').text(new_sum_lib);
    table_row.parent().find('#purchased_products_total_price_lib').val(new_sum_lib);
	
	 //este to musim odfaklit zo session
	 tmp_id =  table_row.find('.per_sum').attr('name').split('_');
	 //alert(tmp_id[2]);

	 $j.ajax({
				   type: "GET",
				   url: "/inc2/product/ajax/removeProductFromBasket.php",
				   data: "product_id=product_id_"+tmp_id[2],
				   success: function(msg)
				   {
						//$j('#result').html(msg);
				   }
			   });		
			   
 
    
    $j(elem).parent().parent().remove();

    //musim skontrolovat ci netreba zmenit vypocet ceny z package na activity-only
    product_item = $j('#purchased_products .product_item');

     if(product_item.length < 2)
     {
        per_sum = parseInt(product_item.find('.per_sum').val());
        per_sum_lib = parseInt(product_item.find('.per_sum_lib').val());
        quantity = parseInt(product_item.find('.quantity').val());
        nights =  product_item.find('.nights');
        
        if(nights.length > 0)
         {
            updatet_sum = quantity*per_sum*parseInt(nights.val());
            updatet_sum_lib = quantity*per_sum_lib*parseInt(nights.val());
         }
         else
         {
            updatet_sum = quantity*per_sum;
            updatet_sum_lib = quantity*per_sum_lib;
         }
         product_item.find('.sum').text(updatet_sum);
         product_item.find('.sum_lib').text(updatet_sum_lib);
         
         product_item.parent().find('.total_price_text').text(updatet_sum);
         product_item.parent().find('#purchased_products_total_price').val(updatet_sum);
         
         product_item.parent().find('.total_price_lib_text').text(updatet_sum_lib);
         product_item.parent().find('#purchased_products_total_price_lib').val(updatet_sum_lib);
         
         product_item.parent().find('#price_type').text('( Activity only price)');
         
     }
  }
  
  function updateSum(elem)
  {
     table_row = $j(elem).parent().parent();
     td_sum =  parseInt(table_row.find('.sum').text());
     td_quantity = parseInt(table_row.find('.quantity').val());
     
     //ak sa jedna o min 2 produkty tak pocitam zo zvyhodnenou sumou
     product_items_count = $j('#purchased_products .product_item');
     
     

     if(product_items_count.length > 1)
     {
        per_sum = parseInt(table_row.find('.per_sum_package').val());
     }
     else
     {
        per_sum = parseInt(table_row.find('.per_sum').val());
     }

     td_nights =  table_row.find('.nights');
     

     
     if(table_row.find('.nights').length > 0)
     {
      updatet_sum = td_quantity*per_sum*parseInt(td_nights.val());
     }
     else
     {
      updatet_sum = td_quantity*per_sum;
     }
     
     if(isNaN(updatet_sum))
     {
        updatet_sum = 0;
     }

     table_row.find('.sum').text(updatet_sum);
     all_sums = $j('#purchased_products .sum');
     

     updatet_total_price = 0;
     
     for(i = 0; i<all_sums.length;i++)
     {
        updatet_total_price += parseInt($j(all_sums.get(i)).text());
     }
     
     table_row.parent().find('.total_price_text').text(updatet_total_price);
     table_row.parent().find('#purchased_products_total_price').val(updatet_total_price);
     
  }
  
  function updatelibSum(elem)
  {
     table_row = $j(elem).parent().parent();
     td_sum =  parseInt(table_row.find('.sum_lib').text());
     td_quantity = parseInt(table_row.find('.quantity').val());
     
     //ak sa jedna o min 2 produkty tak pocitam zo zvyhodnenou sumou
     product_items_count = $j('#purchased_products .product_item');
     if(product_items_count.length > 1)
     {
        per_sum = parseInt(table_row.find('.per_sum_package_lib').val());
     }
     else
     {
        per_sum = parseInt(table_row.find('.per_sum_lib').val());
     }
     
     
     td_nights =  table_row.find('.nights');

     
     if(table_row.find('.nights').length > 0)
     {
      updatet_sum = td_quantity*per_sum*parseInt(td_nights.val());
     }
     else
     {
      updatet_sum = td_quantity*per_sum;
     }
     
     if(isNaN(updatet_sum))
     {
        updatet_sum = 0;
     }

     table_row.find('.sum_lib').text(updatet_sum);
     all_sums = $j('#purchased_products .sum_lib');
     

     updatet_total_price = 0;
     
     for(i = 0; i<all_sums.length;i++)
     {
        updatet_total_price += parseInt($j(all_sums.get(i)).text());
     }
     
     table_row.parent().find('.total_price_lib_text').text(updatet_total_price);
     table_row.parent().find('#purchased_products_total_price_lib').val(updatet_total_price);
     
  }
  
	
});

