/**
* Update our product page when we select a product variation
* Set up our tab control
*/
$(document).ready(function()
{
    $(".variation_option_definition").each(function(i){
        $(this).bind("change",load_product_variation);
    });
	
	/*if($(".variation_option_definition").length > 0){ //have select boxes, disable this until they are used
	$('#quantity, #add_to_cart').attr('disabled','disabled');
	}*/
	/*$("#id").bind('keyup',function(){
		
	}*/

});

function show_in_stock()
{
    $('#quantity, #add_to_cart').removeAttr('disabled');
	$('#no_stock_message').hide();
	$('#add_to_cart').show();
}

function show_no_stock()
{
	$('#quantity, #add_to_cart').attr('disabled','disabled');
    $('#add_to_cart').hide();
	$('#no_stock_message').show();
}

/**
* Updates product variation fields on the page using Ajax with jquery
*/
function load_product_variation()
{
    $('#sku').html('Loading ...');

	$('#quantity, #add_to_cart').attr('disabled','disabled');

	$('#main_image').attr('src','/img/ajax-loader.gif');
    
    //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] = this.value;

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

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

	//console.debug(selected_variations);

    //Update all of our selection boxes with the new data
    $.get('/product/ajax_get_product_variation',selected_variations,update_page);
}

function update_page(response)
{
    $('variationoption',response).each(function(i)
    {
        var variation_option_name = $('name',this).text();
        
		var select_input = $('#option_'+variation_option_name).get(0);

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

		//Repopulate the options
		$('option',this).each(function(select_index){
															 //text                                               //value 
			select_input.options[select_index] = new Option( unescape($('value',this).text().replace(/\+/g," ")) ,$('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());
    //$('#sku').html($('variation>sku',response).text());
    
    var price = parseFloat( $('variation>price',response).text() );
    var sale_price = parseFloat( $('variation>sale_price',response).text() );
    var non_sale_price = parseFloat( $('variation>normal_price',response).text() );
    var retail_price = parseFloat( $('variation>retail_price',response).text() );
    var wholesale =  $('variation>wholesale_price',response).text();
    var wholesale_from =  $('variation>wholesale_price_from',response).text();
	$('#main_image img').attr('src',$('variation>image',response).text());
	$('#main_image a').attr('href',$('variation>image_full',response).text());
	$('#enlarge_link').attr('href',$('variation>image_full',response).text());

    if(sale_price>0)
    {
        var save = (retail_price > 0) ? retail_price - sale_price : non_sale_price - sale_price;

    }else
    {
        var save = (retail_price > 0 && retail_price > price) ? retail_price - price : 0; 
    }

    if(sale_price > 0 && sale_price < non_sale_price ){
		$('#price').html(non_sale_price);
		$('#price').css('text-decoration','line-through');
		//$('#price').css('color','darkred');
		$('#sale_pricing').css('color','darkred');
		$('#sale_pricing').html(sale_price);
		$('#sale_pricing').show();
    	$('#sale_pricing').formatCurrency({useHtml:true});
    }
    else{
        $('#price').html(price);
        $('#price').css('text-decoration','none');
		$('#sale_pricing').hide();
    }
      
    $('#price').formatCurrency({useHtml:true});
    
    if(wholesale && wholesale_from)
    {
    	$('#whole_sale_price').html(wholesale_from + ' or more only $' + wholesale + ' each');
    }
        
    if($('variation>isavailable',response).text() == 1)
    show_in_stock();
    else
    show_no_stock();
    
    if($('variation>back_order_text',response).text())
    {
    	$('#no_stock_message').text($('variation>back_order_text',response).text());
    	$('#no_stock_message').css('color','darkred');
    	$('#no_stock_message').css('font-weight','bold');
    	$('#no_stock_message').show();
    }
}

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


//called when some click on swatch image
function swapthumb(a_el)
{
	var src = $(a_el).attr('href');            
            
    //$('#main_image img').attr( 'src', src );       
    //$('#main_image a').attr( 'href', src ); 
	
			var number = $(a_el).find("img").attr('id');
			var filename = number.replace(/.jpg/i,'');
			var prod_id = $('#product_id').val();		
			var string = prod_id + ',' + filename;
			
			var url = "/json/index/get_image_src/"+string;
			$.getJSON(url,function(data){
				if(data.src_main)
				{
					$('#main_image img').attr( 'src', data.src_main );
					$('#main_image a').attr( 'href', data.src_big );
					$('#enlarge_link').attr( 'href', data.src_big );
				}
				});
	return false;
}
