var imageBaseURL = "http://www.galco.com/images/catalog/sb/"; // DES 08/03/2010
//var imageBaseURL = "http://webdev.galco.com/images/catalog/sb/"; // DES 08/03/2010
var is_xchrome = null;
var is_msie = null;
var is_xsafari = null;
var lastOptionDiv = null; // DES 08/03/2010
var lastSB = null; // DES 08/03/2010
var sbChildDiv = null;
var sbChildPrefix = "}";
var sbChildSB = null;
var sbCurrentParentSB = null;
var sbImageDiv = null;
var sbImageSBDiv = null;
var sbOptionImage = null;
var selectedSB = null;

function addOptionDiv(oOption, optionIndex, optionText, optionImage) {  //  DES 08/03/2010
    var pixels = oOption.offsetWidth;
    var work = "<div id='customOption" + optionIndex + "'";
    work += " class='opt'";
    if (optionIndex > 0) {
        work += " onmousedown='clicker(this)'";
    }
    var work2 = " onmouseover='doMouseOver(this, \"" + optionImage + "\")'>";
    work += work2 + optionText + "</div>";
    oOption.innerHTML += work;
}

function applyIframe(oDiv, oIframe) {  // DES 08/03/2010
    oIframe.style.left = findLeft(oDiv);
    oIframe.style.top  = findTop(oDiv);
    oIframe.style.width = oDiv.offsetWidth;
    oIframe.style.height = oDiv.offsetHeight;
    oDiv.style.zIndex = 10000;
    oIframe.style.zIndex = oDiv.style.zIndex - 1;
    oIframe.style.display = "block";
}

function clicker(oDiv) {
    var mommy = oDiv.parentNode;
    var optionIndex = parseInt(oDiv.id.substring(12));
    var parmSeqNumIndex = mommy.seqNumIndex;
    var parmRaw = parms[parmSeqNumIndex][optionIndex][0];
    var parmSeqNum = parms[parmSeqNumIndex][0][0];
    //var oSB = document.getElementById("parmvalue" + parmSeqNumIndex);
    var oSB = eval("document.form1.parmvalue" + parmSeqNumIndex);
    oSB.selectedIndex = optionIndex;
    document.form1.selseqnum.value = parmSeqNum;
    submitter();
}

function doMouseOver(oOptionDiv, imageName) {   //   DES  08/03/2010
    highlightOption(oOptionDiv);
    showImage(imageName);
}

function findLeft(obj) {
    if (!obj) return  0;
    return obj.offsetLeft + findLeft(obj.offsetParent);
}

function findTop(obj) {
    if (!obj) return 0;
    return obj.offsetTop + findTop(obj.offsetParent);
}

function getChildDiv() {
    if (!sbChildDiv) sbChildDiv = document.getElementById("sbChildDiv");
    return sbChildDiv;
}

function getChildSB() {
    if (!sbChildSB) sbChildSB = document.form1.sbChildSB;
    return sbChildSB;
}

function getImageDiv() {
    //if (!sbImageDiv) sbImageDiv = document.form1.imageDiv;
    if (!sbImageDiv) sbImageDiv = document.getElementById("imageDiv");
    return sbImageDiv;
}

function getImageSBDiv() {
    //if (!sbImageSBDiv) sbImageSBDiv = document.form1.imageSBDiv;
    if (!sbImageSBDiv) sbImageSBDiv = document.getElementById("imageSBDiv");
    return sbImageSBDiv;
}

function getOptionImage() {
    if (!sbOptionImage) sbOptionImage = document.form1.optionImage;
    return sbOptionImage;
}

function hideImage() {    //   DES  08/03/2010
    //document.getElementById("imageDiv").style.visibility = "hidden";
    getImageDiv().style.visibility = "hidden";
    if (is_ie()) {
        var oIfr = document.getElementById("iframeImageDiv");
	oIfr.style.display = "none";
    }
}

