var purchased=new Array();
var purchased2=new Array();
var productscount=totalproducts;
var stack = 0;
var total_items =0;
var linked = 0;
var images_listed =productscount;
$(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}); 
		} 

	});
	*/
	$(".product img").draggable({
	
	containment: 'document',
	opacity: 0.6,
	revert: 'invalid',
	helper: 'clone',
	zIndex: 100
	
	});
//	$(".product img").css({'margin' : '0', 'padding' : '0', 'float' : 'left'});
	
	
	$("div.content.drop-here").droppable({
	
			drop:
					function(e, ui)
					{
						var param = $(ui.draggable).attr('src');
						
						if($.browser.msie && $.browser.version=='6.0')
						{
							param = $(ui.draggable).attr('style').match(/src=\"([^\"]+)\"/);
							param = param[1];
						}
						//trace2(param);
					//trace();
						addlist(param);
					}
	
	});
	
	


	$("#textareaing").dblclick(function()
	{
		// alert($("#textareaing option:selected"));
		objecttest = $("#textareaing option:selected");
		addlist2(objecttest["0"].value);
		popa(objecttest["0"].value);
		$("#inputs").focus();
	}
	);
	
	$("#inputs").keyup(
		function(e)
		{
			addlist3($("#inputs").val());	
		}
	);
	
	//addlist2(objecttest["0"].value);
	//alert(linked);
});
function callme(id)
{
	addlist2(id);
//	$("#inputs").focus();
}
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
		{
			$("#suggestions").html('');
			add_to_cart(msg);
			$("#inputs").focus();
		}
		
			//totalprice+=msg.price;
			//update_total();
	
	}
	});
}
function addlist2(param)
{
	$.ajax({
	type: "POST",
	url: "ajax/addtocart2.php",
	data: 'img='+encodeURIComponent(param),
	dataType: 'json',
	beforeSend: function(x){$('#ajax-loader2').css('visibility','visible');},
	success: function(msg){
		
		$('#ajax-loader').css('visibility','hidden');
					
		if(parseInt(msg.status)!=1)
		{
			return false;
		}
		else
		{
			$("#suggestions").html('');
			if(images_listed < 4)
				add_to_cart(msg);
			else
				add_to_cart2(msg);
			//totalprice+=msg.price;
			//update_total();

		}
	
	}
	});
}
function addlist3(param)
{
	var arr = '';
	for(i=0; i<purchased.length;i++)
	{
		if(i==0)
			arr =  purchased[i].id;
		else
			arr =  purchased[i].id + "," +arr;
	}
	
	$.ajax({
	type: "POST",
	url: "ajax/addtocart3.php",
	data: 'img='+encodeURIComponent(param)+'&arr='+arr,
	dataType: 'json',
	beforeSend: function(x){$('#ajax-loader3').css('visibility','visible');},
	success: function(msg){
		
		$('#ajax-loader').css('visibility','hidden');
					
		if(parseInt(msg.status)!=1)
		{		
			//alert(param);
			return false;
		}
		else
		{
			$("#suggestions").html(msg.txt);
		}
	}
	});
}
function popa(id)
{	

	var output = '';
	//$('#trace').append(purchased2.length);
		
	if(purchased2.length==0)
	{
		productscount--;
		$('#img'+id).parent().remove();
		return;
	}
	object = purchased2.pop();
	$('#img'+id).parent().remove();
	//trace2(object['name']);
	remove_from_cart(object);
}

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

function trace2(str)
{
	$('#trace').html(str);
}
function trace()
{
	
	trace2("totalproducts = "+totalproducts+"\nproductscount = "+productscount+"\nPurchased = "+purchased.length+"\npurchased2 = "+purchased2.length+"\nimages_listed = "+images_listed);
}
function remove(id)
{
	//trace();
	var i=findpos(id);
	object = purchased[i];
	purchased.splice(i, 1);
//	$('#img'+id).parent().remove();
//alert(productscount+" count:total "+totalproducts);
	if(productscount < totalproducts)
	{
		productscount++;
		remove_from_cart(object);
	}
	else
	{
		purchased2.push(object);
	}
//	alert(purchased[i].cnt);
	
	$('#table_'+id).remove();
	
	if($('#item-list').html() == "")
	{
		$('#item-list').html("Drag Ingredients Here");
	}
	//update_total();
}
function remove_from_cart(object)
{
//	alert(productscount);
		app = '<div class="product"><img id="img'+object['id']+'" src="'+object['img']+'" alt="'+encodeURI(object['name'])+'" width="82" class="pngfix" /><br />'+object['name']+'</div>';
		//stack++;
		/*
	productscount < totalproducts
	if($('#item-list').html() == "")
*/

	if(1)
	{
		
		$('#new-products').append(app);
		$("#textareaing").append('<option value="'+object['id']+'">'+object['name']+'</option>');
//		productscount++;
//			$("#new-products").css({'margin' : '0', 'padding' : '0', 'float' : 'left'});
		$("#new-products img").draggable({
		
		containment: 'document',
		opacity: 0.6,
		revert: 'invalid',
		helper: 'clone',
		zIndex: 100
		});
	}
	else
	{
	}
}
function add_to_cart(msg)
{
	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)
	{
		if($('#item-list').html() == "Drag Ingredients Here")
		{
			$('#item-list').html("");
		}
		$('#item-list').append(msg.txt);
	}
		
	if(!check)
	{
		//$('#trace').append("id = "+id+"\npurchased.length = "+purchased.length+"\nstr = "+str);
		//$('#trace').append();
//		var i=findpos(id);
//		$("#textareaing option:contains('value=\""+msg.id+"\"')").css
		popa(msg.id);
		findindex(msg.id);
		purchased.push({id:msg.id,img:msg.img, name:msg.name});
		//tracing = $("#textareaing option[value:"+msg.id+"]");
	}
	else
	{
		//alert(total_items);
//never possible
		if(cnt>=3) return false;
		purchased[i].cnt++;
		$('#'+msg.id+'_cnt').val(purchased[i].cnt);
	}
	$("#inputs").focus();
	$("#inputs").val('');
}
function add_to_cart2(msg)
{
	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)
	{
		if($('#item-list').html() == "Drag Ingredients Here")
		{
			$('#item-list').html("");
		}
		$('#item-list').append(msg.txt);
	}
		
	if(!check)
	{
		//$('#trace').append("id = "+id+"\npurchased.length = "+purchased.length+"\nstr = "+str);
		//$('#trace').append();
//		var i=findpos(id);
//		$("#textareaing option:contains('value=\""+msg.id+"\"')").css
		//popa(msg.id);
		findindex(msg.id);
		purchased.push({id:msg.id,img:msg.img, name:msg.name});
		//tracing = $("#textareaing option[value:"+msg.id+"]");
	}
	else
	{
		//alert(total_items);
//never possible
		if(cnt>=3) return false;
		purchased[i].cnt++;
		$('#'+msg.id+'_cnt').val(purchased[i].cnt);
	}
	$("#inputs").focus();
	$("#inputs").val('');
}
function findindex(id)
{
	$("#textareaing option").each(
		function( intIndex )
		{
			if($(this).val() == id)
			{
				$(this).remove();
			}
		}
	);
}

function add_toc(id)
{
	$('#table2_'+id).remove();
	addlist2(id);
	//alert('#table2_'+id);
}
