$(document).ready(function() {
	$("table.titles_subs tr:even").addClass("rowEven");
	$("table.titles_subs tr:odd").addClass("rowOdd");
});

function featured2Swap (what){
	value = what.options[what.selectedIndex].value.split("|");
	if (value[0] != '' && value[1] != '') {
		var elem = document.getElementsByTagName("div");
		for(var i = 0; i < elem.length; i++){
			curr = elem[i].id;
			current = elem[i].id.split("_");
			if ( current[0] == value[0] ){
				document.getElementById(curr).style.display="none";
			}
		}
		curr = value[0]+"_"+value[1];
		document.getElementById(curr).style.display="";
	}
}

function color_switch_grid(base_id, show_id) {
	$("div." + base_id).hide();
	$("div#" + base_id + "_" + show_id).show();
}

/*alpha category select functions

*/
function select_letter(letter) {
	$(".alphabet_menu").empty();
	$(".heading_letter").html(letter.toUpperCase());
	$(".alphabet_menu").append('<table border="0" class="alpha_links" cellspacing="0" cellpadding="0"></table>');
	window.location.hash = letter;
	//$(".alphabet_menu").append('<tr class="heading"><td>' + letter.toUpperCase() + '</td></tr>');
	for(var i in prod_alpha[letter]) {
		var className;
		var designer;
		if(prod_alpha[letter][i]['type'] == 'product') {
			className = 'alphabet_link product';
			designer ='';
		} else {
			className = 'alphabet_link subtype';	
			designer = '<br/><span class="alpha_subtype">'+subtype_message+'</span>';
		}
		$(".alphabet_menu table").append('<tr class="'+ className +'"><td><a href="' + prod_alpha[letter][i]['url'] + '#'+letter+'">' + prod_alpha[letter][i]['name'] + '</a>'+designer+'</td></tr>');
	}
	
	bind_letter_hover();
	//$(".alphabet_menu").append('</table>');
}

function bind_letter_hover() {
	$(".alphabet_link td").hover(function() {
		$(this).addClass("alphabet_linkOver");
		//alert('wtf');
	}, function() {
		$(this).removeClass("alphabet_linkOver");
	}).click(function() {
		window.location.href = $(this).find("a").attr("href");
	});	
}



/*
Site wide JS functions
*/

function copyBillingInfo(form) {
	if (form.SameAsBilling.checked == 1) {
		form.Ship_CompanyName.value = form.Bill_CompanyName.value;
		form.Ship_FirstName.value = form.Bill_FirstName.value;
		form.Ship_LastName.value = form.Bill_LastName.value;
		form.Ship_Address1.value = form.Bill_Address1.value;
		form.Ship_Address2.value = form.Bill_Address2.value;
		form.Ship_City.value = form.Bill_City.value;
		form.Ship_Zipcode.value = form.Bill_Zipcode.value;
		form.Ship_ShippingPhoneDay.value = form.Bill_HomePhoneDay.value;
		
		

		if($("select.Ship_CountryID").length > 0) {
			for (i=0; i<form.Ship_CountryID.options.length; i++) {
				if (form.Ship_CountryID.options[i].value == form.Bill_CountryID.options[form.Bill_CountryID.selectedIndex].value) {
					form.Ship_CountryID.selectedIndex = i;
				}
			}
			//$("select.Ship_CountryID").change();
			$("select[name='Ship_StateID']").attr("disabled",$("select.Bill_StateID").attr("disabled"));
		}
		/**/
		$("select.Ship_StateID:not(.country)").html($("select.Bill_StateID:not(.country)").html());
		for (i=0; i<form.Ship_StateID.options.length; i++) {
			if (form.Ship_StateID.options[i].value == form.Bill_StateID.options[form.Bill_StateID.selectedIndex].value) {
				form.Ship_StateID.selectedIndex = i;
			}
		}


	}
	else {
		form.Ship_CompanyName.value = "";
		form.Ship_FirstName.value = "";
		form.Ship_LastName.value = "";
		form.Ship_Address1.value = "";
		form.Ship_Address2.value = "";
		form.Ship_City.value = "";
		form.Ship_StateID.selectedIndex = 0;
		form.Ship_Zipcode.value = "";
		form.Ship_ShippingPhoneDay.value = "";
		
		if(form.Ship_CountryID != null) {
			form.Ship_CountryID.selectedIndex = 0;
			$("select[name='Ship_StateID']").attr("disabled",false);
		}
	}
}

