$(document).ready(function() {
	var signUpNewsLetterTitle = "Sign up for Exclusive Online Savings!";
	var signUpNewsLetterBody = "Want great savings you won\'t find anywhere else? Sign up for Home Hardware emails for exclusive offers, sneak peeks at flyers, email-only promotions and more.<br/><br/><div><a class=\"button\" style=\"float: left;\" href=\"#\"><span class=\"inner\">Sign Up Now</span></a><a class=\"closelink\" style=\"float: right; margin-top: 10px;\" href=\"#\">No Thanks</a></div>";

	$("<div id='email_signup_and_promo_popup_box' style='display:none'></div>").insertAfter("#wrap");
	$("#email_signup_and_promo_popup_box").dialog({
		autoOpen: 	false,
		resizable: 	false,
		width: 		400,					//change the width..
		modal: 		true,
		title:		signUpNewsLetterTitle, 	//Title of dialog
		minHeight: 	80,
		dialogClass:'clearfix ',
		open: function() {
			//TODO: you can use this ALSO to put the tracking code for the opening of the POPup.
		}
	});
	$("#email_signup_and_promo_popup_box").bind('dialogclose', function(event, ui) {
		if (event.which == 1) {
			if ((typeof _gaq !== "undefined") && (_gaq !== null)) {
				//GA tracking code when user clicks the "X" button on the POPUP box!
				_gaq.push(['_trackEvent', 'NewsletterAcquisition', 'Click', 'PopupBox - X (close) button']);
			}
		}
	});

	/** DBB added this for the SPECIAL Project on SIGN-UP for Newsletter **/
	$("#email_signup_and_promo_popup_box").html(signUpNewsLetterBody);	// Set the dialog html/text
	$("#email_signup_and_promo_popup_box").addClass("bodyContent");		// Add class for proper formatting 
	$("#email_signup_and_promo_popup_box .closelink").click(function(e) {
		e.preventDefault();
		$("#email_signup_and_promo_popup_box").dialog("close");
		if ((typeof _gaq !== "undefined") && (_gaq !== null)) {
			//GA tracking code when user clicks the "Close" button on the POPUP box!
			_gaq.push(['_trackEvent', 'NewsletterAcquisition', 'Click', 'PopupBox - Close button']);
		}
	});
	$("#email_signup_and_promo_popup_box .button").click(function(e) {
		e.preventDefault();
		$("#email_signup_and_promo_popup_box").dialog("close");
		if ((typeof _gaq !== "undefined") && (_gaq !== null)) {
			//GA tracking code when users click on the Signup button from the POPUP box!
			_gaq.push(['_trackEvent', 'NewsletterAcquisition', 'Click', 'PopupBox - Signup button']);
		}
		
		window.location.assign('/en/forms/newsletter_signup?referrer=1');
	});

	var the_cookie = $.cookie('HH_FIRST-TIME-USER');
	if (the_cookie != null) {
		if (the_cookie.indexOf('|') > -1) {
			var firstSplit = the_cookie.split('|');
			var promoName;
			for (i=0;i<(firstSplit.length);i++) {
				var keyName = dbbGetKey('=', firstSplit[i]);
				
				if (keyName == 'promoName') {
					promoName = dbbGetValue('=', firstSplit[i]);
				}
				
				if ((keyName == 'showPopup') && (dbbGetValue('=', firstSplit[i]) == 'true')) {
					$("#email_signup_and_promo_popup_box").dialog("open");
					$.cookie('HH_FIRST-TIME-USER', null, { expires: 365, path: '/', raw: true });		
					$.cookie('HH_FIRST-TIME-USER', ('promoName='+promoName), { expires: 365, path: '/', raw: true });
					
					if ((typeof _gaq !== "undefined") && (_gaq !== null)) {
						//GA tracking code when POPUP box opens! (this only pops up once, unless user clears cookies OR a new promo is set in the springapp-servlet.xml)
						_gaq.push(['_trackEvent', 'NewsletterAcquisition', 'Popup', 'PopupBox']);
					}
				}
			} 
		}
	}
});
