var currNav = ''; // Set current nav id

// Replace a text field with a value
function replaceValue(target, defaultText, newValue)
{
	if (target.value == defaultText) target.value = newValue;
}

// Handle rollover images - insert '_over' and take it out again
function rollover(target)
{
    if (target != currNav)
    {
        if (document.getElementById(target).src.indexOf('_over') == -1)
        {
            newImg = document.getElementById(target).src.replace(/\.(.{3,4})$/g, "_over.$1");
            document.getElementById(target).src = newImg;
        }
    }
}

function rollout(target)
{
    if (target != currNav)
    {
        newImg = document.getElementById(target).src.replace(/\_over/g, "");
        document.getElementById(target).src = newImg;
    }
}

// Set right-column nav hilights
function setThirdNav(target, path)
{
	document.getElementById(target).style.background = 'url('+path+'images/bg_sidenav_over.gif) top left no-repeat #e9dcb9';
}

// 'Open' our top search with YUI
function toggleTop()
{
	if (document.getElementById('topContents').style.top == '0px')
	{
		var anim = new YAHOO.util.Anim('topContents', {top: {to: 116} }, 0.3, YAHOO.util.Easing.easeOut);
		document.getElementById('chatBtn').style.display = 'block';
	} else {
		var anim = new YAHOO.util.Anim('topContents', {top: {to: 0} }, 0.3, YAHOO.util.Easing.easeOut);
		document.getElementById('chatBtn').style.display = 'none';
	}
	anim.animate();
}

// Everybody's friend, the Dreamweaver Image Preloader
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

// Popup window - give me the url, width, and height, and I'll give you a popup
function popUp(url, x, y)
{
	if (isIE() == true)
	{
		y += 40;
	}
    var args = "width=" + x + ",height=" + y;
    var date = new Date();
    var now = date.getTime();
    var newName = (now).toString(10);

    window.open(url,newName,args);
}

// Popup window - this one has scrollbares, resizing, the whole 9
function popUpDeluxe(url, x, y)
{
	if (isIE() == true)
	{
		y += 40;
	}
    var args = "width=" + x + ",height=" + y + ",status=1,resizable=1,scrollbars=1,menubar=1,location=1";
    var date = new Date();
    var now = date.getTime();
    var newName = (now).toString(10);

    window.open(url,newName,args);
}

// Popup a 'frame' - just a popup w/ more options and a standard width/height
function popFrame(url)
{
	if (isIE() == true)
	{
		var args = "width=780,height=640,resizable=1";
	} else {
		var args = "width=780,height=600,resizable=1";
	}
    var date = new Date();
    var now = date.getTime();
    var newName = (now).toString(10);

    window.open(url,newName,args);
}

/*** Form checking functions ***/
function checkField (val, field)
{
    var error = "";
    if (val == "")
    {
      error = "You must enter a value in the " + field + " field.\n";
    }
    return error;
}
function checkEmail(val, field)
{
    var error = "";
    if ((val == "" || val.length < 3) ||
        (val.indexOf("@") == "-1") ||
        (val.indexOf(".") == "-1"))
    {
        error = "Please enter a valid " + field + " address.\n";
    }

    return error;
}

function isIE()
{
	//Detect IE5.5+
	version=0;
	if (navigator.appVersion.indexOf("MSIE")!=-1) {
		temp=navigator.appVersion.split("MSIE");
		version=parseFloat(temp[1]);
	}
	if (version >= 5.5)
	{ //NON IE browser will return 0
		return true;
	} else {
		return false;
	}	
}

function showDiv(target)
{
	document.getElementById(target).style.display = 'block';
}
function hideDiv(target)
{
	document.getElementById(target).style.display = 'none';
}

/** Send to box **/
var sendToTimer;
// Prop send-to box
function activateSendTo()
{
	clearTimeout(sendToTimer);
	showDiv('sendToBoxBg');
	showDiv('sendToBoxBg2');
	showDiv('sendToBoxBg3');
	showDiv('sendToBoxBg4');
	showDiv('sendToBox');
}

function setSendToTimer()
{
	sendToTimer = setTimeout(deactivateSendTo, 10000);
}

function deactivateSendTo()
{
	hideDiv('sendToBoxBg');
	hideDiv('sendToBoxBg2');
	hideDiv('sendToBoxBg3');
	hideDiv('sendToBoxBg4');
	hideDiv('sendToBox');
}

/*** Sale/Lease logic ***/
function setPropFieldsForLease(yes)
{
	if (yes == true)
	{
		document.getElementById('prop_type').style.display = 'none';
		document.getElementById('class').style.display = 'block';
		document.getElementById('priceSale').style.display = 'none';
		document.getElementById('priceLease').style.display = 'block';
	}
}
function setPropFieldsForSale(yes)
{
	if (yes == true)
	{
		document.getElementById('prop_type').style.display = 'block';
		document.getElementById('class').style.display = 'none';
		document.getElementById('priceSale').style.display = 'block';
		document.getElementById('priceLease').style.display = 'none';
	}
}

