$(document).ready(function() {
	$("div#thumbs img:last-child").css({marginRight:"0"});
	$("div#topLevelCategory p:first").css({fontWeight:"bold"});
	var sendtofriendHtml = $("form.sendtofriend").html();
	var feedbackHtml = $("form.feedback").html();
	var contactHtml = $("form#feedbackForm").html();
	//$("div#catListWrapper div:nth-child(3)").css({marginRight:"0"});
	//$("div#catListWrapper div:nth-child(6)").css({marginRight:"0"});
	
	var options = { //Jqzoom options
			zoomWidth: 316,
			zoomHeight: 343,
			xOffset: 10,
			yOffset: 0,
			showEffect: 'fadein',
			fadeinSpeed: 'slow',
			hideEffect: 'fadeout',
			fadeoutSpeed: 'slow',
			lens: true
	};
	$("#product #image a").jqzoom(options);
	
	/*if ($("select.selectinput").length > 0) { //Check if product options exists and hide the quantity box if they are 
		$("div.itemQuantity").hide();
	}*/	

	
	/*$("select.selectinput").change(function() { //Hide show the quantity boxes if product options exists and one is selected/des-selected
		if ($(this).attr('value') > 0) {
			$("input + div.itemQuantity").fadeIn('slow');
		} else {
			$("input + div.itemQuantity").fadeOut('slow');
		}
	});*/
	
	/*$("a.backLink").click(function() {
		history.back();		
	});*/
	
	/* Gallery code below */
	$("#thumbs a").click(function() {
		$(".jqZoomWindow").remove();
		$("#image a").removeAttr("href");
		var src = $(this).attr("href");
		var link = $(this).attr("rel");
		var alt = $(this).attr("title");
		var str = '<a href="'+link+'" title="'+alt+'"><img alt="'+alt+'" src="'+src+'" /></a>';
		$("#product #image a").fadeOut("slow");
		$("#product #image img").remove();
		$("#product #image a").remove();
		$("#product #image").append(str).css('display', 'none');
		$("#product #image").fadeIn(2000);
		$("#product #image a").jqzoom(options); //Rebind jqzoom
		return false;
	});
	
	
	//$("form.sendtofriend div.response").fadeOut();	
	
	/*Send to  a friend form submission*/
	$("form.sendtofriend").submit(function(){
		var formHtml = $("form.sendtofriend").html();
		var url = $("input[name=url]").val();
		var senderName = $("input[name=senderName]").val();
		var senderEmail = $("input[name=senderEmail]").val();
		var recipientName = $("input[name=recipientName]").val();
		var recipientEmail = $("input[name=recipientEmail]").val();
		$.ajax({
			type: "POST",
			url: "/mailer.php",
			data: "url="+url+"&senderName="+senderName+"&senderEmail="+senderEmail+"&recipientName="+recipientName+"&recipientEmail="+recipientEmail,
			dataType: "json",
			success: function(j){
				$.each(j, function(e, val) {
					if (e == "error") {
						$.each(val, function(k, v) {
							$("label."+k+"").addClass("formError").html(v);
						});
					} else {
						$.each(val, function(k, v) {
							$("form.sendtofriend").html(sendtofriendHtml);
							$("div.response").html("Your message has been sent.");
						});
						//$("form.sendtofriend").html(formHtml);
					}
				});
			}
		});
		return false;
	});
	//Dynamically resize sub cat divs depending on how many there are...
	var subCatCount = $("div.category").length;
	if ($("div.category").length < 4 ) {
		//$("div.topLevelCatImg").css("height", "348px");
		//$("div.category").css("margin-bottom", "0");
	}
	
	//Feedback form
	$("form.feedback").submit(function(){
		var formHtml = $("form.feedback").html();
		var name = $("input[name=name]").val();
		var email = $("input[name=email]").val();
		var message = $("textarea[name=message]").val();
		 message = message.replace("&"," and ");
		$.ajax({
			type: "POST",
			url: $(this).attr("action"),
			data: "name="+name+"&email="+email+"&message="+message,
			dataType: "json",
			success: function(j) {
				$.each(j, function(e, val) {
					if (e == "error") {
						$.each(val, function(k, v) {
							$("label."+k+"").addClass("formError").html(v);
						});						
					} else {
						//$("form.feedback").html(formHtml);
						//$("div.response").html("Thank you for sending your feedback.");
						$.each(val, function(k, v) {
							$("form.feedback").html(feedbackHtml);
							$("div.response").html(v);
							//var resp = $("div.response");
							//resp.html(v).fadeIn("slow");
							//doTimeout();
						});
					}
				});	
			}
		});
		return false; 
	});
	
	//Sending a message form
	//$("form#feedbackForm div.response").fadeOut();
	$("form#feedbackForm").submit(function(){
		var formHtml = $("form#feedbackForm").html();
		var name = $("input[name=name]").val();
		var email = $("input[name=email]").val();
		var message = $("textarea[name=message]").val();
		message = message.replace("&"," and ");
		$.ajax({
			type: "POST",
			url: $(this).attr("action"),
			data: "name="+name+"&email="+email+"&message="+message,
			dataType: "json",
			success: function(j) {
				$.each(j, function(e, val) {
					if (e == "error") {
						$.each(val, function(k, v) {
							$("label."+k+"").addClass("formError").html(v);
						});
					} else {
						//$("form#feedbackForm").html(contactHtml);
						//$("div.response").html("Thanks you for sending your message.");
						$.each(val, function(k, v) {
							$("form#feedbackForm").html(contactHtml);
							$("div.response").html(v);
							//var resp = $("div.response");
							//resp.html(v).fadeIn("slow");
							//doTimeout();
						});
					}
				});	
			}
		});
		return false; 
	});
});
