$(document).ready(function() {

	//Store principal color
	var princColor = $('.mainColor').css("color");

	//Scroll up and down the competitor menu ----------------------------------------------------
	$("#flecheHaut").hover(function(){
		$("#imagesCompetitors").stop().animate({top:"0px"},{queue:false,duration:200});
		$("#flecheHaut img").attr("src","/images/bg/flecheHaut_faded.png");
		$("#flecheBas img").attr("src","/images/bg/flecheBas.png");
	});
	
	$("#flecheBas").hover(function(){
		$("#imagesCompetitors").stop().animate({top:"-120px"},{queue:false,duration:200});
		$("#flecheHaut img").attr("src","/images/bg/flecheHaut.png");
		$("#flecheBas img").attr("src","/images/bg/flecheBas_faded.png");
	});
	
	//Set opacity for the competitor menu -------------------------------------------------------
	$(".imgCompetitor").fadeTo(0,0.5);
	$("#imagesCompetitors .first").fadeTo(0,1);
	
	$("#arrowTop").attr("src","/images/bg/flecheHaut_faded.png");
	
	
	//Set opacity on hover in competitor menu

	
	$(".imgCompetitor").hover(function(){
		if($("#infoBtn a").attr("href") != "/en/car-specs/?id="+ $(this).children(".id").html()){
			$(this).stop().fadeTo(200,0.7);
		}
	}, function() {
		if($("#infoBtn a").attr("href") == "/en/car-specs/?id="+ $(this).children(".id").html()){
			$(this).stop().fadeTo(200,1);
		}else{
			$(this).stop().fadeTo(200,0.5);
		}
	});
	

	
	
	//Show car infos in competitor box-----------------------------------------------------------
	$(".imgCompetitor").click(function(){
		if($("#infoBtn a").attr("href") != "/en/car-specs/?id="+ $(this).children(".id").html()){
			
			$(".imgCompetitor").fadeTo(250,0.5);
			$(this).fadeTo(250,1);
			
			$(".fade").fadeOut(0);
			
			$("#activeCar").attr("src","/images/cars" + $(this).children(".image").html());
			$("#carInfos h2 .make").html($(this).children(".make").html());
			$("#carInfos h2 .name").html($(this).children(".name").html());
			$("#carInfos h2 .trim").html($(this).children(".trim").html());
			$("#carInfos .flag").attr("src","/images/flags/" + $(this).children(".country").html() + "/small.jpg");
			$("#carInfos .type").html($(this).children(".type").html());
			$("#carInfos .engine").html($(this).children(".engine").html());
			$("#carInfos .powertrain").html($(this).children(".powertrain").html());
			$("#infoBtn a").attr("href","/en/car-specs/?id="+ $(this).children(".id").html()  );
			
			$(".fade").fadeIn(400);
		}
	});
	
	
	
	//Manage de popular cars box-----------------------------------------------------------------
	$(".popularCar").hover(function(){
		$(this).find(".popularSlider").stop().animate({borderColor: princColor});
		$(this).find(".popularSlider").animate({width: "150px"},{queue:false,duration:200});
		$(this).find(".popularSlider .out").fadeOut(200, function(){
			$(this).parent().find(".hover").fadeIn(200);
		}
		);
		$(this).find('.imgPop').stop().fadeTo(200,0.7);
		
	}, function() {
		$(this).find('.imgPop').stop().fadeTo(200,1);
		
		$(this).find(".popularSlider .hover").fadeOut(200, function(){
			$(this).parent().find(".out").fadeIn(200);
			$(this).parent().animate({borderColor: '#7A7A7A'});
			$(this).parent().animate({width: "85px"},{queue:false,duration:200});
		}
		);
		
		
		
	});
	
	
	//Manage car selection in competitor box------------------------------------------------------
	$(".competitor-check").change(function(){
		var c = this.checked ? '#eee' : '#fff';
		$(this).parent().parent().animate({backgroundColor: c});
	});
	
	
	//Manage car selection in competitor box------------------------------------------------------
	$("#search-grid tr").click(function(){
		id = $(this).children("td:first-child").html();
		$(location).attr('href',"/en/car-specs/?id="+id);
	});
	
	
	
	
	
	
	
	
	
	
	
	
	// Add Car section ---------------------------------------------------------------------------------
	//
	// -------------------------------------------------------------------------------------------------
	
	//Manage add car boxes
	if($("#makeActual").val()== 'new'){
		$("#makeSection").css('display','block');
	}
	if($("#modelActual").val()== 'new'){
		$("#modelSection").css('display','block');
	}
	$("#makeActual").change(function(){
		if($(this).val()== 'new'){
			$("#makeSection").css('display','block');
		}else{
			$("#makeSection").css('display','none');
		}
	});
	$("#modelActual").change(function(){
		if($(this).val()== 'new'){
			$("#modelSection").css('display','block');
		}else{
			$("#modelSection").css('display','none');
		}
	});
	
	
	
	
	
	
});

/*

$("#flecheHaut").hover(function(){
		$("#imagesCompetitors").stop().animate({top:"-130px"},{queue:false,duration:200});
	}, function() {
		$("#imagesCompetitors").stop().animate({top:"0px"},{queue:false,duration:200});
	});

*/

