$(document).ready(function(){
	
	
    $("#enquiryform").attr("action","http://www.phase.com.au/forms/afp_spam.asp?"+$("#enquiryform").attr("alt"));
	 $("#careers_form").attr("action","http://www.phase.com.au/forms/afp_spam.asp?"+$("#careers_form").attr("alt"));
	
	//alert($("#enquiryform").attr("action"));
	showHideSub("#virtual","#subnav-virtual")
	showHideSub("#finance","#subnav-finance")
	showHideSub("#fleet","#subnav-fleet")
	showHideSub("#parts","#subnav-parts")
	showHideSub("#service","#subnav-service")
	showHideSub("#contact","#subnav-contact")
	showHideSub("#used","#subnav-used")
	showHideSub("#newdemo","#subnav-newdemo")
	showHideSub("span.spTerms","p.spTerms")
	var active=false;
	
	
	$(".datePicker").click(function(){
		$(this).datepicker({showOn:'focus',dateFormat: 'dd/mm/yy', minDate: 0, beforeShowDay: excludeSunday}).focus();
	})
	
	
	$(".subnav > ul li a").hover(function(){
		 if($(this).hasClass("active"))
		  active=true;
		  if(active==false)
		   $(this).css("color","#fff");
		  
		$(this).oneTime(300,function(){
			
			var thisId = $(this).attr('id')
			var nextDiv = "#preview-"+thisId
			vis(".preview",true)
			vis(nextDiv)
			subnavLoad($(this),nextDiv)
		})
	},function(){
		if(active==false)
		   {
			   $(this).css("color","#000");
			   $(this).removeClass("active");
		   }
		 active=false;
		$(this).stopTime()})
	
	$(".scroll-content-item.specials").click(function(){
		// Hide all specials
		vis(".speDiv",true)
		vis("p.spTerms",true)
		$(".scroll-content-item.specials").removeClass('active')
		$(this).addClass('active')
		vis("#special-"+($(this).index()+1))
	})
	
	vis(".vec-features:first")
	
	$("#new-vehicle-nav a").each(function(){
			var linkAttr = $(this).attr("href")
			$(this).attr("name",linkAttr)
			$(this).removeAttr("href")
	})
	
	//Show/hide Features
	$("#new-vehicle-nav a:not(#build)").click(function(){
		//vis(".vehicleRange",true)
		$(".vehicleRange").css({position:'absolute',visibility:'hidden'})
		var href = $(this).attr('name')
		$("#new-vehicle-nav a").each(function(){
			var hrefLi = $(this).attr('name')
			vis(hrefLi,true)
			$(this).removeClass('active')
		})
		vis(href)
		if($(this).attr('id') == 'nav-range') {
			$(".vehicleRange").css({position:'relative',visibility:'visible'})
		}
		$(this).addClass('active')
		
	})
	
	$("#build").click(function(){
           window.location=$(this).attr("name");
		});
	
	//Show different colours
	$("#new-colours li").click(function(){
		var yPos = $(this).attr('id')
		var imgSrc = $(this).children('input').val()
		var tmpVal = parseInt($("#new-colours ul").css('padding-left'))
		var arrowPos = ((yPos*-1)/179)*34+(tmpVal+14)
		$("#arrowPos").animate({left :arrowPos+"px"})
		$("#new-colours ul li span").removeClass('active')
		$(this).children('span').addClass('active')
		var thisContent = $(this).children('span').html()
		$("#colourDesc").html(thisContent)
		var thisTitle = $(this).attr('title');
		$("#new-colours p").html(thisTitle);
		loadColImg(imgSrc)
	});
	
	if($("#new-colours").length) loadColImg($("#new-colours li:first input").val())
	
	$("#colourDesc").html($("#new-colours ul li:first span").html())
	
	// Hide/show right hand side accordion
	$("#content-right h4").click(function(){
		//vis($("#content-right h4").next('div'),true)
		$("#content-right h4").next('div')
			.removeClass('boxVisible')
			.addClass('boxHidden')
		
		$("#content-right h4").removeClass('active')
		$(this).addClass('active')
		
		$(this).next('div')
			.removeClass('boxHidden')
			.addClass('boxVisible')
	})
	
	
	thisUl = ''
	thisUlHeight = ''
	diffH = ''
	thisUlTop = ''
	
	/*$(".scroll-content-item").hover(function(){
		thisUl = $(this).children("div").children("ul")
		thisUlHeight = parseInt(thisUl.css("height"))
		thisUlTop = parseInt(thisUl.css("top"))
		diffH = thisUlHeight - parseInt($(this).css("height"))
		if(thisUlTop < diffH) {
			thisUl.animate({top:'-'+(diffH)+'px'},(diffH*60),"linear")
		}
	},function(){
		if(thisUlTop*-1 < diffH) thisUl.stop()
		else thisUl.css({top:'0px'})
		$("#tmpStat").html(thisUlTop*-1 + ' / ' + diffH)
	})*/
	
	$(".scroll-content-item").hover(function(){
		//$(this).children(".mRangeSpecs ul").css({position:'relative',background:'#FFF'})
		$(this).children(".mRangeSpecs").css({overflowY:'scroll'})
	},function(){
		//$(this).children(".mRangeSpecs ul").css({position:'relative',background:'#FFF'})
		$(this).children(".mRangeSpecs").css({overflowY:'hidden'})
		})
	
	
	// Hide/show shortcuts content
	$("#shortcuts h4").click(function(){
		if($(this).hasClass('active')) {
			$(this).removeClass('active')
			vis("#shortcuts-content",true)
			vis("#shortcuts p, #shortcuts ul",true)
		}
		else {
			$(this).addClass('active')
			vis("#shortcuts-content")
			vis("#shortcuts p, #shortcuts ul")
		}
	})
	
	// Shortcuts right hand side menu
	$("#sc-right ul li a").click(function(){
		$("#sc-right ul li a").removeClass('active')
		$(this).addClass("active")
		var thisIndex = $(this).parent().index()
		vis("#sc-right div",true)
		vis("#sc-right > div:nth-child("+(thisIndex+1)+")")
	})
	
	$("#home_budget .Term_box_trigger").mouseover(function(){
		$("#home_budget .Term_box").toggle("slow");
	});
	$("#home_budget .Term_box_trigger").mouseout(function(){
		$("#home_budget .Term_box").toggle("slow");
	});
	
	
   $("#home_repayment .Term_box_trigger").mouseover(function(){
		$("#home_repayment .Term_box").toggle("slow");
	});
	$("#home_repayment .Term_box_trigger").mouseout(function(){
		$("#home_repayment .Term_box").toggle("slow");
	});
	
	
})


