/*===========================================================================
	global.js
	
	This JavaScript executes dynamic behaviors such as:
	- Patch missing JavaScript features for certain browser versions
	- Define commonly used functions and variables
	- Preload images
	- Flash Replacements (i.e. sIFR, UFO, SWFObject, etc.)
	- Decorate the DOM with presentational elements
	- Collapse/expand/animate sections
	- Validate forms
	- Pop-up windows
===========================================================================*/

$j = jQuery.noConflict();

$j(document).ready(function() {
	
	// Set active class on primary nav
	var url = location.pathname;
	url = url.substr(1);
	
	// If visiting coupon page when 18+ laws failed:
	if (url == "coupons" && $j.cookie("onecareAgeVerification") == 1) {
			$j("#contentBlock-2").html('<p>We&rsquo;re sorry but we can&rsquo;t accept your messages or personal information because you do not meet our eligibility requirements. But that doesn&rsquo;t mean you can&rsquo;t explore the rest of our site and learn more about us! <a href="/">Click here</a> to return to the homepage.</p>');
		}
	
	if (!url) {
		$j("li#nav-home").addClass("active");
	} else {
		$j("li#nav-" + url).addClass("active");
	}
	
	// Place labels in subscription form fields - only for home page
	$j("label.hint").labelOver("over");
	
	$j("div.translucent input#jimty-jimty").one("focus", function() {
		$j(this).removeAttr("value");
	});
	
	// Submit subscription form to Campaign Monitor
	$j("#subscribe-form").submit(function() {
		var form = $j(this);
		log("form submitted");
		
		if (!checkEmail($j("#jimty-jimty").val())) {
			log("Email provided tested invalid");
			alert("Please enter a valid email address");
			return false;
		}
		
		var str = form.serialize();
		log(str);
		str += "&action=" + form.attr("action");
		log(str);
		
		$j.ajax({
			url: "/fileadmin/templates/downyez.com/cm-proxy.php",
			type: "POST",
			data: str,
			success: function(html) {
				form.html("<p>Thank you for subscribing.</p>");
			}
		});
		
		return false;
	});
	
	// Home page flash
	/*$j('#flash-wrapper').flash({
		src: '/fileadmin/media/ClothesLine5.swf',
		width: 1600,
		height: 375,
		wmode: 'transparent'
	});*/
	
	// Clothesline
	$j("#slide-right").click(function(){
			$j("#clothesline").animate({"left": "-=300px"}, "slow");
		});

	    $j("#slide-left").click(function(){
			$j("#clothesline").animate({"left": "+=300px"}, "slow");
	    });
	
	// Show/hide coupon form controls
	var emailForm = $j("div#email-form-wrapper");
	var regMailForm = $j("div#regular-mail-form-wrapper");
	
	// If confirmation page, unhide the appropriate wrapper
	$j(".tx-powermail-pi1_thx").closest("div[id$='wrapper']").show();
	
	$j("a#show-email-form").click(function() {
		emailForm.show();
		regMailForm.hide();
		
		return false;
	});
	
	$j("a#show-mail-form").click(function() {
		regMailForm.show();
		emailForm.hide();
		
		return false;
	});
	
	/* FORMSPRING VALIDATION */
	/* Email form */
	$j("#fsSubmitButton763608").attr("onclick","");
	$j("#fsSubmitButton763608").click(function() {
		if ($j("#field7653993_2").is(":checked")) {
			$j.cookie('onecareAgeVerification', '1', { expires: null});
			$j("#contentBlock-2").html('<p>We&rsquo;re sorry but we can&rsquo;t accept your messages or personal information because you do not meet our eligibility requirements. But that doesn&rsquo;t mean you can&rsquo;t explore the rest of our site and learn more about us! <a href="/">Click here</a> to return to the homepage.</p>');
			return false;
		}
	});
	
	/* Mail form */
	$j("#fsSubmitButton763609").attr("onclick","");
	$j("#fsSubmitButton763609").click(function() {
		if ($j("#field7654004_2").is(":checked")) {
			$j.cookie('onecareAgeVerification', '1', { expires: null});
			$j("#contentBlock-2").html('<p>We&rsquo;re sorry but we can&rsquo;t accept your messages or personal information because you do not meet our eligibility requirements. But that doesn&rsquo;t mean you can&rsquo;t explore the rest of our site and learn more about us! <a href="/">Click here</a> to return to the homepage.</p>');
			return false;
		}
	});
		
	// Coupon Form Validation
	// $j("form#downloadcoupon").submit(function() {
	// 	var failed = 0;
	// 	$j(this).find("input.required").each(function() {
	// 		if (!$j(this).val()) {
	// 			$j(this).css("border","1px solid red");
	// 			failed = 1;
	// 		}
	// 	});
	// 	
	// 	if (failed) {
	// 		$j("p.messaging").text("Please complete all required fields");
	// 		$j("p.messaging").show();
	// 		return false;
	// 	}
	// });
});

/* Utilities
===========================================================================*/

function checkEmail(email) {
	log("Validating email: " + email);
	var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	log(pattern.test(email));
	return pattern.test(email);
}

jQuery.fn.labelOver = function(overClass) {
	return this.each(function(){
		var label = jQuery(this);
		var f = label.attr('for');
		if (f) {
			var input = jQuery('#' + f);
			
			this.hide = function() {
			  label.css({ textIndent: -10000 })
			}
			
			this.show = function() {
			  if (input.val() == '') label.css({ textIndent: 0 })
			}

			// handlers
			input.focus(this.hide);
			input.blur(this.show);
		  label.addClass(overClass).click(function(){ input.focus() });
			
			if (input.val() != '') this.hide(); 
		}
	})
}

// Enable console logging
function log(msg) {
	if (window.console && window.console.log)
		console.log(msg);
}

// Prevent IE6 background flicker
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(e) {}

// Apply CSS rules to IE6's unknown elements
document.createElement("abbr");