var box = 0;

function addItemToCart()
{
	prodid = $("#productid").val();
	prodqty = parseInt( $("#qty").val() );
	prodstock = parseInt( $("#productstock").val() );
	prodname = $("#productname").html();
	prodprice = $("#productprice").html();
	prodcore = $("#core").val();
	prodopt = "";
	
	$(".options").each( function () {
		if ( prodopt )
			prodopt = prodopt + ", ";
		num = this.id.substr( 8 );
		prodopt = prodopt + $("#optionname_"+num).html() + " ";
		prodopt = prodopt + $(this).val();
	} );
	
	if ( prodqty < 1 )
	{
		alert("You must specify a quantity greater than zero to add an item to your cart")
		return false;
	}
	else if ( prodqty > prodstock )
	{
		alert("We're sorry, but we only have "+prodstock+" in stock");
		return false;
	}
	
	$.get( "ajax.cartadd.php", { id: prodid, name: prodname, qty: prodqty, core: prodcore, options: prodopt, price: prodprice }, addedToCart );

	return false;
}

function addedToCart( x )
{
	try
	{
		src = $("#mainimg").attr("src");
		z = $("#mainimg").clone(true).css("position","absolute").css("left",$("#mainimg").offset().left).css("top",$("#mainimg").offset().top).insertBefore("#mainimg").animate({
			width: 0,
			height: 0,
			top: $("#cartcount").offset().top,
			left: $("#cartcount").offset().left
		}, 600, "", function () { $(z).remove() } );
	}
	catch( err )
	{
	}
	
	if ( x == 0 )
	{
		$("#cartcount").html("&nbsp;");
		$("#topcartcount").html("Your Cart is Empty");
	}
	else if ( x == 1 )
	{
		$("#cartcount").html("&nbsp;(1)");
		$("#topcartcount").html("Cart has 1 Item");
	}
	else
	{
		$("#cartcount").html("&nbsp;("+x+")");
		$("#topcartcount").html("Cart has "+x+" Items");
	}
	
	loaded = 0;
}

var showing = 0;
var loaded = 0;
var carthtml = "";

function showTopCart()
{
	if ( loaded != 1 )
		$("#topcart").load("inc.topcart.php", '', function () { $("#topcart:hidden").slideDown(400); } );
	else
		$("#topcart:hidden").slideDown(400);
		
	setTimeout('doHideTopCart()', 1000);
	showing = 1;
	loaded = 1;
}

function hideTopCart()
{
	showing = 0;
	setTimeout( 'doHideTopCart()', 200 );
}

function doHideTopCart () 
{ 
	if ( ! showing ) 
		$("#topcart:visible").hide(); 
	else
		setTimeout('doHideTopCart()', 500);
}

function removeFromCart(id)
{
	$.get( "ajax.cartremove.php", { id: id }, removedFromCart );
}

function removedFromCart(x)
{
	if ( x == 0 )
	{
		$("#cartcount").html("&nbsp;");
		$("#topcartcount").html("Your Cart is Empty");
	}
	else if ( x == 1 )
	{
		$("#cartcount").html("&nbsp;(1)");
		$("#topcartcount").html("Cart has 1 Item");
	}
	else
	{
		$("#cartcount").html("&nbsp;("+x+")");
		$("#topcartcount").html("Cart has "+x+" Items");
	}
	
	$("#topcart").hide();
	
	loaded = 0;
	
	showTopCart();
}

function removeItemViewCart(x)
{
	ans = window.confirm( "Are you sure you want to remove this item from your cart?" );
	
	if ( ans )
	{
		$("tr#prodrow_"+x).remove();
		$.get( "ajax.cartremove.php", { id: x }, removedItemFromCart );
	}
	
	return false;
}

function removedItemFromCart(x)
{
	if ( x == 0 )
	{
		$("#cartcount").html("&nbsp;");
		$("#topcartcount").html("Your Cart is Empty");
		$("#viewcartcount").html("Your cart is currently empty");
	}
	else if ( x == 1 )
	{
		$("#cartcount").html("&nbsp;(1)");
		$("#topcartcount").html("Cart has 1 Item");
		$("#viewcartcount").html("There is 1 item in your Cart");
	}
	else
	{
		$("#cartcount").html("&nbsp;("+x+")");
		$("#topcartcount").html("Cart has "+x+" Items");
		$("#viewcartcount").html("There are "+ x +" items in your Cart");
	}
	
	$("#topcart").hide();
	
	updateViewCartSubtotal();
	
	loaded = 0;
}