function copyBillingInfo2(form) {
	if (form.SameAsBilling.checked == 1) {
		form.shipAddress1.value = form.billAddress1.value;
		form.shipAddress2.value = form.billAddress2.value;
		form.shipCity.value = form.billCity.value;
		form.shipZipcode.value = form.billZipcode.value;
		
		for (i=0; i<form.shipState.options.length; i++) {
			if (form.shipState.options[i].value == form.billState.options[form.billState.selectedIndex].value) {
				form.shipState.selectedIndex = i;
			}
		}

	}
	else {
		form.shipAddress1.value = "";
		form.shipAddress2.value = "";
		form.shipCity.value = "";
		form.shipState.selectedIndex = 0;
		form.shipZipcode.value = "";
	}
}

function winopen( url, name, features ) {
        win = window.open( url, name, features );
		win.focus();
}

function textCounter(field, countfield, maxlimit, minlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		countfield.value = minlimit + field.value.length - minlimit;
	}
}

// Auto tabbing
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
	if(arr[index] == ele)
		found = true;
	else
		index++;
	return found;
}

function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
			else i++;
		return index;
	}
return true;
}

// Dynamic menu
var menuwidth='165px'; //default menu width
//var menubgcolor='lightyellow'; //menu bgcolor
var disappeardelay=250; //menu disappear speed onMouseout (in miliseconds)
var hidemenu_onclick="yes"; //hide menu when user clicks within menu?

/////No further editting needed

var ie4=document.all;
var ns6=document.getElementById&&!document.all;

if (ie4||ns6){
	// Makes problems in IE
	document.write('<div id="dropmenudiv" style="padding-left: 10px; visibility:hidden;width:'+menuwidth+';" onMouseover="clearhidemenu()" onMouseout="dynamichide(event)"></div>');
}

function getposOffset(what, offsettype){
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null){
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}

function showhide(obj, e, visible, hidden, menuwidth){
	if (ie4||ns6)
		dropmenuobj.style.left=dropmenuobj.style.top="-500px";
	if (menuwidth!=""){
		dropmenuobj.widthobj=dropmenuobj.style;
		dropmenuobj.widthobj.width=menuwidth;
	}
	if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
		obj.visibility=visible;
	else if (e.type=="click")
		obj.visibility=hidden;
}

function iecompattest(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function clearbrowseredge(obj, whichedge){
	var edgeoffset=0;
	if (whichedge=="rightedge"){
		var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15;
		dropmenuobj.contentmeasure=dropmenuobj.offsetWidth;
		if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
			edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth;
	}else{
		var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset;
		var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18;
		dropmenuobj.contentmeasure=dropmenuobj.offsetHeight;
		if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure){ //move up?
			edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight;
		if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
			edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge;
		}
	}
	return edgeoffset;
}

function populatemenu(what){
	if (ie4||ns6) dropmenuobj.innerHTML=what.join("");
}


function dropdownmenu(obj, e, menucontents, menuwidth){
	if (menucontents.length < 1){
		return;
	}
	if (window.event)
		event.cancelBubble=true;
	else if (e.stopPropagation) e.stopPropagation()
		clearhidemenu();
	dropmenuobj=document.getElementById? document.getElementById("dropmenudiv") : dropmenudiv;
	populatemenu(menucontents);

	if (ie4||ns6){
		showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth);
		dropmenuobj.x=getposOffset(obj, "left");
		dropmenuobj.y=getposOffset(obj, "top");
		dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px";
		dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px";
	}

	return clickreturnvalue();
}

function clickreturnvalue(){
	if (ie4||ns6)
		return false;
	else
		return true;
}

function contains_ns6(a, b) {
	while (b.parentNode)
		if ((b = b.parentNode) == a)
			return true;
	return false;
}

function dynamichide(e){
	if (ie4&&!dropmenuobj.contains(e.toElement))
		delayhidemenu()
	else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
		delayhidemenu()
}

function hidemenu(e){
	if (typeof dropmenuobj!="undefined"){
		if (ie4||ns6)
			dropmenuobj.style.visibility="hidden";
	}
}

