var logo = 'ford';

$(document).ready(function(){
						   
						   
	//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);
		}
	}
					
	$("#finance-product h3").click(function(){
		if($(this).attr('class') != 'active') {
			$(this).next('div').slideDown();
			$(this).attr('class','active');
		}
		else {
			$(this).next('div').slideUp();
			$(this).attr('class','');
		}
	});
					
	$("#preferred_booking_date").click(function(){
		$(this).datepicker({showOn:'focus',dateFormat: 'dd/mm/yy', minDate: 0}).focus();
	});
						   
	//reset links to preserve accessibility without making the screen "jump"
	$("#quickQuote,#valueMyCar").each(function(){
		if($(this).attr('id')) {
			var linkAttr = $(this).attr("href");
			$(this).attr("name",linkAttr);
			$(this).removeAttr("href");
		}
	});
	
	$("#new-vehicle-nav a").each(function(){
			var linkAttr = $(this).attr("href");
			$(this).attr("name",linkAttr);
			$(this).removeAttr("href");
	});
	
	//Reveal and adapt form
	$("#nav-brochures,#valueMyCar,#quickQuote,#viewStock,#nav-pricelist").click(function(){
		var attrVal = $(this).attr('id');
		//vis('#new-vehicle-directions',true);
		vis('#flashcontent',true);
		vis('#current-offers',true);
		switch(attrVal) {
			case '':
				break;
			case 'viewStock':
				vis('#currentStock')
				vis('#new-vehicle-form',true);
				break;
			default:	
				vis('.enquiry-trade');
				$("#formComment").html("Comments");
				//vis('.main-form',true);
				var intro = '';
				var url = "http://www.phase.com.au/forms/afp_spam.asp?Details=1&formid=";
				var formTitle = ' <span onclick="vis(\'#new-vehicle-form\',true);vis(\'#flashcontent\');vis(\'#current-offers\');$(\'#new-vehicle-hero embed, object\').css(\'visibility\',\'visible\');"><img src="images/new-close-btn.gif" alt="Hide this form" title="Hide this form" /></span>';
				
				switch(attrVal) {
					case 'nav-brochures':
						url += '1245';
						formTitle = "Accessing Brochures Area"+formTitle;
						$("#formComment").html("Comments");
						intro = 'Thank you for your interest. So that we can monitor the interest in this section of the site, please take a moment to complete the brief form below. You will gain instant access to the brochures area.';
						break;
					case 'nav-pricelist':
						url += '1244';
						formTitle = "Get our Internet Price List"+formTitle;
						$("#formComment").html("Comments");
						intro = 'Thank you in advance for contacting us. Please complete this brief form to receive our internet price list. One of our friendly, professional consultants will be in touch with you shortly.';
						break;
					case 'quickQuote':
					default:
						url += '624';
						//url += '1236';
						formTitle = "Quick Quote"+formTitle;
						$("#formComment").html("Comments");
						intro = 'Thank you in advance for contacting us. Please complete this brief form to make an obligation-free enquiry. One of our friendly, professional consultants will be in touch with you shortly.';
						break;
					case 'valueMyCar':
						url += '1246';
						formTitle = "Value my car"+formTitle;
						$("#formComment").html("Details about Your Car");
						$(".enquiry-trade").css("display","none");
						intro = 'Thank you in advance for contacting us about a valuation on your vehicle.<br>So that we can help you best, please complete this brief form. One of our representatives will be in touch with you shortly. ';
						break;
				}
				vis('#currentStock',true);
				$("#new-vehicle-form h2").html(formTitle);
				$("#enquiry_form").attr("action",url);
				$("#new-vehicle-form p#info").html(intro);
				$("#new-vehicle-hero embed, object").css("visibility","hidden");
				vis('#new-vehicle-form');
		}
		Cufon.replace('#new-vehicle-form h2, #new-vehicle-directions h2');
	});
	
	$("#new-vehicle-directions h2 span").click(function(){
		vis('#new-vehicle-directions',true);
		vis('.main-form');
		$('#new-vehicle-hero embed, object').css('visibility','visible');
	});
	
	$(".main-form h2 span").click(function(){
		vis('.main-form',true);
		$('#new-vehicle-hero embed, object').css('visibility','visible');
	});
	
	$("#new-specs h3").click(function(){
		if(!$(this).children("span").attr('id')) {
			$(this).next("ul").slideDown();
			$(this).children("span").attr('id','active');
		}
		else {
			$(this).next("ul").slideUp();
			$(this).children("span").attr('id','');
		}
	});
	
	//Show/hide Features
	$("#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');
		}
		//alert(navId+'-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();});
		});
	});
	
	//Models drop-down
	$("#new-vehicle-models h3").click(function(){
		if(!$(this).attr('id')) {
			$(this).next("ul").slideDown();
			$(this).attr('id','active');
		}
		else {
			$(this).next("ul").slideUp();
			$(this).attr('id','');
		}
	});
	
	function changeh2(visible,hidden) {
		//$("#h2-"+visible).css('background','#F5F5F5');
		$("#h2-"+visible).attr('class','active');
		$("#h2-"+visible).css('color','#FFF');
		for(i in hidden) {
			//$("#h2-"+hidden[i]).css('background','#E4E4E4');
			$("#h2-"+hidden[i]).attr('class','');
			$("#h2-"+hidden[i]).css('color','#333');
			Cufon.replace("#h2-"+hidden[i]);
		}
		Cufon.replace("#h2-"+visible);
	}
	
	function hiddenDivs(id) {
		var toBeHidden = Array('ford','fpv','landrover');
		for(i in toBeHidden) {
			if(toBeHidden[i] == id) {
				toBeHidden.splice(i,1);
				break;
			}
		}
		return toBeHidden;
	}
	
	$("#new h2").click(function(){
		var id = $(this).attr('id').replace('h2-','');
		toBeHidden = hiddenDivs(id);
		for(i in toBeHidden) {
			vis("#div-"+toBeHidden[i],true);
		}
		vis("#div-"+id);
		changeh2(id,toBeHidden);
	});
	
	$("#new h2").hover(function(){
		$(this).css('color','#FFF');					
		Cufon.replace($(this));
	},function(){
		if($(this).attr('class') != 'active') {
			$(this).css('color','#333');
			Cufon.replace($(this));
		}
	});
	
});


function switchLogo(switchTo) {
	$("#logo").oneTime(3000,function(){
		logo = switchTo;
		newLogo = logo == 'ford' ? 'fpv' : 'ford';
		$(this).fadeOut('normal',function(){$(this).attr('src','images/header-logo-'+logo+'.jpg');$(this).fadeIn('normal',function(){switchLogo(newLogo);})});
	})
}

//Animates menu icons on hover
function animImg(element,leftX) {
	$(element).hover(function(){
		$(this).stopTime();
		$(this).prev("img").animate({left:[leftX+10]+"px"},200);
	},function(){
		$(this).oneTime(300,function(){
			$(this).prev("img").animate({left:leftX+"px"},200);
		});
	});
}

function vis(element,hide) {
	if(!hide) var hide = false;
	var change = hide ? 'none' : 'block';
	$(element).css('display',change);
}