function vis(element,hide) {
	if(!hide) var hide = false
	var change = hide ? 'none' : 'block'
	$(element).css('display',change)
}

function showHideSub(trigger,element) {
	if(!$(element).hasClass('active')) {
		$(element).css('z-index',150);
		var both = trigger+','+element
		$(both).hover(function(){
			$(trigger).stopTime()
			$(trigger).addClass("hover")
			$(element).css('display','block')
		},function(){
			$(trigger)
				.oneTime(250,function(){$(element).fadeOut(250,function(){$(trigger).removeClass("hover")})})
		})
	}
	else $(element).css('z-index',140);
}

function subnavLoad(trigger,destination) {
	//$("#subnav-virtual a").removeClass('active')
	trigger.addClass('active')
	//var modName = trigger.html()
	var modName = trigger.attr('rel')
	$(destination).html('<img src="images/waitstock.gif" width="169" height="100" />')
	$(destination).load("subnav-ajax.php?model="+escape(modName))
 }
 
function loadColImg(imgSrc) {
	$("#new-colours-switch div").prepend('<div id="ncsCache"></div>')
	$("#new-colours-switch div img")
		.attr('src', imgSrc)
		.one("load",function(){
			var me = $(this)
				$("#ncsCache").fadeOut(function(){
					$("#new-colours-switch div").html(me)
				})
		})
		// In case IE ignores load event, force image to display after 2 secs
		.oneTime(2000,function(){
			if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) 
			$(this).trigger("load");
		})
}

// Remove sunday from datepicker
function excludeSunday(date) {
    var day = date.getDay();
    return [(day != 0), ''];
}
