// For main nav movie - sometimes this isn't always set
// To toggle a button in our main nav as active, just set this var to 1, 2...6
var curNav = 0;

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

function rollout(target)
{
	newImg = target.src.replace(/\_over/g, "");
	target.src = newImg;
}

// 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];}}
}

// Select an item in the amenities nav menu and kill its rollover
function rolloverNavSelect(navId)
{
	target = document.getElementById(navId);
	newImg = target.src.replace(/\.(.{3,4})$/g, "_over.$1");
	target.src = newImg;
	target.onmouseover = function() {};
	target.onmouseout = function() {};
}

// Slide open a div w/ scriptaculous
// Note that we need to include scriptaculous and prototype before trying this
function slideOpen(target)
{
    Effect.toggle(target, 'slide');
}

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

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

function setPostcard(num)
{
     document.getElementById('postcard').value = num;
}

function checkTextareaLimit(target, max)
{
     if (target.value.length > max)
     {
         target.value = target.value.substr(0, (target.value.length-1));
     } else {
         return true;
     }
}

/*** 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)
{
    var error = "";
    if ((val == "" || val.length < 3) ||
        (val.indexOf("@") == "-1") ||
        (val.indexOf(".") == "-1"))
    {
        error = "Please enter a valid email address.\n";
    }

    return error;
}

// If this is Other, Broker/Realtor or Referral, open up a box and give it a proper label
// Otherwise, close that box
function checkHowFound(val)
{
	var titleText = document.getElementById('howFoundDetailTitle');
	var openBox = document.getElementById('howFoundOpen');
	switch(val)
	{
		case "Other Internet Advertising":
			titleText.innerHTML = 'Other:*';
			if (openBox.style.display == 'none') slideOpen('howFoundOpen');
			break;
		case "Broker/Realtor":
			titleText.innerHTML = 'Who:*';
			if (openBox.style.display == 'none') slideOpen('howFoundOpen');
			break;
		case "Referral":
			titleText.innerHTML = 'Referral:*';
			if (openBox.style.display == 'none') slideOpen('howFoundOpen');
			break;
		default:
			openBox.style.display = 'none';
			document.getElementById('how_found_other').value = '';
			break;
	}
}

/*** Music player functions ***/

// Music player close
function openMusic(path)
{
    //alert(URLEncode(top.window.location));
    top.window.location = path + 'homepage?ref=' + URLEncode(top.window.location);
}

// Music player close
function closeMusic()
{
    basicURL = top.window.frames[1].location;
    top.window.location = basicURL;
}

/*** Gallery text swap ***/
function swapText(target, text)
{
  document.getElementById(target).innerHTML = text;
}


// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that
// (a) you leave this copyright notice intact, and
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
function URLEncode(input)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = input + "";
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function sizeBlanket()
{
	var width  = jQuery(window).width();
	var height = jQuery(window).height();

	if(jQuery(document).height() > height)
		height = jQuery(document).height();
		
	jQuery("#blanket").css({width: width+'px', height: height+'px'});
}

function placePopup(id)
{
	var pageHeight   = jQuery(window).height();
	var popupHeight  = parseInt(jQuery("#"+id).css("height"));
	var topPosition = parseInt((pageHeight-popupHeight)/2);
	
	jQuery("#"+id).css("margin-top",topPosition+"px");
}

function showBlanket()
{
	//$.cookie("alreadyShownPopup","0");
	if($.cookie("alreadyShownPopup") != "1")
	{
		jQuery("#blanket").fadeIn(250);
		$.cookie("alreadyShownPopup","1");
	}	
}
function hideBlanket(){ jQuery("#blanket").fadeOut(250); }

jQuery(document).ready(function(){	
	jQuery('.hover').hover(
		function(){ jQuery(this).css("cursor","pointer"); },
		function(){ jQuery(this).css("cursor","default"); }
	);
});