$(document).ready(function(){
	
	$("#enquiryform").attr("action","http://www.phase.com.au/forms/afp_spam.asp?formid="+$("#enquiryform").attr("alt"));
	
	
	
	//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 h3').mouseover(function(){

		$('#home-search h3').removeClass('active');
		$(this).addClass('active');
		$('#home-search-cars, #home-search-budget').hide();
		thisDiv = $(this).attr('id');
		thisDiv = '#' + thisDiv.replace('-link', '');
		$(thisDiv).show();
	});
	//Hide/show div depending on url (new vehicles page)
	var URL = unescape(window.document.location);
	if (URL.indexOf("#") > -1) {
		var splitUrl = URL.split("#");
		var visDiv = splitUrl[1];
		toBeHidden = hiddenDivs(visDiv);
		if(visDiv == 'ford' || visDiv == 'fpv') {
			for(i in toBeHidden) {
				vis("#div-"+toBeHidden[i],true);
			}
			vis("#div-"+visDiv);
			changeh2(visDiv,toBeHidden);
		}
	}
	
	//New Vehicles Landing page
	function hiddenDivs(id) {
		var toBeHidden = Array('ford','fpv');
		for(i in toBeHidden) {
			if(toBeHidden[i] == id) {
				toBeHidden.splice(i,1);
				break;
			}
		}
		return toBeHidden;
	}
	
	$("#h2-ford, #h2-fpv").click(function(){
		var id = $(this).attr('id').replace('h2-','');
		toBeHidden = hiddenDivs(id);
		for(i in toBeHidden) {
			vis("#div-"+toBeHidden[i],true);
		}
		vis("#div-"+id);
		$("#h2-ford, #h2-fpv").attr('class','inactive');
		$(this).attr('class','active');
		Cufon.replace('#h2-ford, #h2-fpv');
	});
	
	$("#h2-ford, #h2-fpv").hover(function(){
		$(this).addClass('activestate');
		Cufon.replace('#h2-ford, #h2-fpv');
	},function(){
		$(this).removeClass('activestate');
		Cufon.replace('#h2-ford, #h2-fpv');
		}
	);
	
	// 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);
}

	// Search slidedown
	$('#nav-search-button').click(function() {
		$('#nav-search-content').slideToggle(600);
		return false;
	});
	$('#nav-search-regular, #nav-search-budget').mouseover(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;
	});

		
