$(document).ready(function() {
	
	/* *** Enter the Cufon *** */
	Cufon.replace('h1 strong, .tips h3, #end small, #signup small, .pricingcard li span, .pricingcard h4, #right h4, #foot h4, .range h4, .head h3, .artwork h3, #intro .contact, #head strong, .range h3, .toptip h4, .special h4, .why h4, #page .generic h1, #intro h1, .featured h3, .other h3, .packages h3, .prices .left h3, .prices .right p');
	
	/* *** Intro Shadow *** */
	$("#intro, #banners").append("<span class='shadow'></span>");
    
    /* *** Cards Tab *** */
	var cardsTab = {    
		sensitivity: 40, // number = sensitivity threshold (must be 1 or higher)    
		interval: 100, // number = milliseconds for onMouseOver polling interval    
		over: cardsOn, // function = onMouseOver callback (REQUIRED)    
		timeout: 100, // number = milliseconds delay before onMouseOut    
		out: cardsOff // function = onMouseOut callback (REQUIRED)    
	};
	function cardsOn(){
		$(this).children().stop(true, true);
		$(this).children().animate( { left: "0px" }, 200);
	}
	function cardsOff(){
		$(this).children().animate( { left: "-235px" }, 200 );
	}
	if ($("body").hasClass("cards")) {
		$("#navigation li.cards a").children().css({ 'left' : '0px' });
	} else {
		$("#navigation li.cards a").hoverIntent( cardsTab );
	}
	
	 /* *** Packaging Tab *** */
	var packagingTab = {    
		sensitivity: 40, // number = sensitivity threshold (must be 1 or higher)    
		interval: 100, // number = milliseconds for onMouseOver polling interval    
		over: packagingOn, // function = onMouseOver callback (REQUIRED)    
		timeout: 100, // number = milliseconds delay before onMouseOut    
		out: packagingOff // function = onMouseOut callback (REQUIRED)    
	};
	function packagingOn(){
		$(this).children().stop(true, true);
		$(this).children().animate( { left: "0px" }, 200);
	}
	function packagingOff(){
		$(this).children().animate( { left: "-235px" }, 200 );
	}
	if ($("body").hasClass("packaging")) {
		$("#navigation li.packaging a").children().css({ 'left' : '0px' });
	} else {
		$("#navigation li.packaging a").hoverIntent( packagingTab );
	}
    
    /* *** Prices Tab *** */
    var pricesTab = {    
		sensitivity: 40,
		interval: 100,
		over: pricesOn,
		timeout: 100,
		out: pricesOff
	};
	function pricesOn(){
		$(this).children().stop(true, true);
		$(this).children().animate( { left: "0px" }, 200);
	}
	function pricesOff(){
		$(this).children().animate( { left: "-215px" }, 200 );
	}
    if ($("body").hasClass("prices")) {
		$("#navigation li.prices a").children().css({ 'left' : '0px' });
	} else {
		$("#navigation li.prices a").hoverIntent( pricesTab );
	}
	
	/* *** Quote Tab *** */
	var quoteTab = {    
		sensitivity: 40,
		interval: 100,
		over: quoteOn,
		timeout: 100,
		out: quoteOff
	};
	function quoteOn(){
		$(this).children().stop(true, true);
		$(this).children().animate( { left: "0px" }, 200);
	}
	function quoteOff(){
		$(this).children().animate( { left: "-272px" }, 200 );
	}
    if ($("body").hasClass("quote")) {
		$("#navigation li.quote a").children().css({ 'left' : '0px' });
	} else {
		$("#navigation li.quote a").hoverIntent( quoteTab );
	}
    
    /* *** Contact Tab *** */
    var contactTab = {    
		sensitivity: 40,    
		interval: 100,    
		over: contactOn,   
		timeout: 100,    
		out: contactOff    
	};
	function contactOn(){
		$(this).children().stop(true, true);
		$(this).children().animate( { left: "0px" }, 200);
	}
	function contactOff(){
		$(this).children().animate( { left: "-270px" }, 200 );
	}
    if ($("body").hasClass("contact")) {
		$("#navigation li.contact a").children().css({ 'left' : '0px' });
	} else {
		$("#navigation li.contact a").hoverIntent( contactTab );
	}
	
	/* *** Logo Glow *** */
	$("#head h1 a").hover(function(){
		$(this).stop(true, true);
    	$(this).animate( { opacity: 0.1 }, 300);
	},
	function(){
		$(this).animate( { opacity: 1.0 }, 200);
	});

	/* *** Quote Button *** */
	$("#left h1 a").fadeTo("fast", 0.9);
	$("#left h1 a").hover(function(){
		$(this).stop(true, true);
    	$(this).animate( { marginRight: "0px", opacity: 1.0 }, 200);
	},
	function(){
		$(this).animate( { marginRight: "4px", opacity: 0.9 }, 200);
	});
	
	/* *** Quote Button (Home) *** */
	$(".leading h3 a.quote").fadeTo("fast", 0.9);
	$(".leading h3 a.quote").hover(function(){
		$(this).stop(true, true);
    	$(this).animate( { marginLeft: "4px", opacity: 1.0 }, 200);
	},
	function(){
		$(this).animate( { marginLeft: "0px", opacity: 0.9 }, 200);
	});
	
	$(".prices .left h3 a").fadeTo("fast", 0.9);
	$(".prices .left h3 a").hover(function(){
		$(this).stop(true, true);
    	$(this).animate( { right: "-4px", opacity: 1.0 }, 200);
	},
	function(){
		$(this).animate( { right: "0px", opacity: 0.9 }, 200);
	});
	
	/* *** Prices Action Buttons *** */
	$(".action p a").fadeTo("fast", 0.9);
	$(".action p a").hover(function(){
		$(this).stop(true, true);
    	$(this).animate( { left: "4px", opacity: 1.0 }, 200);
	},
	function(){
		$(this).animate( { left: "0px", opacity: 0.9 }, 200);
	});
	
	/* *** Quote Button (Prices) *** */
	$(".prices .head a.quote").fadeTo("fast", 0.9);
	$(".prices .head a.quote").hover(function(){
		$(this).stop(true, true);
    	$(this).animate( { right: "21px", opacity: 1.0 }, 200);
	},
	function(){
		$(this).animate( { right: "24px", opacity: 0.9 }, 200);
	});
	
	/* *** Go to Card Button (Prices) *** */
	$(".prices .head a.link").fadeTo("fast", 0.9);
	$(".prices .head a.link").hover(function(){
		$(this).stop(true, true);
    	$(this).animate( { right: "20px", opacity: 1.0 }, 200);
	},
	function(){
		$(this).animate( { right: "25px", opacity: 0.9 }, 200);
	});
	
	/* *** Promo Ads (Home) *** */
	$("#banners .ad").hover(function(){
		$(this).stop(true, true);
    	$(this).animate( { right: "4px" }, 100);
	},
	function(){
		$(this).animate( { right: "0px" }, 100);
	});
	
	/* *** Hub Photos *** */
	$(".featured img, .other img, .packages img, .sticker img").hover(function(){
		$(this).stop(true, true);
    	$(this).animate( { marginTop: "-3px" }, 100);
	},
	function(){
		$(this).animate( { marginTop: "0px" }, 100);
	});
	$("ul.gallery img").hover(function(){
		$(this).stop(true, true);
    	$(this).animate( { top: "3px", opacity: 0.8 }, 100);
	},
	function(){
		$(this).animate( { top: "0px", opacity: 1 }, 100);
	});
	
	/* *** Card Range *** */
	$(".range a").hover(function(){
		$(this).children().stop(true, true);
    	$(this).children("img").animate( { top: "2px" }, 100);
    	$(this).children("em").animate( { opacity: 0.6 }, 200);
	},
	function(){
		$(this).children("img").animate( { top: "0px" }, 100);
    	$(this).children("em").animate( { opacity: 1.0 }, 200);
	});
	
	/* *** Extras Button *** */
	$("li.extras a, .spec h4 a, .featured h3 a, .other p a, .spec a").fadeTo("fast", 0.8);
	$("li.extras a, .spec h4 a, .featured h3 a, .other p a, .spec a").hover(function(){
		$(this).stop(true, true);
    	$(this).fadeTo("fast", 1.0);
	},
	function(){
		$(this).fadeTo("fast", 0.8);
	});
	$(".packages h3 a").fadeTo("fast", 0.9);
	$(".packages h3 a").hover(function(){
		$(this).stop(true, true);
    	$(this).fadeTo("fast", 1.0);
	},
	function(){
		$(this).fadeTo("fast", 0.9);
	});

	
	/* *** Email Link *** */
	$("#intro .contact a").hover(function(){
		$(this).stop(true, true);
    	$(this).animate( { opacity: 0.7 }, 300);
	},
	function(){
		$(this).animate( { opacity: 1.0 }, 300);
	});
	
	/* *** Signup Button *** */
	$("#signup fieldset.button input, .form fieldset input.submitform").hover(function(){
		$(this).stop(true, true);
    	$(this).fadeTo("fast", 0.8);
	},
	function(){
		$(this).fadeTo("fast", 1.0);
	});
	
	/* *** Signup Input *** */
	$("#foot input.email").focus(function(){
		if ( $(this).val() == "Enter your email address")
        $(this).val('');
	});
	$("#foot input.email").blur(function(){
		if ( $(this).val() == "")
		$(this).val('Enter your email address');
	});
	
	/* *** Pricing Nav *** */
	$("li.howmuch").addClass("active");
	$("li.howmuch").click(function(){
		$("li.spec").removeClass("active");
		$(this).addClass("active");
		$(".scroller .chart").animate( { left: "0px" } );
		$(".scroller .spec").animate( { left: "390px" } );
		return false;
	});
	$("li.spec").click(function(){
		$("li.howmuch").removeClass("active");
		$(this).addClass("active");
		$(".scroller .chart").animate( { left: "-380px" } );
		$(".scroller .spec").animate( { left: "0px" } );
		return false;
	});
	
	/* *** Pricing Tables *** */
	$(".chart table td, .chart table th, .big-chart table td, .big-chart table th").nthCol(1).addClass("amount");
	$(".chart table td, .chart table th, .big-chart table td, .big-chart table th").nthCol(2).addClass("highlight");
	$(".chart table td:eq(26), #contactless .chart table td:eq(21), #promotional .chart table td:eq(21), #keytag .chart table td:eq(17), .big-chart td:eq(26), .big-chart td:eq(56), .big-chart td:eq(86), .big-chart td:eq(116), .big-chart td:eq(146), .big-chart td:eq(176), .big-chart td:eq(206), .big-chart td:eq(236)").css({ 'padding-bottom' : '5px', 'border-bottom' : '1px solid #e0f2f5' });
	$(".chart table th, .chart table td").nthCol(3).css({ 'padding-left' : '20px' });
	$(".chart table th, .chart table td, .big-chart table th, .big-chart table td").nthCol(5).css({ 'padding-right' : '0px' });
	$(".prices table th:last-child").css({ 'border-right' : '1px solid #dff2f5' });
	$(".prices table td:nth-child(2n+3)").css({ 'background-color' : '#f8fbfc' });
	
	/* *** Fancyzoom *** */
	$("li.extras a, .examples a, .prices h4 a, ul.gallery a, .spec dd a, .prices p a.extras").fancybox();
	
	/* *** Example Cards *** */
	$(".examples a").hover(function(){
		$(this).stop(true, true);
    	$(this).children().animate( { left: "3px", opacity: 0.8 }, 100);
	},
	function(){
		$(this).children().animate( { left: "0px", opacity: 1.0 }, 100);
	});
	
	/* *** Artwork Dropdown *** */
	$("ol li.artwork").click(function(){
		$("#dropit").slideToggle("fast");
		return false;
	});
	
	/* *** Forms *** */
	// Ajax form here
    $('#the-form form').ajaxForm({ 
      beforeSubmit: function(){
        return $('.validate').valid()
      },
      success: function(){
        window.location.replace("http://www.theplasticcardshop.com/thanks/");
      }
    });
    $('#subForm').validate();
    $('#unsubForm').validate();
    
    // Campaign Monitor
    $(function()
	{
		$("#subForm input:submit").click(function() {	
			
			// First, disable the form from submitting
			$('form#subForm').submit(function() { return false; });
			
			// Grab form action
			formAction = $("form#subForm").attr("action");
			
			// Hacking together id for email field
			// Replace the xxxxx below:
			// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
			emailId = "xxxxx";
			emailId = emailId.replace("/", "");
			emailId = emailId + "-" + emailId;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				alert("Please enter a valid email address");
				return;
			}
			
			// Serialize form values to be submitted with POST
			var str = $("form#subForm").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "proxy.php",
				type: "POST",
				data: final,
				success: function(html){
					$("#theForm").hide(); // If successfully submitted hides the form
					$("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
				}
			});
		});
	});
	function checkEmail(email)
	{	
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	}
	
	/* *** PNG Fix *** */
	$(document).pngFix();
			
});