// Preload images
preimgs = new Array("bg.jpg","content-bg.jpg","header.jpg","main-nav-buying-over.png","main-nav-selling-over.png","main-nav-financing-over.png","main-nav-about-us-over.png","main-nav-contact-us-over.png","main-nav-mls-over.png","main-nav-mls-sign-over.png");
for(i=0; i<=preimgs.length; i++) {
	pic= new Image(); 
	pic.src="/images/new-site/"+preimgs[i];
}

// Nav mouse over
function mouse_over(name) {
	document.getElementById('main-nav-'+name).src = "/images/new-site/main-nav-"+name+"-over.png";
}

// Nav mouse out
function mouse_out(name) {
	document.getElementById('main-nav-'+name).src = "/images/new-site/main-nav-"+name+".png";
}

// Checks if a value is an integer
function is_int(input){
	return typeof(input)=='number'&&parseInt(input)==input;
}

// Modal Popup
$(document).ready(function(){
	
	// For IE 7
	var ie7 = false;
	if($.browser.msie && parseInt($.browser.version, 10) == 7) {
    	ie7 = true;
	}   
					   		   
	//When you click on a link with class of poplight and the href starts with a # 
	$('a.poplight[href^=#]').click(function() {
		var popID 	= 	$(this).attr('rel'); //Get Popup Name
		var popURL 	= 	$(this).attr('href'); //Get Popup href to define size
				
		//Pull Query & Variables from href URL
		var query 		= 	popURL.split('?');
		var dim 		= 	query[1].split('&');
		var popWidth 	= 	dim[0].split('=')[1]; //Gets the first query string value

		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="javascript:void();" class="close" style="float: right;"><span style="border: none;" class="btn_close">close window</span></a>');
		
		//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop 	= ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
		
		//Apply Margin to Popup
		$('#' + popID).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		if (!ie7) {
			$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		}
		
		return false;
	});
	
	
	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	  	$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  
	}); //fade them both out
		
		return false;
	});

	
});

function frontPageLookup() {
	
	window.location="/listings.php";
	
}

function openImage(photoId, url) {
	
	// Get and render image from url
	document.getElementById('open-image').src = url;
	
	// Set image navigation
	document.getElementById('image-nav').innerHTML = document.getElementById('hidden-image-nav-'+photoId).innerHTML;
	
}

function changeImage(url) {
		
	// Get and render image from url
	document.getElementById('open-image').src = url;
}

function removeListingBlock(id) {
	document.getElementById(id).style.display = "none";
}

function doNothing() {
	
}

function filterListings(listingCount) {
	
	var listingId = "";
	// Bring back any previously removed listing blocks
	for (i=0; i<=listingCount; i++) {
		listingId = 'listing-'+listingIds[i];
		document.getElementById(listingId).style.display = "block";
	}
	
	// Select values
	var selectedMinPrice = document.getElementById('select-min-price').value;
	var selectedMaxPrice = document.getElementById('select-max-price').value;
	var selectedBeds = document.getElementById('select-beds').value;
	var selectedBaths = document.getElementById('select-baths').value;
	var selectedCity = document.getElementById('select-city').value;
	var selectedPropertyType = document.getElementById('select-property-type').value;
	
	// NA value
	var naValue = "********";
	
	// Loop through all shown listings
	var listingPrice = "";
	var listingBeds = "";
	var listingBaths = "";
	var listingType = "";
	for (i=0; i<=listingCount; i++) {
		
		listingId = 'listing-'+listingIds[i];
		listingPrice = listings[listingId]['price']-1000;
		listingCity = listings[listingId]['city'];
		listingBeds = listings[listingId]['beds'];
		listingBaths = listings[listingId]['baths'];
		listingPropertyType = listings[listingId]['type'];
		
		// Remove visibility of listing block that does not meet filter criteria
		
		// Min/Max Price
		if (listingPrice < selectedMinPrice) {
			removeListingBlock(listingId);
		}
		
		if (listingPrice >= selectedMaxPrice) {
			removeListingBlock(listingId);
		}
		
		// City
		if (listingCity != selectedCity) {
			if (selectedCity != "ANY") {
				removeListingBlock(listingId);
			}
			
		}
		
		// Property Type
		if (listingPropertyType != selectedPropertyType) {
			if (selectedPropertyType != "ANY") {
				removeListingBlock(listingId);
			}
		}
		
		// Beds
		if (listingBeds != naValue) {
			if (listingBeds < selectedBeds) {
				removeListingBlock(listingId);
			}
		}
		
		// Baths
		if (listingBaths != naValue) {
			if (listingBaths < selectedBaths) {
				removeListingBlock(listingId);
			}
		}
		
	}
	
}

// Send email from contact form
function sendFromContactForm() {
	
	// Form values
	var frmName = document.getElementById('contact-form-name').value;
	var frmEmail = document.getElementById('contact-form-email').value;
	var frmCompany = document.getElementById('contact-form-company').value;
	var frmMessage = document.getElementById('contact-form-message').value;
	
	// Submit form data to server
	$.post( "/send.php", { name: frmName, company: frmCompany, email: frmEmail, message: frmMessage } ,
	  function(data) {
		  if (data == "1") {
			  
			  // Display success message
			  bodyContent = $.ajax({
				  url: "/send.php?complete=1",
				  global: false,
				  type: "POST",
				  dataType: "html",
				  async:false
			  }).responseText;
			  
			  document.getElementById('contact-form-submit-result').innerHTML = bodyContent;
			  
		  }
		  
		  else {
			
			// Unsuccessful send message
			document.getElementById('contact-form-submit-result').innerHTML = "There was a problem and the message was not sent";
			  
		  }
	  }
	);
	
}
