/****************************************************
	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;
	}
}



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

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");	
			}	
		}
	});
	
	// gift wrap modal
	$('div.vCart-cartDetail-container table th.col-giftwrap a').click(function(e){
		e.preventDefault();
		
		var $container = $('div.vCart-cartDetail-container'),
			$overlay = $('.overlay', $container),
			$giftwrapmodal = $('.giftwrapmodal', $container);
			
		$('select').css('visibility','hidden');
		
		$overlay.show()
		.css("height",$(document).height()+"px")
		.css("width",$(document).width()+"px")
		.css("opacity","0")
		.fadeTo("slow", 0.9);
		
		$giftwrapmodal.show()
		.css("opacity","0")
		.fadeTo("slow", 1);
		
		$overlay.add('a.close', $giftwrapmodal).click(function(event) {
			$overlay.add($giftwrapmodal).fadeTo("slow", 0 ,function(){
				//set display of the element to none after it's visibility has been brought to 0
				$overlay.add($giftwrapmodal).hide();
				$('select').css('visibility','visible');
			});
		});
	});
	

});
