var shown = false;
var timer;

//window.onload = function () { document.getElementById("search-terms").focus(); }

function showStoreLocator(sku) {
var url = SITE_ROOT + "products/searchStoreForProd.ex?dispatch=initialize&sku=" + sku;
window.open(url, "","location=0,menubar=0,resizable=0,scrollbars=1,width=450,height=350"); 
}

function doSearch(formInfo) {


var sExec = "products/searchStoreForProd.ex";

var sParam = "";

for (var i=0;i<formInfo.length;i++) {
    if (formInfo.elements[i].name != "searchOption")
        sParam = sParam + "&" + formInfo.elements[i].name + "=" + formInfo.elements[i].value;
    else
        if (formInfo.elements[i].checked)
            sParam = sParam + "&" + formInfo.elements[i].name + "=" + formInfo.elements[i].value;
}

var path = setPath( sExec, sParam );  

    dojo.xhrGet( {
        url: path,
        handleAs: "text",
        timeout: 60000,
        load: function(response, ioArgs) {
            dojo.byId("findStoreResults").innerHTML = response;           
            bProcessingAutoComp = false;
            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;
            }
    } );
}
//var description = document.createTextNode("");
        
//descriptionContainer.appendChild(description);
        
function showThumbnail(sku) {
  
  if (document.getElementById) {
    var thumb = document.getElementById(sku);
    thumb.style.visibility = "visible";
  }
}

function hideThumbnail(sku) {
  
  if (document.getElementById) {
    var thumb = document.getElementById(sku);
    thumb.style.visibility = "hidden";
  }
} 

                        
/*************************************************************
* NLB Background Color Fader v1.0
* Author: Justin Barlow - www.netlobo.com
*
* Description:
* The Background Color Fader allows you to gradually fade the
* background of any HTML element.
*
* Usage:
* Call the Background Color Fader as follows:
*   NLBfadeBg( elementId, startBgColor, endBgColor, fadeTime );
*
* Description of Parameters
*   elementId - The id of the element you wish to fade the
*             background of.
*   startBgColor - The background color you wish to start the
*             fade from.
*   endBgColor - The background color you want to fade to.
*   fadeTime - The duration of the fade in milliseconds.
*************************************************************/

var nlbFade_hextable = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ]; // used for RGB to Hex and Hex to RGB conversions
var nlbFade_elemTable = new Array( ); // global array to keep track of faded elements
var nlbFade_t = new Array( ); // global array to keep track of fading timers
function NLBfadeBg( elementId, startBgColor, endBgColor, fadeTime )
{
	var timeBetweenSteps = Math.round( Math.max( fadeTime / 300, 30 ) );
	var nlbFade_elemTableId = nlbFade_elemTable.indexOf( elementId );
	if( nlbFade_elemTableId > -1 )
	{
		for( var i = 0; i < nlbFade_t[nlbFade_elemTableId].length; i++ )
			clearTimeout( nlbFade_t[nlbFade_elemTableId][i] );
	}
	else
	{
		nlbFade_elemTable.push( elementId );
		nlbFade_elemTableId = nlbFade_elemTable.indexOf( elementId );
	}
	var startBgColorRGB = hexToRGB( startBgColor );
	var endBgColorRGB = hexToRGB( endBgColor );
	var diffRGB = new Array( );
	for( var i = 0; i < 3; i++ )
		diffRGB[i] = endBgColorRGB[i] - startBgColorRGB[i];
	var steps = Math.ceil( fadeTime / timeBetweenSteps );
	var nlbFade_s = new Array( );
	for( var i = 1; i <= steps; i++ )
	{
		var changes = new Array( );
		for( var j = 0; j < diffRGB.length; j++ )
			changes[j] = startBgColorRGB[j] + Math.round( ( diffRGB[j] / steps ) * i );
		if( i == steps )
			nlbFade_s[i - 1] = setTimeout( 'document.getElementById("'+elementId+'").style.backgroundColor = "'+endBgColor+'";', timeBetweenSteps*(i-1) );
		else
			nlbFade_s[i - 1] = setTimeout( 'document.getElementById("'+elementId+'").style.backgroundColor = "'+RGBToHex( changes )+'";', timeBetweenSteps*(i-1) );
	}
	nlbFade_t[nlbFade_elemTableId] = nlbFade_s;
}
function hexToRGB( hexVal )
{
	hexVal = hexVal.toUpperCase( );
	if( hexVal.substring( 0, 1 ) == '#' )
		hexVal = hexVal.substring( 1 );
	var hexArray = new Array( );
	var rgbArray = new Array( );
	hexArray[0] = hexVal.substring( 0, 2 );
	hexArray[1] = hexVal.substring( 2, 4 );
	hexArray[2] = hexVal.substring( 4, 6 );
	for( var k = 0; k < hexArray.length; k++ )
	{
		var num = hexArray[k];
		var res = 0;
		var j = 0;
		for( var i = num.length - 1; i >= 0; i-- )
			res += parseInt( nlbFade_hextable.indexOf( num.charAt( i ) ) ) * Math.pow( 16, j++ );
		rgbArray[k] = res;
	}
	return rgbArray;
}
function RGBToHex( rgbArray )
{
	var retval = new Array( );
	for( var j = 0; j < rgbArray.length; j++ )
	{
		var result = new Array( );
		var val = rgbArray[j];
		var i = 0;
		while( val > 16 )
		{
			result[i++] = val%16;
			val = Math.floor( val/16 );
		}
		result[i++] = val%16;
		var out = '';
		for( var k = result.length - 1; k >= 0; k-- )
			out += nlbFade_hextable[result[k]];
		retval[j] = padLeft( out, '0', 2 );
	}
	out = '#';
	for( var i = 0; i < retval.length; i++ )
		out += retval[i];
	return out;
}
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function( val, fromIndex ) {
		if( typeof( fromIndex ) != 'number' ) fromIndex = 0;
		for( var index = fromIndex, len = this.length; index < len; index++ )
			if( this[index] == val ) return index;
		return -1;
	}
}
function padLeft( string, character, paddedWidth )
{
	if( string.length >= paddedWidth )
		return string;
	else
	{
		while( string.length < paddedWidth )
			string = character + string;
	}
	return string;
}

