showDealerSearch = function() {jQuery('#locator').slideToggle(1000);}

validate_dealer_search = function() {
	var err = '';
	if(jQuery('#brand').val() == '') {
		err += "Please select a brand\n";
	}
	if(jQuery('#state').val() == '') {
		err += "Please select a state\n";
	}
	jQuery('#postcode').attr('value', jQuery.trim(jQuery('#postcode').attr('value')));
	if( jQuery('#postcode').attr('value') != '' && !(/^[0-9]{3,4}$/.test(jQuery('#postcode').attr('value'))) ) {
		err += "Postcode is invalid\n";
	}

	if(err == '') {
		return true;
	} else {
		alert(err);
		return false;
	}
	
}

dealer_search_result = function(brand, state, postcode, curPage, pageTotal, total) {	
	jQuery.ajax({
		type: "GET",
		url: '/index.php/dealer/search_result_json',
		data: 'brand='+brand+'&state='+state+'&postcode='+postcode+'&curPage='+curPage,
		dataType: 'json',
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			
		},
		success: function(data){
			var resultHtml = '';
			if(data.dealers_not_found_within && data.total != 0) {
				resultHtml += '<p id="message">Unfortunately there are no dealers located within the post code area specified.<br/>Please find below a list of the "' + data.brand + '" dealers located in "' + data.state + '".</p>';
			} else if(!data.dealers_not_found_within && data.postcode == '') {
				resultHtml += '<p id="message">Your search for "' + data.brand + '" in "' + data.state + '"' + ((data.postcode!='')?(' &amp; "' + data.postcode + '"'):'') + ' returned ' + data.total + ' dealer' + ((data.total>1)?'s':'') + '</p>';				
			} else {
				resultHtml += '<p id="message">Your search for "' + data.brand + '" in "' + data.state + '"' + ((data.postcode!='')?(' &amp; "' + data.postcode + '"'):'') + ' returned ' + data.total + ' dealer' + ((data.total>1)?'s':'') + ' within a 50km area</p>';				
			}
			if(data.total != 0) {
				resultHtml += '<table class="formatted">';
				resultHtml += '<tr><th>Name</th><th>Address</th><th>Phone</th><th>Fax</th><th>Service</th><th>&nbsp;</th></tr>';

				var dealers = data.dealers;
				for(i in dealers) {
					if (dealers[i].website.length > 1) { 
						webAddress = '<br /><br /><a target="_blank" href="http://' + dealers[i].website + '">' + dealers[i].website + '</a>'
					} else {
						webAddress = "";
					};
					
					var dealer_class = '';
					if(dealers[i].is_petrol_expert.toLowerCase() == 'yes' && data.brand_petrol_or_diesel.toLowerCase() == 'petrol' || dealers[i].is_diesel_expert.toLowerCase() == 'yes' && data.brand_petrol_or_diesel.toLowerCase() == 'diesel') {
						dealer_class = 'expert';
					} else {
						dealer_class = '';
					}					

					resultHtml += '<tr class="' + dealer_class + '">' + 
						'<td class="dealer-name">' + dealers[i].name + webAddress +'</td>' + 
						'<td>' + dealers[i].address.replace(/,/g,"<br />") + '</td>' + 
						'<td>' + dealers[i].phone + '</td>' + 
						'<td>' + dealers[i].fax + '</td>' + 
						'<td>' + ((dealers[i].is_petrol.toLowerCase() == 'yes')?'Petrol<br/>':'') + ((dealers[i].is_diesel.toLowerCase() == 'yes')?'Diesel<br/>':'') + '</td>' + 
						'<td>' + ((dealer_class == 'expert')?'<img src="/img/epg_epgines_expert.jpg" title="Expert" class="png" />':'') + '</td>' + 
						'</tr>';
				}

				resultHtml += '</table>';

				resultHtml += '<p id="pagination">';

				resultHtml += '<span class="page-number">'+ 'page ' + data.curPage + ' of ' + data.pageTotal + '</span>';

				if(data.curPage > 1) {
					resultHtml += '<a class="previous" href="/index.php/dealer/search_result?brand=' + brand + '&state=' + state + '&postcode=' + postcode + '&curPage=' + (data.curPage-1) + '"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></a>';
				}
				
				if(data.curPage < data.pageTotal) {
					resultHtml += '<a class="next" href="/index.php/dealer/search_result?brand=' + brand + '&state=' + state + '&postcode=' + postcode + '&curPage=' + (data.curPage+1) + '"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></a>';
				}
			}
			jQuery('#result').html(resultHtml);
		}		
	 });

}


