//Array used to display checkboxes
var interestedIn = Array('Barina','Cruze','Calais','Sportwagon','Captiva','Ute','Epica','Commodore','Statesman','Caprice','Colorado','Combo','Clubsport R8','GTS','Grange','Maloo R8','Senator Signature','Tourer R8');
var interestedLimit = 6;
var interestedInTouch = Array('New Holden','Demonstrator','Pre-owned','Parts','Service','Gifts / Merchandise','Employment','Owners\' Info','Special Offer','Finance','Other');
var interestedInFinance = Array('Consumer Loan','Chattel Mortgage','Hire Purchase','Leasing','Comprehensive Vehicle Insurance','Loan Protection Insurance','Gap Cover','Extended Warranty','Not Sure at This Stage');

$(document).ready(function(){
						   
	//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");
	});
	
	//Reveal and adapt form
	$("#new-vehicle-hero-right li a, .co-enquiry").click(function(){
		var attrVal = $(this).attr('id');
		//vis('.main-form',true);
		switch(attrVal) {
			case 'directions':
				vis('#new-vehicle-form, .main-form',true);
				$("#new-vehicle-hero embed, object").css("visibility","hidden");
				vis('#new-vehicle-directions');
				$("#formComment").html("Comments");
				break;
			case 'finance': case 'service': case 'parts': case 'getintouch':
				vis('#new-vehicle-form, #new-vehicle-directions',true);
				vis('.main-form');
				$("#formComment").html("Comments");
				break;
			case '':
				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?formid=";
				var formTitle = ' <span onclick="vis(\'#new-vehicle-form\',true);vis(\'.main-form\');$(\'#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 'brochures':
						url += '1149';
						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 'enquiry':
					default:
						url += '1148';
						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 'value':
						url += '1150';
						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;
				}
				$("#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', { fontFamily: 'Corbel' });
	});
	
	$("#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');
	});
	
	//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).attr('id','');
		});
		vis(href);
		$(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();});
		});
	});
	
	//Models drop-down
	$("#new-vehicle-models 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','');
		}
	});
	
	//Rounded corners on the models box
	$("#new-vehicle-nav li a:first").corner("top 5px");
	if($.browser.msie) {
		$("#new-vehicle-nav li a:first").css("margin-bottom","-2px");
		//$("#cars-actions a").corner("5px");
	}
	$("#new-vehicle-nav li a:last").corner("bottom 5px");
	
	
	animImg("a#directions",10);
	animImg("a#service",5);
	animImg("a#parts",0);
	animImg("a#getintouch",7);
	animImg("a#finance",7);
	
});

//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);
		});
	});
}

//Generates HTML checkboxes
function generateCheckBoxes(array,name,limit,linebreak) {
	if(!limit) var limit = 4;
	if(!linebreak) var linebreak = false;
	var output = '';
    var cnt = 0;
    for(i in array) {
    	if(cnt == limit) {
       	cnt = 0;
           output += '</td><td>';
       }
       var altVer = array[i].replace(' ','_').toLowerCase();
       output += '<input type="checkbox" name="'+name+'[]" id="'+name+altVer+'" value="'+array[i]+'" /> <label for="'+name+altVer+'">'+array[i]+'</label>';
	   output += linebreak ? '<br />' : '';
       cnt++;
    }
    document.write(output);
}

function vis(element,hide) {
	if(!hide) var hide = false;
	var change = hide ? 'none' : 'block';
	$(element).css('display',change);
}

