/* Function to addEventListener to onload
 * @param func - a function which should be executed once the page has loaded
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * it will work even if something has previously been assigned to window.onload
 * without using addLoadEvent itself. 
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


/*
 * rDropDown
 * Adds class of 'hover' to LI onmouseover. Removes class onmouseout. Adds iFrame fix for IE
 */
function rDropDown() {

	this.initialize();
}
rDropDown.prototype = {
	open: false,
	timeout: false,
	openLi: null,
	initialize: function() {
	    var userAgent = navigator.userAgent.toLowerCase()
        if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1)
            jQuery("#TopNav > ul").addClass('macFF');
            
		var lis = jQuery("#TopNav > ul > li");
		for (var i=0; i<lis.length; i++) {
			jQuery(lis[i]).bind('mouseover', {parentThis: this, li:lis[i]}, function(params) {
				params.data.parentThis.show(params.data.li);
			});
			jQuery(lis[i]).bind('mouseout', {parentThis: this, li:lis[i]}, function(params) {
				params.data2 = params.data;
				params.data.parentThis.timeout = setTimeout(function() {
					params.data = params.data2;
					params.data.parentThis.hide(params.data.li);
				}, 1);
			});
		}
	},
	show: function(li) {
		if (this.openLi && this.openLi != li) {
			this.hide(this.openLi);
		}
		if(this.timeout){
			clearTimeout(this.timeout);
			this.timeout = false;
		}
		if(this.open){
			return;
		}
		if (jQuery(li).hasClass('hasSub')) {
			jQuery(li).addClass('hoverSub');
		}
		else {
			jQuery(li).addClass('hover');
		}
		this.openLi = li;
		this.open = true;
		this.iframeFix(li);
	},
	hide: function(li) {
		if(!this.open){
			return;
		}
		jQuery(li).removeClass('hover');
		jQuery(li).removeClass('hoverSub');
		this.open = false;
		if (this.iframe) {
			this.iframe.style.display = "none";
		}
	},
	iframeFix: function(li) {
		if(!document.all) {
			//return;
		}
		
		var subnav = jQuery('div.subnav', li)[0];
		if (subnav) {
			if (!this.iframe) {
				this.iframe = document.createElement('iframe');
				this.iframe.style.position = 'absolute';
				this.iframe.frameBorder = 0;
				this.iframe.style.filter = 'alpha(opacity=0)';
				this.iframe.style.zIndex = -1;
				document.body.appendChild(this.iframe);
			}
			this.iframe.style.display = "block";
			this.iframe.style.top = jQuery(subnav).offset().top + 'px';
			this.iframe.style.left = jQuery(subnav).offset().left + 'px';
			this.iframe.style.width = jQuery(subnav).width() + 'px';
			this.iframe.style.height = jQuery(subnav).height() + 'px';
		}
	}
}

/*/ Hack thickbox to have a callback for tb_showIframe (called when iframe contents are ready to show)
tb_showIframe2 = tb_showIframe;
window.tb_showIframe = function() {
	var a = arguments;
	tb_showIframe2.apply(this, a);
	setTimeout(tb_restrictTabOrder, 100);
	// tb_restrictTabOrder();
}

// Hack thickbox to have a callback for tb_remove
tb_remove2 = tb_remove;
window.tb_remove = function() {
	var a = arguments;
	tb_remove2.apply(this, a);
	tb_unrestrictTabOrder();
}
*/
/*
 * onLoad functions
 * Initializes our functions on page load
 */
jQuery(document).ready(function(){
	new rDropDown();
   
});


