$(document).ready(function(){
	
	//Models drop-down
	$("#new-vehicle-models h3").click(function(){
		if(!$(this).attr('class')) {
			$(this).next("ul").slideDown();
			$(this).attr('class','active');
		}
		else {
			$(this).next("ul").slideUp();
			$(this).attr('class','');
		}
		Cufon.replace("#new-vehicle-models h3");
	});
	
	
	//Homepage Search tabs
	$('#home-search h2').click(function(){
		$('#home-search h2').removeClass('active');
		$(this).addClass('active');
		Cufon.replace("#home-search h2");
		$('#home-search-cars, #home-search-budget').hide();
		thisDiv = $(this).attr('id');
		thisDiv = '#' + thisDiv.replace('-link', '');
		$(thisDiv).show();
	});
	
	// Search slidedown
	$('#nav-search-button').click(function() {
		$('#nav-search-content').slideToggle(600);
		return false;
	});
	$('#nav-search-regular, #nav-search-budget').click(function() {
		$('#nav-search-regular-content, #nav-search-budget-content').css('display','none');
		$('#' + $(this).attr('id') + '-content').css('display','block');
		$('#nav-search-buttons a').removeClass('active');
		$(this).addClass('active');
		return false;
	});
		
	
	//Show/hide Call to action panels	
	$("#new-vehicle-right #calls-action a").each(function(){
			var linkAttr = $(this).attr("href");
			if (linkAttr.indexOf("#") > -1 && linkAttr.indexOf("#") == 0) {
				$(this).attr("name",linkAttr);
				$(this).removeAttr("href");
			}
	});
	$("#new-vehicle-right #calls-action a").click(function(){
		var href = $(this).attr('name');
		if(href != '#' && !$(this).attr('href')) {
			$("#new-vehicle-right #calls-action a").each(function(){
				var hrefLi = $(this).attr('name');
				$(this).attr('class','');
				vis(hrefLi,true);
			});
			vis('#flashcontent',true);
			vis(href,false);
		$(this).attr('class','active');
	   }
	});
	
	
	//Close button on hidden panels
	$('.close-button').click(function(){
		$('.hidden-panel').css('display','none')
		$('#flashcontent').css('display','block')
	});
	

	//Show/hide New Model Features
	$("#new-vehicle-nav a").each(function(){
		var linkAttr = $(this).attr("href");
		$(this).attr("name",linkAttr);
		$(this).removeAttr("href");
	});
	$("#new-vehicle-nav a").click(function(){
		var href = $(this).attr('name');
		if(href != '#') {
			$("#new-vehicle-nav a").each(function(){
				var hrefLi = $(this).attr('name');
				var thisId = $(this).attr('id');
				if(hrefLi != '#') {
					$(this).attr('id',thisId.replace('-active',''));
					vis(hrefLi,true);
				}
			});
			vis(href);
		$(this).attr('id',$(this).attr('id')+'-active');
		}
	});
	
	
	//Set the default colour picker description then deletes all title attributes for the colours list
	var firstTitle = $("#new-colours li:first").attr("title");
	$("#new-colours p").html(firstTitle);
	$("#new-colours li").each(function() {
        $this = $(this);
        $.data(this, 'title', $this.attr('title'));
        $this.removeAttr('title');
    });

	
	//Show different colours
	$("#new-colours li").click(function(){
		$("#new-colours p").css("visibility","hidden");
		$("#new-colours-switch").prepend("<div id='new-colours-switch-cache'></div>");
		$("#new-colours-switch-cache").css('opacity','0.9');
		var yPos = $(this).attr('id');
		var thisTitle = $.data(this, 'title');
		$("#new-colours p").html(thisTitle);
		$("#new-colours-switch img").animate({top :"-"+yPos+"px"},function(){
			$("#new-colours p").css("visibility","visible");
			$("#new-colours-switch-cache").fadeOut('fast',function(){$(this).remove();});
		});
	});
	
});


function vis(element,hide) {
	if(!hide) var hide = false;
	var change = hide ? 'none' : 'block';
	$(element).css('display',change);
}
