// ==ClosureCompiler==
// @output_file_name actions.min.js
// @compilation_level SIMPLE_OPTIMIZATIONS
// ==/ClosureCompiler==

/* Cars Global site actions

VERSION: 2011-07-07

*/

// 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
	$("#socialLinks a[href*='facebook']").bind('click', function(){
		doLogging('/cars/Social_Media/Facebook/');
	});
	$("#socialLinks a[href*='twitter']").bind('click', function(){
		doLogging('/cars/Social_Media/Twitter/');
	});
	$("#socialLinks a[href*='youtube']").bind('click', function(){
		doLogging('/cars/Social_Media/Youtube/');
	});
	
	$(".visit a[href*='facebook']").bind('click', function(){
		doLogging('/cars/Social_Media/Facebook/');
	});
	$(".visit a[href*='twitter']").bind('click', function(){
		doLogging('/cars/Social_Media/Twitter/');
	});
	
	//live chat
	// If not on a showroom (dealer) site/domain there will be a 'showroom' object
	if (typeof showroom === 'undefined' && navigator.userAgent.toLowerCase().indexOf('android') < 0) {
		$('body').append("<div id='liveChat'><a href='#' style='top: 40%; right: 0pt; z-index: 50000; position: fixed;'><img width='29' height='190' border='0' alt='Honda Live Chat' src='http://livechat.honda-uk.sophus3.com/image.php?id=05&amp;type=inlay'></a></div>")
		$('#liveChat a').click(function(e){
			e.preventDefault();
			window.open('http://livechat.honda-uk.sophus3.com/chat.php','','width=500,height=550,left=0,top=0,resizable=yes,menubar=no,location=no,status=yes,scrollbars=yes');
		});
		
		$('#liveChat a').hover(
		  function () {
		    $("img",this).attr("src","http://livechat.honda-uk.sophus3.com/image.php?id=08&amp;type=inlay");
		  }, 
		  function () {
		    $("img",this).attr("src","http://livechat.honda-uk.sophus3.com/image.php?id=05&amp;type=inlay");
		  }
		);
	}
	
});


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);
				};
			});
	});


	
	// If on a showroom (dealer) site/domain there will be a 'showroom' object
	// When this is true, do not allow the toolbox to bind, instead we leave it to 
	// .htaccess to redirect to the showroom contact us page.
	if (typeof showroom === 'undefined') {
		// Load the honda toolbox JS if not loaded already.
		if (window.hondaToolboxFromID) {
			$(".hijaxTool").livequery(function() {
				$(this).hondaToolbox();
			});
		} else {
			$.ajax({url:"/_assets/behaviour/jquery.hondatoolbox-dev.js", type:"GET", dataType:"script", cache:true, success:function(){
				$(".hijaxTool").livequery(function() {
					$(this).hondaToolbox();
				});
			}});
		}
	}
}


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
		}
	}
	
}

