// JavaScript Document
$(document).ready(function() {
		
		
		
		
		$('#s').click(function(){
			
			if($('#s').val()=='search'){$('#s').val('');}
		});
		
		$('#banner-slider').cycle({
			fx: 'fade' 
		});
		
		$('#banner-slider-small').cycle({
			fx: 'fade' 
		});
		
		if($('#slider').length!=0){
			$('#slider').cycle({
				fx: 'fade' ,
				timeout: '9000',
				next:   '#slider_next', 
    			prev:   '#slider_prev' ,
				before:     function() {
					$('#slide-title').html(this.alt);
					$('#slide-copy').html(this.title);
					//$('#slider-file').html(this.alt);
				}
	
			});
			// set up the first slide content in case its a 1 image slider
			try{$('#slide-title').html($('#slider img')[0].alt);}catch(err){}
			try{$('#slide-copy').html($('#slider img')[0].title);}catch(err){}
		}
		
		if($('#slider-features').length!=0){
			$('#slider-features').cycle({
				fx: 'fade' ,
				timeout: '9000',
				next:   '#slider-features_next', 
    			prev:   '#slider-features_prev' ,
				before:     function() {
					$('#slide-title-features').html(this.alt);
					$('#slide-copy-features').html(this.title.substring(0,270));
					//$('#slider-file').html(this.alt);
				}
	
			});
			// set up the first slide content in case its a 1 image slider
			try{$('#slide-title-features').html($('#slider-features img')[0].alt);}catch(err){}
			try{$('#slide-copy-features').html($('#slider-features img')[0].title);}catch(err){}
		}
			
		$('#slider_prev').hide();
		$('#slider_next').hide();
		
		$('#slider-container').hover(function() {
         	$('#slider_next').stop(true, true).fadeIn(500); 
			$('#slider_prev').stop(true, true).fadeIn(500);      
		}, function() {
			$('#slider_next').stop(true, true).fadeOut(500); 
			$('#slider_prev').stop(true, true).fadeOut(500); 
		});

		
		$('#slider-features_prev').hide();
		$('#slider-features_next').hide();
		
		$('#slider-container-features').hover(function() {
         	$('#slider-features_next').stop(true, true).fadeIn(500); 
			$('#slider-features_prev').stop(true, true).fadeIn(500);      
		}, function() {
			$('#slider-features_next').stop(true, true).fadeOut(500); 
			$('#slider-features_prev').stop(true, true).fadeOut(500); 
		});
		
		
		$('#nav li').mouseover(function(){
			$(this).css('background-color','#666');
			$(this).find("a").css('color','#ffffff');
			
		}).mouseout(function(){
			$(this).css('background-color','#ffffff');
			$(this).find("a").css('color','#666');
			
		});
		
		$('#nav-current li').mouseover(function(){
			$(this).css('background-color','#666');
			$(this).find("a").css('color','#ffffff');
			$('#nav-7 ').css('background-color','#666');
			$('#nav-7 a').css('color','#ffffff');
			
		}).mouseout(function(){
			$(this).css('background-color','#ffffff');
			$(this).find("a").css('color','#666');
			$('#nav-7 ').css('background-color','#ffffff');
			$('#nav-7 a').css('color','#666');
			
		});
		
		$('#nav-upcomming li').mouseover(function(){
			$(this).css('background-color','#666');
			$(this).find("a").css('color','#ffffff');
			$('#nav-4').css('background-color','#666');
			$('#nav-4 a').css('color','#ffffff');
			
		}).mouseout(function(){
			$(this).css('background-color','#ffffff');
			$(this).find("a").css('color','#666');
			$('#nav-4').css('background-color','#ffffff');
			$('#nav-4 a').css('color','#666');
			
			
		});
		
		
		
		$('#nav-7').mouseover(function() {
			$('#nav-current').show();
			$('#nav-upcomming').hide();
			$('.community-flyout').hide();
		});
		
		$('#nav-4').mouseover(function() {
			$('#nav-current').hide();
			$('#nav-upcomming').show();
			$('.community-flyout').hide();
		});
		
		$('#nav-2').mouseover(function() {
			$('#nav-current').hide();
			$('#nav-upcomming').hide();
			$('.community-flyout').hide();
		});
		
		$('#nav-14').mouseover(function() {
			$('#nav-current').hide();
			$('#nav-upcomming').hide();
			$('.community-flyout').hide();
		});
		
		$('#banner-slide-container').mouseover(function() {
			$('#nav-current').hide();
			$('#nav-upcomming').hide();
			$('.community-flyout').hide();
		});
		
		$('#banner-slider-small').mouseover(function() {
			$('#nav-current').hide();
			$('#nav-upcomming').hide();
			$('.community-flyout').hide();
		});
		
		$('#blog-title').mouseover(function() {
			$('#nav-current').hide();
			$('#nav-upcomming').hide();
			$('.community-flyout').hide();
		});
		
		
		
		$('.nav-item-community').mouseover(function(){
			$('.community-flyout').hide();
			$('#flyout-'+$(this).attr('alt')).show();
			
		});
		
		
		$('#form-contact').submit(function(){
			var err='';
			if(!$('#fname').val()){err+='Please enter your first name.\n';}
			if(!$('#lname').val()){err+='Please enter your last name.\n';}
			if(!$('#street').val()){err+='Please enter your address.\n';}
			if(!$('#city').val()){err+='Please enter your city.\n';}
			if(!$('#tel1').val()){err+='Please enter your phone number.\n';}
			if(!$("#region option:selected").length){err+='Please select a province.\n';}
			if(!$('#email').val()){err+='Please enter your email address.\n';}
			if(!$('#code').val()){err+='Please enter your postal code.\n';}
			// protext aginast malicoius form submits, CAPTCHA or simialr technique
			if(err){
				alert(err);
				return false;
			} else {
				// submit form
				$.post($('#dest').val(), $(this).serialize(),handleResult,"json");
				return false;
			}
		});
		
		function handleResult(data){
			if(data.errors){
				alert(data.errors.replace(/\\n/gi,"\n"));
			} else {
				$('#thanks').show();
				$('#form-contact').hide();
			}
				
		}
		
		if($('#thanks')){$('#thanks').hide();} // hide the thanks div
		
		
		
		$('#form-reg').submit(function(){
			var err='';
			if(!$('#fname').val()){err+='Please enter your first name.\n';}
			if(!$('#lname').val()){err+='Please enter your last name.\n';}
			if(!$('#street').val()){err+='Please enter your address.\n';}
			if(!$('#city').val()){err+='Please enter your city.\n';}
			if(!$('#tel1').val()){err+='Please enter your phone number.\n';}
			if(!$("#region option:selected").length){err+='Please select a province.\n';}
			if(!$('#email').val()){err+='Please enter your email address.\n';}
			if(!$('#code').val()){err+='Please enter your postal code.\n';}
			// protext aginast malicoius form submits, CAPTCHA or simialr technique
			if(err){
				alert(err);
				return false;
			} else {
				// submit form
				$.post($('#dest').val(), $(this).serialize(),handleResultReg,"json");
				return false;
			}
		});
		
		function handleResultReg(data){
			if(data.errors){
				alert(data.errors.replace(/\\n/gi,"\n"));
			} else {
				$('#thanks').show();
				$('#form-reg').hide();
			}
				
		}
		/*
		if($('#slider-range').length!=0){
			if($.cookie('price-min')){pricemin=$.cookie('price-min');} else {pricemin=175000;}
			if($.cookie('price-max')){pricemax=$.cookie('price-max');} else {pricemax=950000;}
			
			$( "#slider-range" ).slider({
				range: true,
				step: 1000,
				min: 175000,
				max: 950000,
				values: [ pricemin, pricemax  ],
				change:updateHomes,
				slide: function( event, ui ) {
					$( "#amount" ).val( "$" + formatNumber(ui.values[ 0 ]) + " - $" + formatNumber(ui.values[ 1 ]) );
					$('#price-min').val($( "#slider-range" ).slider( "values", 0 ));
					$('#price-max').val($( "#slider-range" ).slider( "values", 1 ));
					
				}
			});
			
			$( "#amount" ).val( "$" + formatNumber($( "#slider-range" ).slider( "values", 0 )) +
				" - $" + formatNumber($( "#slider-range" ).slider( "values", 1 )) );
			$('#price-min').val($( "#slider-range" ).slider( "values", 0 ));
			$('#price-max').val($( "#slider-range" ).slider( "values", 1 ));
			
			$('#city').change(function(){updateHomes();});
			$('#style').change(function(){updateHomes();});
			$('#type').change(function(){updateHomes();});
			$('#size').change(function(){updateHomes();});
			$('#bedrooms').change(function(){updateHomes();});
			
			// init
			updateHomes();	
			

			
		}
		*/	
			
		
		if($('#Slider1').length!=0){
			var values = $('#Slider1').slider("value").split(";");
			$("#amount").val( "$" + formatNumber(values[0]) + " - $" + formatNumber(values[1]) );
			$('#price-min').val(values[0]);
			$('#price-max').val(values[1]);
			$('#city').change(function(){updateHomes();});
			$('#style').change(function(){updateHomes();});
			$('#type').change(function(){updateHomes();});
			$('#size').change(function(){updateHomes();});
			$('#bedrooms').change(function(){updateHomes();});
			
			// init
			updateHomes();	
		}
			
			
			
		
		
	
		
});

