$(document).ready(function() {
	
	$("header nav a").click(function() {
		$("header nav a.active").removeClass("active");
		$("body").attr("id", "");
		$(this).addClass("active");	
	});
	
	$("a.detailsBtn").click(function() {
		$(this).addClass("active");
	});
	
	$("#pagination a").click(function() {
		$(this).addClass("active");
	});
	
	
	$(".backBtn").click(function() {
		history.go(-1);	
		return false;
	});
	
	
	$("a.printBtn").click(function() {
		window.print();
	});
	
	$("a.emailBtn").click(function() {
	
		$("#contactForm h3").text($(this).attr("data-title"));
		$("#contactForm input[name=emailto]").val($(this).attr("data-emailto"));
		$("#contactForm input[name=vin]").val($(this).attr("data-vin"));
		$("#contactForm input[name=car]").val($(this).attr("data-car"));
	
		$.fancybox({	
				'href'				: "#contactForm",
				'scrolling'			: "no",
				'autoDimensions'	: false,
				'width'				: 350,
				'height'			: 335				
				});	
				
		return false;
	
	});
	
	$("a.submitEmail").live("click", function() {
	
		$.post('/email/' + Number(new Date()),{
		   		content: $("#contactForm form").serialize()
		   		});
		
		$("#contactForm").html("<h3>Thank you for contacting us</h3><p>We will get back to you soon.</p>");
		
		$.fancybox.resize();	
				
		return false;
		
	
	});
		
	$(".shareBtn").click(function() {
		if ($("#shareIcons").hasClass("active")) {
			$(this).removeClass("active");
			$("#shareIcons").removeClass("active");
			$("#shareIcons").fadeOut(250);
		} else {
			$(this).addClass("active");
			$("#shareIcons").addClass("active");
			$("#shareIcons").fadeIn(250);	
		}	
		
		return false;			
	});	
	
	$(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (!$clicked.hasClass("shareBtn")) {
        	$(".shareBtn").removeClass("active");
			$("#shareIcons").removeClass("active");
			$("#shareIcons").fadeOut(250);
        }
    });
	
	
	$(".videoBtn").click(function () {
		$.fancybox(	{	
			'width' 			: parseInt($(this).attr("data-width")),
			'height'			: parseInt($(this).attr("data-height")),
			'padding'			: 0,
			'href'				: "/includes/flash/videoplayer.swf",
			'type'			: 'swf',
			'swf'			: {
			    'wmode'				: 'transparent',
				'allowfullscreen'	: 'true',
				'flashvars'			: 'width='+$(this).attr("data-width")+'&height='+$(this).attr("data-height")+'&autoplay=1&sourceurl='+$(this).attr("rel")
			}								
		});									
		return false;
	});
	
	
	
	if ($('input[name=search]').val() == "") {
		$('input[name=search]').val(searchText);
	}
	
	$('input[name=search]').focus(function() {
		if ($(this).val() == searchText) {
			$(this).attr("value","");
		} 
	});
	$('input[name=search]').blur(function() {
		if($(this).val() == "") {
			$(this).val(searchText);
		}
	});
	

	$(".compare-check").attr("checked", "");

	$(".compare-check").click(function() {
		
		count = $(".compare-check:checked").length;

		if (count > 1) {
			$("#compareBtn").removeClass("disabled").addClass("enabled");	
		} else
		{
			$("#compareBtn").removeClass("enabled").addClass("disabled");	
		}
		
	
	});
	
	$("#compareBtn").click(function() {
		if ($(this).hasClass("disabled")) return false;
		
		$("#compareForm").submit();
		$(".compare-check:checked").attr('checked', false);
		$("#compareBtn").removeClass("enabled").addClass("disabled");
		return false;
	});

	$("a[rel=fancy]").fancybox();


	$("a#fancyinit").click(function() {
		$("a[id="+$(this).attr("rel")+"]").trigger("click");	
		return false;
	});	

	$("#thumbs img").click(function() {		
		$("img.main").attr("src", $(this).attr("id"));	
		$("img.main").parent().attr("rel", $(this).attr("rel"));
	
	});


});



