// JavaScript Document
$(document).ready(function(){
  /* register any elements that need validation 
	*	force validation on field change*/
	$(".validate").blur(function() {
		$(this).validate.init(this);
	});
	$(".validate").focus(function() {
		$(this).validate.init(this);
	});
	$("p.cart_button a").click(function(e) {
		$("#cart_add_dialogue").fadeIn('slow');
		e.preventDefault();
	});
	$('.date_picker').each(function() {$(this).calendar(this);});
	$("a.cart_view").click(function(e) {
		$("#cart_view_dialogue").fadeIn('slow');
		$(this).fadeOut('fast');
		e.preventDefault();
	});
	$('img[@src$=.png]').pngfix();
	$("a.more,a.less,a.delete").click(function(e){
		infostring = this.href.split(".php");
		args = infostring[1].split("/");
		$.post("/resources/helpers/cart/update_product.php",
		   { product_id: args[args.length-3], key: args[args.length-2], value: args[args.length-1] },
		   function(data){
			 $.post("/resources/helpers/cart/update_cart_view.php",function(data){
				if (data== ''){
					$("#cart_view_dialogue").fadeOut("slow");
				}else{
					$("#cart_view_dialogue").html(data)
				}
			});
			 $.post("/resources/helpers/cart/update_cart_shortlist.php",function(data){
				if (data == ''){
					$("ul.shortlist").parent().fadeOut("slow");
				}else{
					$("ul.shortlist").html(data);
				}
			});
		   }
		 );
		e.preventDefault();
	});
	$("form").submit(function(e){
		var approve = true;
		$(this).find(".validate").each(function() {
			$(this).validate.init(this);
			//approve = false;
		});
		$(this).find(".error").each(function() {approve = false});
		if (!approve){
			e.preventDefault();
		}
	});
});	

function register(obj){
		//obj = $(obj);
		//alert("test");
	obj.click(function(e){
		infostring = this.href.split(".php");
		args = infostring[1].split("/");
		$.post("/resources/helpers/cart/update_product.php",
		   { product_id: args[args.length-3], key: args[args.length-2], value: args[args.length-1] },
		   function(data){
			 $.post("/resources/helpers/cart/update_cart_view.php",function(data){
				if (data== ''){
					$("#cart_view_dialogue").fadeOut("slow");
				}else{
					$("#cart_view_dialogue").html(data)
				}
			});
			 $.post("/resources/helpers/cart/update_cart_shortlist.php",function(data){
				if (data == ''){
					$("ul.shortlist").parent().fadeOut("slow");
				}else{
					$("ul.shortlist").html(data);
				}
				//$("ul.shortlist").html(data)
			});
		   }
		 );
		e.preventDefault();
	});
}
function less(obj){
	infostring = obj.split(".php/products/");
	args = infostring[1].split("/");
	$.post("/resources/helpers/cart/update_product.php",
	   { product_id: args[3], key: args[4], value: args[5] },
	   function(data){
		 $.post("/resources/helpers/cart/update_cart_view.php",function(data){$("#cart_view_dialogue").html(data)});
		 e.preventDefault();
	   }
	 );
}
function more(obj){
		infostring = $(obj).href.split(".php");
		args = infostring[1].split("/");
		$.post("/resources/helpers/cart/update_product.php",
		   { product_id: args[args.length-3], key: args[args.length-2], value: args[args.length-1] },
		   function(data){
			 $.post("/resources/helpers/cart/update_cart_view.php",function(data){
				if (data== ''){
					$("#cart_view_dialogue").fadeOut("slow");
				}else{
					$("#cart_view_dialogue").html(data)
				}
			});
			 $.post("/resources/helpers/cart/update_cart_shortlist.php",function(data){
				if (data == ''){
					$("ul.shortlist").parent().fadeOut("slow");
				}else{
					$("ul.shortlist").html(data);
				}
				//$("ul.shortlist").html(data)
			});
		   }
		 );
		e.preventDefault();
}
function cancel_add(){
	$("#cart_add_dialogue").fadeOut('slow');
}
function add(argString){
	sizes = -1;
	custom_inputs = -1;
	$(".sizes").each(function(){
		if (sizes == -1){
			sizes = false;
		}
		if ($(this).attr("checked") || $(this).attr("type") == 'hidden'){
			argString += "/code/"+$(this).attr("value");
			sizes = true;
		}
	});
	custStart = "/custom/";
	custDivider = "";
	$(".custom_inputs").each(function(){
		if (custom_inputs == -1){
			custom_inputs = false;
		}
		if ($(this).attr("value") != '' && typeof($(this).attr("value")) != 'undefined'){
			argString += custStart + custDivider + $(this).attr("name")+"="+$(this).attr("value");
			custStart = "";
			custDivider = ";";
			custom_inputs = true;
		}
	});
	if (!sizes || !custom_inputs){
		if (!sizes){
			alert("you must make a selection");
		}else{
			alert("you must supply personalized text to continue");
		}
	}else{
		document.location.href=argString;
	}
}
function cancel_view(){
	$("#cart_view_dialogue").fadeOut('slow');
	$("a.cart_view").fadeIn('fast');
}