function updatePriceValuesFromSlider(value){
	var values = value.split(";");
	$("#amount").val( "$" + formatNumber(values[ 0 ]) + " - $" + formatNumber(values[ 1 ]) );
	$('#price-min').val(values[ 0 ]);
	$('#price-max').val( values[ 1 ]);
	updateHomes();
}

function updateRegPriceValuesFromSlider(value){
	var values = value.split(";");
	$("#amount-reg").val( "$" + formatNumber(values[ 0 ]) + " - $" + formatNumber(values[ 1 ]) );
	$('#price-min-reg').val(values[ 0 ]);
	$('#price-max-reg').val( values[ 1 ]);
}



function formatNumber(num) {
	return (''+num).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}


function updateHomes(){
	// update the cookies to the current values in the form
	$.cookie('price-min', $('#price-min').val(), { expires: 365, path: '/'});
	$.cookie('price-max', $('#price-max').val(), { expires: 365, path: '/'});
	$.cookie('size', $('#size').val(), { expires: 365, path: '/'});
	$.cookie('type', $('#type').val(), { expires: 365, path: '/'});
	$.cookie('city', $('#city').val(), { expires: 365, path: '/'});
	$.cookie('bedrooms', $('#bedrooms').val(), { expires: 365, path: '/'});
	
	
	
	
	$.ajax({url:'http://www.branthaven.com/wp-content/themes/bh/js/get_homes.php?requestid='+Math.random()*Math.random(),type:"GET",
		success:function(data) {
			$('#express').html(data.express);
			$('#homes').html(data.homes);
			$('#models').html(data.models);
		}, dataType: 'json'}
	);
	
	
	
}
	
	
function printpage(){
	print();
}


    
	
	function codeAddressSales(obj, address,the_title){
	 //alert(address);
	  geocoder.geocode( { 'address': address}, function(results, status) {
	  if (status == google.maps.GeocoderStatus.OK) {
		obj.setCenter(results[0].geometry.location);
		var marker = new google.maps.Marker({
			map: obj, 
			position: results[0].geometry.location,
			title: the_title +": " + address
		});
		 
	  } else {
		alert("Geocode for [" +address + "] was not successful for the following reason: " + status);
	  }
	});
	  
	}
	
	function addMarker(obj,address,lat,long,the_title){
	
		var myLatLng = new google.maps.LatLng(lat,long);
		obj.setCenter(myLatLng);
		
		var marker = new google.maps.Marker({
			map: obj, 
			position: myLatLng,
			title: the_title +": " + address
		});
	
	}

