/// <summary>
/// Handle vehicle details tab selection
/// </summary>
$(document).ready(function() {
    //Toggle the over / out state of the top level flyout items.
    $("div.displayStackMenuContainer > div.displayStackMenuButton > h2 > a").click(
		function () {
			switchTabState($(this), "vehicleDetails");
			return false;
		}
    );
    
    //bind the CTA buttons
    BindCta($('div.vehicleViewPanel'));
    
    BindForms($('div.vehicleViewPanel'));
}); 


//function used to bind the form links within a given container
function BindForms(container) {	
	//bind the send to a friend link
	container.find('a.form-click-staf').each(function() {
		var link = $(this);
		
		link.click(function(event) {
			event.preventDefault();
			
			//build the url for this lightbox form
			var path = window.location.href.substring(0,window.location.href.indexOf('.co.uk')+6);
			var params = link.attr('name');
			var url = CONST_FORM_STAF_REPLACE;
			url = url.replace(/{PATH}/,path);
			url = url.replace(/{ITM}/,getCtaParam(params,'itm'));
			url = url.replace(/{VOI}/,getCtaParam(params,'voi'));
			url = url.replace(/{ARGS}/,getCtaParam(params,'args'));
			
			//replace the href to with the correct link
			link.attr('href',url);
			
			//run the lightbox functionality to show the form
			var t = this.title || this.name || null;
			var a = this.href || this.alt;
			var g = this.rel || false;
			tb_show(t,a,g);
			this.blur();
			return false;
		});
	});
	
	//bind the vehicle enquiry links
	container.find('a.form-click-ve').each(function() {
		var link = $(this);
		
		link.click(function(event) {
			event.preventDefault();
			
			//build the url for this lightbox form
			var path = window.location.href.substring(0,window.location.href.indexOf('.co.uk')+6);
			var params = link.attr('name');
			var url = CONST_FORM_VE_REPLACE;
			url = url.replace(/{PATH}/,path);
			url = url.replace(/{FRN}/,getCtaParam(params,'frn'));
			url = url.replace(/{VOI}/,getCtaParam(params,'voi'));
			url = url.replace(/{LOC}/,getCtaParam(params,'loc'));

			//replace the href to with the correct link
			link.attr('href',url);
			
			//run the lightbox functionality to show the form
			var t = this.title || this.name || null;
			var a = this.href || this.alt;
			var g = this.rel || false;
			tb_show(t,a,g);
			this.blur();
			return false;
		});
	});
	
		//bind the vehicle enquiry links
	container.find('a.form-click-vt').each(function() {
		var link = $(this);
		
		link.click(function(event) {
			event.preventDefault();
			
			//build the url for this lightbox form
			var path = window.location.href.substring(0,window.location.href.indexOf('.co.uk')+6);
			var params = link.attr('name');
			var url = CONST_FORM_VT_REPLACE;
			url = url.replace(/{PATH}/,path);
			url = url.replace(/{FRN}/,getCtaParam(params,'frn'));
			url = url.replace(/{LCN}/,getCtaParam(params,'lcn'));
			url = url.replace(/{LOC}/,getCtaParam(params,'loc'));
			url = url.replace(/{VOI}/,getCtaParam(params,'voi'));
			url = url.replace(/{CTEXT}/,getCtaParam(params,'ctext'));

			//replace the href to with the correct link
			link.attr('href',url);
			
			//run the lightbox functionality to show the form
			var t = this.title || this.name || null;
			var a = this.href || this.alt;
			var g = this.rel || false;
			tb_show(t,a,g);
			this.blur();
			return false;
		});
	});
	
	
    //bind the what car road test link
	container.find('a.form-click-wcrt').each(function() {
		var link = $(this);
		
		link.click(function(event) {
			event.preventDefault();
			
			//build the url for this lightbox form
			var path = window.location.href.substring(0,window.location.href.indexOf('.co.uk')+6);
			var params = link.attr('name');
			var url = CONST_FORM_WCRT_REPLACE;
			url = url.replace(/{PATH}/,path);
			url = url.replace(/{RTID}/,getCtaParam(params,'rtid'));
			url = url.replace(/{EDID}/,getCtaParam(params,'edid'));
			url = url.replace(/{DRTV}/,getCtaParam(params,'drtv'));

			//replace the href to with the correct link
			link.attr('href',url);
			
			//run the lightbox functionality to show the form
			var t = this.title || this.name || null;
			var a = this.href || this.alt;
			var g = this.rel || false;
			tb_show(t,a,g);
			this.blur();
			return false;
		});
	});	

}


//<%# Site.RootPath %>/forms/vehicle-enquiry.aspx?frn=<%# FranchiseSiteId %>&amp;loc=<%# LocationSiteId %>&amp;voi=<%# Search.CurrentVehicle.GetData("Vehicle", "LongTitle") %>-<%# Search.CurrentVehicle.GetData("Vehicle", "Registration") %>&amp;KeepThis=true&amp;TB_iframe=true&amp;height=500&amp;width=620
