function initNav(){
	var nav = document.getElementById('nav');
	var navlis = nav.getElementsByTagName('li');

	for (var i=0; i<navlis.length; i++){
		if (navlis.item(i).getElementsByTagName('ul')[0]){
			addAnEvent(navlis.item(i),'keyup',showNav);
			addAnEvent(navlis.item(i),'mouseover',showNav);
			addAnEvent(navlis.item(i),'mouseout',hideNav);
			addAnEvent(navlis.item(i),'blur',hideNav);
			addAnEvent(navlis.item(i),'focus',hideNav);
		}
	}
}
function hideNav(){
	this.getElementsByTagName('ul')[0].className = "";
	if (this.getElementsByTagName('ul')[1]) this.getElementsByTagName('ul')[1].className = "col2";
}
function showNav(){
    this.getElementsByTagName('ul')[0].className = "show";
	if (this.getElementsByTagName('ul')[1]) this.getElementsByTagName('ul')[1].className += " show";
}

function initButtons(){
	var inputs = document.getElementsByTagName("input");
	for (var i = 0; i<inputs.length; i++){
		if(inputs[i].type=="image"){
			addAnEvent(inputs.item(i),'mouseover',buttonHover);
			addAnEvent(inputs.item(i),'mouseout',buttonHoverout);
			inputs.item(i).style.borderWidth = "0px"
		}
	}
}
function buttonHover(){
	if (this.src.indexOf("1") == -1){
		var file = this.src.split(".gif");
		this.src = file[0] + "1.gif";
	}
}
function buttonHoverout(){
	var file = this.src.split("1.gif");
	this.src = file[0] + ".gif";
}

//tracking and receipt pop ups
function initLinks(){
    var as = document.getElementsByTagName('a');
    for (var i=0; i<as.length; i++){
        if (as[i].className.indexOf("popup2") != -1){
            addAnEvent(as.item(i),'click',popupWin2);
        } else if (as[i].className.indexOf("popup") != -1){
	        addAnEvent(as.item(i),'click',popupWin);
	    } else if (as[i].className.indexOf("mediapop") != -1){
		    addAnEvent(as.item(i),'click',popupWin3);
		}    
    }
}
function popupWin(){
    appWindow = window.open (this.href,"appWindow","width=800,height=500,toolbar=0,resizable=1,scrollbars=1");
    appWindow.focus ();
    return false;
}

function popupWin2(){
    appWindow = window.open (this.href,"appWindow","width=500,height=250,toolbar=0,resizable=1,scrollbars=1");
    appWindow.focus ();
    return false;
}

function popupWin3(){
    appWindow = window.open (this.href,"appWindow","width=420,height=600,toolbar=0,resizable=1,scrollbars=1");
    appWindow.focus ();
    return false;
}

function initBrandNav(){
	var bn = document.getElementById("brandnav");
	var ul = bn.getElementsByTagName("ul");
	var a = bn.getElementsByTagName("a");
	ul[0].className = "js"
	bn.innerHTML = "<div class=\"top\"></div>" + bn.innerHTML + "<div class=\"bot\"></div>";
	
	for (var i = 0; i<a.length; i++){
		var brand = a[i].innerHTML;
		brand = brand.toLowerCase();
		//replace spaces with dash
		var brandbits = brand.split(" ")
		brand = brandbits.join("-")
		//remove special characters except for dashes
		brand = brand.replace(/[^A-Za-z0-9-]+/g, '');
		a[i].style.background = "url(img/bn-"+brand+".gif) 0 0 no-repeat";
		addAnEvent(a.item(i),'mouseover',navHover);
		addAnEvent(a.item(i),'mouseout',navHoverout);
	}	
}
function navHover(){
	if (this.style.backgroundPosition){
		var pos = this.style.backgroundPosition.split(" ");
		var y = pos[1];
		this.style.backgroundPosition = "-157px " + y;
	} else 
		this.style.backgroundPositionX = "-157px";
}
function navHoverout(){
	if (this.style.backgroundPosition){
		var pos = this.style.backgroundPosition.split(" ");
		var y = pos[1];
		this.style.backgroundPosition = "0 " + y;
	} else
		this.style.backgroundPositionX = "0";
}

function initCategoryNav(){
	var cn = document.getElementById("categorynav");
	cn.innerHTML = "<div class=\"top\"></div>" + cn.innerHTML + "<div class=\"bot\"></div>";	
}

