$(function() {
	// Navigation rollovers - http://greg-j.com/static-content/hover-fade-redux.html
	$('.fadeNav').append('<span class="hover"></span>').each(function () {
	  var $span = $('> span.hover', this).css('opacity', 0);
	  $(this).hover(function () {
	    $span.stop().fadeTo(200, 1);
	  }, function () {
	    $span.stop().fadeTo(200, 0);
	  });
	});

        // Rounded corners - small grey
        $('.roundGrey').corner({
          tl: { radius: 4 },
          tr: { radius: 4 },
          bl: { radius: 4 },
          br: { radius: 4 }});
});

$(document).ready(function() {
	jQuery(".slideshow .dashboard .back").click(function(event){
		var imageToHide = jQuery(this).parent().parent().children(".image[show=1]");
		var imageCount 	= jQuery(this).parent().parent().children(".image").length;
		var display 	= jQuery(this).parent().parent().children(".dashboard").children(".display").children("span");
		var curImageNum = jQuery(imageToHide).attr("imageNum");

		var newImageNum = curImageNum - 1;
		if(newImageNum == 0)
			newImageNum = imageCount;

		var imageToShow = jQuery(this).parent().parent().children(".image[imageNum="+newImageNum+"]");

		jQuery(imageToHide).attr("show","0");
		jQuery(imageToHide).fadeOut("fast",function(){
			jQuery(imageToShow).attr("show","1");
			jQuery(imageToShow).fadeIn("fast");
		});
		if (!isNaN(newImageNum)) jQuery(display).html(newImageNum);
	});

	jQuery(".slideshow .dashboard .forward").click(function(event){
		var imageToHide = jQuery(this).parent().parent().children(".image[show=1]");
		var imageCount 	= jQuery(this).parent().parent().children(".image").length;
		var display 	= jQuery(this).parent().parent().children(".dashboard").children(".display").children("span");
		var curImageNum = jQuery(imageToHide).attr("imageNum");

		var newImageNum = parseInt(curImageNum)+1;
		if(newImageNum > imageCount)
			newImageNum = 1;

		var imageToShow = jQuery(this).parent().parent().children(".image[imageNum="+newImageNum+"]");

		jQuery(imageToHide).attr("show","0");
		jQuery(imageToHide).fadeOut("fast",function(){
			jQuery(imageToShow).attr("show","1");
			jQuery(imageToShow).fadeIn("fast");
		});
		if (!isNaN(newImageNum)) jQuery(display).html(newImageNum);
	});
});

// Set top and sometimes left nav
function setNav(topNav, sideNav)
{
	if (topNav != undefined) {
		if (document.getElementById(topNav)) {
			document.getElementById(topNav).style.backgroundPosition = '0 -60px';
		}
	}
	if (sideNav != undefined) {
		if (document.getElementById(sideNav)) {
			document.getElementById(sideNav).className = 'current';
		}
	}
}

/*********************/

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


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

// For popup thing
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 isIE6()
{
	//Detect <= IE6
	version=0;
	if (navigator.appVersion.indexOf("MSIE")!=-1) {
		temp=navigator.appVersion.split("MSIE");
		version=parseFloat(temp[1]);
		if (version <= 6)
		{
			return true;
		}

	}
	return false;
}

/*** Property functions ***/
function loadGoogleMap(lat, lon)
{
  if (GBrowserIsCompatible())
  {
    var width  = 196;
    var height = 168;

    map = new GMap2(document.getElementById("propMap"), { size: new GSize(width,height) } );
    map.setUIToDefault();

    map.setCenter(new GLatLng(lat, lon), 11);
    var marker = new GMarker(map.getCenter());
    map.addOverlay(marker);
  }
}
