// ==ClosureCompiler==
// @output_file_name actions.min.js
// @compilation_level SIMPLE_OPTIMIZATIONS
// ==/ClosureCompiler=

/* Cars Global site actions

VERSION: 2010-04-22

*/

// If livequery not already loaded, include a version compatible with the loaded jQuery version
if (typeof $.fn.livequery != "function") {
	var path = (parseFloat($().jquery) > 1.2) ? "jquery.livequery-1.1.0.min.js" : "jquery.livequery-1.0.3.min.js";
	$.ajax({url: "/_assets/behaviour/"+path, type: "GET", dataType: "script", cache: true, success: function(){
		$(function(){
			liveBindings()
		})
	}})
} else {
	liveBindings()
}


function Memory() {
	this.gradeID		= null;
	this.engineID		= null;
	this.transmissionID	= null;
}

var memory = new Memory();

Memory.prototype.reset = function() {
	memory = new Memory();
}

$(document).ready(function(){

	$('#promo').randomiseBrandStory({
		customClass: 'fixPng'
	})
	
	//Code for Postcode Checker in the product pages
	$("#testDriveForm button").click(
		function(e) {
			var validPostCodeTest =  isValidPostcode( $("#testDriveForm #location").val() );
			if (!validPostCodeTest) {
				e.preventDefault();
				alert("Please enter a valid UK postcode");
			}
			
		}
	);
	
	// Tracking for social media links
	$(".visit a[href*='facebook']").bind('click', function(){
		doLogging('/cars/Social_Media/Facebook/');
	});
	$(".visit a[href*='twitter']").bind('click', function(){
		doLogging('/cars/Social_Media/Twitter/');
	});

});


function liveBindings() {

	$(".bookatestdrive ul.testDriveProductChooser li .carButton").livequery(function(){
		$(this).addClass("hideCarButton");
	});
	
	$(".bookatestdrive ul.testDriveProductChooser li").livequery("click", function() {

		$(this).find("input").attr("checked",true);

				
		$(this).siblings("li").each(function(){
			var currentClass = $(this).find("a").attr("class");
			if (currentClass.substring(0,6) == "chosen") {
				$(this).find("a").removeClass(currentClass); // Remove class
				$(this).find("a").addClass(currentClass.substring(6,currentClass.length));
			}
		});
		var currentClass2 = $(this).find("a").attr("class");
		if (currentClass2.substring(0,6) != "chosen") {
			$(this).find("a").attr("class","chosen"+currentClass2);		
		}
	});


	// swap car colour out when swatch is clicked
	$(".carColours a").livequery("click",function(){
	
		var carPath = $(this).attr("href");
		var carAlt = $(this).attr("title");
		var swatchClicked = $(this).parent("li").attr("id");
		var swatchNumclicked = swatchClicked.replace("swatch", "");
		
		removeClassFromSelector("ul.carColours","sw");
		$("#colourChooser ul").addClass("sw" + swatchNumclicked);
		
		if ($("#mainCar").attr("src") != carPath){
			$("#mainCar").fadeOut("fast", function() {
				$("#mainCar").attr({ src: carPath, alt: carAlt, title: carAlt });
				$("#overviewColours p").html(carAlt); 
				$("#mainCar").fadeIn("fast");
			});
		}
		return false;
	});

	
	/**** Leave until last incase liveQuery not available ****/
	$('.placeSearch').livequery(function() {
		var placeLabel = $(this).find("label.place").remove().text();
		$('.place').addClass('placeholder').val(placeLabel)
			.focus(function() {
				if (this.value == placeLabel) {
					$(this).removeClass('placeholder').val('');
				};
			})
			.blur(function() {
				if (this.value == '') {
					$(this).addClass('placeholder').val(placeLabel);
				};
			});
	});


	// Load the honda toolbox JS if not loaded already.
	if (window.hondaToolboxFromID) {
		$(".hijaxTool").livequery(function() {
			$(this).hondaToolbox();
		});
	} else {
		$.ajax({url:"/_assets/behaviour/jquery.hondatoolbox.js", type:"GET", dataType:"script", cache:true, success:function(){
			$(".hijaxTool").livequery(function() {
				$(this).hondaToolbox();
			});
		}});
	}

}


/*function runDeptGlossary() {
	glossaryLoad("/cars/glossary/_assets/data/glossary-data.js");
}


function panelCallbackPage(state) {
	switch(state) {
		// Called each time the panel transitions have completed
		case "ready":
			runDeptGlossary();
			break;
	}
}*/


function isValidPostcode(p) {
	var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
	return postcodeRegEx.test(p);
}


function removeClassFromSelector(selector, classtoremove) {
	
	// Get all selectors
	var Classes = $(selector).attr("class").split(" ");

	// If classtoremove matches, then remove it.
	for (i=0;i<=Classes.length-1;i++) {
		if (Classes[i].substring(0,classtoremove.length) == classtoremove) {
			$(selector).removeClass(Classes[i]); // Remove class
		}
	}
	
}
