/**
* 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;
            })});
    }
    
    load_product_variation();

});

function show_in_stock()
{
    //$('#add_to_cart').attr('src','/images/buttons/add-to-cart.png');
    $('#quantity').removeAttr('disabled');
    $('#add_to_cart').removeAttr('disabled');
    $('#out_of_stock_message').html('<span>*</span>This item is in stock and ships within 48 hours.');
}

function show_no_stock()
{
    $('#add_to_cart').attr('src','/images/buttons/add-to-cart-off.png');
    $('#out_of_stock_message').html('<span>*</span>This popular item is currently out of stock.  Please check back soon.');
}

/**
* Updates product variation fields on the page using Ajax with jquery
*/
function load_product_variation(num)
{
    $('#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;
    
    if (num>-1){
    	this.name = 1;
    	selected_variations[this.name] = escape(document.getElementById("product_1").options[num].value);
    	above_selected_option = false;	
    }
    	    
    $(".variation_option_definition").each(function(i){

        if(above_selected_option)
        //alert(this.value);
        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){
        
        
        $(response).find('variationoption').each(function(i)
        {
            var variation_option_name = $('name',this).text();
            var select_input = $('#product_'+variation_option_name).get(0);

			if (variation_option_name != changed_select_name)
			{
				var selected = select_input.selectedIndex;
				var oldvalue = select_input.value;
				var length = select_input.options.length;
				//alert(length);
			}

            //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();
            
            if (variation_option_name != changed_select_name)
			{
				if (select_input.options.length == length)
				{
					select_input.selectedIndex = selected;
					select_input.value = oldvalue;
				}
			}
        });

        //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());
        $('#sku').html($('variation/sku',response).text());
	
        var our_price = parseFloat( $('variation/ourprice',response).text() );
        var suggested_price = parseFloat( $('variation/retailprice',response).text() );
        
        $('#suggested_price').html('Suggested Price: $'+suggested_price.toFixed(2));
        $('#our_price').html('Our Price $'+our_price.toFixed(2));
       
        if($('variation/isavailable',response).text() == 1)
        show_in_stock();
        else
        show_no_stock();
        
    });
}

/**
* 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());
    });
}
