/**
 * @class GE_Main
 * @author ed.hicks@frogdesign.com
 * this class does...
 */
GE_Main = new function() {

	this.initFns = [];
	this.mappedFns = {};

	this.debugEl = null;
	this.debugTxt = "DEBUG BOT v1.3x9b\n";
	this.debug = function(txt) {
		if (this.debugEl == null) {
			this.debugEl = document.body.appendChild( Builder.node("textarea", {id:"debugLog"}) ).setOpacity(.75);
		}

		this.debugTxt += txt + "\n";
		this.debugEl.value = this.debugTxt;

	}

	this.onInit = function() {
		for (var i=0; i<this.initFns.length; i++) {
			this.initFns[i]();
		}
		this.bindFns(document);
	}

	this.addInitFn = function(fn) {
		this.initFns.push(fn)
	}

	this.bindFns = function(el) {
		var mFns = this.mappedFns;
		var all = el.getElementsByTagName('*');
		var len = all.length;
		for (var i=0;i<len;i++) {
		   var el = all[i];
			var cns = el.className.split(" ");
			var j = cns.length;
			while(j--) {
				var fn = mFns[ cns[j] ];
				if (fn != null) {
					new fn(el);
				}
			}
		}
	}

 	this.mapCSSToFn = function(cssClass, fn) {
 		if (this.mappedFns[cssClass]) {
 			
 		}
 		this.mappedFns[cssClass] = fn;
 	}
	//TODO: get rid of all instances of mapFnToCSS across site js (argument order is wrong);
	this.mapFnToCSS = this.mapCSSToFn;


	//to prevent image flicker in IE/6
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}


	 // kudos to Andrea Giammarchi for the "DOMContentLoaded Final Solution."
	 // (C) webreflection.blogspot.com
	window.__onContent__ = this.onInit.bind(this);

	if(document.addEventListener) {
		document.addEventListener("DOMContentLoaded", __onContent__, false);
	}

	if( /WebKit|Khtml/i.test(navigator.userAgent) || (window.opera && parseInt(window.opera.version())<9) ) {
		(function(){
			/loaded|complete/.test(document.readyState) ? window.__onContent__() : setTimeout(arguments.callee, 1);
		})();
	}
	else if (/MSIE/i.test(navigator.userAgent)) {
		document.write("<script defer='defer' src='//:' onreadystatechange='( function(element){if(element.readyState === \"complete\") window.__onContent__();} )(this);'></script>");
	}
}

function GE_containsElement(el1, el2) {
	if (el2==null) return false;
	var pel = el2.parentNode;
	while(pel != null) {
		if (el1==pel) return true;
		pel = pel.parentNode;
	}
	return false;
}
function GE_detectMouseLeave( srcEl, e ) {
	// relTarg is "where the mouse goes to"
	if (!e) var e = window.event;
	var relTarg = e.relatedTarget || e.toElement;

	return ( !GE_containsElement(srcEl, relTarg) );
}
function GE_detectMouseEnter( srcEl, e ) {

	// relTarg is "where the mouse comes from"
	if (!e) var e = window.event;
	var relTarg = e.relatedTarget || e.fromElement;

	return ( !GE_containsElement(srcEl, relTarg) );
}

//Flash related functions ----
function GE_pop(url, name, w, h) {
   window.open(url, name,'height='+h+'px,width='+w+'px,menubar=0,scrollbars=0,titlebar=0,resizable=0,toolbar=0,location=0,status=0,directories=0');
}

//----

// sIFR
var GE_Inspira_Medium = { src: 'http://www.gesensinginspection.com/css/GE_Inspira_Medium.swf' };
var GE_Inspira = { src: 'http://www.gesensinginspection.com/css/GE_Inspira.swf' };
// var GE_Inspira_Medium = { src: GE_baselink + 'GE_Inspira_Medium.swf', wmode: 'transparent'  };
// var GE_Inspira = { src: GE_baselink + 'GE_Inspira.swf', wmode: 'transparent'  };
sIFR.activate(GE_Inspira_Medium, GE_Inspira);

GE_Main.addInitFn(
	function() {
		sIFR.initialize();

		/*flash replace for all other pages*/
		sIFR.replace(GE_Inspira_Medium, { selector: 'h2', wmode: 'transparent', css: {'.sIFR-root': {'color': '#333333','leading':'-1','font-size':'24px'} } });
		sIFR.replace(GE_Inspira, { selector: '#splash_content p', wmode: 'transparent', css: {'.sIFR-root': {'color': '#505060','leading':'8','font-size':'17px'} } });

	}
);




GE_dropdown = function(containerEl,values) {

   //public properties
   this.selectedIndex=-1;
   this.selectedValue=null;
   this.items = null;


   var html='<strong class="selected"></strong><ul>';
   for(var i=0;i<values.length;i++){
      html += '<li><a href="javascript:void(0)">'+values[i]+'</a></li>';
   }
   html += '</ul>';
	new Insertion.Top(containerEl,html);

	this.containerEl = $(containerEl);
	this.titleEl = this.containerEl.down();
	this.ulEl = this.titleEl.next("UL");
	this.items = this.ulEl.immediateDescendants();
	// this.titleLi = this.items.shift(0);
	// this.titleSpan = this.titleLi.down("SPAN");
	this.isOpen = false;
	this.mouseOn = false;

   //dropdown event listeners
   Event.observe(this.containerEl,"click", function() {
      this.toggleSelect();
   }.bind(this));
   Event.observe(this.containerEl,"mouseover", function() {
      this.mouseOn = true;
   }.bind(this));
   Event.observe(this.containerEl,"mouseout", function() {
      this.mouseOn = false;
   }.bind(this));
   Event.observe(document.body,"click", function() {
      if(!this.mouseOn && this.isOpen){
         this.close();
      }
   }.bind(this));

   for(var i=0;i<this.items.length;i++){
      var el = this.items[i];
      el.observe("click", function() {
         this.dd.select( this.idx );
      }.bind( { dd:this, idx:i } ));
   }

   this.select(0);
   this.close();
}

GE_dropdown.prototype.toggleSelect = function() {
   this.isOpen ? this.close() : this.open();
}

GE_dropdown.prototype.close = function() {
   for(var i=0;i<this.items.length;i++){
      this.ulEl.hide();
   }
   this.isOpen = false;
}

GE_dropdown.prototype.open = function() {
   for(var i=0;i<this.items.length;i++){
      this.ulEl.show();
   }
   this.isOpen = true;
}

GE_dropdown.prototype.select = function(idx) {
   this.selectedIndex = idx;
   this.selectedValue = this.items[idx].firstChild.innerHTML;
   this.titleEl.update(this.selectedValue);
   this.onSelect(this);
}
GE_dropdown.prototype.onSelect = function(dd) { };

GE_Main.addInitFn( function() { var imgEl = $('footer').nextSiblings().each( function(el) { el.hide() }) });

document.write('<script type="text/javascript" src="ge_nav.js"></script>');
document.write('<script type="text/javascript" src="ge_overlay.js"></script>');
document.write('<script type="text/javascript" src="ge_audioPlayer.js"></script>');
document.write('<script type="text/javascript" src="search_results.js"></script>');