/***************************
**** drop down ****
***************************/
function showHideProdDetail()
{
    var curDiv = document.getElementById("fillThisSpace");
    if (curDiv)
    {
//        var myVis = curDiv.style.display;
//        if (myVis == 'none')
//        {
            goToPage('1', null);
//        }
//        else
//        {
//            curDiv.innerHTML = '';
//            curDiv.style.display = 'none';
//        }
    }
}



function toggleDescription(element, e) {    
    var div = element.id;
    div = document.getElementById(div).style;
    
    var h = element.id + 'h';
    h = document.getElementById(h).style; 
    
    var p = element.id + 'p';
    p = document.getElementById(p).style;

    if (e.type == 'mouseover') {
      //  div.border = '1px solid #c8ccce';
      //  div.backgroundColor = "#fff";
        
        h.color = "#ff6600";
        h.textDecoration = 'none';
        
      //  p.width = '295px';
      //  p.height = 'auto';
    }
    else {
      // div.border = '0px';
        
        h.color = "#00599c";
        h.textDecoration = 'none';
        
      //  p.width = 'auto';
      //  p.height = '1em';
    }
}

/* old stuff */

/*//-----( @Help )-----------------------------------------------
var Help = {
	init: function() {
		var helpLinks = getElementsByClassName("help");
    
    
		helpLinks.each(function(helpLink) {
			Event.observe(helpLink, "click", Help.showPopup.bind(this), false);
		});
	},
	showPopup: function(event) {
		var href = Event.element(event);
		if (href != null || href != "") {
			var name = "fcom_help_popup";
			var features = "width=500,height=400,";
			features += "directories=no,location=no,menubar=no,";
			features += "resizable=no,scrollbars=yes,status=yes,toolbar=no";
			popupUrl(href, name, features);
			Event.stop(event);
		}
	}
}
//-----( END )-------------------------------------------------*/

/* Change + or - character for related items section on click event. */
var stateOther, stateRelated, fastPad;	// states of icons

function changeExpandedState(ref, state) {
    
    if (ref.hasChildNodes) {
    
	var detailsHeader = ref.firstChild; // h3 or whitespace
	var divElement;
	
	if (detailsHeader.nodeName.toLowerCase() == "h3") {
	    divElement = ref.nextSibling;
	} else if (detailsHeader.nodeType == 3) {
	    divElement = ref.nextSibling.nextSibling;
	}
	    
	if (divElement.nodeType == 3) {	//check if whitespace node
	    divElement = ref.nextSibling.nextSibling;
	}
	
    var indicator = ref.firstChild;	// +/- span
    
    if (indicator.nodeType == 3) {
	
	if (indicator.nextSibling.firstChild.nodeType == 3) {
	    indicator = indicator.nextSibling.firstChild.nextSibling;
	} else {
	    indicator = ref.firstChild.nextSibling.firstChild;
	}
    } else {
	indicator = ref.firstChild.firstChild;
    }
    
	if (divElement.style.display == 'none') {
	    divElement.style.display = 'block';
            indicator.className = "minimize";
	} else {
	    divElement.style.display = 'none';
            indicator.className = "maximize";
	}
    }
    
    return state;
}

// Sets a store in the cookie
function populateBack(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(storeCode + " is now your default store");	
	    dojo.byId("storeSelector").innerHTML = response;	
	    //dojo.byId("storeCode").value = storeCode;	           
            bProcessingAutoComp = false;
            return response;
        },
        error: function(response, ioArgs) {
            if (ioArgs.xhr.status == '200')
            {
		alert(storeCode + " is now your default store");
		dojo.byId("storeSelector").innerHTML = response;
            }
            else
            {
            dojo.byId("findStoreResults").innerHTML = "<p class='error'>Sorry, there has been an error, try again.</p>";
            }
            bProcessingAutoComp = false;
            return response;
        }
    } );	
}