jQuery(InitSite);

var selectweight=null;

cart={
	refreshCart: function()
	{
		var ah=new ajaxHandler();
		ah.request(baseurl+"ajax/getcart",
			function(data)
			{
				$("#cart_total").fadeTo(200,0.1).html(data.total).fadeTo(200,1);
				$("#cart_contents").fadeTo(200.5,0.1).html(data.message).fadeTo(200,1);
			}
		);
	},
	addItem: function(id, qty, weight)
	{
		var ah=new ajaxHandler();
		var url=baseurl+"ajax/addtocart/"+id+"/"+qty;
		
		if(weight!=null)
			url+="/"+weight;
		else
			url+="/null";
		
		ah.request(url,
			function(data)
			{			
				var windowpos=$(window).scrollTop();
				var basketpos=$("#deadline_container").offset().top;
				var topoffset=50;
				var bottomoffset=20;
			
				var lastrow=($("#cart_contents tr:last").height()*2);
			
				if(windowpos>(basketpos-topoffset) && windowpos<(basketpos+bottomoffset))
				{
					$(window).scrollTop($("#cart_contents tr:last").offset().top);
					cart.refreshCart();
				}
				else
				{
					if(windowpos > basketpos)
					{
						$(".cartbubble").remove();
						
						$("#page").prepend("<div class='cartbubble'>"+data.message+"</div>").children(".cartbubble").fadeIn().delay(5000).fadeOut();
						
						$(window).unbind("scroll").scroll(
							function(e)
							{
								if($(this).scrollTop()<=$("#deadline_container").offset().top)
								{
									cart.refreshCart();
									$(this).unbind("scroll");
									$(".cartbubble").fadeOut().remove();
								}
							}
						);
					}
					else
						cart.refreshCart();
				}
			}
		);
	}
};

function ajaxHandler()
{
	this.request=function(url,callback)
	{
		this.finished=callback;
		var parentObj=this;
		
		$.ajax({
			async: true,
			url: url+"/"+Math.round(new Date().getTime() / 1000),
			dataType: 'json',
			success: function(data, textStatus, XMLHttpRequest){parentObj.success(data,textStatus,XMLHttpRequest);}
		});		
	};
	
	this.success=function(data, textStatus, XMLHttpRequest)
	{
		if(data.status=="OK")
			this.finished(data);
		else
			this.error(data.message);
	};
	
	this.error=function(errorText)
	{
		alert("error method thrown: "+errorText);
	};
}