function updateViewCart()
{
	setTimeout('doUpdateViewCart()', 100);
	return false;
}

function doUpdateViewCart()
{
	changed = 0;
	
	$(".viewcartqty").each( function () {
		id = $(this).attr("id").substr(5);
		
		if ( wdirty[id] ) 
		{
			qty = $(this).val();
			changed = 1;
			
			if ( qty > 0 )
			{
				price = parseFloat( $("#price_"+id).html() );
				prod = price * qty;
				$("#viewcartsubtotal_"+id).html( prod.toFixed(2) );
				$.get( "ajax.cartupdate.php", { id: id, qty: qty }, changeViewCartItemSubtotal );
			}
			else
			{
				$("tr#prodrow_"+id).remove();
				$.get( "ajax.cartremove.php", { id: id }, removedItemFromCart );
			}
		}
	} );
	
	if ( changed )
	{
		for( i = 0; i < wdirty.length; i++ )
			wdirty[i] = 0;
			
		loaded = 0;
		setTimeout( 'updateViewCartSubtotal()', 200 );
	}
}

function nofunct (x) { return true; }

function viewCartSetDirty(x) 
{
	wdirty[x] = 1;
}

function updateViewCartSubtotal()
{
	$("#viewcartsubtotal").load( "ajax.carttotal.php" );
}

function changeViewCartItemSubtotal(x)
{
	myarr = x.split("|");
	if ( myarr[0] )
		$("#viewcartsubtotal_"+myarr[0]).html(myarr[1]);
}

function addItemToWishlist()
{
	prodid = $("#productid").val();
	prodqty = $("#qty").val();
	prodstock = $("#productstock").val();
	prodname = $("#productname").html();
	prodopt = "";
	
	$(".options").each( function () {
		if ( prodopt )
			prodopt = prodopt + ", ";
		num = this.id.substr( 8 );
		prodopt = prodopt + $("#optionname_"+num).html() + " ";
		prodopt = prodopt + $(this).val();
	} );
	
	$.get( "ajax.wishlistadd.php", { id: prodid, name: prodname, qty: prodqty, options: prodopt }, addedToWishlist );
	
	return false;
}

function addedToWishlist( x )
{
	try
	{
		src = $("#mainimg").attr("src");
		z = $("#mainimg").clone(true).css("position","absolute").css("left",$("#mainimg").offset().left).css("top",$("#mainimg").offset().top).insertBefore("#mainimg").animate({
			width: 0,
			height: 0,
			top: $("#wishcount").offset().top,
			left: $("#wishcount").offset().left
		}, 600, "", function () { $(z).remove() } );
	}
	catch( err )
	{
	}
	
	if ( x == 0 )
		$("#wishcount").html("&nbsp;");
	else if ( x == 1 )
		$("#wishcount").html("&nbsp;(1)");
	else
		$("#wishcount").html("&nbsp;("+x+")");
	
	loaded = 0;
}

function updateViewWishlist()
{
	setTimeout('doUpdateViewWishlist()', 100);
	return false;
}

function doUpdateViewWishlist()
{
	changed = 0;
	
	$(".viewcartqty").each( function () {
		id = $(this).attr("id").substr(5);
		
		if ( wdirty[id] ) 
		{
			qty = $(this).val();
			changed = 1;
			
			if ( qty > 0 )
				$.get( "ajax.wishlistupdate.php", { id: id, qty: qty }, nofunct );
			else
			{
				$("tr#prodrow_"+id).remove();
				$.get( "ajax.wishlistremove.php", { id: id }, removedItemFromWishlist );
			}
		}
	} );
	
	if ( changed )
	{
		for( i = 0; i < wdirty.length; i++ )
			wdirty[i] = 0;
			
		loaded = 0;
	}
}

function removeItemViewWishlist(x)
{
	ans = window.confirm( "Are you sure you want to remove this item from your wishlist?" );
	
	if ( ans )
	{
		$("tr#prodrow_"+x).remove();
		$.get( "ajax.wishlistremove.php", { id: x }, removedItemFromWishlist );
	}
	
	return false;
}

