////////////////////////////////////////////////////////////////////////////
// CLASS FINDER
// Used to find all the locations of the specified class

function getElementsByClassNameGS(node, classname)
{
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

////////////////////////////////////////////////////////////////////////////
// LEFT AND TOP POSITION FINDER
// Used to find the left and top position of the element

function findPosLeft(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
		}
	}
	return curleft;
}

function findPosTop(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curtop += obj.offsetTop
		}
	}
	return curtop;	
}

////////////////////////////////////////////////////////////////////////////
// ACTIVATE AND DEACTIVATE SELECT DROPDOWN'S FOR IE6
// Searches for html select dropdown's on form pages and toggles them 
// whether the overlay is on or off.

deactivateSelect = function() {
	if (isIE) {
		var selects = document.getElementsByTagName('select');
		for (var i=0; i<selects.length; i++) {
			if (selects[i].className.match('insideOverlay')!='insideOverlay') {
				selects[i].style.visibility = 'hidden';
			}
		}
	}
}

activateSelect = function() {
	if (isIE) {
		var selects = document.getElementsByTagName('select');
		for (var i=0; i<selects.length; i++) {
			if (selects[i].style.visibility == 'hidden') {
				selects[i].style.visibility = 'visible';
			}
		}
	}
}




////////////////////////////////////////////////////////////////////////////
// OVERLAY REPOSITIONER
// Positions the overlay depending on the size and location of the browser
var clientw;
var carticon;
var cart;

overlayPosition = function() {

	if (cartOverlayEnabled)
	{
	
		var overlay = document.getElementById('overlay');
		clientw = document.body.clientWidth;
		clientw = clientw / 2;	
		var diff = (isIE) ? 462:468;
		
		if (clientw > diff) {
			clientw = clientw - diff;
		} else {
			clientw = 0;
		}
		overlay.style.left = clientw + "px";
	
		//cover = getElementsByClassNameGS(document, 'cover');
		var cover = $('cover');	
		cover.style.width = (isIE) ? document.body.scrollWidth + "px" : document.documentElement.scrollWidth + "px";
		cover.style.height = (isIE) ? document.body.scrollHeight + "px" : document.documentElement.scrollHeight + "px";
		
		var left = findPosLeft(cart[0]);
		var top = findPosTop(cart[0]);
		carticon.style.left = (left - 15) + "px";
		carticon.style.top = (top - 5) + "px";
				
		if (carticon.id == 'myaccount')
		{
		carticon.style.width = Element.getWidth(cart[0]) + 13 + 'px';
		}
		else if (carticon.id  == 'cart')
		{
		carticon.style.width = Element.getWidth(cart[0]) + 'px';
		}
	}
}

window.onresize = overlayPosition;
////////////////////////////////////////////////////////////////////////////
// CART OVERLAY OPENER
// Used to open the cart overlay


var cartOverlayEnabled = false;
cartOverlay = function(id, layerId) {

	
	cartOverlayEnabled = true;	
	var layer = document.getElementById(layerId);
	cart = getElementsByClassNameGS(document, id);
	
	carticon = document.getElementById(id);
	carticon.style.display = 'block';

	layer.style.top = findPosTop(cart[0]) + 25 + "px";
	overlayPosition();
	//layer.style.display = 'block';
	
	//cover = getElementsByClassNameGS(document, 'cover');	
	var cover = $('cover');		
	cover.style.top = "0px";
	cover.style.left = "0px";	
	cover.style.width = (isIE) ? document.body.scrollWidth + "px" : document.documentElement.scrollWidth + "px";
	cover.style.height = (isIE) ? document.body.scrollHeight + "px" : document.documentElement.scrollHeight + "px";
	cover.style.display = "block";

	/* This scriptaculous effect has other options such as 'slide' or 'blind'. There could be more options. The same line applies for toggling on and off. */
	deactivateSelect();
	Effect.toggle(layerId, 'appear');
}

////////////////////////////////////////////////////////////////////////////
// CART OVERLAY CLOSER
// Used to close the cart overlay


hideCart = function(layerId) {

	closeWindow('shoppingCartpopup');

	cartOverlayEnabled = false;
	/* This scriptaculous effect has other options such as 'slide' or 'blind'. There could be more options. The same line applies for toggling on and off. */
	Effect.toggle(layerId, 'appear');
	
	
	var idToHide = document.getElementById(layerId);
	
	var carticon = document.getElementById('cart');
	carticon.style.display = 'none';	
	var myaccount = document.getElementById('myaccount');
	myaccount.style.display = 'none';	

	//cover = getElementsByClassNameGS(document, 'cover');
	var cover = $('cover');
	cover.style.display = 'none';
	activateSelect();
	
	$('eCommerceIframe').src = '';
	$('accountIframe').src = '';
}

changeTab = function(tabName)
{

	var carticon = document.getElementById('cart');
	var myaccount = document.getElementById('myaccount');

	carticon.style.left = (isIE) ? 939 + 'px' : 996 + 'px';
	carticon.style.top =  (isIE) ? 67 + 'px' : 75 + 'px';
	
	myaccount.style.left = (isIE) ? 1009 + 'px' : 1066 + 'px';
	myaccount.style.top = (isIE) ? 67 + 'px' : 75 + 'px';
		
	cart = getElementsByClassNameGS(document, tabName);
	
	if (tabName == 'myaccount') 
	{
		myaccount.style.display = 'block';
		carticon.style.display = 'none';
	}	
	if (tabName == 'cart')
	{
		carticon.style.display = 'block';
		myaccount.style.display = 'none';
	} 
}