/*** Quick Search ***/
var countSuccess = function(o){
	if(o.responseText !== undefined){
		countTarget.innerHTML = "&nbsp;" + o.responseText;
		document.getElementById('_last_count').value = o.responseText;
	}
}
var countFailure = function(o){
	if(o.responseText !== undefined) {
		countTarget.innerHTML = "--";
	}
}
var callback = {success:countSuccess, failure: countFailure};

function quickCount(where, imgsrc, formName)
{
	if (formName == undefined)
	{
		formName = 'quickSearch';
	}	
	countTarget.innerHTML = '&nbsp;<img src="'+imgsrc+'" width="16" height="16" style="padding: 0 0 2px 0;" />';
	YAHOO.util.Connect.setForm(formName);
	YAHOO.util.Connect.asyncRequest('GET', where, callback);
}

/*** Property functions ***/
function loadGoogleMap(lat, lon, address, citystate)
{
  if (GBrowserIsCompatible())
  {
    map = new GMap2(document.getElementById("propMap"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());

    // Make info window content
  	var fullAddress = address + ' ' + citystate;
    var dirLink = "http://maps.google.com/maps?f=q&q=" + fullAddress;
    var balloonText  = '<div class="big" style="margin-bottom: 5px;"><b>' + address + "</b></div>";
    balloonText += citystate + "<br /><br />";
    balloonText += '<a href="' + dirLink + '" target="_blank">Get Directions</a>';

    if (lat != 0 && lon != 0)
    {
			map.setCenter(new GLatLng(lat, lon), 15);
      var marker = new GMarker(map.getCenter());
      map.addOverlay(marker);
      marker.bindInfoWindowHtml(balloonText);
    } else {
      geocoder = new GClientGeocoder();
  	  showAddress(fullAddress, balloonText);
    }
  }
}

function showAddress(address, balloonText)
{
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          // Crap, it's not there, erase the map!
          document.getElementById("propMap").style.display = 'none';
          document.getElementById("propMapBar").style.display = 'none';
        } else {
          map.setCenter(point, 15);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          marker.bindInfoWindowHtml(balloonText);
        }
      }
    );
  }
}

// AJAX save fave prop object
var ajaxSaveFave = {
	success: function(o) {
		if (this.isDetailsPage == false)
		{
        	document.getElementById('fave' + this.id).innerHTML = o.responseText;
       	} else {
       		document.getElementById('fave' + this.id).innerHTML = '<div style="padding-top: 11px;">'+o.responseText+'</div>';
       	}
	},

	failure: function(o) {
		document.getElementById('fave' + this.id).innerHTML = "Save error!";		
	},
	timeout: 5000,
	id: 0,
	isDetailsPage: true,
	
	go: function(basePath) {
		var urlTarget = basePath + 'property-details/add/' + this.id;
		YAHOO.util.Connect.asyncRequest('POST', urlTarget, this, "");
	}
};
// AJAX call to save property favorite
function saveFave(id, basePath, saveText, isDetailsPage)
{
	document.getElementById('fave' + id).innerHTML = saveText;
	if (isDetailsPage == undefined) ajaxSaveFave.isDetailsPage = false;
	// Call the ajax magic
	ajaxSaveFave.id = id;
	ajaxSaveFave.go(basePath);	
}

// AJAX save search object
var ajaxSaveSearch = {
	success: function(o) {
		if (this.overwriteText == null)
		{
        	document.getElementById(this.targetDiv).innerHTML = o.responseText;
       	} else {
       		document.getElementById(this.targetDiv).innerHTML = this.overwriteText;
       	}
	},

	failure: function(o) {
		document.getElementById(this.targetDiv).innerHTML = "Save error!";		
	},
	timeout: 5000,
	targetDiv: 'searchText',
	overwriteText: null,
	
	go: function(basePath) {
		var urlTarget = basePath + 'properties/save_search';
		YAHOO.util.Connect.asyncRequest('POST', urlTarget, this, "");
	}
};
// AJAX call to save search - no id necessary, we use a CookieForm, yay!
// targetDiv and overwriteText aren't necessary
function saveSearch(basePath, saveText, targetDiv, overwriteText)
{
	if (targetDiv != undefined) ajaxSaveSearch.targetDiv = targetDiv;
	if (overwriteText != undefined) ajaxSaveSearch.overwriteText = overwriteText;
	document.getElementById(ajaxSaveSearch.targetDiv).innerHTML = saveText;
	// Call the ajax magic
	ajaxSaveSearch.go(basePath);	
}