 /* requires jquery */

var gReady = false; 

function doEffects(noDelay) {
	if (!gReady) { 
		if (location.href.indexOf("editor/editor") == -1 && $("#header.home").length) { 
			docReady(); 
		} else { /* editor or not home page */
			return;  
		} 
	}
	function effect3() {
		$("#header img").slideDown("slow");
	} 
	function effect2() {
		$("#logoBand").animate( {top:49,height:85}, 1700, effect3 );
	} 
	function effect1() {
		$("#logoBand").fadeTo("slow", 0.5, effect2); 
	}
	if (noDelay) { effect1(); }
	else { setTimeout(effect1, 600); }
}

function docReady() {
	//should run before onload
	if (location.href.indexOf("editor/editor") != -1) { 
   		//$(document.body).addClass("editorOn");
		document.body.className = "editorOn";
	} else {
		if ($("#header.home").length) { //if we're on the home page
			if (gReady) { return; /* in case called twice */ }
			$("#header img").hide();
			$("#logoBand").addClass("start");
			gReady = true;
			if ($.browser.safari) { doEffects(true); }
		} else if (screen.availHeight < 769) { //not on home page and small screen
      $("#header").addClass("short");
    }
    fixLinks();
		if ($.browser.msie) { 
			function resize() {
				if (document.body.offsetWidth % 2 != 0) {
					window.resizeBy(1,0); //fixes rendering bug
				}
			};
			resize();
      window.onresize = resize;
		}
   }
   var y = new Date().getFullYear();
   if (y > 2007) { 
		$("#year")[0].innerHTML = y;
   }
}

function fixLinks() {
  var i, lnk, url, domain = location.host, rxPdf = /\.pdf$/i; 
  for (i = document.links.length; --i > -1; ) {
    lnk = document.links[i];
    if (lnk.target != "" || lnk.href.indexOf("http") != 0) { 
    	//it seems IE sometimes does not return complete URLs before onload complete? or when links altered by js?
    	continue; 
    }
    if ( typeof lnk.onclick != "function" && (lnk.href.indexOf(domain) == -1 || rxPdf.test(lnk.href)) ) { //external link
      lnk.target = "_blank";
      lnk.title = "Opens in new window";
    } 
  }
}

function verifyContact(form) {
	if (form.EO_Message.value.replace(/^\s+/,"").replace(/\s+$/,"") == "") {
		alert("The message field is empty.");
		return false;
	}
	if (form.Comments.value != "") { 
		//reject spam submissions in hidden fields
		return false; 
	}
	alert("Thank you. Your message will be sent to Elevator One and then you will be returned to the home page.");
	return true;
}

window.onload = doEffects;

$(document).ready( function() {  
	if ($.browser.safari) { //avoid FOUC - may be fixed in new safari late 2007
		window.onload = docReady;
	} else {
		docReady();
	}
});
