var lastTab = "";
var suppressBuyNow = false;

$('document').ready(function(){
    $('.tabs').click(function(){
        if(lastTab == ""){
            lastTab = $('#defaultTab').val();
        }
        if (lastTab != "") {
            $('#' + lastTab + '_display').removeClass("show");
            $('#' + lastTab + '_display').addClass("hide");
            switchTabColor(lastTab, "Gray");
        }

        lastTab = this.name;

        $('#' + this.name + '_display').removeClass("hide");
        $('#' + this.name + '_display').addClass("show");
        switchTabColor(this.name, $('#tabColor').val());
    });

    $('.buynow').click(function(){
        if (this.type == "radio" && this.checked) {
            postBuyNow();
        }
        else
            if (this.type != "radio") {
                postBuyNow();
            }
    });

    if(!suppressBuyNow){
        postBuyNow();
    }
});

function switchTabColor(name, color){
    $('#' + name + '_left').attr({src: '/en/images/tab_left_' + color + '.gif'});
    $('#' + name + '_right').attr({src: '/en/images/tab_right_' + color + '.gif'});
    $('#' + name + '_tab').css({backgroundImage: 'url(/en/images/tab_center_' + color + '.gif)'});
}

function postBuyNow(){
    var dataArray = {};
    dataArray["type"] = "xml";
    var count = 0;

    $('.buynow').each(function(){
        if (this.type == "radio" && this.checked) {
            dataArray[this.name] = this.value;
        }
        else if (this.type != "radio") {
            dataArray[this.name] = this.value;
        }
    });

    $.post('/products/sku-lookup.jsp', dataArray, function(xml){
        $('.listPrice').html(getValue(xml, "listPrice"));
        $('.campaignPrice').html(getValue(xml, "campaignPrice"));
        $('.largeImage').attr("src", getValue(xml, "largeImage"));

        if(getValue(xml, "goodType") == "hardgood"){
            $('#shippingDetails').css("display", "block");
            $('#downloadDetails').css("display", "none");
        }
        else if(getValue(xml, "goodType") == "downloadable"){
            $('#shippingDetails').css("display", "none");
            $('#downloadDetails').css("display", "block");
        }
        else{
            $('#shippingDetails').css("display", "none");
            $('#downloadDetails').css("display", "none");
        }
    });
}

function getValue(xml, name){
    return xml.substring(xml.indexOf(name + ">") + name.length+1, xml.indexOf("</" + name));
}
