var purchased=new Array();
var totalprice=0;

$(document).ready(function(){
	//loadCart();
	$('.button').click(function(event) {
		event.preventDefault();
	});
	$(".product").draggable({
		containment: 'document',
		opacity: 0.6,
		revert: 'invalid',
		helper: 'clone',
		zIndex: 100
	});

	$(".drop-here").droppable({
		drop:
			function(e, ui)
			{
				var idStr = $(ui.draggable).attr('id');
				var id = idStr.slice(idStr.lastIndexOf('-') + 1, idStr.length);
				var param = id;
				
				if($.browser.msie && $.browser.version=='6.0')
				{
					param = $(ui.draggable).attr('style').match(/src=\"([^\"]+)\"/);
					param = param[1];
				}

				addlist(param);
			}
	});
});

function loadCart() {
	$.ajax({
	type: "POST",
	url: "ajax/loadCart.php",
	dataType: 'html',
	success: function(response){
		var results = response;
		results = results.split(';');
		for(i in results) {
			var values = results[i].split(':');
			var key = values[0];
			var qty = values[1];
			var itemPrice = values[2];
			//purchased.push({id:key,cnt:qty,price:itemPrice});
			addlist(key);
			$('#' + key + '_cnt').val(qty);
			//change(key);
		}
	}
	});
}

function updateServer(id, qty) {
	//alert(purchased.length);
	alert('id:' + id + ' qty:' +qty);
	$.ajax({
	type: "POST",
	url: "ajax/updateCart.php",
	data: 'PID='+id + '&qty=' + qty,
	dataType: 'html',
	//beforeSend: function(x){$('#ajax-loader').css('visibility','visible');},
	success: function(msg){
		//alert('success');
		//alert(msg);
	}
	});
}

function addlist(param)
{
	//alert(param);
	$.ajax({
	type: "POST",
	url: "ajax/addtocart.php",
	data: 'PID='+param,
	dataType: 'json',
	beforeSend: function(x){$('#ajax-loader').css('visibility','visible');},
	success: function(msg){
		var quantity = '';
		$('#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);
				$('#cart').height($('#cart').height() + $('#productId-' + msg.id).height());
				$('.content-area').height($('.content-area').height() + $('#productId-' + msg.id).height());
				
			if(!check)
			{
				purchased.push({id:msg.id,cnt:1,price:msg.price});
				quantity = 1;
			}
			else
			{
				//if(cnt>=3) return false;
				
				purchased[i].cnt++;
				quantity = purchased[i].cnt;
				$('#'+msg.id+'_cnt').val(purchased[i].cnt);
			}
			totalprice+=msg.price;
			update_total();
			updateServer(param, quantity);
			
		}
	
	}
	});
}

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;
	
	$('#cart').height($('#cart').height() - $('#productId-'+id).height());
	$('.content-area').height($('.content-area').height() - $('#productId-'+id).height());
	
	$('#productId-'+id).remove();
	update_total();
	updateServer(id, 'remove');
}

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();
	updateServer(id, purchased[i].cnt);
}

function update_total()
{
	
	if(totalprice)
	{
		$('#total').html('total: $'+totalprice.toFixed(2));
		$('a.button').css('display','block');
		//updateServer();
	}
	else
	{
		$('#total').html('');
		$('a.button').hide();
	}
}