﻿/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}
/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10)
}

/* for other browsers */
window.onload = init;

/* Golbal scoped vars */
var x, y;
var placeHolder ;

/* dom loaded call */
function init() {
// quit if this function has already been called
	if (arguments.callee.done) return;
// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
// kill the timer
	if (_timer) clearInterval(_timer);
  
  if (!document.getElementsByTagName) return false ;
  if (!document.getElementById) return false ;
  if (!document.createElement) return false ;
  if (!document.createTextNode) return false ;
  if (!document.appendChild) return false ;
  
  tooltips() ;
  imageGallery() ;
}

/* Photo Gallery */
function imageGallery() {
  if (getElementsByClass('gallery', null, 'div') == '') return false ;
  var imageGallery = getElementsByClass('gallery',null,'div')[0] ;
  
  // Create place holder image
  placeHolder = document.createElement('img') ;
  placeHolder.className = 'placeHolder' ;
  imageGallery.appendChild(placeHolder) ;
  
  
  // Add click event to each link
  var links = imageGallery.getElementsByTagName('a') ;
  for (i = 0; i < links.length; i++) {
    links[i].onclick = showImage ;
  }
  
  // Set placeholder src
  placeHolder.src = getLarge(links[0].firstChild) ;
  
  // position thumbnails
  thumbs = imageGallery.getElementsByTagName('div')[0] ;
  thumbs.className = 'thumbs' ;
}

function showImage() {
  placeHolder.src = getLarge(this.firstChild) ;
  return false ;
}
function getLarge(sml) {
  lrgSrc = sml.src ;
  lrgSrc = lrgSrc.substring(lrgSrc.lastIndexOf('/'), lrgSrc.lastIndexOf('.')) + 'Lrg.jpg' ;
  lrgDir = sml.src ;
  lrgDir = lrgDir.substring(0, lrgDir.lastIndexOf('/')) + '/Large' ;
  lrgSrc = lrgDir + lrgSrc ;
  return lrgSrc ;
}

/* site plan tooltips */
function tooltips() {
  if (getElementsByClass('sitePlanMap', null, 'map') == '') return false ;
  
  var sitePlan = getElementsByClass('sitePlan',null,'img')[0].setAttribute('alt', '') ;
  var map = getElementsByClass('sitePlanMap')[0] ;
  var areas = map.getElementsByTagName('area') ;
  for (i = 0; i < areas.length; i++) {	
    if (areas[i].getAttribute('title') == null) continue ;
		areas[i].txt = areas[i].getAttribute('title') ;
		areas[i].setAttribute('title', '') ;
    areas[i].onmouseover = toggleTip ;
    areas[i].onmouseout = toggleTip ;
  }
}
function toggleTip(e) {
		
  if (this.tip != null) {
// Remove Tip
    this.parentNode.removeChild(this.tip) ;
		this.tip = null ;
    document.onmousemove = null;
		
  } else {
// Show Tip			
    tip = document.createElement('div') ;
    tip.className = 'toolTip' ;
    tip.style.display = 'none' ;
		
    var lines = this.txt.split('.') ;
    for (i = 0; i < lines.length; i++) {
      var tipText = document.createTextNode(lines[i]) ;
      tip.appendChild(tipText) ; 
      br = document.createElement('br') ; 
      tip.appendChild(br) ;     
    }
    tip.id = 'tip' ;
    this.tip = tip ;
    this.parentNode.appendChild(tip) ;
    document.onmousemove = setXY; 
  }
}
function setXY(e) {
  if (!document.getElementById('tip')) return true ;
  document.getElementById('tip') ;
  x = (window.Event) ? e.pageX + 20 : event.clientX + 20;
  y = (window.Event) ? e.pageY + 20 : event.clientY + 20;
  tip.style.left = x + 'px' ;
  tip.style.top = y + 'px' ;
  tip.style.display = 'block' ;
}

//============ Build flash object =========================//
function build_flash() {
		document.write('<object type="application/x-shockwave-flash" data="/Images/c.swf?path=/Images/Flash/FlashHom.swf" width="606" height="355">') ;
	document.write('<param name="movie" value="/Images/c.swf?path=/Images/Flash/FlashHom.swf" />') ;
	document.write('<img src="/Images/HomeMainImg.jpg alt="" />') ;
	document.write('</object>') ;
}

/* common methods */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}