// Contains standard JavaScript functions used by the website

// Adds an event handler to the onload event of the window object
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

//**************************************************************************************
// Thumb -> Larger Image on Properties Detail
//**************************************************************************************

function imgzoom(imgurl, alt, width, height){

	// You don't normally need to pass width & height as this is re-used by Javascript
	// from the size of the current image displayed -- as they should all be the same
	// size, nothing changes. However, it's possible images uploaded to the site aren't
	// the correct size, so these checks will ensure consistency:
	
	if (width==null) { width=364 };
	if (height==null) { height=273 };
	if (alt==null) { alt="Picture of this property" };

	theImg=document.getElementById("enlargement");

	if (theImg!=null) {
		theImg.setAttribute("src", imgurl);
		theImg.setAttribute("alt", alt);
		theImg.setAttribute("width", width);
		theImg.setAttribute("height", height);
	}
}

//**************************************************************************************
// Functions for homepage Here to Help Panel
//**************************************************************************************

var currentpanel=1;

function panelswap(direction) {
	
	paneltotal = findtotalpanels();
	
	if (paneltotal!=null || paneltotal!=0) {
	
	previouspanel = currentpanel;

	if (direction=="next") {
		currentpanel = currentpanel+1;
		if (currentpanel>paneltotal) { currentpanel = 1; }
	} 
	
	else if (direction=="previous") {
		currentpanel = currentpanel-1;
		if (currentpanel<=0) { currentpanel = paneltotal; }
	}
	
	currentpanelname = "panel"+currentpanel;
	previouspanelname = "panel"+previouspanel;
	
	// alert("Previous:"+previouspanel+"\nCurrent:"+currentpanel);

	document.getElementById(currentpanelname).style.display = 'block';
	document.getElementById(previouspanelname).style.display = 'none';
	}
}

function findtotalpanels() {

var paneltotal = 0;
var panelsID = document.getElementById("listpanels");
var panelchildren = panelsID.childNodes;

    for (i=0; i<panelchildren.length; i++) {
       // alert(panelchildren.item(i).tagName); 
		 if (panelchildren.item(i).tagName=="DIV")
		 	paneltotal++;
	}
	return paneltotal;
} 


//**************************************************************************************
// Homepage Image rotator
//**************************************************************************************

var imagesToRotate = new Array;

imagesToRotate[0] = "images/home-cycle01.jpg";
imagesToRotate[1] = "images/home-cycle02.jpg";
imagesToRotate[2] = "images/home-cycle03.jpg";
imagesToRotate[3] = "images/home-cycle04.jpg";
imagesToRotate[4] = "images/home-cycle05.jpg";
imagesToRotate[5] = "images/home-cycle06.jpg";

var counter = 0;

function rotateImages()
{
	if (imagesToRotate.length > 1)
		{			
		var imageID = document.getElementById("rotator");
		if (imageID)
			{	imageID.setAttribute('src', imagesToRotate[counter]);
				// imageID.setAttribute('alt', imagesAltText[counter]);
				counter ++; }
		if (counter == imagesToRotate.length)
			{ counter = 0; }
		setTimeout("rotateImages()",5000); 
		}
}


//**************************************************************************************
// Bookmark function
//**************************************************************************************

function bookmarkthis(title,url) {
  if (window.sidebar) { // firefox
     window.sidebar.addPanel(title, url, "");
  } else if (document.all) { // IE
     window.external.AddFavorite(url, title);
  } else if (window.opera && window.print) { // opera
     var elem = document.createElement('a');
     elem.setAttribute('href',url);
     elem.setAttribute('title',title);
     elem.setAttribute('rel','sidebar');
     elem.click();
  }
}