/* ***********************************************************************
*  Showcase animation - home page
*  *********************************************************************** */
	returnPosition = 0;
	largePosition = 0;
	marginOffsetLeft = 10;
	marginOffsetTop = 10;
	smallImageWidth = 116;
	smallImageHeight = 130;
	largeImageWidth = 365;
	largeImageHeight = 410;
	textFadeInSpeed = 1000;
	textFadeOutSpeed = 500;
	imageSlideInSpeed = 1000;
	imageSlideOutSpeed = 750;
	numberOfAds = 3;
	currentAdNumber = null;
	showcaseState = "stopped";
	
	function showcaseSetState(state) {
		showcaseState = state;
	}
	
	function showcaseAnimateIn(adNumber,continuousPlay,attachMouseEvents) {
		currentAdNumber = adNumber; 
		largePosition = jQuery("#showcase-box-main").position();
		returnPosition = jQuery("#showcase-box-"+adNumber).position();
				
		showcaseSetState('initing');
		jQuery("#showcase-box-"+adNumber).fadeTo(0, 1.00);
		jQuery("#showcase-box-"+adNumber).css({'border-color':'#888888'});
		
		newLink = jQuery("#showcase-link-"+adNumber).attr("href");
		
		jQuery("#showcase-box-mouse").attr("href", newLink); 
		
		// Init large image starting pos and size
		jQuery("#showcase-image-"+adNumber).css({
			"display"	: "block",
			"position"	: "absolute"
		});
		if ( jQuery("#showcase-image-"+adNumber).css("opacity") == 0.0 ) {jQuery("#showcase-image-"+adNumber).fadeTo(0,0.01);}

		// Init the text block
		if ( jQuery("#showcase-image-"+adNumber).css("opacity") == 0.0 ) {jQuery("#showcase-text-"+adNumber).fadeTo(0,0.0);}
		jQuery("#showcase-text-"+adNumber).css({"display":"block"});
				
		// Animate large image to it's newposition
		// 		Chain the text fade in after the image anim
		showcaseSetState('animating in');
		jQuery("#showcase-text-"+adNumber).animate( { opacity: 1.0 }, { duration: textFadeInSpeed, complete: function() {}} );
		jQuery("#showcase-image-"+adNumber).css( { 'top':12, 'left':570, 'width': largeImageWidth, 'height': largeImageHeight } );
		jQuery("#showcase-image-"+adNumber).fadeTo(textFadeInSpeed,1.0);
		/*
		jQuery("#showcase-image-"+adNumber).animate(
			{
				top: 12,
				left: 570,
				width: largeImageWidth,
				height: largeImageHeight,
				opacity: 1.0
			},
			{
				duration: imageSlideInSpeed,
				complete: function() {}}
		);
		*/
		
	}

	function showcaseAnimateOut(adNumber,continuousPlay,attachMouseEvents) {
		largePosition = jQuery("#showcase-box-main").position();
		returnPosition = jQuery("#showcase-box-"+adNumber).position();
				
		showcaseSetState('animating out');
		jQuery("#showcase-box-"+adNumber).fadeTo(textFadeOutSpeed, 0.70);
		
		jQuery("#showcase-box-"+adNumber).css({'border-color':'#dddddd'});
		
		// Init large image starting pos and size
		jQuery("#showcase-image-"+adNumber).css({
			"position"	: "absolute"
		});

		// Animate large image to it's newposition
		// 		Chain the text fade in after the image anim
		jQuery("#showcase-text-"+adNumber).fadeTo(textFadeOutSpeed, 0.0);
		jQuery("#showcase-image-"+adNumber).fadeTo(textFadeOutSpeed, 0.0);
		/*
		jQuery("#showcase-image-"+adNumber).animate(
			{
				top: returnPosition.top + marginOffsetTop,
				left: returnPosition.left + marginOffsetLeft,
				width: smallImageWidth,
				height: smallImageHeight,
				opacity: 0.0
			},
			{
				duration: imageSlideOutSpeed,
				complete: function () {jQuery("#showcase-image-"+adNumber).css({"display":"none"});
				}
			}			
		);
		*/
		
	}

	function pauseShowcase() {
		// stop mouse events from registering for the moment
		// showcaseClearEventHandlers();
		
		// stop any animations in their tracks
		jQuery("#showcase-text-"+currentAdNumber).stop(true);
		jQuery("#showcase-image-"+currentAdNumber).stop(true);
		
		// show the current ad
		showcaseAnimateIn(currentAdNumber,false,true);
	}

	function continueShowcase() {
		jQuery("#showcase-text-"+currentAdNumber).stop(true);
		jQuery("#showcase-image-"+currentAdNumber).stop(true);
		showcaseAnimateOut(currentAdNumber,false,true);
	}

	function showcaseSetEventHandlers() {
		jQuery("#showcase-box-mouse").css("display","none");		
		//jQuery("#showcase-box-mouse").mouseenter( function() {pauseShowcase()});
		//jQuery("#showcase-box-mouse").mouseleave( function() {continueShowcase()});
		jQuery("#showcase-box-1").mouseenter( function() {
				if (currentAdNumber != 1) showcaseAnimateOut(currentAdNumber,false,false); 
				showcaseAnimateIn(1,false,false); 
			} 
		);
		jQuery("#showcase-box-2").mouseenter( function() { 
				if (currentAdNumber != 2) showcaseAnimateOut(currentAdNumber,false,false); 
				showcaseAnimateIn(2,false,false); 
			} 
		);
		jQuery("#showcase-box-3").mouseenter( function() { 
				if (currentAdNumber != 3) showcaseAnimateOut(currentAdNumber,false,false); 
				showcaseAnimateIn(3,false,false);
			} 
		);
	}
	function showcaseClearEventHandlers() {	
		jQuery("#showcase-box-1").unbind();	
		jQuery("#showcase-box-2").unbind();	
		jQuery("#showcase-box-2").unbind();	
		jQuery("#showcase-box-mouse")
			.unbind(); // "mouseenter", "mouseleave"
	}
	
	function animateShowcase() {
		// init starting positions for animated images
		for (adNumber=1; adNumber < 4; adNumber++ ) {
			returnPosition = jQuery("#showcase-box-"+adNumber).position();
						
			jQuery("#showcase-image-"+adNumber).css({
				"position"	: "absolute",
				"top"		: returnPosition.top + marginOffsetTop,
				"left"		: returnPosition.left + marginOffsetLeft,
				"width"		: smallImageWidth,
				"height"	: smallImageHeight
			});
		}
		
		// Set the text layers to invisible
		jQuery("#showcase .text").css({
			"opacity"	: 0.0
		});
		
		//
		jQuery("#showcase .small").fadeTo(0, 0.70);
		// , #showcase-box-main
		jQuery("#showcase .small").css({
			'border-width' :	'1px',
			'border-style'	:	'solid',
			'border-color'	:	'#cccccc'
		});
		jQuery("#showcase-box-main .text").css({'background-color':'#ffffff'});
		
		//Start animation
		showcaseAnimateIn(1,false);		
	}

/* ***********************************************************************
*  Brand products animation
*  *********************************************************************** */
	function productsSetEventHandlers() {		
		jQuery("#products .blurb").fadeTo(0,0.0);
		jQuery("#products .blurb").css("display","block");
		jQuery("#products a.product").bind("mouseenter", function(eventObject) {jQuery(this).children(".blurb").fadeTo(750,0.95)});
		jQuery("#products a.product").bind("mouseleave", function(eventObject) {jQuery(this).children(".blurb").fadeTo(750,0.0)});
	}

