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

$(document).ready(function(){
	
	$('.product').simpletip({
		
		offset:[40,0],
		content:'<img src="http://shop.thesmonkey.com/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('http://shop.thesmonkey.com/ajax/tips.php',{img:param}); 
		} 
		

	});
	
	$('.como_comprar_es_ES').simpletip({
		fixed:true,
	    position: ["0", "22"],
		offset:[40,0],
		content:'<img src="http://shop.thesmonkey.com/img/ajax_load.gif" alt="loading" style="margin:10px;" />',
		onShow: function(){				
			this.load('http://shop.thesmonkey.com/video.txt'); 
		} 
		

	});
	
	$('.como_comprar_en_GB').simpletip({
		fixed:true,
	    position: ["0", "22"],
		offset:[40,0],
		content:'<img src="http://shop.thesmonkey.com/img/ajax_load.gif" alt="loading" style="margin:10px;" />',
		onShow: function(){				
			this.load('http://shop.thesmonkey.com/video_en.txt'); 
		} 
		

	});
	
	$(".product img").draggable({
	
	containment: 'document',
	opacity: 0.6,
	revert: 'invalid',
	helper: 'clone',
	zIndex: 100
	
	});

	$("div.content.drop-here").droppable({
	
			drop:
					function(e, ui)
					{
						var param = $(ui.draggable).attr('src');
						
						var id = $(ui.draggable).attr('id');
						
						var clase=".talla"+id;
						
						var talla=$(clase).val(); 
						
						$('#ex').hide();
						
						if($.browser.msie && $.browser.version=='6.0')
						{
							param = $(ui.draggable).attr('style').match(/src=\"([^\"]+)\"/);
							param = param[1];
						}
						
						addlist(param,talla);
					}
	
	});

});

function reloading(){
	
	$('.product').simpletip({
		
		offset:[40,0],
		content:'<img src="http://shop.thesmonkey.com/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('http://shop.thesmonkey.com/ajax/tips.php',{img:param}); 
		} 

	});
	
	$(".product img").draggable({
	
	containment: 'document',
	opacity: 0.6,
	revert: 'invalid',
	helper: 'clone',
	zIndex: 100
	
	});

	$("div.content.drop-here").droppable({
	
			drop:
					function(e, ui)
					{
						var param = $(ui.draggable).attr('src');
						
						var talla=$(".talla").val();
						
						if($.browser.msie && $.browser.version=='6.0')
						{
							param = $(ui.draggable).attr('style').match(/src=\"([^\"]+)\"/);
							param = param[1];
						}

						addlist(param,talla);
					}
	
	});

}


function addlist(param,talla)
{
	$.ajax({
	type: "POST",
	url: "http://shop.thesmonkey.com/ajax/addtocart.php",
	data: 'talla='+talla+'&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)
				{
				
					if (purchased[i].talla==msg.talla)
					{
					check=true;
					cnt=purchased[i].cnt;
					
					break;
					}
				}
			}
			
			if(!cnt)
				$('#item-list').append(msg.txt);
				
			if(!check)
			{
				purchased.push({id:msg.id,talla:msg.talla,cnt:1,price:msg.price});
			}
			else
			{
				if(cnt>=90) return false;
				
				purchased[i].cnt++;
				$('#'+msg.id+'_'+msg.talla+'_cnt').val(purchased[i].cnt);
			}
			
			totalprice+=msg.price;
			update_total();

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

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

function remove(id)
{
	
	var key_=id.split("_");
	var key_0=key_[0];
	var key_1=key_[1];
	
	var i=findpos(key_0,key_1);

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

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



function change(id)
{
	
	var key_=id.name.split("_");
	var key_0=key_[0];
	var key_1=key_[1];
	var i=findpos(key_0,key_1);
	

	totalprice+=(id.value-purchased[i].cnt)*purchased[i].price;
	
	purchased[i].cnt=id.value;
	update_total();
}

function update_total()
{
	if(totalprice)

	{
		$('#total').html('total: '+Math.round(totalprice*Math.pow(10,2))/Math.pow(10,2)+' &euro;');
		$('a.button').css('display','block');
	}
	else
	{
		$('#total').html('');
		$('a.button').hide();
	}
}
