// Mighty Leaf Interface functions

/**********************************
	CLEAR INPUT SCRIPTS 
**********************************/
function clearValue(varObj,varValue) {
	if($(varObj).val() == varValue) {
		$(varObj).val("");
	}
}

function testValue(varObj,varValue) {
	if($(varObj).val() == "") {
		$(varObj).val(varValue);
	}
}


/**********************************
	MODAL WINDOW SCRIPTS 
**********************************/
function buildModal()
{	
	// create main quickview container, append to current container
	jQuery("body").append("<div class=\"quick-modal\" style=\"display: none;\"></div>");
	
	// hide selects
	jQuery("select").css("visibility","hidden");
	
	// add close button
	jQuery(".quick-modal").append("<a href=\"#\" class=\"close\">Close Quick View</a>");
	
	jQuery(".quick-modal a.close").click(function() {
		removeModal();
		return false;
	});
	// add inner container
	showOverlay();
	var modalContent = getModalContent();
	jQuery(".quick-modal").append("<div class=\"inner-content\">"+modalContent+"</div>");
	
	jQuery(".quick-modal").show();
}
function buildModalFromUrl(url)
{
	jQuery.get(url, function(data) {
		// create main quickview container, append to current container
		jQuery("body").append("<div class=\"quick-modal\" style=\"display: none;\"></div>");
		
		// hide selects
		jQuery("select").css("visibility","hidden");
		
		// add close button
		jQuery(".quick-modal").append("<a href=\"#\" class=\"close\">Close Quick View</a>");
		
		jQuery(".quick-modal a.close").click(function() {
			removeModal();
			return false;
		});
		// add inner container
		showOverlay();
		var modalContent = data
		jQuery(".quick-modal").append("<div class=\"inner-content\">"+modalContent+"</div>");
		
		jQuery(".quick-modal").show();
	});
	
}
function removeModal()
{
	jQuery(".quick-modal").remove();
	hideOverlay();
	jQuery("select").css("visibility","visible");
}
function showOverlay()
{
	jQuery("body").append("<div id=\"ordersave-loading\" class=\"overlay\" style=\"display: none;\"></div>");
	jQuery("#ordersave-loading").css("height",jQuery(document).height()+"px");
	jQuery("#ordersave-loading").css("width",jQuery(document).width()+"px");
	jQuery("#ordersave-loading").fadeTo("fast", 0.5);
	jQuery("#ordersave-loading").fadeTo("fast", 0, function() { jQuery("#ordersave-loading").css("display","block"); });
	jQuery("#ordersave-loading").fadeTo("slow", 0.9);
	jQuery(document).scrollTo("#util-navigation",500);
}

function hideOverlay()
{
	jQuery("#ordersave-loading").remove();
}

function getModalContent()
{
	var modalContent = "";
	
	modalContent += "<h2>Start your Regular Tea Ritual today. Sign up and Save.</h2>";
	modalContent += "<h3>Convenient, automatic delivery of your favorite tea with flexibility to fit your lifestyle.</h3>";
	modalContent += "<p>You can choose how much, how often and which favorite tea pouch or loose tea blends you want.  Save on every order with Regular Tea Rituals.  There is no cost to join and you can cancel anytime.</p>";
	modalContent += "<ul>";
	modalContent += 	"<li><strong>SUBSCRIBER SAVINGS:</strong> Save 5% on each box of signature tea pouches or bag of loose tea.  If recurring items are on sale, you save 5% on top of the sale price. Choose from our wide selection or order a combination of both.</li>";
	modalContent += 	"<li><strong>AUTOMATIC DELIVERY:</strong> Artisan-crafted tea delivered to you on a schedule that you choose.</li>";
	modalContent += 	"<li><strong>ORDER FLEXIBILITY:</strong> Order your favorite teas with the ease of changing your recurring order as needed.</li>";
	modalContent += 	"<li><strong>FREE GIFTS:</strong> Choose from a Mighty Leaf tea mug, loose tea filters or a loose tea storage tin.</li>";
	modalContent += 	"<li><strong>REWARDS CREDIT:</strong> Receive credit towards Mighty Leaf Rewards for every recurring order purchase. <a href=\"/index.cfm/fuseaction/account.rewards/\">Sign Up for Rewards Today</a>.</li>";
	modalContent += 	"<li><strong>MANAGE YOUR ACCOUNT:</strong> It's easy to control and manage your account with our Regular Tea Rituals control panel located under &quot;My Account&quot;.</li>";
	modalContent += "</ul>";
	
	return modalContent;
}	

function initGlobalPromoRotator()
{
	var speed = 5000,
		currentpos = 0,
		timer = 0,
		$promoHolder = $("#util-navigation div.globalpromo-wrap div.globalpromo p"),
		promoLength = $promoHolder.children().length;
	
	rotateGlobalPromo = function()
	{
		clearTimeout(timer);

		$promoHolder.children().hide();
		$promoHolder.children().eq(currentpos).show();
		
		currentpos = currentpos + 1;
		if(currentpos >= promoLength){
			currentpos = 0;
		}
		
		timer = setTimeout("rotateGlobalPromo()",speed);
	}
	
	rotateGlobalPromo()
}
/******************************************
email signup
******************************************/

function areCookiesOn() {
    $.cookie('test_cookie', '1', { expires: 365, path: '/' });
    return $.cookie('test_cookie')
}

function setSignUpCookie(value) {
	var EXPIRY_MAP = { 'not interested': 2100000,  'signed up': 3500 };
	return $.cookie('signup_welcome', value, { expires: EXPIRY_MAP[value], path: '/' });
}

