Custom = new function() {
	this.initialise = function () {
		// ADD CUSTOM MODS HERE
		
		Custom.refresh($(document.body));
	}
	
	this.refresh = function (context) {
		$('#loading_div').remove();
		
		$('.prev, .next', context)
			.delay(2000)
			.fadeOut('slow')
		;
		
		$('.largeImg', context).hover(
			function() {
				$('.prev, .next', context)
					.show()
			},
			function() {
				$('.prev, .next', context)
					.hide()
			}
		)
		
		$('.ssFormValidate', context).each(
			function (index) {
				var thisForm	= $(this);
				
				$(this).ssFormValidate(
					{
						label_class:	'',
						field_class:	'not_valid',
						
						beforeSubmit:
							function () {
								$(document.body)
									.append(
										$('<div/>')
											.attr('id', 'loading_div')
											.css({'top': '0px', 'width':$(window).width(),'height':$(document).height()})
											.css({'position': 'absolute', 'cursor': 'wait'})
											.css({'z-index': '9999'})
											.append('&nbsp;')
									)
							},
						
						success:
							function (data, textStatus, jqXHR, formObj) {
								$('#loading_div').remove();
								
								$(document.body).append(data);
								
								$(':input', thisForm).each(function() {
									var type = this.type;
									var tag = this.tagName.toLowerCase(); // normalize case
									if (type == 'text' || type == 'password' || tag == 'textarea')
										this.value = "";
									else if (type == 'checkbox' || type == 'radio')
										this.checked = false;
									else if (tag == 'select')
										this.selectedIndex = -1;
								});
								
								$('.ajax_load').each(
									function (index) {
										var target_container	= $('#' + $(this).attr('rel'));
										var ajax_url			= $(this).attr('href');
										
										target_container
											.load(
													ajax_url,
												'',
												function (responseText, textStatus, XMLHttpRequest) {
													
												}
											)
									}
								)
							}
					}
				);
			}
		);
	}
}

$(document).ready(function() {
	
	
	$(document).unbind().bind('keypress', function(event) {
		switch (event.keyCode) {
	    	case 37:
	    		event.preventDefault();
	    		$('a.prev').click();
	    		break;
	    	
	    	case 39:
	    		event.preventDefault();
	    		$('a.next').click();
	    		break;
	    }
	}); 
	
	$('.killTarget').live(
		'click',
		function (e) {
			e.preventDefault();
			
			var target	= $(this).attr('target');
			
			target.remove();
		}
	)
	
	$('.killParent').live(
		'click',
		function (e) {
			e.preventDefault();
			
			$(this).parent().remove();
		}
	)
	
	$('.ajaxLoad').live(
		'click',
		function (e) {
			e.preventDefault();
			
			document.title	= '';
			
			var target_selector	= '#' + ($(this).attr('target') || "content");
			
			var target_url		= $(this).attr('href');
			
			try {
				window.history.replaceState('Object', 'Title', target_url);;
				
				$(document.body)
					.append(
						$('<div/>')
							.attr('id', 'loading_div')
							.css({'top': '0px', 'width':$(window).width(),'height':$(document).height()})
							.css({'position': 'absolute', 'cursor': 'wait'})
							.css({'z-index': '9999'})
							.append('&nbsp;')
					)
				
				$(target_selector).fadeOut('slow');
				
				$.ajax({
					url: target_url,
					success: function (data) {
						$(document.body).removeClass('home');
						
						$("html").scrollTop(0);
						
						var replacement	= $(data).find(target_selector);
						replacement.hide();
						
						var page_title	= $(data).filter('title').text();
						
						var car_make	= $(data).find('.car_make').text();
						
						var car_model	= $(data).find('.car_model').text();
						
						if (page_title)
							document.title	+= page_title;
						
						if (car_make) {
							if (document.title != '')
								document.title	+= " - ";
							
							document.title	+= " " + car_make + " " + car_model;
						}	
						
						$(target_selector)
							.replaceWith(replacement)
							
						$(target_selector)
							.fadeIn('slow')
						
						Custom.refresh($(target_selector));
					}
				})
			} catch (e) {
				document.location	= target_url;
			}	
		}
	)
});
