document.observe('dom:loaded', function() {
    $$('.HomePageFeatureArea').each(function(element) {
        new Fader(element, .5, 5);
    });
});

function setCarousel(){
    alert('bb');
    jQuery(".first-and-second-carousel").jcarousel();
    alert('aa');
}


function onSearchMouseOver()
{
	(window.status='Search...');
	return(true);
}

function searchClick()
{
	location.href='/searchResults.aspx?tSearch=' + document.fSearch.tSearch.value;
}

function setTab(link, content) {
    var tabContainer = $(link).up().up();
    var tab = $(link).up();

    tabContainer.getElementsBySelector('li').each(function(element) {
        if (element.hasClassName('current')) element.removeClassName('current');
        if (element == tab) element.addClassName('current');
    } .bind(this));

    $('ProductTabContent').childElements().each(function(element) {
        if (element.hasClassName('Description')) element.hide().removeClassName('Description');
        if (element.identify() == content) {
            element.appear({ duration: 0.3 });
        }
        else {
            if (element.visible()) element.fade({ duration: 0.3 });
        }
    } .bind(this));
}

function validateOptions() {
    var optionGroup = ($('optionGroup')) ? (parseInt($('optionGroup').value)) : (0);
    var optionCount = ($('optionCount')) ? (parseInt($('optionCount').value)) : (0);
    var multiple = ($('multiple')) ? (parseInt($('multiple').value)) : (1);
    var submit = true;

    for (var x = 1; x <= multiple; x++) {
        //  first, empty current option value
        if ($('options_' + x)) {
            $('options_' + x).value = '';
        }

        var val = '';

        //  second, assign values based on user-selected values
        for (var y = 0; y < optionCount; y++) {
            var optionsBoxName = 'optionsBox_' + y + '_' + x;
            if ($(optionsBoxName)) {
                val += $(optionsBoxName).value + ((y < (optionCount - 1)) ? (';') : (''));
            }
        }

        //$('ProductOptions').childElements().each(function(element) {
        document.getElementById("ProductOptions").childElements().each(function(element) {
            if (element.value.toLowerCase().split(';').uniq().sort().join(';') == val.toLowerCase().split(';').uniq().sort().join(';')) {
                if ($('options_' + x)) {
                    $('options_' + x).value = optionGroup + ';' + element.identify();
                }
            }
        } .bind(this));

        //  third, validate whether all options are accounted for
        if ($('options_' + x) && $('options_' + x).value == '') submit = false;
    }

    return submit;
}

function replaceMainImage(element, source) {
    if ($(element) && $(element).src != source) $(element).src = source;
}

function replaceMainImageAndAnchor(element, imageName) {
    var anchorSource = '/getdynamicimage.aspx?path=' + imageName + '&w=600&h=600';
    var imageSource = '/getdynamicimage.aspx?path=' + imageName + '&w=348&h=348';

    if ($(element) && $(element).href != anchorSource) {
        $(element).href = anchorSource;
        $(element).childElements()[0].src = imageSource;
    }
}

function setExtra(multiple) {
    var optionCount = ($('optionCount')) ? (parseInt($('optionCount').value)) : (0);
    var extra = '';
    var val = '';

    for (var y = 0; y < optionCount; y++) {
        var optionsBoxName = 'optionsBox_' + y + '_' + multiple;
        if ($(optionsBoxName)) {
            val += $(optionsBoxName).value + ((y < (optionCount - 1)) ? (';') : (''));
        }
    }

    //$('ProductOptions').childElements().each(function(element) {
    document.getElementById("ProductOptions").childElements().each(function(element) {
        if (element.value.split(';').uniq().sort().join(';') == val.toLowerCase().split(';').uniq().sort().join(';')) {
            var extraID = element.identify() + '_extra'
            if ($(extraID)) {
                setPrice($(extraID).value.split(';')[0]);
                //  TODO:  setImage($(extraID).value.split(';')[1]);
                //  TODO:  setStockStatus($(extraID).value.split(';')[3]);
            }
        }
    } .bind(this));

    validateOptions();
}

