// util.js

var isNS4 = (document.layers) ? true : false;
var isIE4 = (document.all && !document.getElementById) ? true : false;
var isIE5 = (document.all && document.getElementById) ? true : false;
var isNS6 = (!document.all && document.getElementById) ? true : false;
var iedom = document.all||document.getElementById ? true: false;
var dom = document.getElementById ? true : false;

	function getElement(elementId) {
		//identify the element based on browser type
		if (isNS4){
			objElement = document.layers[elementId];
		}else if (isIE4) {
			objElement = document.all[elementId];
		}else if (isIE5 || isNS6) {
			objElement = document.getElementById(elementId);
		}
		return objElement;
	}
	
	
	var fadeTime = 1000;
	var curcanvas="canvas0";
	var lastcanvas;
	var curimageindex=0;
	var preloadedimages=new Array();
	var fadeimages;
	var fadelinks;
 
	function faderPreLoadImages() { 
		for (p=0;p<fadeimages.length;p++){ 
			preloadedimages[p]=new Image(); preloadedimages[p].src=fadeimages[p];
		}
	}
  
	function rotateimage() {
		var newObj = getElement(curcanvas);

		if (typeof( lastcanvas ) != 'undefined') {
			var lastObj = getElement(lastcanvas);
			if (lastObj.style.zIndex < 100) {
				lastObj.style.zIndex = 900;
			}
			newObj.style.zIndex = lastObj.style.zIndex - 1;
		}
		var imgString = '<img src="'+fadeimages[curimageindex]+'">';


		if (fadelinks) {
			
			newObj.innerHTML = '<a href="' + fadelinks[curimageindex] + '">' + imgString + '</a>';
		}
		else {
			newObj.innerHTML = imgString;
		}
		
		Show(curcanvas);
		if (lastcanvas != null) {
			Hide(lastcanvas, fadeTime);
		}
		
		lastcanvas = curcanvas;
		curcanvas=(curcanvas=="canvas0")? "canvas1" : "canvas0";

		if (pause > 0)
		{
			curimageindex=(curimageindex + 1) % fadeimages.length;
			setTimeout("rotateimage()", fadeTime + pause);
		}
	}
	

	 
	function startFader() {

		// check that images are loaded
		for (p=0;p<fadeimages.length;p++){ 
			if (!preloadedimages[p].complete)
			{
				setTimeout("startFader()", 300);
				return;
			}
		}

		rotateimage();
		var houseOuterObj = getElement('houseOuter');
		if (houseOuterObj != null)
		{
			houseOuterObj.style.backgroundColor = 'white';
		}
	}
  
	function showPic(i)
	{
		curimageindex = i;
		if (lastcanvas != null)
		{
			Hide(lastcanvas);
			lastcanvas = null;
		}
		rotateimage();
	}
  

	function writeCanvasDivs()
	{
		document.write('<div id="canvas0" style="z-index:900;text-align:center;vertical-align:middle;position:absolute;width:'+slideshow_width+';height:'+slideshow_height+';filter:alpha(opacity=10);-moz-opacity:10"></div>');
		document.write('<div id="canvas1" style="z-index:901;text-align:center;vertical-align:middle;position:absolute;width:'+slideshow_width+';height:'+slideshow_height+';filter:alpha(opacity=10);-moz-opacity:10"></div>');
	}

 
  
	function Hide(id) { Hide(id,0); }
	function Hide(id, time)
	{
		if (time == 0) {
			var d = document.getElementById(id);
			d.style.display = 'none';
			changeOpac (0, id);
		} else {
			opacity(id, 100, 0, time);
		}
	}
	function Show(id) { Show(id,0); }
	function Show(id, time)
	{
		var d = document.getElementById(id);
		d.style.display = 'block';
		if (time == 0) {
			changeOpac (100, id);
		} else {
			opacity(id, 0, 100, time);
		}
	}
  
	function opacity(id, opacStart, opacEnd, millisec) {
      //speed for each frame
      var speed = Math.round(millisec / 100);
      var timer = 0;
  
      //determine the direction for the blending, if start and end are the same nothing happens
      if(opacStart > opacEnd) {
          for(i = opacStart; i >= opacEnd; i--) {
              setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
              timer++;
          }
      } else if(opacStart < opacEnd) {
          for(i = opacStart; i <= opacEnd; i++)
              {
              setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
              timer++;
          }
      }
      if (opacEnd == 0) {
          setTimeout("Hide('" + id + "', 0)",(timer * speed));
      }
	}
  
	//change the opacity for different browsers
	function changeOpac(opacity, id) {
		var object = document.getElementById(id).style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}

	function popup(url) { 
		window.open(url, "", "fullscreen=no,toolbar=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,directories=yes,location=yes,width=" + (screen.width - 100) + ",height=" + (screen.height - 100) + ",left=50,top=50"); 
	}
	function popup2(url,width,height) { 
		window.open(url, "", "fullscreen=no,toolbar=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,directories=yes,location=yes,width=" + width + ",height=" + height + ",left=50,top=50"); 
	}
	function popupMin(url,width,height) { 
		window.open(url, "", "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=" + width + ",height=" + height + ",left=50,top=50"); 
	}
	function popupMinResize(url,width,height) { 
		window.open(url, "", "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=yes,directories=no,location=no,width=" + width + ",height=" + height + ",left=50,top=50"); 
	}

	function toggle(strDivName) {
		var objElement = getElement(strDivName);
	
		if (objElement.style.display == 'block') {
			objElement.style.display = 'none';
		} else {
			objElement.style.display = 'block';
		}
	}
	
	
  	function getElementsByClassName(ClassName,tagName,parentElement){
		var elements=new Array();
		var d=parentElement ? parentElement : document;
		var allElements;
		
		if(tagName)	
			allElements = d.all && d.all.tags(tagName) 
				|| d.getElementsByTagName && d.getElementsByTagName(tagName);
		else 
			allElements=d.all || d.getElementsByTagName("*");
		
		for(var i=0,len=allElements.length; i<len; i++)
			if(allElements[i].className==ClassName)
				elements[elements.length]=allElements[i];
				
		return elements;
	}
	
	function tblAltRows(table, firstCss, secondCss, start, inc) {
  		if (!start) start = 1;
  		if (!inc) inc = 1;
  		if (!firstCss) firstCss = 'odd';
  		if (!secondCss) secondCss = 'even';

		var color = 0;
		var i;
		for(i = start; i < table.rows.length; i = i+inc) {
			if (table.rows[i].style.display != 'none') {
				if (color == 0) {
					table.rows[i].className = firstCss;
					color = 1;
				} else {
					table.rows[i].className = secondCss;
					color = 0;
				}
			}
		}
	}
	
  	function altRowsByClass(classname, firstCss, secondCss, start, inc) {
		var elements = getElementsByClassName(classname, 'table') ;
		for (t = 0; t < elements.length; t++) {
			tblAltRows(elements[t], firstCss, secondCss, start, inc);
		}
	}
	
	
	function eraseCombo(cmb) {
		for (var i = cmb.options.length; i >=0; i--) 
			cmb.options[i] = null;
	}
	function getSelectedVal(htmlSelectName) {
		var htmlSelect = getElement(htmlSelectName);
		return htmlSelect.options[htmlSelect.selectedIndex].value;
	}
	
	function setIndexByVal(htmlSelectName, val) {
		var htmlSelect = getElement(htmlSelectName);
		for (var i = 0; i < htmlSelect.options.length; i++) {
			if (htmlSelect.options[i].value == val) {
				htmlSelect.selectedIndex = i;
				return i;
			}
		}
		// set to 0
		htmlSelect.selectedIndex = 0;
		return 0;
	}
	
	function addImageToElement (element, url) {
		var element = getElement(element);
		var img = document.createElement("IMG");
		img.src = url;
		element.appendChild(img);
	}
	
	function enlargeImg(imgId)
	{
		var i = getElement(imgId);
		i.width = i.width + 10;
		i.style.margin = "0px";
	}
	
	function normalSizeImg(imgId)
	{
		var i = getElement(imgId);
		i.width = i.width - 10;
		i.style.margin = "5px";
	}
	