function hideImageSB() {   //   DES  08/03/2010
    hideImage();
    resetLastOption();
    //document.getElementById("imageSBDiv").style.visibility = "hidden";
    getImageSBDiv().style.visibility = "hidden";
    if (is_ie()) {
        var oIfr = document.getElementById("iframeImageSBDiv");
	oIfr.style.display = "none";
    }
}

function highlightOption(oOptionDiv) {   //  DES  08/03/2010
    oOptionDiv.style.backgroundColor = "#06E";
    oOptionDiv.style.color = "white";
    if (lastOptionDiv && lastOptionDiv != oOptionDiv) {
        lastOptionDiv.style.backgroundColor = "white";
	lastOptionDiv.style.color = "black";
    }
    lastOptionDiv = oOptionDiv;
}

function is_chrome() {
    //return true;
    if (is_xchrome) return is_xchrome;
    is_xchrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    return is_xchrome;
}

function is_ie(){
    // return true;
    if (is_msie) return is_msie;
    var work = navigator.appName.toUpperCase();
    is_msie = (work == "MICROSOFT INTERNET EXPLORER");
    return is_msie;
}

function is_safari(){
    if (is_xsafari) return is_xsafari;
    if (is_chrome()) {
        is_xsafari = false;
	return false;
    }	
    is_xsafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
    return is_xsafari;
}

function loadSB(oSBj, seqNumIndex) {
    //var oWork = document.getElementById("imageSBDiv");
    var oWork = getImageSBDiv();
    var k;
    var fc = document.form1.familycode.value;
    var work = "";
    //var sc = document.form1.subfamilycode.value;
    //if (sc == "*") sc = "";
    var fsc = fc + "/"; // "powermod/";
    //if (sc > "") fsc += sc + "/";
    fsc = fsc.toLowerCase();
    oWork.innerHTML = ""; // <div style='float: clear; height: 1px;'></div>";
    oWork.seqNumIndex = seqNumIndex;
    addOptionDiv(oWork, 0, parms[seqNumIndex][0][1],"");
    for (k = 1; k < parms[seqNumIndex].length; k++) {
        work = parms[seqNumIndex][k][2];
	if (work > "") {
	    if (work.substring(work.length - 1) == "/") {
                work = "";
	    }
        } 
	if (work > "") {
            work = fsc + work;
	} 
	addOptionDiv(oWork, k, 
	    parms[seqNumIndex][k][1], work);
	    // fsc + parms[seqNumIndex][k][2]);
    }
    if (is_ie && oWork.offsetHeight > 300) { 
	oWork.style.height="300px";
    }
}

function resetLastOption() {   //  DES  08/03/2010
    if (lastOptionDiv) {
        lastOptionDiv.style.backgroundColor = "white";
	lastOptionDiv.style.color = "black";
	lastOptionDiv = null;
    }
}

function sbChildChanged(me) {
    var index = me.selectedIndex;
    var optionValue = me.options[index].value;
    var childFlag = optionValue.charAt(0) == sbChildPrefix;
    var parmSeqNum;
    var work;
    if (childFlag) {
        optionValue = optionValue.substring(1);
    }
    if (index != 0) {
        if (index == me.length - 1) {
            optionValue = me.options[0].value;
	    childFlag = optionValue.charAt(0) == sbChildPrefix
	    if (childFlag) {
                optionValue = optionValue.substring(1);
		sbLoadChildren(optionValue);
	    } else {
	        sbHideChildren();
            }
            sbCurrentParentSB.selectedIndex = 0;
	    return;
        } 
        if (childFlag) {
            sbLoadChildren(optionValue);
	} else { 
	    parmSeqNum = me.options[0].value;
            childFlag = parmSeqNum.charAt(0) == sbChildPrefix;
	    while (childFlag) {
                parmSeqNum = parmSeqNum.substring(1);
		parmSeqNum = children[parseInt(parmSeqNum)][0][0];
		childFlag  = parmSeqNum.charAt(0) == sbChildPrefix;
	    }
	    parmSeqNum = parms[parseInt(parmSeqNum)] [0] [0];
	    document.form1.selseqnum.value = parmSeqNum;
            work = me.options[me.selectedIndex].value;
	    sbCurrentParentSB.options[sbCurrentParentSB.selectedIndex].value = work;
	    submitter();
	}
    }
}

