/**
* Update our product page when we select a product variation
* Set up our tab control
*/
$(document).ready(function()
{
    $(".variation_option_definition").each(function(i){
        $("#product_"+this.name).bind("change",load_product_variation);
    });

    //Create jquery tabs from our list (specifications, overview, buying guide, related products)
    $('#container-1').tabs();

    //Auto select the first tab
    if($('#first_tab').val() != '')
    {
        var first_tab = $('#' + $('#first_tab').val());
        var container = $('#container-1');
        container.find('>div:visible').addClass('tabs-hide');
        first_tab.removeClass('tabs-hide');
        container.find('>ul>li').removeClass('on');
    }

    if($('.thumbnails').length) {
        $('.thumbnails li a').each(function() {
            $(this).click(function() {
                $('.thickbox').attr({href: $(this).attr('href')});
                $('.thickbox img').attr({src: $('img', this).attr('src')});
                return false;
            })});
    }

});

function show_in_stock()
{
    $('#add_to_cart').attr('src','/images/buttons/add-to-cart.png');
    $('#quantity, #add_to_cart').unset('disabled');
    $('#in-stock-message').show();
    $('#no-stock-message').hide();
}

function show_no_stock()
{
    $('#add_to_cart').attr('src','/images/buttons/add-to-cart-off.png');
    $('#quantity, #add_to_cart').unset('disabled');
    $('#in-stock-message').hide();
    $('#no-stock-message').show();
}

/**
* Updates product variation fields on the page using Ajax with jquery
*/
function load_product_variation()
{
    $('#availability').html('Loading...');
    $('#quantity, #add_to_cart').attr('disabled', 'disabled');
    $('#add_to_cart').attr('src','/images/buttons/add-to-cart-loading.png');

    //Currently selected variation_definition_id => variation_option_value
    var selected_variations = new Object();

    //Name of the select box we just changed
    var changed_select_name = this.name;

    //Get all the current selected variations above and including the changed one
    var above_selected_option = true;
    $(".variation_option_definition").each(function(i){

        if(above_selected_option)
        selected_variations[this.name] = escape(this.value);

        if(this.name == changed_select_name)
        above_selected_option = false;
    });

    selected_variations['product_id'] = $("#product_id").val();

    //Update all of our selection boxes with the new data
    $.post('/product/ajax_get_product_variation.html',selected_variations,function(response){
        $('variationoption',response).each(function(i)
        {
            var variation_option_name = $('name',this).text();
            var select_input = $('#product_'+variation_option_name).get(0);

            //Clear all the select options
            select_input.options.length = 0;

            //Repopulate the options
            $('option',this).each(function(select_index){
                select_input.options[select_index] = new Option($('value',this).text(),$('value',this).text());
            });

            select_input.selectedIndex = $('selectedindex',this).text();
        });

        //Update product page details
        $('#variation_id').attr('value',$('variation/id',response).text());
        $('#availability').html($('variation/availability',response).text());
        $('#ship_latency').html($('variation/shiplatency',response).text());

        var market_price = parseFloat( $('variation/retailprice',response).text() );
        $('#market_price').html(market_price.toFixed(2));

        var our_price = parseFloat( $('variation/ourprice',response).text() );

        var normal_price = parseFloat( $('variation/normalprice',response).text() );

        $('#our_price').hide();
        $('#usually_price').hide();

        if(our_price < normal_price)
        {
            $('#usually_price').show();
            $('#usually_price').html('Usually: $'+normal_price.toFixed(2));
            $('#our_price').html('On Sale: $'+our_price.toFixed(2));
            $('#our_price').addClass('important');
        }else{
            $('#our_price').removeClass('important');
            $('#our_price').html('$'+our_price.toFixed(2));
        }

        $('#our_price').show('slow');

        if($('variation/isavailable',response).text() == 1)
        show_in_stock();
        else
        show_no_stock();

        var save_amount = parseFloat( $('variation/retailprice',response).text() ) - parseFloat( $('variation/ourprice',response).text() ) ;
        var retail_price = parseFloat( $('variation/retailprice',response).text() );
        var save_percentage = (save_amount / retail_price) * 100;

        $('#save_percentage').html(Math.round(save_percentage));
        $('#you_save').html(save_amount.toFixed(2));
    });
}

/**
* Updates product rating on the page using DHTML
*/
function rate(product_id,rating)
{

    $.get('/product/ajax_set_product_rating/'+product_id+'/'+rating,function(response){
        $("#current_rating").css("width", $('average_rating',response).text()+'px');
        $("#selected_rating").css("width", $('selected_rating',response).text()+'px');
        $('#number_of_votes').html($('quantity',response).text());
    });
}