//Added by TWA for 'Open In New Window' script
function addEvent(obj, type, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(type, fn, false);
	} else if (obj.attachEvent)	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn](window.event); }
		obj.attachEvent("on"+type, obj[type+fn]);
	}
}
function removeEvent(obj, type, fn) {
	if (obj.removeEventListener) {
		obj.removeEventListener(type, fn, false);
	} else if (obj.detachEvent)	{
		obj.detachEvent("on"+type, obj[type+fn]);
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

/* Create the new window */
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {
		// Change "_blank" to something like "newWindow" to load all links in the same new window
	    var newWindow = window.open(this.getAttribute('href'), '_blank');
		if (newWindow) {
			if (newWindow.focus) {
				newWindow.focus();
			}
			return false;
		}
		return true;
	}
}

/* Add the openInNewWindow function to the onclick event of links with a class name of "new-window" */
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Change this to the text you want to use to alert the user that a new window will be opened
		var strNewWindowAlert = "Opens in a New Window";
		// Find all links
		var links = document.getElementsByTagName('a');
		//var objWarningText;
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "new-window"
			if (/\bnew\-window\b/.test(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				//objWarningText = document.createElement("em");
				//objWarningText.appendChild(document.createTextNode(strNewWindowAlert));
				//link.appendChild(objWarningText);
				link.alt = strNewWindowAlert;
				link.title = strNewWindowAlert;
				link.onclick = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

addEvent(window, 'load', getNewWindowLinks);
/*
--End Temporary Removal.  Put this code back if the new version doesn't work.
--New version Starts directly below this. */
/*
//Added by TWA for 'Open In New Window' script
var JSTarget = {
	
}
JSTarget.addEvent(window,'load',function() {JSTarget.init("rel","external","(external website, opens in a new window)");});
*/