// 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();

	$("#cart_view_dialogue a.more,#cart_view_dialogue a.less,#cart_view_dialogue a.delete").click(function(e){
		infostring = this.href.split(".php/");
		args = infostring[1].split("/");
		/*alert("item_id: "+args[args.length-3]+", key: "+args[args.length-2]+", value: "+args[args.length-1]); */
		$.post("/resources/helpers/cart/update_item.php",
		   { item_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();
	});
	
	$(".validate").blur(function() {
		$(this).validate.init(this);
	});
	
	$(".validate").focus(function() {
		$(this).validate.init(this);
	});
	
	$("form#order,form#checkout,form#orders,form#contact").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).click(function(e){
		infostring = this.href.split(".php");
		args = infostring[1].split("/");
		$.post("/resources/helpers/cart/update_item.php",
		   { item_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();
	});
}

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_add(){
	$("#cart_add_dialogue").fadeOut('slow');
}
function cancel_view(){
	$("#cart_view_dialogue").fadeOut('slow');
	$("a.cart_view").fadeIn('fast');
}
