
// global variables
var TIMEOUT_ID;
/* single out IE through feature detection */
var isActiveX = (window.ActiveXObject) ? true : false;

// Retrieve the first ancestor of a given element that matches the given 
// tag name.
function getAncestorByTagName(obj, tag) {
    var s = obj ? obj : null;
    if (s == null) return s;
    var tagName;
    do {
        s  = s.parentNode;
        tagName = s.tagName.toLowerCase();
        if (tagName=='body') return null;
    }
    while (tagName != tag);
    return s;   
}

document.getAncestorByClassName = function(obj, className) {
	var s = obj ? obj : null;
    if (s == null) return s;
    var tagName;
    do {
        s = s.parentNode;
        tagName = s.tagName.toLowerCase();
        if (tagName == "body") return null;
    }
    while (!Element.hasClassName(s, className) );
    return s;
}

function childWindowHandler(url,w,h,t,l,mb,tb,lb,sb,r,disclaimer) {
	this.url = url;
	this.w = w ? w : screen.width - "50";
	this.h = h ? h : screen.height - "150";
	this.l = l ? l : (screen.width - this.w) / 2;
	this.t = t ? t : (screen.height - this.h) / 2; 
	this.mb = mb ? mb : "no";
	this.tb = tb ? tb : "no";
	this.lb = lb ? lb : "no";
	this.sb = sb ? sb : "yes";
	this.r = r ? r : "yes";
	this.disclaimer =  disclaimer ? disclaimer : "";
	
	this.newWinName = "appWin";
	this.setWindow = function(name) {
		if(name) this.newWinName = name;
		
		if(this.newWinName.indexOf("opener") != -1){
			window.opener.location = this.url;
			window.close();
		}else{
			
			if (this.disclaimer.length > 1) {
				alert(this.disclaimer);
				}
			this.newWin = window.open(this.url, this.newWinName, "menubar=" + this.mb + ", toolbar=" + this.tb + ",location=" + this.lb + ",resizable=" + this.r + ",scrollbars=" + this.sb + ",left=" + this.l + ",top=" + this.t + ",width=" + this.w + ",height=" + this.h);
			this.newWin.focus();
			try{
				this.newWin.resizeTo(this.w ,this.h);
				}
			catch(e){
			}
		}
	}
}

childWindowHandler.prototype.setURL = function(el,w,h,l,t,mb,lb,sb,r,useTarget,newWinName,disclaimer) {
	this.url = el.href ? el.href : "";
	var i, propName;
	if (el.rel) {
		for (i in WINPROP) {
			if (el.rel.toUpperCase().indexOf(i) != -1) {
				propName = i;
				break;
			}
		}
	}
	if (el.action) {
		for (i in WINPROP) {
			if (el.className.toUpperCase().indexOf(i) != -1) {
				propName = i;
				el.target= this.newWinName;
				break;
			}
		}
	}
	this.w = w ? w : WINPROP[propName].width ? WINPROP[propName].width : "475";
	this.h = h ? h : WINPROP[propName].height ? WINPROP[propName].height : "470";
	this.l = l ? l : WINPROP[propName].left ? WINPROP[propName].left : (screen.width - this.w) / 2;
	this.t = t ? t : WINPROP[propName].top ? WINPROP[propName].top : (screen.height - this.h) / 2;
	this.mb = mb ? mb : WINPROP[propName].menubar ? WINPROP[propName].menubar : "no"
	this.lb = lb ? lb : WINPROP[propName].location ? WINPROP[propName].location : "no"
	this.sb = sb ? sb : WINPROP[propName].scrollbar ? WINPROP[propName].scrollbar : "yes"
	this.r = r ? r : WINPROP[propName].resizable ? WINPROP[propName].resizable : "yes"
	
	this.disclaimer = disclaimer ? disclaimer : WINPROP[propName].disclaimer ? WINPROP[propName].disclaimer : ""	
	
	newWindow.newWinName = newWinName ? newWinName : WINPROP[propName].newWinName ? WINPROP[propName].newWinName : "appWin"

	if (useTarget) {
		this.setWindow(el.target);
	//	el.target = "appWin";
	} else {
		this.setWindow();
	}
}

