function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function toggleNavBoxes(theLink) {
	var navLinks = $$('.navLink');
	var navLink = $(theLink);
	
	nlID = navLink.getAttribute('id');
	
	if (nlID == 'navLink-designers') {
		var showBox = 'linelistMask';
	} 
	else {
		var showBox = navLink.next('ul');
	}
	
	for (var i=0;i<navLinks.length;i++) {
		var flID = $(navLinks[i]).getAttribute('id');
		var thisNL = $(navLinks[i]);

		if ((thisNL.getAttribute('id') != nlID) && thisNL.next('ul')) {
			var fadeBox = thisNL.next('ul');
			Effect.Fade(fadeBox);
			if ($('accountNav')) Effect.Fade('accountNav');
		} else if ((thisNL.getAttribute('id') != nlID) && thisNL.next('#linelistMask')) {
			Effect.Fade('linelistMask');
		}
	}


	Effect.Appear(showBox);
}


var scrollTimeout;
var btnId = '';

function scrollDiv (dir,incr,timeout,maskId,scrollId) {
	/*if (cartOpen == true) return false;*/
	var maskId = (maskId == null) ? 'linelistMask' : maskId;
	var scrollId = (scrollId == null) ? 'navBox-designers' : scrollId;
	
	clearTimeout (scrollTimeout);
	scrollTimeout = null;
	
	if (dir != "stop") {
		// ---	get the mask height... the window that the content scrolls in
		var oMask 	= $(maskId);
		var maskH 	= oMask.clientHeight;
		// ---	get the content height and y position of the scrolling layer
		var oScroll 	= $(scrollId);
		var scrollY 	= Number ( oScroll.style.top.replace ( "px", "" ) );
		var scrollH 	= oScroll.clientHeight;
		// ---	buttons
		
		switch ( dir ) {
			case "init" :
				// ---	if there isn't anything to scroll, hide the buttons
				/* if ( maskH > scrollH ) 	oBtns.style.display 	= "none";*/
				break;
			case "up" :
				if ( scrollY < 7 ) {
					oScroll.style.top = ( scrollY + incr ) + "px";
					$('linelistMask').style.borderColor = '#3d3d3d #333 #222 #000';
				}
				else { 
					oScroll.style.top = "7px";
					$('linelistMask').style.borderColor = '#3d3d3d #333 #222 #000';
				}
				break;
			case "down" :
				if ( scrollY > ( maskH - scrollH ) ) {
					oScroll.style.top = ( scrollY - incr ) + "px";
					$('linelistMask').style.borderColor = '#222 #333 #3d3d3d #000';
				}
				else {
					oScroll.style.top = ( maskH - scrollH ) + "px";
					$('linelistMask').style.borderColor = '#222 #333 #3d3d3d #000';
				}
				break;
			case "auto" :
				if ( scrollY > ( maskH - scrollH ) ) 	oScroll.style.top = ( scrollY - incr ) + "px";
				else oScroll.style.top = ( (maskH - scrollH) + 3 ) + "px";
				break;
		}
		if ( dir != "init") {
			$('linelistMask').style.borderColor = "#111";
			if ( dir == "auto" ) {
				funct = "scrollDiv('auto',autoscroll);";
				funct = "scrollDiv('stop',0);";
				scrollTimeout 	= setTimeout ( funct, timeout );
			} else { 
				funct = "scrollDiv('" + dir + "'," + incr + "," + timeout + ");";
				scrollTimeout 	= setTimeout ( funct, timeout );
			}
		}
	} else {
		$('linelistMask').style.border = "1px solid #111";
		$('linelistMask').style.borderLeft = "none";
	}
}

function prepareLinelist() {
	if (!$('linelistMask')) return false;
	windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	
	var linelist = $('linelistMask');
	
	if ((windowHeight<616) && (windowHeight>=414)) {
		linelist.style.height = (windowHeight - 222) + 'px';
	}
}

function prepareNav() {
	if ($('nav')) {
		var navLinks = $('nav').getElementsByTagName('a');
		for (var i=0;i<navLinks.length;i++) {
			if (navLinks[i].className.match('navLink')) {
				navLinks[i].onclick = function() {
					toggleNavBoxes(this);		
					return false;
				};
			}
		}
	}
}