function removedItemFromWishlist(x)
{
	if ( x == 0 )
	{
		$("#wishcount").html("&nbsp;");
		$("#viewwishlistcount").html("Your wishlist is currently empty");
	}
	else if ( x == 1 )
	{
		$("#wishcount").html("&nbsp;(1)");
		$("#viewwishlistcount").html("There is 1 item in your temporary wishlist");
	}
	else
	{
		$("#wishcount").html("&nbsp;("+x+")");
		$("#viewwishlistcount").html("There are "+ x +" items in your temporary wishlist");
	}
}

function selText(x)
{
	$("#"+x).select();
}

function confirmRemove(x)
{
	ans = window.confirm( "Are you sure you want to remove this "+x );
	
	return ans;
}

function editNote(x)
{
	if ( ! box )
	{
		box = 1;
		$("#note_"+x).hide();
		$("#noteholder_"+x).animate({
			height: 70
		}, 400, "", function () {
			mytext = document.createElement("TEXTAREA");
			$(mytext).val($.trim( $("#note_"+x).html() )).height($("#noteholder_"+x).height()-25).width($("#noteholder_"+x).width()).appendTo("#noteholder_"+x).focus();
			mybutton = document.createElement("INPUT");
			$(mybutton).attr("type","button").attr("value","Update").attr("id","mybutton").appendTo("#noteholder_"+x);
			$("#mybutton").bind("click", function () {
				mycontent = $(mytext).val();
				$("#note_"+x).html(	mycontent );
				$(mytext).remove();
				$(mybutton).remove();
				$("#note_"+x).show();
				$("#noteholder_"+x).height($("#note_"+x).height());
				$.get( "ajax.wishlistnote.php", { id: x, note: mycontent }, nofunct );
				box = 0;
			});
		});
	}
}

function editTemporaryNote(x)
{
	if ( ! box )
	{
		box = 1;
		$("#note_"+x).hide();
		$("#noteholder_"+x).animate({
			height: 70
		}, 400, "", function () {
			mytext = document.createElement("TEXTAREA");
			$(mytext).val($.trim( $("#note_"+x).html() )).height($("#noteholder_"+x).height()-25).width($("#noteholder_"+x).width()).appendTo("#noteholder_"+x).focus();
			mybutton = document.createElement("INPUT");
			$(mybutton).attr("type","button").attr("value","Update").attr("id","mybutton").appendTo("#noteholder_"+x);
			$("#mybutton").bind("click", function () {
				mycontent = $(mytext).val();
				$("#note_"+x).html(	mycontent );
				$("#wishnote_"+x).html(	mycontent );
				$(mytext).remove();
				$(mybutton).remove();
				$("#note_"+x).show();
				$("#noteholder_"+x).height($("#note_"+x).height());
				$.get( "ajax.wishlistupdate.php", { id: x, note: mycontent }, nofunct );
				box = 0;
			});
		});
	}
}

function addWishlistItemToCart(x)
{
	$.get( "ajax.wishlistaddtocart.php", { id: x }, function (y) {
		try
		{
			src = $("#mainimg_"+x).attr("src");
			z = $("#mainimg_"+x).clone(true).css("position","absolute").css("left",$("#mainimg_"+x).offset().left).css("top",$("#mainimg_"+x).offset().top).insertBefore("#mainimg_"+x).animate({
				width: 0,
				height: 0,
				top: $("#cartcount").offset().top,
				left: $("#cartcount").offset().left
			}, 300, "", function () { /*$(z).remove()*/ } );
		}
		catch( err )
		{
		}
		
		if ( y == 0 )
		{
			$("#cartcount").html("&nbsp;");
			$("#topcartcount").html("Your Cart is Empty");
		}
		else if ( y == 1 )
		{
			$("#cartcount").html("&nbsp;(1)");
			$("#topcartcount").html("Cart has 1 Item");
		}
		else
		{
			$("#cartcount").html("&nbsp;("+y+")");
			$("#topcartcount").html("Cart has "+y+" Items");
		}
		
		loaded = 0;
	} );
	
	return false;
}

function addWishlistAllToCart()
{
	setTimeout( 'doAddWishlistAllToCart()', 100 );
	
	return false;
}

function doAddWishlistAllToCart()
{
	$(".wishlistthumb").each( function () {
		num = $(this).attr("id").substr( 8 );
		addWishlistItemToCart(num);
	} );
}

function pauseFunct(m)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < m);
} 