function initOverlay(){
	var $overlay = $('body div.overlay'),
		$esignupmodal = $('#esignupmodal')

	if (!$.cookie('signup_welcome') && areCookiesOn()) {
		$overlay.show()
		.css("height",$(document).height()+"px")
		.css("width",$(document).width()+"px")
		.css("opacity","0")
		.fadeTo("slow", 0.9)
		.click(function() {
			setSignUpCookie('not interested');
		});
		
		$esignupmodal.show()
		.css("opacity","0")
		.fadeTo("slow", 1);
		
		var loadDelay = setTimeout(function() {
			$('form .list-signup', $esignupmodal).addClass('signup-welcome');
		}, 2 * 1000);
	}
	
	$('form li.signup input.input-image', $esignupmodal).click(function() {
		setSignUpCookie('signed up');
	});
	// Email sign up show/hide and highlight
	$overlay.add('#emailsignup a, #esignupmodal a').click(function(event) {
		$overlay.add($esignupmodal).fadeTo("slow", 0 ,function(){
			//set display of the element to none after it's visibility has been brought to 0
			$overlay.add($esignupmodal).hide();
		});
		
		if ($(event.target).is('body div.overlay') || $(event.target).is('#esignupmodal a.close')) {
			setSignUpCookie('not interested');
		}
		
		// Use this to allow other potential handlers to execute
		if (event.target.href && event.target.href.indexOf('unsubscribe') == -1) {
			event.preventDefault()
		}
	});

}
/**********************************
	LOAD EVENTS
**********************************/
$(window).load(function() {
	// set email address input clear and reload of value
	$("#vEmailSignUp-signUpForm-emailAddress-1, #vEmailSignUp-signUpForm-emailAddress-2").focus(function() {
		clearValue($(this),"your email address, please");
	});
	$("#vEmailSignUp-signUpForm-emailAddress-1, #vEmailSignUp-signUpForm-emailAddress-2").blur(function() {
		testValue($(this),"your email address, please");
	});
	
	// set search input clear/reload of value
	$("#vSearch-basicSearchForm-searchString-1")
		.focus(function() {
			clearValue($(this),"what are you looking for?");
		})
		.blur(function() {
			testValue($(this),"what are you looking for?");
		});
	
	// set store location search input clear/reload of value
	$("#vLayout-layout-zipPostalCode-1")
		.focus(function() {
			clearValue($(this),"enter zip");
		})
		.blur(function() {
			testValue($(this),"enter zip");
		});
		
	$("#vContent-homePageTemplate-zipPostalCode-1")
		.focus(function() {
			clearValue($(this),"enter your zip");
		})
		.blur(function() {
			testValue($(this),"enter your zip");
		});
	
	// information hover box
	$("#footer-container .email-signup ul li.samples p a.free-samples").mouseover(function() {
		$("select").css("visibility","hidden");
		$("#footer-container .email-signup ul li.samples .info-hover").css("top","-"+($("#footer-container .email-signup ul li.samples .info-hover").height() + 15)+"px");
		$("#footer-container .email-signup ul li.samples .info-hover").show();
	});
	$("#footer-container .email-signup ul li.samples p a.free-samples").mouseout(function() {
		$("select").css("visibility","visible");
		$("#footer-container .email-signup ul li.samples .info-hover").hide();
	});
	$("#footer-container .email-signup ul li.samples p a.free-samples").click(function() {
		return false;
	});
	
	// recurring item information hover box
	$("a.recurring-item-icon").mouseover(function() {
		$("select").css("visibility","hidden");
		$(this).siblings(".recurring-item-icon-hover").css("top","-"+($(this).siblings(".recurring-item-icon-hover").height() * 2 + 5)+"px");
		$(this).siblings(".recurring-item-icon-hover").show();
	});
	$("a.recurring-item-icon").mouseout(function() {
		$("select").css("visibility","visible");
		$(this).siblings(".recurring-item-icon-hover").hide();
	});
	
	// cart drop down
	$("#util-navigation ul.utility li.cart:has(ul)").mouseover(function() {
		$("select").css("visibility","hidden");
		$(this).addClass("over");
	});
	$("#util-navigation ul.utility li.cart:has(ul)").mouseout(function() {
		$("select").css("visibility","visible");
		$(this).removeClass("over");
	});

	// utility nav "need help?" drop down
	$("#util-navigation ul.utility li.needhelp:has(ul)").mouseover(function() {
		$("select").css("visibility","hidden");
		$(this).addClass("over-help");
	});
	$("#util-navigation ul.utility li.needhelp:has(ul)").mouseout(function() {
		$("select").css("visibility","visible");
		$(this).removeClass("over-help");
	});

	// primary nav drop downs
	$("#primary-navigation ul li:has(ul)").mouseover(function() {
		$("select").css("visibility","hidden");
		$(this).addClass("over");
	});
	$("#primary-navigation ul li:has(ul)").mouseout(function() {
		$("select").css("visibility","visible");
		$(this).removeClass("over");
	});
	// remove functions from children in top nav
	$("#primary-navigation ul li ul li:has(ul)").unbind("mouseover");
	$("#primary-navigation ul li ul li:has(ul)").unbind("mouseout");

	// Teas by drop downs
	$("#search-container dl.teas-by dd ul li:has(ul)").mouseover(function() {
		$("select").css("visibility","hidden");
		$(this).addClass("over");
	});
	$("#search-container dl.teas-by dd ul li:has(ul)").mouseout(function() {
		$("select").css("visibility","visible");
		$(this).removeClass("over");
	});
	
	//Init Global Promo Rotator
	initGlobalPromoRotator();
	initOverlay();
});
