// Header.js
var iconDescriptions = new Array();
var iconLinksOn = new Array();

// associative array of mapping between id's and their position
var iconLinksMapping = new Array();
iconLinksMapping['materialhandlingstoragepackaging'] = 1;
iconLinksMapping['safety'] = 2;
iconLinksMapping['fasteners'] = 3;
iconLinksMapping['toolsequipment'] = 4;
iconLinksMapping['hvac'] = 5;
iconLinksMapping['electrical'] = 6;
iconLinksMapping['chemicalspaints'] = 7;
iconLinksMapping['janitorial'] = 8;
iconLinksMapping['hydraulicspneumaticsplumbing'] = 9;
iconLinksMapping['welding'] = 10;
iconLinksMapping['abrasives'] = 11;
iconLinksMapping['cuttingtoolsmetalworking'] = 12;
iconLinksMapping['metalsalloysmaterialsfabrication'] = 14;
iconLinksMapping['fleetautomotive'] = 15;


function constructDescriptionArray() {
    descriptionArray = descString.split("#");
    var j = 0;

    do {
    
      descHTML = descriptionArray[j].split("[");
        descHTML[0] = "<span class='prodTitle'>" + descHTML[0] + "</span>";
        descHTML[1] = "";
        
        for(var i=0;i<descHTML.length;i++) {
            if (iconDescriptions[j+1] == null) {
                iconDescriptions[j+1] =  descHTML[i];
            } else {
                iconDescriptions[j+1] +=  descHTML[i];
            }
        }
	j++;
    } while (j < descriptionArray.length);
}

/*
*	Preloads the peocut cateogy on state images
*/
function preloadProductIconsOn() { 
    
    if (document.images) {

	for (i = 1; i <= 15; i++) {
	    iconLinksOn[i] = new Image();
	    iconLinksOn[i].src = SITE_ROOT + "common/images/buttons/prod_icons_on/" + i + "_on.gif";
	}
    }
}

/*
*	Sets the on state of the product cateogoy icon based on which section user is in
*/
function setCurrentProductIcon() {
    if (topCat != null) {
        if (document.getElementById(topCat) != null) {
            var currentIcon = document.getElementById(topCat);
            var onState = currentIcon.getAttribute("onmouseover");
            currentIcon.setAttribute("onmouseout", "hideDescription(iconLinksMapping[topCat]);");
            currentIcon.src = SITE_ROOT + "common/images/buttons/prod_icons_on/" + iconLinksMapping[topCat] + "_on.gif";
        
            //alert(onState);
        }  
    }
}

/*
*	Mouse(over/off) functions for product category icons
*/
function toggleIconOn(ref, j) {
    ref.src = SITE_ROOT + "common/images/buttons/prod_icons_on/" + j + "_on.gif";

    showDescription(j);
}

function toggleIconOff(ref, j) {
    ref.src = SITE_ROOT + "common/images/buttons/prod_icons_off/" + j + "_off.gif";

    hideDescription(j);
}

// displays/hides product category descriptions in icon bar in site header
function showDescription(j) {
    var descriptionContainer = document.getElementById("categoryName");
    
    if (j <= 15) {
        descriptionContainer.innerHTML = iconDescriptions[j];
        //NLBfadeBg('categoryName', '#00599c', '#0267b5', '3000')
    } else {
        descriptionContainer.innerHTML = "Choose a Product Category";
        //NLBfadeBg('categoryName', '#00599c', '#0267b5', '3000')
    }
}

function hideDescription(j) {
    
    if (document.getElementById) {
        //setTimeout('clearDescription(' + j + ')', 1000);
        clearDescription(j);
    }
}

function clearDescription(j) {
    var descriptionContainer = document.getElementById("categoryName");
    
    if (j <= 15) {
        descriptionContainer.innerHTML = "Choose a Product Category";
    }
}

function loadStoreInfo(userId) {
  var loggedStore = readCookie(userId + "_STORECODE");
  var storeInfo = document.getElementById("storeInfoContainer");
  storeInfo.innerHTML = "";
  if (loggedStore) {
    var storeInfoAnchor = document.createElement("a");
    storeInfoAnchor.setAttribute("id", "storeIconText");
    storeInfoAnchor.setAttribute("href", SITE_ROOT + "storeInfo.ex?branch=" + loggedStore + "&requestType=search");
    storeInfoAnchor.className = "utilityText supplementaryText";
    var loggedCity = readCookie(userId + "_CITY");
    var loggedState = readCookie(userId + "_STATE");
    if (loggedCity && loggedState) {
        var storeLocation = document.createTextNode("(" + loggedCity + ", " + loggedState + ")");
        storeInfoAnchor.appendChild(storeLocation);
    }
  }
  else {
    var anonymousStore = readCookie("ANONYMOUS_STORECODE");
    if (anonymousStore) {
        var storeInfoAnchor = document.createElement("a");
        storeInfoAnchor.setAttribute("id", "storeIconText");
        storeInfoAnchor.setAttribute("href", SITE_ROOT + "storeInfo.ex?branch=" + anonymousStore + "&requestType=search");
        storeInfoAnchor.className = "utilityText supplementaryText";
        var loggedCity = readCookie("ANONYMOUS_CITY");
        var loggedState = readCookie("ANONYMOUS_STATE");
        if (loggedCity && loggedState) {
            var storeLocation = document.createTextNode("(" + loggedCity + ", " + loggedState + ")");
            storeInfoAnchor.appendChild(storeLocation);
        }
    }
  }
  
  if (storeInfoAnchor) storeInfo.appendChild(storeInfoAnchor);
}





// set store for header
function setCurrentStore(storeCode) {
    var sExec = "products/searchStoreForProd.ex";
    var sParam = "dispatch=selectStore&storeCode=" + storeCode;

    var path = setPath( sExec, sParam );

    dojo.xhrGet( {
            url: path,
            handleAs: "text",
            load: function(response, ioArgs) {
                    //alert('completing set store... ' + path);   
                    bProcessingAutoComp = false;
                    window.location = SITE_ROOT + "home.ex";
                    return response;
            },
            error: function(response, ioArgs) {
                    dojo.byId("findStoreResults").innerHTML = "<p>Sorry, the search took longer than expected, try again.</p>";
                    bProcessingAutoComp = false;
                    console.error("HTTP status code", ioArgs.xhr.status);
                    return response;
            }
    } );	
}	
