// JavaScript Document
$(document).ready(function(){
	$('#car-result-list .comparitorLabel').click(function(e){
		var target = e.target;
		var $me = null;
		var $label = $(this);
		var dataObj = { ajax:null, car_id:null };
		if(target.tagName == 'INPUT')
		{
			$me = $(target);
		}
		else if(target.tagName == 'A')
		{
			return true;
		}
		else
		{
			$me = $('input', $(this));
			if($me.is(':checked'))
			{
				$me.attr('checked','');
			}
			else
			{
				$me.attr('checked','checked');
			}
		}
		
		if($me.is(':checked'))
		{
			dataObj.ajax = 'add_compare';
		}
		else
		{
			dataObj.ajax = 'remove_compare';
			$('.selectOneMore, .clickToCompare', $label).hide();
		}
		dataObj.car_id = $me.attr('value');
		
		$.ajax({
			data: dataObj,
			url: 'inventory.php',
			dataType: 'json',
			success: function(json) {
				$('#comparitorCount').html(json.count);
				$('#comparitorCount-car').html('car' + (json.count != 1 ? 's' : ''));
				
				if(json.count == 1)
				{
					if(dataObj.ajax == 'add_compare')
					{
						$('.clickToCompare:visible').hide();
						$('.selectOneMore:hidden', $label).show();
					}
					else
					{
						$('.comparitorLabel .comparitor:checked').each(function(){
							var $p = $(this).parents('.comparitorLabel');
							$('.clickToCompare:visible', $p).hide();
							$('.selectOneMore:hidden', $p).show();
						});
					}
				}
				else
				{
					$('.comparitorLabel .comparitor:checked').each(function(){
						var $p = $(this).parents('.comparitorLabel');
						$('.clickToCompare:hidden', $p).show();
						$('.selectOneMore:visible', $p).hide();
					});
				}
				
				if(json.count > 0)
				{
					$('#comparitor-deselect:hidden').show();
					$('#comparitor-deselect-span:visible').hide();
					$('#comparitorTop-container .comparitor-seperator:first').removeClass('hidden');
				}
				else
				{
					$('#comparitor-deselect:visible').hide();
					$('#comparitor-deselect-span:hidden').show();
					$('#comparitorTop-container .comparitor-seperator:first').addClass('hidden');
				}
				
				if(json.count > 1)
				{
					$('#comparitor-link:hidden').show();
					$('#comparitor-link-span:visible').hide();
					$('#comparitorTop-container .comparitor-seperator:last').removeClass('hidden');
				}
				else
				{
					$('#comparitor-link:visible').hide();
					$('#comparitor-link-span:hidden').show();
					$('#comparitorTop-container .comparitor-seperator:last').addClass('hidden');
				}
			},
			error: function(e1,e2,e3) { return; }
		});
	});
	
	$('#comparitor-deselect').click(function(){
		$.ajax({
			data: { ajax:'remove_compare_all' },
			url: 'inventory.php',
			dataType: 'json',
			success: function(json) {
				$('#comparitorCount').html('0');
				$('#comparitorCount-car').html('cars');
				$('#comparitor-deselect:visible, #comparitor-link:visible').hide();
				$('#comparitor-deselect-span:hidden, #comparitor-link-span:hidden').show();
				$('#comparitorTop-container .comparitor-seperator').addClass('hidden');
				$('#car-result-list .comparitorLabel :checked').attr('checked','');
			},
			error: function(e1,e2,e3) { return; }
		});
		return false;
	});

	$('#details-tabContainer .tabs .tab').click(
		function()
		{
			var rel = $(this).attr('rel');
			
			$('#details-tabContainer .tabs .current, #details-tabContainer .tab-content .current').removeClass('current');
			$(this).addClass('current');
			$('#'+rel).addClass('current');

			$(this).get(0).blur();	//blur element to prevent dotted outline from remaining on tab
			
			return false;
		}
	);
});

