/*
    Colors/finishes functions and global vars
    Copyright (C) 2000-2008 Kohler Company.    All Rights Reserved.
    Author: Dennis Spaag
    Date Created: 01/07/08
 */
function switchTab(tab) {
    var activeIndex = getTabIndex(tab);
    $('.ctab').each(function(i) {
        updateTabImages(this, activeIndex);
        showActiveTab(activeIndex,i+1);
    });
}

function showActiveTab(activeIndex, loopIndex) {
    if (loopIndex==activeIndex) {
       $("#colorfinishtab" + loopIndex).show();
    } else {
        $("#colorfinishtab" + loopIndex).hide();
    }
};

function switchSubTab(tab) {
    var activeIndex = getTabIndex(tab);
    // get all children images
    var relatedImages = $(tab).parent('.colorfinish-subnav').children('img.node');
    // get all children tables
    var relatedSubTabs = $(tab).parent('.colorfinish-subnav').next('div').children('table');

    relatedImages.each(function() {
        updateTabImages(this, activeIndex);
    });
    relatedSubTabs.each(function(i) {
        if (activeIndex==i) {
            $(this).show();
        } else {
            $(this).hide();
        }
    });
}

function updateTabImages(img, activeIndex) {
    var src = $(img).attr("src");
    var thisTabIndex = getTabIndex(img);
    var thissrc = "";
    if (src.indexOf("-on") > -1 && thisTabIndex != activeIndex) {
        thissrc = src.split("\-on")[0] + ".gif";
        $(img).attr("src", thissrc);
    }
    else if (src.indexOf("-on") == -1 && thisTabIndex == activeIndex) {
        thissrc = src.split("\.")[0] + "-on.gif";
        $(img).attr("src", thissrc);
    }
}

function getTabIndex(tab) {
    return  $(tab).attr("id").split("-")[2];
}

function updateColorFinishDropdowns() {
    var selectedOption = $('#productTypeSelect')[0].selectedIndex;
    populateColorFinishSelects(
        clearColorFinishDropdowns(),
        selectedOption   
    );
}

function clearColorFinishDropdowns() {
    var colorCodeSelect = $('#colorCode');
    var colorNameSelect = $('#colorCodeName');
    var initialCodeOption = colorCodeSelect[0].options[0];
    var initialNameOption = colorNameSelect[0].options[0];
    colorCodeSelect.children().remove();
    colorNameSelect.children().remove();
    colorCodeSelect.append(initialCodeOption);
    colorNameSelect.append(initialNameOption);
    var result = new Array();
    result[0] = colorCodeSelect;
    result[1] = colorNameSelect;
    return result;
}

function populateColorFinishSelects(selects, selectedIndex)
{
    var codeSelect = selects[0];
    var nameSelect = selects[1];
    if (selectedIndex > 0) {
        var idx =  selectedIndex-1;
        for (var i=0; i<colorDropDowns[idx].length; i++) {
            var pair = colorDropDowns[idx][i];
            var pairLast = pair.split("|").length-1;
            var value = pair.split("|")[0];
            var name = pair.split("|")[pairLast];
            codeSelect.append('<option value="' + value + '">' + value + '</option>');
            nameSelect.append('<option value="' + value + '">' + name  + '</option>');
        }
    }
    codeSelect[0].selectedIndex=0;
    nameSelect[0].selectedIndex=0;
}

function syncColorSelections(whichOne) {
    if (whichOne==0) {
        $('#colorCodeName')[0].selectedIndex = $('#colorCode')[0].selectedIndex;
    } else {
        $('#colorCode')[0].selectedIndex = $('#colorCodeName')[0].selectedIndex;
    }
}

// on-DOM-load
$(function() {

    $('.ctab').click(function() {
        switchTab(this);
    });

    $('.node').click(function() {
        switchSubTab(this);
    });

    $('#productTypeSelect').change(function(){
        updateColorFinishDropdowns();
    });
    $('#colorCode').change(function(){
        syncColorSelections(0);
    });
    $('#colorCodeName').change(function(){
        syncColorSelections(1);
    });

    $("#productColorSearchSubmit").click(function() {
        if ($('#colorCode')[0].selectedIndex > 0) {
            $("#productColorSearchForm")[0].submit();    
        }
        return false;
    });
});



