$(document).ready(function(){
	// Put your headers that need to be converted to Cufon here
	 Cufon.replace('#anav li a, #header h1, #header span, #breadcrumbs, button, .btn-calculate, #new-vehicle-nav ul li, a#cars-action-search-sale, a#cars-action-search-budget, form.body label, .submit, #cars-actions li a, .inv-col-head, .ui-tabs-nav, #news-subscribe h4', { fontFamily: 'Chica Mono', hover: true });
	 Cufon.replace('#header h1, #header p, #header span', { fontFamily: 'SerpentineDBol' });
	 Cufon.replace('ul.rounded-white li a, #new-vehicle h1, #hp-h2', { fontFamily: 'cibreo' });
	 
	 
	$("#enquiryform").attr("action","http://www.phase.com.au/forms/afp_spam.asp?formid="+$("#enquiryform").attr("alt"));
	 $("#subscribe_form").attr("action","http://www.phase.com.au/forms/afp_spam.asp?formid="+$("#subscribe_form").attr("alt"));
   // alert($("#enquiryform").attr("action"));
	 //alert($("#contact-details").scrollTop())
	
	
	 // Make the left nav tool bar follow the user
/*	$(window).scroll(function() {
		var topY = $(this).scrollTop()
		var offsetNews = $("#news-bottom").offset().top
		var offsetNewsTop = $(".news").offset().top
	 	var offsetFooter = $("#footer").offset().top
		
		if(topY >= 185 && offsetNews <= (offsetFooter-20)) $(".news").css({position:'fixed',top:'0'})
		else {
			if(offsetNewsTop < 185) offsetNewsTop = 185
			$(".news").css({position:'absolute',top:offsetNewsTop+'px'})
		}
	})*/
	 
	 $("#nse").focus(function(){if($(this).val() == 'Enter your email') $(this).val('')})
	 
	 //reset links to preserve accessibility without making the screen "jump"
	$("#new-vehicle-hero-right li a").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")
	})
	
	vis(".vec-features:first")
	
	$("#new-vehicle-models h3").click(function(){
		if(!$(this).hasClass('active')) {
			$(this).addClass('active')
			vis($(this).next())
		}
		else {
			$(this).removeClass('active')
			vis($(this).next(),true)
		}
	})
	
	
	//Show/hide Features
	$("#new-vehicle-nav a").click(function(){
		var href = $(this).attr('name')
		$("#new-vehicle-nav a").each(function(){
			var hrefLi = $(this).attr('name')
			vis(hrefLi,true)
			$(this).parent().removeClass('active')
		})
		vis(href)
		$(this).parent().addClass('active')
		if($(this).attr("id")=="build-my-holden")
           window.location=$(this).attr("name");

	})
	
	//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');
		$("#new-colours ul li span").removeClass('active')
		$(this).children('span').addClass('active')
		var thisContent = $(this).children('span').html()
		$("#colourDesc").html(thisContent)
		var thisTitle = $.data(this, 'title');
		$("#new-colours p").html(thisTitle);
		var thisHeight = (parseInt($("#new-colours-switch div img").css('height')) * $(this).index() - 30) * -1
		$("#new-colours-switch div").animate({top :thisHeight+"px"},function(){
			//$("#new-colours p").css("visibility","visible");
			//$("#new-colours-switch-cache").fadeOut('fast',function(){$(this).remove();});
		});
	});
	
	$("#colourDesc").html($("#new-colours ul li:first span").html())
	
	// anav submenus
	showHideSub("#anav-holden","#subnav-holden")
	showHideSub("#anav-hsv","#subnav-hsv")
	showHideSub("#anav-offers","#subnav-offers")
	showHideSub("#anav-service","#subnav-service")
	showHideSub("#anav-cars","#subnav-cars")
	showHideSub("#anav-contact","#subnav-contact")
	showHideSub("#anav-finance","#subnav-finance")
	showHideSub("#anav-fleet","#subnav-fleet")
	showHideSub("#anav-parts","#subnav-parts")
	/*$("#anav li a").each(function(){
								  //alert('yes')
		//alert($(this).next('ul').attr('id'))
		showHideSub($(this),$(this).next('ul'))
	})*/
	
	$("#specials_left li").click(function(){
		$("#specials_left li").each(function(){
			var thisH5 = $(this).children('h5')
			thisH5.removeClass("active")
		})
		var thisH5 = $(this).children('h5')
		thisH5.addClass("active")
	})
	

	 $("#financial_title").click(function(){
		  $("#financial_div").slideToggle("slow");
	  });
	  
	  $(".hide_this").click(function(){
		 $("#financial_div").slideToggle("slow");
	  });

	$("h3.expandable:visible:even").addClass('even')
	 
	// Hide / show left column sections on model details page
	$("h3.expandable").live('click',function(){
		var toShow = $(this).hasClass('active') ? false : true
		if(toShow) { 
			$("h3.expandable").each(function(){hideExp($(this))})
			showExp($(this))
		}
		else hideExp($(this))
	})
})


function showExp(trigger) {
			var thisDiv = trigger.next("div")
			trigger.addClass('active')
			thisDiv
				.addClass('active')
				.prepend('<img src="images/arrow.png" alt="" id="expArrow" />')
		}
		
		function hideExp(trigger) {
			var thisDiv = trigger.next("div")
			trigger.removeClass('active')
			thisDiv
				.removeClass('active')
				.children("#expArrow").remove()
		}


function vis(element,hide) {
	if(!hide) var hide = false
	var change = hide ? 'none' : 'block'
	$(element).css('display',change)
}

function showHideSub(trigger,element) {
	var both = trigger+','+element
	$(both).hover(function(){
		$(trigger).stopTime()
		$(element).css('display','block')
	},function(){
		$(trigger).oneTime(250,function(){$(element).fadeOut(250)})
	})
}
