// (c) 2010 jdbartlett, MIT license
(function(a){a.fn.loupe=function(b){var c=a.extend({loupe:"loupe",width:200,height:150},b||{});return this.length?this.each(function(){var j=a(this),g,k,f=j.is("img")?j:j.find("img:first"),e,h=function(){k.hide()},i;if(j.data("loupe")!=null){return j.data("loupe",b)}e=function(p){var o=f.offset(),q=f.outerWidth(),m=f.outerHeight(),l=c.width/2,n=c.height/2;if(!j.data("loupe")||p.pageX>q+o.left+10||p.pageX<o.left-10||p.pageY>m+o.top+10||p.pageY<o.top-10){return h()}i=i?clearTimeout(i):0;k.show().css({left:p.pageX-l,top:p.pageY-n});g.css({left:-(((p.pageX-o.left)/q)*g.width()-l)|0,top:-(((p.pageY-o.top)/m)*g.height()-n)|0})};k=a("<div />").addClass(c.loupe).css({width:c.width,height:c.height,position:"absolute",overflow:"hidden"}).append(g=a("<img />").attr("src",j.attr(j.is("img")?"src":"href")).css("position","absolute")).mousemove(e).hide().appendTo("body");j.data("loupe",true).mouseenter(e).mouseout(function(){i=setTimeout(h,10)})}):this}}(jQuery));

$(document).ready(function() {

	var origsrc=$("#productMainImage img").attr("src");
	
	$("#" + $("select#stores").val()).show();

	$("input").focus(function() {
		storval=$(this).val();
		$(this).val("");
	});
	
	$("input").blur(function() {
		if($(this).val()=="") {					 
			$(this).val(storval);
		}
	});
	
	$("select#stores").change(function() {
		$(".statestores").hide();
		$("#" + $(this).val()).show();
	});
	
	$("a.presslink").click(function() {
		$("#pressContent").addClass("loading").html("<img src='includes/templates/vkoo/images/press/" + $(this).attr("iloc") + ".jpg' alt='' class='nodisp'>");
		$("#pressContent img").load(function() {
			$(this).fadeIn("slow", function() {
				$("#pressContent").removeClass("loading");
			});
		}) 
	})
	
	$("input#attrib-1-23").parent().hide();
	$("input#attrib-2-22").parent().hide();	
		
	$("a.imageSwitch img").click(function() {
		var src=$(this).attr("src").split("/")[$(this).attr("src").split("/").length-1];
		src=src.replace(".jpg","_LRG.jpg");
		var img=$("#productMainImage img");
		var alt=img.attr("alt");	
		$.ajax({
	    	url:'images/loupe/' + src,
	    	type:'HEAD',
	    	success: function() {
	        	var img=$("<img>").attr({
		        		src:"images/loupe/" + src,
		        		width:325,
		        		height:460,
		        		alt:alt})
	        		.css({
	        			width:325,
	        			height:460
	        		})
	        		.load(function() {
					$("#productMainImage").html(img);
					img.loupe();
				});
	    	},
	    	error: function() {
	        	var img=$("<img>").attr({
		        		src:"images/alt_imgs/" + src,
		        		width:325,
		        		height:460,
		        		alt:alt})
	        		.css({
	        			width:325,
	        			height:460
	        		})
	        		.load(function() {
					$("#productMainImage").html(img);
				});
	    	}
		});
	});
		
	$("label.attribsRadioButton img").click(function() {
		j=0;
		$(this).parent().parent().parent().find(".attribImg").find("label").find("img").removeClass("selected");
		$(this).addClass("selected");
		attribs=new Array;
		if($("input#" + $(this).parent().attr("for")).attr("name").substr(3,1)==1) { 
			option_id=2;
		} else {
			option_id=1;
		}
		var result = 'prod_id=' + $("input[name=products_id]").attr("value") + '&option_id=' + option_id + '&option_val_id=' + $("input#" + $(this).parent().attr("for")).attr("value");
		$.post('includes/templates/vkoo/jscript/ajax/get_attribs.php', result, function(data) {
			$.each($("label.attribsRadioButton"), function () {
				if($(this).attr("for").match("-" + option_id + "-")) {
					$(this).parent().hide();
				}
			});
			for (i in data) {
				$("label.attribsRadioButton[for=attrib-" + option_id + "-" + data[i] + "]").parent().show();
			}
		},"json");
		attrcount=$(this).attr("alt").substr($(this).attr("alt").indexOf("_")+1,1);
		color=$(this).attr("alt").substr(0,$(this).attr("alt").indexOf("_"));
		$("span.sizecontainer" + attrcount).text(color);
		
		var img=$("#productMainImage img");
		var alt=img.attr("alt");	
		var src=origsrc.split("/")[origsrc.split("/").length-1];
		src=src.replace(".jpg", "_" + color.toLowerCase() + ".jpg");
		src=src.replace(". ","");
		src=src.replace("ltgrey","lightgrey");
		src=src.replace("/","-")
		$.ajax({
	    	url:'images/loupe/' + src,
	    	type:'HEAD',
	    	success: function() {
	        	var img=$("<img>").attr({"src":"images/loupe/" + src,"width":325,"height":460,"alt":alt}).load(function() {
					$("#productMainImage").html(img);
					img.loupe();
				});
	    	},
	    	error: function() {
	    		var img=$("<img>").attr({"src":"images/alt_colors/" + src,"width":325,"height":460,"alt":alt}).load(function() {
					$("#productMainImage").html(img);
				});
	    	}
		});

		$("input#" + $(this).parent().attr("for")).attr("checked",true);
	});
	
	$("#product_add_form").submit(function() {
		if($(".sizecontainer1").text()=="" && $(".sizecontainer2").text()=="") {
			alert("Please select both a size and a color.");
			return false;
		} else if($(".sizecontainer1").text()=="") {
			alert("Please select a size.");
			return false;
		} else if($(".sizecontainer2").text()=="") {
			alert("Please select a color.");
			return false;
		} else {
			return true;
		}
	}); 
		
	$("input[name='payment']").change(function() {
		if($(this).attr("id")=="pmt-authorizenet_aim") {
			$("div.ccinfo").show("slow");
		} else {
			$("div.ccinfo").hide("slow");
		}
	})
		
	if($("input#pmt-authorizenet_aim").attr("checked")) {
		$("div.ccinfo").show();
	}
	
	$("input#postcode").keyup(function() {
		if($(this).val().search(/[^\d]/)>-1) {
			alert("Please enter your 5-digit numeric ZIP code.");
			$(this).val("");
		}
	});
	
	if($("img.loupeImg").length)
		loupeIt($("img.loupeImg"));
	
	$("#cartAdd input[type='image']").hover(
		function() {
			var src=$(this).attr("src");
			$(this).attr("src", src.replace(".gif","_on.gif"));
		},
		function() {
			var src=$(this).attr("src");
			$(this).attr("src", src.replace("_on.gif",".gif"));
		}
	);
	
	$("a.size_chart_link").fancybox({
		showCloseButton:false
	});
			
});

function loupeIt(img) {
	var img=$(img);
	var src=img.attr("src").split("/")[img.attr("src").split("/").length-1];
	$.ajax({
    	url:'images/loupe/' + src,
    	type:'HEAD',
    	success: function() {
        	img.attr("src","images/loupe/" + src).loupe();
    	}
	});
}