function setExtra(multiple, index) {
    var elementOne = "optionsBox_1_" + multiple
    var elementZero = "optionsBox_0_" + multiple
    if (index == 0) {
        var selectedIndex = $(elementZero).selectedIndex;
        var selectedItem = $(elementZero)[selectedIndex].value;
        var otherIndex = $(elementOne).selectedIndex;
        var otherItem = $(elementOne)[otherIndex].value;
        if (selectedIndex == 0) {
            populateBoth(multiple);
            for (var i = 0; i < $(elementOne).options.length; i++) {
                if ($(elementOne)[i].value == otherItem) {
                    $(elementOne)[i].selected = true;
                }
            }
        }
        else {
            clearOption(index, multiple);
            populateSizes(selectedItem, index, multiple);
            if (otherIndex != 0 && selectedIndex != 0) {
                for (var i = 0; i < $(elementOne).options.length; i++) {
                    if ($(elementOne)[i].value == otherItem) {
                        $(elementOne)[i].selected = true;
                    }
                }
            }
        }
    }
    if (index == 1) {
        var selectedIndex = $(elementOne).selectedIndex;
        var selectedItem = $(elementOne)[selectedIndex].value;
        var otherIndex = $(elementZero).selectedIndex;
        var otherItem = $(elementZero)[otherIndex].value;
        if (selectedIndex == 0) {
            populateBoth(multiple);
            for (var i = 0; i < $(elementZero).options.length; i++) {
                if ($(elementZero)[i].value == otherItem) {
                    $(elementZero)[i].selected = true;
                }
            }
        }
        else {
            clearOption(index, multiple);
            populateColors(selectedItem, index, multiple);
            if (otherIndex != 0 && selectedIndex != 0) {
                for (var i = 0; i < $(elementZero).options.length; i++) {
                    if ($(elementZero)[i].value == otherItem) {
                        $(elementZero)[i].selected = true;
                    }
                }
            }
        }
    }

    var optionCount = ($('optionCount')) ? (parseInt($('optionCount').value)) : (0);
    var extra = '';
    var val = '';

    for (var y = 0; y < optionCount; y++) {
        var optionsBoxName = 'optionsBox_' + y + '_' + multiple;
        if ($(optionsBoxName)) {
            val += $(optionsBoxName).value + ((y < (optionCount - 1)) ? (';') : (''));
        }
    }
    
    setPrice('0')

    //$('ProductOptions').childElements().each(function(element) {
    document.getElementById("ProductOptions").childElements().each(function(element) {
        if (element.value.split(';').uniq().sort().join(';') == val.toLowerCase().split(';').uniq().sort().join(';')) {
            var extraID = element.identify() + '_extra'
            if ($(extraID)) {
                setPrice($(extraID).value.split(';')[0]);
                //  TODO:  setImage($(extraID).value.split(';')[1]);
                //  TODO:  setStockStatus($(extraID).value.split(';')[3]);
            }
        }
    } .bind(this));
    
    validateOptions();
    //setExtra(multiple);
}

function clearOption(index, multiple) {
    var elementOne = "optionsBox_1_" + multiple
    var elementZero = "optionsBox_0_" + multiple
    if (index == 0) {
        while ($(elementOne).options.length > 1) {
            $(elementOne).remove(1);
        }
    }
    if (index == 1) {
        while ($(elementZero).options.length > 1) {
            $(elementZero).remove(1);
        }
    }
}

function populateBoth(multiple) {
    var elementOne = "optionsBox_1_" + multiple
    var elementZero = "optionsBox_0_" + multiple
    var element = "optionsBox_1_" + multiple;
    //while ($(elementOne).options.length > 1) {
    while ($(element).options.length > 1) {
        $(elementOne).remove(1);
        }
        while ($(elementZero).options.length > 1) {
            $(elementZero).remove(1);
    }
    var colors = "";
    var sizes = "";
    var items = document.getElementById("ProductOptions").getElementsByTagName("input");
    var itemCount = items.length;
    var idRegEx = "_extra$";
    for (var i = 0; i < itemCount; i++) {
        var itema = items.item(i).attributes;
        if (itema.getNamedItem("id").nodeValue.toString().match(idRegEx)){
            if (itema.getNamedItem("value").nodeValue.toString().split(";")[3] > 0) {
                var colorOptOption = document.createElement("option");
                var colorValue = items.item(i - 1).attributes.getNamedItem("value").nodeValue.toString().split(";")[0].toString().toUpperCase().replace("COLOR=", "color=");
                if (!colors.match(colorValue)) {
                    colorOptOption.value = colorValue;
                    colorOptOption.text = colorValue.replace("color=", "");
                    $(elementZero).options.add(colorOptOption);
                    colors = colors + ";" + colorValue;
                }
                
                var sizeOptOption = document.createElement("option");
                var sizeValue = items.item(i - 1).attributes.getNamedItem("value").nodeValue.toString().split(";")[1].toString().toUpperCase().replace("SIZE=", "size=");
                if (!sizes.match(sizeValue)) {

                    var upcharge = "";
                    if (itema.getNamedItem("value").nodeValue.toString().split(";")[0] > 0)
                        upcharge = " (+ " + itema.getNamedItem("value").nodeValue.toString().split(";")[0] + ")";
                    if (itema.getNamedItem("value").nodeValue.toString().split(";")[0] < 0)
                        upcharge = " (- " + itema.getNamedItem("value").nodeValue.toString().split(";")[0] + ")";
                        
                    sizeOptOption.value = sizeValue;
                    sizeOptOption.text = sizeValue.replace("size=", "") + upcharge;
                    $(elementOne).options.add(sizeOptOption);
                    sizes = sizes + ";" + sizeValue;
                }
            }
        }
    }
}

