/*
 * CH page Javascript
 */

$(document).ready(function(){
	$('span.ch_coverage').live('click', function(){
		var productId = $(this).parent().parent().attr('productId');
		var firmwareId = $(this).parent().attr('firmwareId');
		if (firmwareId == '') return;		
		if ($(this).text() == _getText('view-coverage'))
		{
			hideCoverage();			
			getCoverage(productId, firmwareId);
			$('tr.ch_products_coverage[productId="'+productId+'"]').show();
			$(this).text('Hide Coverage');
			$(this).addClass('ch_on');
		}
		else
			hideCoverage();
	});
	
	$('span.ch_guide').live('click', function(){
		var productId = $(this).parent().parent().attr('productId');
		var firmwareId = $(this).parent().attr('firmwareId');
		if (firmwareId == '') return;		
		$.ajax(
				{
					url:'/common/file/get-install-guide/',
					type:'GET',
					data :
					{
						firmware: firmwareId
					},
					dataType:'html',
					success:function(data){
						MB_bottom.addContent("",data,function(){});
					},
	                error : function(xhr) {
	                	ErrorMessage(xhr);
					}
				}
			);		

	});	
	
	$('.table_bull').live('click', function() {
		var model = $(this).attr('modelid');
		var product = $('.ch_on').parent().parent().attr('productId');		
		if(model)
		{
			$.ajax(
					{
						url:'/ajax/product/get-vehicle-product-feature/',
						type:'GET',
						data :
						{
							model: model,
							product_id: product
						},
						dataType:'html',
						success:function(data){
							MB_bottom.addContent("",data,function(){});
						},
		                error : function(xhr) {
		                	ErrorMessage(xhr);
						}
					}
				);
		}		
	});
});

function hideCoverage()
{
	if (!$('.ch_on').length) return; 	
	var productId = $('.ch_on').parent().parent().attr('productId');	
	$('tr.ch_products_coverage[productId="'+productId+'"]').hide();
	$('.ch_on').text(_getText('view-coverage'));
	$('.ch_on').removeClass('ch_on');	
}

function getCoverage(productId, firmwareId)
{
	$('tr.ch_products_coverage[productId="'+productId+'"] td.ch_products_column_coverage').html('<div align="center"><img src="/common/images/animation/loader.gif"></div>');
	$.ajax(
			{
				url:'/ajax/firmware/get-firmware-coverage/firmware_id/'+firmwareId,
				type:'GET',
				dataType:'html',
				success:function(data){					
					$('tr.ch_products_coverage[productId="'+productId+'"] td.ch_products_column_coverage').html(data);
					$('td.product_name').parent('tr').remove();
				},
                error : function(xhr) {
                	ErrorMessage(xhr);
				}
			}
		);
}