////////////////////////////////////////////////////////////////////////////
// DIV POP UP'S
// Opens and closes up a non-browser pop up window

openWindow = function(id, position) {
	var popup = document.getElementById(id);
	popup.style.left = ((Element.getWidth('overlay') - Element.getWidth(popup)) / 2) + "px";
	popup.style.top = ((Element.getHeight('overlay') - Element.getHeight(popup)) / 2) + "px";
	popup.style.display = 'block';
}

openEmailWindow = function(id, position) {
	var popup = document.getElementById(id);
	popup.style.left = ((Element.getWidth('overlay') - Element.getWidth(popup)) / 2) + "px";
	popup.style.top = '100px';
	popup.style.display = 'block';
}

closeWindow = function(id) {
	var popup = document.getElementById(id);
	popup.style.display = 'none';
}

////////////////////////////////////////////////////////////////////////////
// BUTTONS ENABLES
// Enables all buttons if one or more check boxes are checked


buttonsEnabler = function(formName) {
	var enabled = false;

	if($j("#" + formName + " input:checked").length > 0) {
		enabled = true;
	};
	     	
    if (enabled)
    {
    	$j('#removeProductButton').attr("class", "submitButton enabled");
    	$j('#addToShoppingListButton').attr("class", "submitButton enabled");
    	
    	$j('#removeProductButton').bind('click', function() {
    		$('action').value='Remove';
    		$j('#cartForm').submit();
    		return false;
    	});
    	
        $j('#addToShoppingListButton').bind('click', function() {
        	shoppingMgr.addItemsToShoppingLists($('addToShoppingList'), $('cartForm'));
        	return false;
    	});
    }
    else
    {
    	$j('#removeProductButton').attr("class", "submitButton disabled");
    	$j('#addToShoppingListButton').attr("class", "submitButton disabled");
    	$j('#removeProductButton').bind('click', function() {
    		return false;
    	});
    	
        $j('#addToShoppingListButton').bind('click', function() {
        	return false;
    	});
    }
};


////////////////////////////////////////////////////////////////////////////
// ACCORDION EFFECT
// Does the accordion effect without the special effects


accordion = function(section, body, num) {
	var sections = getElementsByClassNameGS(document, section);
	//alert(sections[num].childNodes[1].childNodes[1].className);
	choose = (isIE) ? sections[num].childNodes[0].childNodes[0] : sections[num].childNodes[1].childNodes[1];
	choose.className = 'arrowdown headertext';
	var bodies = getElementsByClassNameGS(document, body);	
	bodies[num].style.display = 'block';
	
	for (var i=0; i < sections.length; i++) {
		if (i != num) {
			if (isIE) {
				sections[i].childNodes[0].childNodes[0].className = 'arrowright headertext';
			} else {
				sections[i].childNodes[1].childNodes[1].className = 'arrowright headertext';
			}
			bodies[i].style.display = 'none';
		}
	}
}

closeAccordion = function(section, body)
{
	var sections = getElementsByClassNameGS(document, section);
	var bodies = getElementsByClassNameGS(document, body);
	
	for (var i=0; i < sections.length; i++) 
	{
		if (isIE) {
			sections[i].childNodes[0].childNodes[0].className = 'arrowright headertext';
		} else {
			sections[i].childNodes[1].childNodes[1].className = 'arrowright headertext';
		}
	
		bodies[i].style.display = 'none';
	}
}

accordionLoad = function() {
	accordion('section', 'body', 0);
}

function productPageAddToCart(productNumber)
{
    s.linkTrackVars = 'events,eVar13,products'
    s.linkTrackEvents='scAdd'
    s.events="scAdd";
    s.eVar13="Product Page Tab";
    s.products = ';' + productNumber;
    var str = 'Product Page Tab';
    /************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
    s.tl(this,'o',str);//-->
}

function productPageAddToShoppingList(productNumber)
{
    s.linkTrackVars = 'events,eVar14,products'
    s.linkTrackEvents='event38'
    s.events="event38";
    s.eVar14="Product Page Tab";
    s.products = ';' + productNumber;
    var str = 'Product Page Tab';
    /************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
    s.tl(this,'o',str);//-->
}

function catalogPageAddToCart(productNumber, str)
{
    s.linkTrackVars = 'events,eVar13,products'
    s.linkTrackEvents='scAdd'
    s.events="scAdd";
    s.eVar13=str;
    s.products = ';' + productNumber;
    /************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
    s.tl(this,'o',str);//-->
}

function catalogPageAddToShoppingList(productNumber, str)
{
    s.linkTrackVars = 'events,eVar14,products'
    s.linkTrackEvents='event38'
    s.events="event38";
    s.eVar14=str;
    s.products = ';' + productNumber;
    /************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
    s.tl(this,'o',str);//-->
}