var setView = 1;
function initViews(){
	var a = document.getElementById("product-thumbs").getElementsByTagName("a");
	a[0].className = "current";
	for (var i=0; i<a.length;i++){
		addAnEvent(a.item(i),'click',setMain);
		addAnEvent(a.item(i),'mouseover',switchMain);
		addAnEvent(a.item(i),'mouseout',switchBack);		
	}
}
function switchMain(){
	var mi = document.getElementById("main-image");
	var n = this.hash.split("#");
	n = n[1];
	mi.src = large_views[n];
	return false;
}
function switchBack(){
	var mi = document.getElementById("main-image");
	mi.src = large_views[setView];
	return false;
}
function setMain(){
	var mi = document.getElementById("main-image");
	var a = document.getElementById("product-thumbs").getElementsByTagName("a");
	var n = this.hash.split("#");
	setView = n[1];
	for (var i=0; i<a.length;i++){
		a[i].className = "";
	}
	a[setView-1].className = "current";
	mi.src = large_views[setView];
	return false;
}

function addAnEvent(target, eventName, functionName){
	eval('target.on'+eventName+'=functionName');
}

function initSort(){
    var sort = document.getElementById('sort');
    addAnEvent(sort,'mouseover',showSort);
    addAnEvent(sort,'mouseout',hideSort);
}
function showSort(){
    this.getElementsByTagName('ul')[0].className = "hover";
}
function hideSort(){
    this.getElementsByTagName('ul')[0].className = "";
}

// move brands from select to select functions
function initMoveBrands () {
    var mr = document.getElementById("movebrandsright");
    addAnEvent(mr,'click',moveBrands);
    var ml = document.getElementById("movebrandsleft");
    addAnEvent(ml,'click',moveBrands);
}

var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue){
    var newOpt = new Option(theText, theValue);
    var selLength = theSel.length;
    theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex){ 
    var selLength = theSel.length;
    if(selLength>0) theSel.options[theIndex] = null;
}

function moveBrands(){
    if (this.id == "movebrandsright"){
        var theSelFrom = document.getElementById("brands-unselected[]");
        var theSelTo = document.getElementById("brands-selected[]");
    } else {
        var theSelFrom = document.getElementById("brands-selected[]");
        var theSelTo = document.getElementById("brands-unselected[]");
    }
    
    var selLength = theSelFrom.length;
    var selectedText = new Array();
    var selectedValues = new Array();
    var selectedCount = 0;
    var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
    for(i=selLength-1; i>=0; i--){
        if(theSelFrom.options[i].selected){
            selectedText[selectedCount] = theSelFrom.options[i].text;
            selectedValues[selectedCount] = theSelFrom.options[i].value;
            deleteOption(theSelFrom, i);
            selectedCount++;
        }
    }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
    for(i=selectedCount-1; i>=0; i--){
        addOption(theSelTo, selectedText[i], selectedValues[i]);
    }
}

function sfTarget () {
    var sfEls=document.getElementsByTagName("div");
    var aEls = document.getElementsByTagName("a");
    document.lastTarget = null;
    for (var i=0; i<sfEls.length; i++) {
        if (sfEls[i].id) {
            if (location.hash==("#" + sfEls[i].id)) {
                sfEls[i].className+=" sftarget";
                document.lastTarget=sfEls[i];
            }
            for (var j=0; j<aEls.length; j++) {
                if (aEls[j].hash==("#" + sfEls[i].id)) aEls[j].targetEl = sfEls[i]; aEls[j].onclick = function() {
                    if (document.lastTarget) document.lastTarget.className = document.lastTarget.className.replace(new RegExp(" sftarget\\b"), "");
                    if (this.targetEl) this.targetEl.className+=" sftarget"; document.lastTarget=this.targetEl;
                    return true;
                }
            }
        }
    }
}

function initPage(){
	if (document.getElementById){
		if (document.all && !document.getElementById("chromeless")) sfTarget();
		initNav();
		initButtons();
		initLinks();
		if (document.getElementById("brandnav")) initBrandNav();
		if (document.getElementById("categorynav")) initCategoryNav();
		if (document.getElementById("product-thumbs")) initViews();
		if (document.getElementById("sort")) initSort();
		if (document.getElementById("highlight-inner")) fillup();
		if (document.getElementById("newsletter-brands")) initMoveBrands();
	}
}

window.onload=initPage;