function openTabLayer(theLayer) {
	windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	var toOpen = $(theLayer);
	var	toOpenY = (60 - windowHeight);
	
	toOpen.down(0).next().style.display = 'block';
	
	new Effect.MoveBy(toOpen,toOpenY,0,{
		afterFinish: function() {
			toOpen.addClassName('tabOpen');
			toOpen.style.top = '28px';
		}
	});
}

function closeTabLayer(theLayer) {
	windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	var toClose = $(theLayer);
	var toCloseY = windowHeight - 56;
	
	var tabContents = toClose.getElementsBySelector('div.content');
	
	for(var i=0;i<tabContents.length;i++) {
		$(tabContents[i]).style.overflow = 'hidden';
	}
	
	new Effect.MoveBy(toClose,toCloseY,0,{ 
		afterFinish: function() { 
			toClose.removeClassName('tabOpen');
			toClose.style.top = '';
			toClose.style.height = '28px';
		}
	});
}

function toggleTabLayers(openLayer) {
	windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;

	var openTabs = $$('div.tabOpen');
	if (openTabs.size < 1) var tabsOpen = false;
	
	for (var i=0;i<openTabs.length;i++) {
		if (openTabs[i] == $(openLayer)) {
			closeTabLayer(openTabs[i]);
			return false;
		} else {
			closeTabLayer(openTabs[i]);
		}
	}
	if (openLayer) {
		var tabsOpen = true;
		$(openLayer).style.height = 'auto';
		$(openLayer).style.top = (windowHeight - 28) + 'px';
		openTabLayer(openLayer);
	}
}

function prepareTabLayers() {
	var tabLayerLinks = $$('a.tabLayerLink');
	var tabLayerHeads = $$('div.tabLayerHead');
	
	for (var i=0;i<tabLayerLinks.length;i++) {
		tabLayerLinks[i].onclick = function() {
			var tabLayer = this.up('.tabLayer').getAttribute('id');
			
			toggleTabLayers(tabLayer);
			return false;
		};
	}
	for (var i=0;i<tabLayerHeads.length;i++) {
		tabLayerHeads[i].onclick = function() {
			var tabLayer = this.up('.tabLayer').getAttribute('id');
			
			toggleTabLayers(tabLayer);
			return false;
		};
	}
	
	if ($('tabLayerHeadVideo')) {
		new Effect.Pulsate('tabLayerHeadVideo', { pulses: 5, duration: 10.0 });
	}
}

function sizeTabLayers() {
	var tabLayers = document.getElementsByClassName('tabLayer');
	
	windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	
	for (var i=0;i<tabLayers.length;i++) {
		var theTabLayer = $(tabLayers[i]);
		if (theTabLayer.hasClassName('tabOpen')) {
			theTabLayer.style.height = 'auto';
			theTabLayer.style.top = '28px';
			
		} else {
			theTabLayer.style.top = (windowHeight - 28) + 'px';
			theTabLayer.style.height = '28px';
		}
	}
}

function toggleTopTab(theTab,moveY) {
	windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	
	if (theTab) {
		toggleTabLayers();
	
		var topTab = $(theTab);
		if (!topTab.hasClassName('topTabOpen')) {
			topTab.addClassName('topTabOpen');
			new Effect.MoveBy(topTab,moveY,0);
		} else {
	
			new Effect.MoveBy(topTab,-moveY,0,{ afterFinish: function() {
						topTab.removeClassName('topTabOpen');
					}
				});
		}
	} else {
		
	}
}

function toggleFauxSelect(theList) {

	var toToggle = $(theList);
	if(toToggle.style.display == 'none') {
		
		toToggle.style.display = 'block';
	
	} else {
		
		toToggle.style.display = 'none';

	}
}

function closeItAll() {
	var fauxSelectLists = document.getElementsByClassName('fauxSelectList');
	var openTabs = document.getElementsByClassName('tabOpen');
	var openTopTabs = document.getElementsByClassName('topTabOpen');
	
		/*for (var i=0;i<fauxSelectLists.length;i++) {
			$(fauxSelectLists[i]).hide();
		}*/
		
	for (var i=0;i<openTabs.length;i++) {
		closeTabLayer(openTabs[i]);
	}
}

function slideToggle(theEl) {
	var el = $(theEl);
	
	if (el.style.display != 'none') {
		new Effect.SlideUp(el);
	}
	else {
		new Effect.SlideDown(el);
	}
}