var purchased=new Array();
var totalprice=0;

$(document).ready(function(){
	
	/*$('.product').simpletip({
		
		offset:[40,0],
		content:'<img src="img/ajax_load.gif" alt="loading" style="margin:10px;" />',
		onShow: function(){
			
			var param = this.getParent().find('img').attr('src');
			
			if($.browser.msie && $.browser.version=='6.0')
			{
				param = this.getParent().find('img').attr('style').match(/src=\"([^\"]+)\"/);
				param = param[1];
			}
			
			this.load('ajax/tips.php',{img:param}); 
		} 

	});*/
	
	$(".productImageWrap img").draggable({
	
	containment: 'document',
	opacity: 0.6,
	revert: 'invalid',
	helper: 'clone',
	zIndex: 100
	
	});
	
	
	
	$("div.dropped.drop-here").droppable({
		
		 drop: function(e, ui) 
		 { 
		 	
		 	
		 	
	         var param = $(ui.draggable).attr('id');
			 //alert(param);
			
			var productIDValSplitter 	= param.split("_");
			var productIDVal 			= productIDValSplitter[1];
			
			/*
			var productX 		= $("#productImageWrapID_" + productIDVal).offset().left;
			var productY 		= $("#productImageWrapID_" + productIDVal).offset().top;
			
			if( $("#productID_" + productIDVal).length > 0){
				var basketX 		= $("#productID_" + productIDVal).offset().left;
				var basketY 		= $("#productID_" + productIDVal).offset().top;			
			} else {
				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("#productImageWrapID_" + productIDVal)
			.css({'position' : 'absolute'})
			.animate({opacity: 0.4}, 100 )
			.animate({opacity: 0.1}, 1200, function() {
					alert('RRRR');																																																																				  			$(this).remove();
		      */
				$("#notificationsLoader").html('<img src="http://www.lux-obuvki.com/js/addtocart/images/loader.gif">');
			
				$.ajax({  
					
					
					type: "POST",  
					url: "http://www.lux-obuvki.com/js/addtocart/functions.php",  
					data: { productID: productIDVal, action: "addToBasket"},  
					success: function(theResponse) {
						
					temp_03 = theResponse.split('||');
					new_all_text_03 = temp_03[0];
					new_all_text_04 = temp_03[1];	
					new_all_text_05 = temp_03[2];
						
						if( $("#productID_" + productIDVal).length > 0){
							$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
							$("#productID_" + productIDVal).before(new_all_text_03).remove();
							$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
							$("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
							$("#notificationsLoader").empty();
							$("#price_total").html('<div border:1px solid #444; width: 100px; height: 30px;>'+new_all_text_04+'</div>');
					        $("#price_total_01").html('<a href="'+new_all_text_05+'">ПОТВЪРДИ ПОРЪЧКАТА</div>');
							
						} else {
							$("#basketItemsWrap li:first").before(new_all_text_03);
							$("#basketItemsWrap li:first").hide();
							$("#basketItemsWrap li:first").show("slow");  
							$("#notificationsLoader").empty();		
							$("#price_total").html('<div border:1px solid #444; width: 100px; height: 30px;>'+new_all_text_04+'</div>');
							$("#price_total_01").html('<a href="'+new_all_text_05+'">ПОТВЪРДИ ПОРЪЧКАТА</div>');
						
						}
						
					}  
				});  
			
			/*});*/
		
		
		}
		
		
		
	});
	

});

/*
function addlist(param)
{
	$.ajax({
	type: "POST",
	url: "ajax/addtocart.php",
	data: 'img='+encodeURIComponent(param),
	dataType: 'json',
	beforeSend: function(x){$('#ajax-loader').css('visibility','visible');},
	success: function(msg){
		
		$('#ajax-loader').css('visibility','hidden');
		if(parseInt(msg.status)!=1)
		{
			return false;
		}
		else
		{
			var check=false;
			var cnt = false;
			
			for(var i=0; i<purchased.length;i++)
			{
				if(purchased[i].id==msg.id)
				{
					check=true;
					cnt=purchased[i].cnt;
					
					break;
				}
			}
			
			if(!cnt)
				$('#item-list').append(msg.txt);
				
			if(!check)
			{
				purchased.push({id:msg.id,cnt:1,price:msg.price});
			}
			else
			{
				if(cnt>=3) return false;
				
				purchased[i].cnt++;
				$('#'+msg.id+'_cnt').val(purchased[i].cnt);
			}
			
			totalprice+=msg.price;
			update_total();

		}
		
		$('.tooltip').hide();
	
	}
	});
}

function findpos(id)
{
	for(var i=0; i<purchased.length;i++)
	{
		if(purchased[i].id==id)
			return i;
	}
	
	return false;
}

function remove(id)
{
	var i=findpos(id);

	totalprice-=purchased[i].price*purchased[i].cnt;
	purchased[i].cnt = 0;

	$('#table_'+id).remove();
	update_total();
}

function change(id)
{
	var i=findpos(id);
	
	totalprice+=(parseInt($('#'+id+'_cnt').val())-purchased[i].cnt)*purchased[i].price;
	
	purchased[i].cnt=parseInt($('#'+id+'_cnt').val());
	update_total();
}

function update_total()
{
	if(totalprice)
	{
		$('#total').html('total: $'+totalprice);
		$('a.button').css('display','block');
	}
	else
	{
		$('#total').html('');
		$('a.button').hide();
	}
}
*/