$(document).ready(function(){
	/*
	 UI visual cue for quick search
	*/
	var defSearchVal = 'Search';
	$('.quick_search_input').css('color', '#cccccc').val(defSearchVal);
	
	$('.quick_search_input').focus(function(){
		if ($(this).val() == defSearchVal ){
			$(this).css('color', '#222222').val('');
		}
	});
	$('.quick_search_input').blur(function(){
		if ($(this).val() == '' )
		{
			$(this).css('color', '#cccccc').val(defSearchVal);
		}
	});
	
	$('#athena_messaging').cycle({
		speed:2000,
		timeout: 5000
	});
	
	$('.lightbox').lightBox();
	
	/*
	Ajax form submission - uses CI app to process forms
	form structure:
	<div id="FORM_WRAPPER>
		<div id="LOADING_ELEM">LOADING GIF</div>
		<div id="ERROR_CONTAINER"></div>
		<div id="SUCCESS_CONTAINER"></div>
		<form id="FORM_ID"></form>
	</div>
	Run submitAjaxForm in the submit method for the form:
	i.e. 
	$(myForm).submit(function(){ 
		submitAjaxForm(options); return false; 
	});
	
	you must return false in the submit method!
	
	You must also include the submission options array as a paramter to the submitAjaxForm function
		OPTIONS
		 form: id of form
		 url: post url
		 errorElem: id of error container
		 successElem: id of success container
		 formElem: id of elem to scroll to, typically the form wrapper element. (optional),
		 loadingElem: id of loading div
	*/
	function submitAjaxForm(options)
	{
		var formElem = options['form'];
		var postURL = options['url'];
		var loadingElem = options['loadingElem'];
		var errorElem = options['errorElem'];
		var successElem = options['successElem'];
		
		$('#'+loadingElem).show();
		$('#'+errorElem).hide();
		$('#'+successElem).hide();
		
		// check to see if we are scrolling to element
		if (options['scrollToElem'] != null)
		{
			$.scrollTo('#'+options['scrollToElem'], {duration:500});
		}
		
		var dataString = $('#'+formElem).serializeArray();
		
		$.post(postURL, dataString, function(data){
			$('#'+loadingElem).hide();
			if (data.error == 'true')
			{
				$('#'+errorElem).empty();
				$('#'+errorElem).fadeIn('slow');
				$('#'+errorElem).append(data.content);
			}
			else
			{
				$('#'+successElem).empty();
				$('#'+successElem).fadeIn('slow');
				$('#'+successElem).append(data.content);
				$('#'+formElem).each(function(){
					this.reset();
				});
			}
		}, 'json');
		
		return false;
	}
	
	// dealer form
	$('#DealerForm').submit(function(){
		submitAjaxForm({
		 'scrollToElem': 'FormWrapper',
		 'form': 'DealerForm',
		 'url': '/ci/index.php/form/dealer',
		 'errorElem': 'FormError',
		 'successElem': 'FormSuccess',
		 'loadingElem': 'FormLoading'
		});
		return false;
	});
	// warranty form
	$('#WarrantyForm').submit(function(){
		submitAjaxForm({
		 'scrollToElem': 'FormWrapper',
		 'form': 'WarrantyForm',
		 'url': '/ci/index.php/form/warranty',
		 'errorElem': 'FormError',
		 'successElem': 'FormSuccess',
		 'loadingElem': 'FormLoading'
		});
		return false;
	});
				   
});