function populateColors(selectedSize, index, multiple) {
    var elementOne = "optionsBox_1_" + multiple
    var elementZero = "optionsBox_0_" + multiple
    var items = document.getElementById("ProductOptions").getElementsByTagName("input");
    var itemCount = items.length;
    var idRegEx = "_extra$";
    var colors = "";
    for (var i = 0; i < itemCount; i++) {
        var itema = items.item(i).attributes;
        if (itema.getNamedItem("id").nodeValue.toString().match(idRegEx) && itema.getNamedItem("id").nodeValue.toString().toLocaleUpperCase().match(selectedSize.toString().toLocaleUpperCase())) {
            if (itema.getNamedItem("value").nodeValue.toString().split(";")[3] > 0) {
                var optOption = document.createElement("option");
                var value = items.item(i - 1).attributes.getNamedItem("value").nodeValue.toString().split(";")[0].toString().toUpperCase().replace("COLOR=", "color=");
                if (!colors.match(value)) {
                    optOption.value = value;
                    optOption.text = value.replace("color=", "");
                    if (index == 0) {
                        $(elementOne).options.add(optOption);
                    }
                    if (index == 1) {
                        $(elementZero).options.add(optOption);
                    }
                    colors = colors + ";" + value;
                }
            }
        }
    }
}

function populateSizes(selectedColor, index, multiple) {
    var elementOne = "optionsBox_1_" + multiple
    var elementZero = "optionsBox_0_" + multiple
    var items = document.getElementById("ProductOptions").getElementsByTagName("input");
    var itemCount = items.length;
    var idRegEx = "_extra$";
    var sizes = "";
    for (var i = 0; i < itemCount; i++) {
        var itema = items.item(i).attributes;
        if (itema.getNamedItem("id").nodeValue.toString().match(idRegEx) && itema.getNamedItem("id").nodeValue.toString().toLocaleUpperCase().match(selectedColor.toString().toLocaleUpperCase())) {
            if (itema.getNamedItem("value").nodeValue.toString().split(";")[3] > 0) {

                var upcharge = "";
                if (itema.getNamedItem("value").nodeValue.toString().split(";")[0] > 0)
                    upcharge = " (+ " + itema.getNamedItem("value").nodeValue.toString().split(";")[0] + ")";
                if (itema.getNamedItem("value").nodeValue.toString().split(";")[0] < 0)
                    upcharge = " (- " + itema.getNamedItem("value").nodeValue.toString().split(";")[0] + ")";
            
                var optOption = document.createElement("option");
                var value = items.item(i - 1).attributes.getNamedItem("value").nodeValue.toString().split(";")[1].toString().toUpperCase().replace("SIZE=", "size=");
                if (!sizes.match(value)) {
                    optOption.value = value;
                    optOption.text = value.replace("size=", "") + upcharge;
                    if (index == 0) {
                        $(elementOne).options.add(optOption);
                    }
                    if (index == 1) {
                        $(elementZero).options.add(optOption);
                    }
                    sizes = sizes + ";" + value;
                }
            }
        }
    }
}

function setPrice(upcharge) {
    //  Make sure we're dealing with a number that is not zero
    if (!isNaN(upcharge) && parseFloat(upcharge) != 0) {
        if ($('currentPrice') && $('productPrice')) {
            $('currentPrice').innerHTML = '$' + formatCurrency(parseFloat($('productPrice').value) + parseFloat(upcharge));
        }

        if ($('currentSale') && $('productSale')) {
            $('currentSale').innerHTML = '$' + formatCurrency(parseFloat($('productSale').value) + parseFloat(upcharge));
        }
    }
    else {
        if ($('currentPrice') && $('productPrice')) {
            $('currentPrice').innerHTML = '$' + formatCurrency(parseFloat($('productPrice').value));
        }

        if ($('currentSale') && $('productSale')) {
            $('currentSale').innerHTML = '$' + formatCurrency(parseFloat($('productSale').value));
        }
    }
}

function formatCurrency(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}