function sbHideChildren() {
    getChildDiv().style.visibility = "hidden";
    if (sbCurrentParentSB) sbCurrentParentSB.selectedIndex = 0;
}

function sbLoadChildren(newOptions) {
    var cIndex = parseInt(newOptions);
    var i;
    var o = getChildSB();
    for (i = o.length - 1; i > 0; i--) {
        o.options[i] = null;
    }
    for (i = 0; i < children[cIndex].length; i++) {
        o.options[i] = new Option(children[cIndex] [i] [1], children[cIndex] [i] [0]);    }
    o.options[o.length] = new Option("<< GO BACK >>", "");
    o.options[0].selected = true;
    o.size = o.length;
}

function sbParentChanged(oParent) {
    var optionValue = oParent.options[oParent.selectedIndex].value;
    var work = oParent.name.substring(9);
    var sbIndex = parseInt(work);
    sbCurrentParentSB = oParent;
    if (optionValue != "") {
        if (optionValue.charAt(0) == sbChildPrefix ) {
	    optionValue = optionValue.substring(1);
	    sbLoadChildren(optionValue);
	    sbShowChildren(oParent);
	    getChildSB().focus();
	} else {
            submitter();
	}
    } 
}

function sbShowChildren(oParent) {
    var o = getChildDiv();
    o.style.position = "absolute";
    o.style.top = findTop(oParent); 
    o.style.left = findLeft(oParent);
    o.style.visibility = "visible";
}

function setSelection(ndx, objSB) {
    document.form1.selseqnum.value = parms[ndx][0][0];
    selectedSB = objSB; // document.getElementById("parmvalue" + ndx);
    //alert ("SB name = " + objSB.name);
    //alert ("ndx is " + ndx); 
    //alert (document.getElementById("parmvalue" + ndx));
    //alert ("sbChildDiv" + document.getElementById("sbChildDiv").id); 
}

function showImage(imageName) {   //   DES  08/03/2010
    //var oImageDiv = document.getElementById("imageDiv");
   var oImageDiv = getImageDiv();
    //var oOptionImage = document.getElementById("optionImage");
   var oOptionImage = getOptionImage();
   if (imageName != "") {
      oOptionImage.src = imageBaseURL + imageName;
	 if (is_ie()) {
	    applyIframe(oImageDiv, document.getElementById("iframeImageDiv"));
	 } else {
         oImageDiv.style.zIndex = 9999;
      }
	 oImageDiv.style.visibility = "visible";
   } else {
      hideImage();
   }
}

function showImageSB(oSBj, seqNumIndex) {
    if (parms[seqNumIndex] [0] [2] != ".") return;
    if (is_chrome() || is_safari()) return;
    var oSBDiv = getImageSBDiv();
    var oImageDiv = getImageDiv();
    var oIframeImageSBDiv = document.getElementById("iframeImageSBDiv");
    loadSB(oSBj, seqNumIndex);
    var left = findLeft(oSBj);
    var top = findTop(oSBj);
    oSBDiv.style.left = left;
    oSBDiv.style.top = top + 20;
    if (is_ie()) applyIframe(oSBDiv, oIframeImageSBDiv);
    oSBDiv.style.visibility = "visible";
    if (left > 500) left -= 480;
    oImageDiv.style.left = left + 170;
    oImageDiv.style.top = oSBDiv.style.top;
    lastOptionDiv = null;
    oSBDiv.focus();
}


