/****************************************************
	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);
	}
	
	$('form li.signup input.input-image', $esignupmodal).click(function() {
		setSignUpCookie('signed up');
	});
	// Email sign up show/hide and highlight
	$overlay.add('#emailsignup a').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');
			_gaq.push(['_trackEvent', 'Modal', 'Action', 'Decline']);
		}
		
		// 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;
	}
	
$(document).ready(function(){
	
	var $carousel = $('#body-container div#carousel:visible').jCarouselLite({
			btnNext: "a.carousel-next",
			btnPrev: "a.carousel-prev",
			visible: 1,
			circular: false,
			btnGo:
			["ul.carousel-pages .carousel-page1", "ul.carousel-pages .carousel-page2", "ul.carousel-pages .carousel-page3", "ul.carousel-pages .carousel-page4"],
			afterEnd: function(elm){
				var $elm = $(elm),
					$activeRange = $elm.attr('id'),
					$carouselControls = $('div.carousel-controls', $carousel),
					$activeButton = $('a[href="#'+$activeRange+'"]', $carouselControls),
					$nextBtn = $('a.carousel-next', $carouselControls),
					$prevBtn = $('a.carousel-prev', $carouselControls);

				$elm.parent('ul').find('li.active').removeClass('active');
				$elm.addClass('active');
			
				$('li.selected', $carouselControls).removeClass('selected');
				$activeButton.parent('li').addClass('selected');
				
				if ($activeButton.parent('li:first-child').hasClass('selected')) {
					$prevBtn.addClass('disabled');
				} else if ($activeButton.parent('li:last-child').hasClass('selected')) {
					$nextBtn.addClass('disabled');
				} else {
					$prevBtn.removeClass('disabled');
					$nextBtn.removeClass('disabled');
				}
	
			}
		}),	
		$controlsList = $('div.carousel-controls-wrap .carousel-controls ul.carousel-pages', $carousel),
		$yearRange = $('.carousel-wrap .carousel-content ul li', $carousel),
		$yearListSize = $('li', $controlsList).size(),
		$btnCarousel = $('li a', $controlsList);

	$('a', $yearRange).each(function(){
		var $this = $(this),
			$year = $this.attr('id').replace('year-','');
			
		$('a#year-'+$year+', div.tip'+$year, $yearRange).hover(function(){
			$('div.tip'+$year, $yearRange).show();
		}, function (){
			$('div.tip'+$year, $yearRange).hide();
		});
	});
	
				
	$('div',$yearRange).each(function(){
		var $this = $(this),
			$year = $this.attr('class').replace('tooltip tip','').replace('clearfix',''),
			$left = $('a#year-'+$year, $yearRange).position().left + 60,
			$right = $yearRange.width()-$('a#year-'+$year, $yearRange).position().left,
			$totalWidth = $left+$('div.tip'+$year, $yearRange).width();
			
		if ($totalWidth<=780) {	
			$('div.tip'+$year, $yearRange).css('left', $left);
		} else {
			
			$('div.tip'+$year, $yearRange).css('right', $right);
			if ($('a#year-'+$year, $yearRange).position().left-$('div.tip'+$year, $yearRange).width() < 0) {
				$('div.tip'+$year, $yearRange).css('left', 15+"px");	
			}	
		}
	});

	// 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();
	
});
