// Image preload script
(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery)

// Document ready functions
$(document).ready(function () {  

	// Check element exists
	jQuery.fn.exists = function(){return jQuery(this).length>0;}
	
	//Preload menu background
	jQuery.preLoadImages("assets/images/navigation/nav_menu_background.png", "assets/images/navigation/ul_dot.png");
	
	// Top navigation hover
	$('#topNav li').hover(
		function() {
			$(this).children('a').addClass('activeButton');
		},
		function() {
			$(this).children('a').removeClass('activeButton');			
		}						  
	);
	
	// Top navigation hoverIntent
	$('#topNav li').hoverIntent({		
		timeout: 100,
		over: function() {
			$(this).children('div').slideDown('fast');
		},		
		out: function() {
			$(this).children('div').fadeOut('100');
		}
	});	

	// Gallery lightbox
	if( $('#gallery').exists() )
	{
		$('#gallery a').lightBox();
	}
	
	// Additional lightboxes
	if( $('.inlinepics').exists() )
	{
		$('.inlinepics a').lightBox();
	}
	
	// UI tabs
	if( $('#fabricTabs').exists() )
	{
		$('#fabricTabs').tabs();
	}
	
	// Tooltips
	if( $('.tooltip_trigger').exists() )
	{
		$('.tooltip_trigger').tooltip({ position: 'top right'});
	}
	
	// Clear default text
	if( $('#postcode').exists() )
	{
		$('#postcode').click( function() {
			if( $(this).val() == '6000' )
			{
				$(this).val('');	
			}
		});
	}	
	
	// Find a dealer ajax request
	if( $('#btnFindDealer').exists() )
	{
		$('#btnFindDealer').click( function() {
			ExecuteAjax("index.php", "id=14&pc=" + $('#postcode').val(), $("#ajaxresult"));
		});
	}
	
	// Quote ajax request
	if( $('#frmRequestQuote').exists() )
	{
		var myForm = $('#frmRequestQuote')
		
		myForm.validate({
						
			submitHandler: function() {
				
					var requestData = 
					{
						"name" : $('#txtName').val(), 
						"phone" : $('#txtPhone').val(), 
						"email" : $('#txtEmail').val(), 
						"suburb" : $('#txtSuburb').val(), 
						"comment" : $('#txtComment').val()
					}
												  
					ExecuteAjax("index.php?id=15", requestData, $("#ajaxresult"));
					return false;
				}
		});
	}
		
	// Ajax request
	function ExecuteAjax(url, requestData, resultTarget)
	{
		$.ajax({
				type: "GET",
				url: url,
				data: requestData,
				timeout:5000,
				beforeSend: function(){
					resultTarget.html('<p><img src="assets/images/ajax-loader.gif" /> Loading data...</p>');
				}, 
				error: function(req,error){
				  if(error === 'error'){error = req.statusText;}
				  var errormsg = 'An error('+error+') occurred while submitting your request, please try again.';
				  resultTarget.html(errormsg).effect('highlight',{color:'#c00'},1000);
				},
				success: function(data){
					resultTarget.html(data).effect('highlight',{},1000);
				}
		});
	}	
	
});