function delayhidemenu(){
	if (ie4||ns6)
		delayhide=setTimeout("hidemenu()",disappeardelay);
}

function clearhidemenu(){
	if (typeof delayhide!="undefined")
		clearTimeout(delayhide);
}

if (hidemenu_onclick=="yes"){
	document.onclick=hidemenu;
}


// Collapsable Categories (Categories 2)

var openLists = [], oIcount = 0;

function compactMenu(oID,oAutoCol,oPlMn,oMinimalLink) {
	if( !document.getElementsByTagName || !document.childNodes || !document.createElement ) { return; }
	var baseElement = document.getElementById( oID ); if( !baseElement ) { return; }
	compactChildren( baseElement, 0, oID, oAutoCol, oPlMn, baseElement.tagName.toUpperCase(), oMinimalLink && oPlMn );
}
function compactChildren( oOb, oLev, oBsID, oCol, oPM, oT, oML ) {
	if( !oLev ) { oBsID = escape(oBsID); if( oCol ) { openLists[oBsID] = []; } }
	for( var x = 0, y = oOb.childNodes; x < y.length; x++ ) { if( y[x].tagName ) {
		
		//for each immediate LI child
		var theNextUL = y[x].getElementsByTagName( oT )[0];
		
		if( theNextUL && y[x].id != 'nocollapse') {
			//alert ();
			//collapse the first UL/OL child
			theNextUL.style.display = 'none';
			//create a link for expanding/collapsing
			var newLink = document.createElement('A');
			newLink.setAttribute( 'href', '#' );
			newLink.onclick = new Function( 'clickSmack(this,' + oLev + ',\'' + oBsID + '\',' + oCol + ',\'' + escape(oT) + '\');return false;' );
			//wrap everything upto the child U/OL in the link
			if( oML ) { var theHTML = ''; } else {
				var theT = y[x].innerHTML.toUpperCase().indexOf('<'+oT);
				var theA = y[x].innerHTML.toUpperCase().indexOf('<A');
				var theHTML = y[x].innerHTML.substr(0, ( theA + 1 && theA < theT ) ? theA : theT );
				while( !y[x].childNodes[0].tagName || ( y[x].childNodes[0].tagName.toUpperCase() != oT && y[x].childNodes[0].tagName.toUpperCase() != 'A' ) ) {
					y[x].removeChild( y[x].childNodes[0] ); }
			}
			
			y[x].insertBefore(newLink,y[x].childNodes[0]);
			y[x].childNodes[0].innerHTML = oPM + theHTML.replace(/^\s*|\s*$/g,'');
			theNextUL.MWJuniqueID = oIcount++;
			compactChildren( theNextUL, oLev + 1, oBsID, oCol, oPM, oT, oML );
			}
		} 
	}
}

function clickSmack( oThisOb, oLevel, oBsID, oCol, oT ) {
	if( oThisOb.blur ) { oThisOb.blur(); }
	oThisOb = oThisOb.parentNode.getElementsByTagName( unescape(oT) )[0];
	if( oCol ) {
		for( var x = openLists[oBsID].length - 1; x >= oLevel; x-=1 ) { if( openLists[oBsID][x] ) {
			openLists[oBsID][x].style.display = 'none'; if( oLevel != x ) { openLists[oBsID][x] = null; }
		} }
		if( oThisOb == openLists[oBsID][oLevel] ) { openLists[oBsID][oLevel] = null; }
		else { oThisOb.style.display = 'block'; openLists[oBsID][oLevel] = oThisOb; }
	} else { oThisOb.style.display = ( oThisOb.style.display == 'block' ) ? 'none' : 'block'; }
}
function stateToFromStr(oID,oFStr) {
	if( !document.getElementsByTagName || !document.childNodes || !document.createElement ) { return ''; }
	var baseElement = document.getElementById( oID ); if( !baseElement ) { return ''; }
	if( !oFStr && typeof(oFStr) != 'undefined' ) { return ''; } if( oFStr ) { oFStr = oFStr.split(':'); }
	for( var oStr = '', l = baseElement.getElementsByTagName(baseElement.tagName), x = 0; l[x]; x++ ) {
		if( oFStr && MWJisInTheArray( l[x].MWJuniqueID, oFStr ) && l[x].style.display == 'none' ) { l[x].parentNode.getElementsByTagName('a')[0].onclick(); }
		else if( l[x].style.display != 'none' ) { oStr += (oStr?':':'') + l[x].MWJuniqueID; }
	}
	return oStr;
}
function MWJisInTheArray(oNeed,oHay) { for( var i = 0; i < oHay.length; i++ ) { if( oNeed == oHay[i] ) { return true; } } return false; }