var newWindow = new childWindowHandler();

function setOnClick() {
	if (!document.getElementsByTagName) return;
	var i, j;
	var anchors = document.getElementsByTagName("a");	
	for (i = anchors.length; i != 0; i--) {
		var a = anchors[i-1];
		if(a.href && a.href.indexOf("#close")!=-1){
			a.onclick = function() { window.close(); };
			continue;
		}
		if (!a.href || !a.rel) continue;
		for (j in WINPROP) {
			if (a.rel.toUpperCase().indexOf(j) != -1) {
				a.title += "[opens in a new window]";
				a.onclick = function() { newWindow.setURL(this);return false; };
				break;
			}
		}
	}	
}

var navEffect = null;
var sitenavOn= false;
var height;
var currentOn = null;

var duringUp = false; 
var duringDown = false;

function hideMenu() {
	if (currentOn != $('sitenav') && currentOn != $('subsitenav') && !duringDown && sitenavOn) {
		sitenavOn = false;
		navEffect = Effect.BlindUp('subsitenav',{duration:1.0});
	}
}
	
function showMenu() {
	try {
		if (currentOn == $('sitenav')  ) {
			if (!duringUp && !sitenavOn) {
				navEffect = Effect.BlindDown('subsitenav',{duration:1.0}, height);
				sitenavOn= true;
			}
		}
	 } catch(e) {}
}
	
function setNavEffect(){
	if (document.body.id=="home") {
		$('sitenav-home').getElementsByTagName("a")[0].onclick = function(){return false;};
		//$('logo').getElementsByTagName("a")[0].onclick = function(){return false;};
	}
	
	height = $('subsitenav').clientHeight;
	Event.observe($(document), 'mouseover', follow, false);
	Event.observe($('sitenav'), 'mouseover', function(){setTimeout('try{showMenu();}catch(e){}',400);}, false);
	Event.observe($('sitenav'), 'mouseout', function(){setTimeout('try{hideMenu();}catch(e){}',300);}, false);
	Event.observe($('subsitenav'), 'mouseout', function(){setTimeout('try{hideMenu();}catch(e){}',300);}, false);
	
		$$('#sitenav li').each(function(item) {
			Event.observe($(item), 'mouseover', function(){ $("sub"+item.id).className +=" over";}, false);
			Event.observe($(item), 'mouseout', function(){ $("sub"+item.id).className=$("sub"+item.id).className.replace("over","");$("sub"+item.id).className=$("sub"+item.id).className.replace("  ","");}, false);
		});	
	
		$$('#subsitenav div').each(function(item) {
			Event.observe($(item), 'mouseover', function(){ $(item).className +=" over";$(item.id.replace('sub','')).className +=" over";}, false);
			Event.observe($(item), 'mouseout', function(){ $(item).className=$(item).className.replace("over","");$(item).className=$(item).className.replace("  ","");$(item.id.replace("sub","")).className=$(item.id.replace("sub","")).className.replace("over","");$(item.id.replace("sub","")).className=$(item.id.replace("sub","")).className.replace("  ","");}, false);
		});
	
	}
	
	follow = function(evt){
		var targetEl = evt.target ? evt.target : evt.srcElement;
		while(targetEl.parentNode != "undefined" && targetEl.parentNode != null) {
			targetEl = targetEl.parentNode;
			if(targetEl.id && (targetEl.id=='sitenav'||targetEl.id=='subsitenav') ){
				break;
		}
	}
	currentOn = targetEl;
		
}
	
var initNavEffect = new domFunction(function() {
		setNavEffect(); 
	}, { 'subsitenav' : 'id' } );

var initOnClick = new domFunction(function() {
		setOnClick();
	}, { 'footer' : 'id' } );
	
var targetEl="";

if(/Firefox\/1.0/.test(navigator.userAgent)){
	Event.observe(document, "mousemove", function(evt){targetEl = evt.target ? evt.target : evt.srcElement;},false);
}