function InitSite()
{
	var cufon=".cufon, h1,h2:not(.nocufon),h3";
	
	Cufon.now();
	Cufon.replace(cufon);
	Cufon.replace("#nav li",{hover: {color: '#8b0f26'}});
	
    if($("#account_nav").size())
    {
        $("#account_nav a").each(
            function()
            {        
                if($(this).attr("href")==window.location.href)
                    $(this).addClass("current");
            }
        );
    }	
	
	$("#search").focus(
		function()
		{
			if($(this).hasClass("defaultstate"))
				$(this).val("").removeClass("defaultstate");
		}
	);
	
	$("#search").blur(
		function()
		{
			if($(this).val()=="")
				$(this).addClass("defaultstate").val("what are you looking for?");
		}
	);
	
	setTimeout(
		function()
		{
			Cufon.refresh(cufon);
			Cufon.refresh("#nav li");
		}
		,500
	);
	
	$("a.addbutton:not(.selectweight)").click(
		function(e)
		{
			e.preventDefault();
			
			var qty=1;
			var productid=$(this).attr("id").substring(8);
			
			if($("#product-"+productid+"-qty").size())
			{
				if(!isNaN($("#product-"+productid+"-qty").val()))
					qty=$("#product-"+productid+"-qty").val();	
			}
				
			cart.addItem(productid,qty,null);
		}
	);
	
	$("#weight").live("keyup",
		function()
		{
			if($(this).val().length && !isNaN($(this).val()))
				$("#selectweight_button").removeClass("disabled").attr("src",$("#selectweight_button").attr("src").replace("add_button_disabled.png","add_button_green.png"));
			else
				$("#selectweight_button").addClass("disabled").attr("src",$("#selectweight_button").attr("src").replace("add_button_green.png","add_button_disabled.png"));
		}
	);
	
	$("a.addbutton.selectweight").click(
		function(e)
		{
			e.preventDefault();
			
			//$(this).children("img").fadeOut().addClass("spinner").attr("src",baseurl+"assets/img/pleasewait.gif").fadeIn();
			
			var productid=$(this).attr("title","Loading...").attr("id").substring(8);
			
			// turn add button gray?
			$(this).children("img:first").fadeTo(0.2);
			//$(this).children("img:first").attr("src",$(this).children("img:first").attr("src").replace("add_button.png","add_button_disabled.png"));
			
			$(this).removeData("tooltip").tooltip({
				cancelDefault: true,
				position: 'bottom center',
				tipClass: 'tooltip_weight',
				effect: 'fade',
				offset: [7,5]
			});

			selectweight=$(this).data("tooltip");
			$(this).unbind("mouseleave").unbind("mouseover").data("tooltip").show();
			
			ah=new ajaxHandler();
			ah.request(baseurl+"ajax/selectweight/"+productid,
				function(json)
				{
					$("div.tooltip_weight").html(json.message);
					$("div.tooltip_weight a.close").click(
						function(e)
						{
							e.preventDefault();
							selectweight.hide();
							
							$(".tooltip_weight").remove();
							return false;
						}
					);
					
					$("#selectweight_button").click(
						function(e)
						{
							e.preventDefault();
							$("#selectweight_form").submit();
						}
					);
					
					$("#selectweight_form").submit(
						function(e)
						{
							e.preventDefault();
							
							if($("#selectweight_button").hasClass("disabled")===false)
							{
								cart.addItem(productid,1,$("#weight").val());
								selectweight.hide();
								
								$(".tooltip_weight").remove();
								
							}
						}
					);
					
					$("#weight").focus();
				}
			);
		}
	);
	
	$("form.validate").validator();
	
	$("a.changedetails").click(
		function(e)
		{
			e.preventDefault();
			var ah=new ajaxHandler();
			ah.request(baseurl+"ajax/changedetails",
				function(data)
				{
					$.fancybox({
						'content': data.message,
						'onComplete': function()
						{
							$("form.validate").validator();
						},
						'onCleanup': function()
						{
							$(".tooltip_validate").fadeOut().remove();
						}
					});
				}
			);
		}
	);
	
	$("a.changedeliverydetails").click(
		function(e)
		{
			e.preventDefault();
			var ah=new ajaxHandler();
			ah.request(baseurl+"ajax/changedeliverydetails",
				function(data)
				{
					$.fancybox({
						'content': data.message,
						'onComplete': function()
						{
							$("form.validate").validator();
						},
						'onCleanup': function()
						{
							$(".tooltip_validate").fadeOut().remove();
						}
					});
				}
			);
		}
	);
	
	//$(".success, .error").delay(3500).slideUp();
	
	$("a.confirm").click(
		function(e)
		{
			e.preventDefault();
			if(confirm("Are you sure you want to delete this "+$(this).attr("title")+"?"))
				window.location=$(this).attr("href");
		}
	);
	
	$("a.supplierinfo").click(
		function(e)
		{
			e.preventDefault();
			
			$(this).unbind("click").text("Please Wait..");
			
			ah=new ajaxHandler();
			ah.request(baseurl+"ajax/supplierinfo/"+$(this).attr("id").substring(9),
				function(data)
				{
					$("a.supplierinfo").parent().append("<p class='hidden'>"+data.message+"</p>").andSelf().next("p.hidden").slideDown().removeClass("hidden");
					$("a.supplierinfo").fadeOut().remove();
				}
			);
			

		}
	);
	
	$("a.fancyenlarge").fancybox();
	
	$("#col1 ul.categorylist li").click(
		function(e)
		{
			window.location=$(this).children("a:first").attr("href");
		}
	);
}


