// External Links
$(document).ready(function(){
	$("a[rel=external]").click(function(){
		var elink = $(this).attr("href");
		window.open(elink);
		return false;
	});
});


// Do nothing on # links 
$(document).ready(function(){
	$("a[href='#']").click(function(){
		return false;
	});
});

// Navigation
$(document).ready(function(){
	// Roll over Top level
	$("#g-nav > ul > li > a").hover(
		function(){	
			var selectedNavItem = $(this);
			// Resets
			$("#g-nav a").removeClass("active"); // Remove active state from all
			$("#g-nav ul ul, #flash-nav").hide(); // Hide all second and third level (flash) nav
		
			// Show corresponding navs
			selectedNavItem.addClass("active").next().fadeIn(); // Show second level
			if ( selectedNavItem.parent().hasClass("g-newvehicles") )
			{ 
				$("#flash-nav").fadeIn(); // Show third level if in New Vehicles tab
			}
		},
		function(){  }); // Do nothing on mouse out.

		$("#banner, #main").hover(
			function(){
				$("#g-nav ul ul, #flash-nav").fadeOut();
				$("#g-nav > ul > li > a").removeClass("active");
		}, function(){});
});

// Navigation Flash Triggering
$(document).ready(function(){
	var flashNav = document.getElementById("flashnav");

	if(flashNav){
		$("#g-nav ul ul li a").hover(
		function(){
			var section = $(this).parent().attr("class");
			flashNav.moveSection(section);
		},
		function(){ return false; });
	}
});

if(typeof sIFR == "function"){
	sIFR.replaceElement(named({
		sSelector:"h1",
		sFlashSrc:"/img/flash/nobel-light.swf",
		sWmode: "transparent",
		nPaddingBottom: "10",
		sColor: "#595959"
	}));
	sIFR.replaceElement(named({
		sSelector: "#banner h2",
		sFlashSrc:"/img/flash/nobel.swf",
		sWmode: "transparent",
		sCase: "upper",
		sColor: "#FFFFFF"
	}));
	sIFR.replaceElement(named({
		sSelector: "#content #vehicles h2",
		sFlashSrc:"/img/flash/nobel.swf",
		sWmode: "transparent",
		sCase: "upper",
		sColor: "#595959",
		sFlashVars: "textalign=right"
	}));
	sIFR.replaceElement(named({
		sSelector: "#content h2",
		sFlashSrc:"/img/flash/nobel.swf",
		sWmode: "transparent",
		
		sColor: "#595959"
	}));
};


/* Home Promo Sliders */
$(document).ready(function() {
	if( $("body").hasClass("s-home") ) {
		$("#promo1").s3Slider({
		  timeOut: 7000
		});
	}
});


// Model page expanding sections
$(function(){

	$(".collapse-body").hide(); // Collapse sections
	$(".collapse-head")
		.addClass("trigger") // adds the button image. no point having it without the functionality
		.click(function(){
			var item = $(this); // cache for speed
			if(!item.hasClass("open")){ item.addClass("open"); } 
			else { item.removeClass("open"); }
			item.next().slideToggle("fast");
		
		}
	);

});


// Time picker and Date Picker
$(function(){
	if($(".form").length > 0){
	    $(".timepicker").timepickr({
	    	convention:12,
	    	dropslide:{trigger: "focus", left: 0, top: 20}
	    });
	    /*
	    $(".datepicker").datepicker({
			dateFormat: "dd/mm/yy"
	    });
	    */
    }
    
//    if ($("#errorSummary").length > 0) { 
//    	$(".datepicker").click(function(){
//    		setTimeout(function(){$("#ui-datepicker-div").css('top','35px');},3000);
//    	});
//    }
   
});


// Email Live Validation
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
$(function() {
	$(".validate-email").parent().next().find("img").hide();
	$(".validate-email").keyup(function(){
		var email = $(this).val();
		var icon = $(this).parent().next().find("img");
		if(email != 0) {
			icon.show();
			if(isValidEmailAddress(email)) {
				icon.attr("src","/img/buttons/check.png");
			} else {
				icon.attr("src","/img/buttons/delete.png");
			}
		} else {
			icon.hide();
		}
	});
});



// Accessory Accordion effect
$(function(){
	if( $("body").hasClass("s-service") ) {
		// Mark all rows as collapsed and hide checkboxes (for ie6)
		$(".feature-accessories .acc-row").addClass("collapsed").find("input[type=checkbox]").hide();
		// Mark first row as active
		$(".feature-accessories .acc-row:first").removeClass("collapsed").addClass("active").find("input[type=checkbox]").show();				
		
		// Collapse rows
		$(".feature-accessories .acc-row.collapsed").css("height", "70px").css("margin-bottom","10px");
		
		// On Heading Click
		$(".feature-accessories .acc-row h3").click(function(){
			// Check if you clicked the active row
			if( $(this).parent().hasClass("active") ) {return false;}
			// If not...
			else {
				// Hide checkboxes and collapse row.
				$(".feature-accessories .acc-row").find("input[type=checkbox]").hide().end().animate({ height: "70px", marginBottom: "10px" },"slow").removeClass("active").addClass("collapsed");
				// Expand row and mark as active
				$(this).parent().removeClass("collapsed").addClass("active").animate({ height: "360px", marginBottom: "0" },"slow").addClass("active").find("input[type=checkbox]").show();
				
			}
		});
	}
});


// Lightbox (fancybox)
$(function(){

	$(".single-img a, .map-popup").fancybox({
		frameWidth: 600,
		frameHeight: 600
	});
	
	$(".pricing-popup").fancybox({
		frameWidth: 730,
		frameHeight: 500,
		callbackOnShow: function(){
			$("#fancy_frame").css("overflow","auto");
		}
	});
	
	$(".sctv li a").fancybox({
		frameWidth: 863,
		frameHeight: 515,
		overlayOpacity: 0.9,
		hideOnContentClick: false
	});
});


// Model bar at the bottom of all the vehicle pages.
$(function(){

	var desc = $("#bar-details strong");
	var size = $("#bar-details span");
	var imgtarget = $("#bar-details img");
	
	$("#car-nav a").hover(function(){
	
		item = $(this); // Cache
		
		// Get values
		var title = item.attr("title").split(",");
		var img = item.attr("rel");
		
		// Set values
		desc.text(title[1]);
		size.text(title[0]);
		imgtarget.attr("src","/img/common/" + img);
	
	},function(){
		return false;
	});

});


// Textbox Watermarks
$(function(){

	$("input[type='text'], textarea",".split-side").each(function(){
		if( ! $(this).hasClass('nowatermark')  ){
			$(this).watermark();
		}
	});

});