function selfLink(oRootElement,oClass,oExpand,oLink) {
	var tmpLink;
	if(!document.getElementsByTagName||!document.childNodes) { return; }
	oRootElement = document.getElementById(oRootElement);
	if( oLink ) {
		tmpLink = document.createElement('a');
		tmpLink.setAttribute('href',oLink);
	}
	for( var x = 0, y = oRootElement.getElementsByTagName('a'); y[x]; x++ ) {
		if( y[x].getAttribute('href') && !y[x].href.match(/#$/) && getRealAddress(y[x]) == getRealAddress(oLink?tmpLink:location) ) {
			y[x].className = (y[x].className?(y[x].className+' '):'') + oClass;
			if( oExpand ) {
				oExpand = false;
				for( var oEl = y[x].parentNode, ulStr = ''; oEl != oRootElement && oEl != document.body; oEl = oEl.parentNode ) {
					if( oEl.tagName && oEl.tagName == oRootElement.tagName ) { ulStr = oEl.MWJuniqueID + (ulStr?(':'+ulStr):''); } }
				stateToFromStr(oRootElement.id,ulStr);
			}
		}
	}
}

function getRealAddress(oOb) { return oOb.protocol + ( ( oOb.protocol.indexOf( ':' ) + 1 ) ? '' : ':' ) + oOb.hostname + ( ( typeof(oOb.pathname) == typeof(' ') && oOb.pathname.indexOf('/') != 0 ) ? '/' : '' ) + oOb.pathname + oOb.search; }

function expandCollapseAll(oElID,oState) {
	if(!document.getElementsByTagName||!document.childNodes) { return; }
	var oEl = document.getElementById(oElID);
	var oT = oEl.tagName;
	var oULs = oEl.getElementsByTagName(oT);
	for( var i = 0, oLnk; i < oULs.length; i++ ) {
		if( typeof(oULs[i].MWJuniqueID) != 'undefined' ) {
			oLnk = oULs[i].parentNode.getElementsByTagName( 'a' )[0];
			if( oLnk && ( ( oState && oULs[i].style.display == 'none' ) || ( !oState && oULs[i].style.display != 'none' ) ) ) {
				oLnk.onclick();
			}
		}
	}
}

// Cookies to not close branches

function retrieveCookie( cookieName ) {
	/* retrieved in the format
	cookieName4=value; cookieName3=value; cookieName2=value; cookieName1=value
	only cookies for this domain and path will be retrieved */
	var cookieJar = document.cookie.split( "; " );
	for( var x = 0; x < cookieJar.length; x++ ) {
		var oneCookie = cookieJar[x].split( "=" );
		if( oneCookie[0] == escape( cookieName ) ) { return unescape( oneCookie[1] ); }
	}
	return null;
}

function setCookie( cookieName, cookieValue, lifeTime, path, domain, isSecure ) {
	if( !cookieName ) { return false; }
	if( lifeTime == "delete" ) { lifeTime = -10; } //this is in the past. Expires immediately.
	/* This next line sets the cookie but does not overwrite other cookies.
	syntax: cookieName=cookieValue[;expires=dataAsString[;path=pathAsString[;domain=domainAsString[;secure]]]]
	Because of the way that document.cookie behaves, writing this here is equivalent to writing
	document.cookie = whatIAmWritingNow + "; " + document.cookie; */
	document.cookie = escape( cookieName ) + "=" + escape( cookieValue ) +
		( lifeTime ? ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * lifeTime ) ) ).toGMTString() : "" ) +
		( path ? ";path=" + path : "") + ( domain ? ";domain=" + domain : "") + 
		( isSecure ? ";secure" : "");
	//check if the cookie has been set/deleted as required
	if( lifeTime < 0 ) { if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return false; } return true; }
	if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return true; } return false;
}

