// <![CDATA[
$(function() {
		   
	$('#brandCycle').cycle({ 
    	fx:     'fade', 
    	speed:   1000, 
    	timeout: 7000, 
    	//next:   '#brandCycle', 
		prevNextClick: null,
    	pause:   1,
		//random: 1,
		before:  onBefore, 
   		after:   onAfter		
	});
	
	$('.brandBannerBullets li a').click(function() { 
		var brandID = $(this).parents('li').attr('class');		
			brandID = parseFloat(brandID.replace(/item_/, "")) - 1;			
		$('#brandCycle').cycle(brandID);		
		return false; 
	}); 
 	
	
	function onBefore() { 
   		//$('.slideInner').stop().animate(
		 //	{
		//		height: '0px'		 
		 //	}
		 //); 
		//$('.bestValue').stop().delay(250).animate(
		 //	{
		//		height: '0px'		 
		 //	}
		// );
	} 
	function onAfter() { 
		//$('.slideInner').stop().delay(500).animate(
//		 	{
//				height: '28px'		 
//		 	}
//	 	); 
//		$('.bestValue').stop().delay(750).animate(
//		 	{
//				height: '28px'		 
//		 	}
//	 	); 
	}
	
	$("#formSearch").bind({
		keyup: function() {
			if ($("#formSearch").val().length > 2){
				$.post( "/assets/ajax/mapSearch.php", {	
					searchString : $("#formSearch").val()
				},
				function(results) {
					$("#results").html(results);	 
					$("#formSearchWrap").show();																					 
				});
			} else {
				$("#formSearch").empty();
			}
			$("#formSearchType").val('');
			$('a.brandLogo').removeClass('current');
		},
		focus: function() {
			if ($("#formSearch").val().length > 2){
				$.post( "/assets/ajax/mapSearch.php", {	
					searchString : $("#formSearch").val()
				},
				function(results) {
					$("#results").html(results);	 
					$("#formSearchWrap").show();																					 
				});
			} else {
				$("#formSearch").empty();
			}
			$("#formSearchType").val('');
			$('a.brandLogo').removeClass('current');
		}
	});  
	
	$("#formSearchSubmit").click(function() {
		if($("#formSearchType").val() == "brandSearch") {
			loadBrand($("#formSearchRef").val());
		} else if ($("#formSearchType").val() == "motoSearch") {
			searchMoto($("#formSearch").val(), $("#formSearchRef").val());
		} else {
			searchLocations($("#formSearch").val());
		}
		$.post( "/assets/ajax/searchSave.php", {
			searchString : $("#formSearch").val(),
			searchType : "search"
		});
		return false;
	});                         

	$(".motoLink").live("click", function() {
		$("#formSearch").val($(this).html());
		$("#formSearchRef").val($(this).attr("rel"));
		$("#formSearchType").val('motoSearch');
		$("#formSearchWrap").hide();
		searchMoto($("#formSearch").val(), $("#formSearchRef").val());
		return false;
	});
	
	$(".brandLink").live("click", function() {
		$("#formSearch").val($(this).html().replace(/&amp;/, '&'));
		$("#formSearchRef").val($(this).attr("rel"));
		$("#formSearchType").val('brandSearch');
		$("#formSearchWrap").hide();
		loadBrand($("#formSearchRef").val());
		$('h3.collapsible:first').addClass('open').next('div').show();
		$('a#brand-'+$("#formSearchRef").val()).addClass('current');
		return false;
	});
	
	$(".resultsLink").live("click", function() {
		$("#formSearch").val($(this).html());
		$("#formSearchType").val('locationSearch');
		$("#formSearchWrap").hide();	
		searchLocations($(this).html());
		return false;
	});
	
	$('#closeSuggestions').live("click", function() {
		$("#formSearchWrap").slideUp("normal");
		return false;
	});
	
	$("#directionSubmit").click(function() {
		$("#directions").empty()
		setDirections($("#fromAddress").attr("value"), $("#toAddress").attr("value"), "en");
		$.post( "/assets/ajax/searchSave.php", {
			fromAddress : $("#fromAddress").val(),
			toAddress : $("#toAddress").val(),
			searchType : "routePlan"
		});
		if($("#stepDirectionsCheck").is(":checked")) {
			$("#directions").slideDown();
		} else {
			$("#directions").hide();
		}
		return false;
	});
	
	$('#stepDirectionsCheck').click(function() {
		if ($(this).is(':checked')) {
			$('#directions').slideDown();
		} else {
			$('#directions').slideUp();
		}
	});
	
	$("#motoNearby").live("click", function() {
		searchLocations($(this).attr("rel"));
		return false;
	});
	
	$(".brandLogo").click(function() {
		if($(this).hasClass('current')) {
			$(this).removeClass('current');
			loadBrand();
		} else {
			loadBrand($(this).attr('id').replace(/brand-/, '').toLowerCase());
			$('.brandLogo.current').removeClass('current');
			$(this).addClass('current');
		}
		return false;
	});
	
	$(".poiLogo").click(function() {
		if($(this).hasClass('current')) {
			$(this).removeClass('current');
			loadBrand();
		} else {
			loadBrand($(this).attr('id'));
			$('.poiLogo.current').removeClass('current');
			$(this).addClass('current');
		}
		return false;
	});
	
	$('#mapReset').click(function() {
		showMap();
		$("#formSearch").val('');
		$("#formSearchRef").val('');
		$("#formSearchType").val('');
		$('h3.open').removeClass('open').next('div').slideUp('normal');
		$('a.brandLogo.current').removeClass('current');
		$('a.poiLogo.current').removeClass('current');
		$("#fromAddress").val('');
		$("#toAddress").val('');
		$("#stepDirectionsCheck").attr('checked', 'checked');
		$('label.overlay').labelOver();
		$("#formSearchWrap").hide();
		$("#directions").empty().hide();
		return false;
	});
	
	$('h3.collapsible').click(function() {
		if($(this).hasClass('open')) {
			$('h3.open').removeClass('open').next('div').slideUp('normal');
		} else {
			$('h3.open').removeClass('open').next('div').slideUp('normal');
			$(this).addClass('open').next('div').slideDown('normal');
		}
		return false;
	});
	$('h4.collapsible:last').addClass('lastHeader').next('div').addClass('lastPanel');
	
	if (document.getElementsByTagName) {
		var inputElements = document.getElementsByTagName("input");
		for (i=0; inputElements[i]; i++) {
			if (inputElements[i].className && (inputElements[i].className.indexOf("disableAutoComplete") != -1)) {
				inputElements[i].setAttribute("autocomplete","off");
			}
		}
	}
});
// ]]>