/****************************************************
	BarriePace Interface functions
****************************************************/

function clearValue(varObj,varValue) {
	if(varObj.value == varValue) {
		varObj.value = "";
	}
}

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

function setElementsVisibility(eContainer, aTagNames, sVisibility) {
	var tmp, i, j;
	var elements = [];
	for (i = 0; i < aTagNames.length; i++) {
		tmp = eContainer.getElementsByTagName(aTagNames[i]);
		for (j = 0; j < tmp.length; j++) {
			elements[elements.length] = tmp[j];
		}
	}	
	for (i = 0; i < elements.length; i++) {
		elements[i].style.visibility = sVisibility;
	}
}

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

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

/******************************************
email signup
******************************************/
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')) {
		$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);
		
		
		$overlay.add('#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()
		}
	});
	}
	
	$('form li.signup input.input-image', $esignupmodal).click(function() {
		setSignUpCookie('signed up');
	});
	// Email sign up show/hide and highlight
	/*
	$overlay.add('#esignupmodal a').add('a',$esignupmodal).add('a.close',$esignupmodal).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()
		}
	});
	*/
}

//	*******************************
//	CUSTOM FUNCTIONS
//	*******************************

function isZip(s){
   reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
   if (!reZip.test(s)) {
		alert("Please enter a valid zip code");
		return false;
   }
return true;
} 

$(window).load(function() {
	// toggle category navigation in left nav, with class of expand.
	$("ul#secondaryCategory-navigation li.expand:has(ul) > a").click(function() {
		$(this).parent("li").toggleClass("active");
		$(this).parent("li").toggleClass("expand-active");
		return false;
	});
	
	// set email address input clear and reload of value
	$("#vEmailSignUp-signUpForm-emailAddress-1, #vEmailSignUp-signUpForm-emailAddress-2").focus(function() {
		clearValue($(this),"Sign up for e-mails");
	});
	$("#vEmailSignUp-signUpForm-emailAddress-1, #vEmailSignUp-signUpForm-emailAddress-2").blur(function() {
		testValue($(this),"Sign up for e-mails");
	});
	
	initOverlay();
	
});

function changeCatalogPage()	{
		//alert($('#catalogNavigationDD :selected').val());
		window.location=$('#catalogNavigationDD :selected').val();
		return false;
	}
