/* ----------------
	GLOBAL functions
------------------*/

// use $ as shortcut to getElementByID, ex: $('id_name')
var $ = YAHOO.util.Dom.get;

// browser check
function Browser() {
  var ua, s, i;
  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
	this.isSF		 = false;	 // include check specifically for Safari to address flash bug
  this.version = null;
  ua = navigator.userAgent;
  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
	// treat all others as 
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
	  if (ua.indexOf("Safari") != -1) {
    	this.isSF = true;
 		}
    this.isNS = true;
    this.version = 6.1;
    return;
  }
  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
}
var browser = new Browser();

// The right way to add load events. By Simon Willison
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


// function to open external links in new window
function openInNewWin() {
	var newWindow = window.open(this.getAttribute('href'), 'newWindow');
	newWindow.focus();
	return false;
}

// function to set external links opening option
function setExtLinks(){
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "ext"){
			newImg = document.createElement('img');
			newImg.setAttribute('src','/images/arrow_ext.gif');
			newImg.setAttribute('alt',' [external link]');
			newImg.setAttribute('title','external link');
			newImg.setAttribute('height','10');
			newImg.setAttribute('width','14');
			anchor.appendChild(newImg);
			anchor.onclick = openInNewWin;
		}
	}
}

// set IE 6 login button hover state
function setLoginForm(){
	if (document.all && document.getElementById) {
		gBtn = $('btn_go');
		gBtn.onmouseover = function(){
			YAHOO.util.Dom.replaceClass(gBtn, "goOff", "goOn");
		}
		gBtn.onmouseout = function(){
			YAHOO.util.Dom.replaceClass(gBtn, "goOn", "goOff");
		}
	}
	$('username').focus();
}





// function to setup show/hide text blocks
// expects 7 page set variables:

// mRoot: name of div that represents root node of collection
// mParentClass: class of parent element that is clicked to activate show/hide
// mParentShowClass: class name that sets open style of parent element
// mParentHideClass: class name that sets closed style of parent element
// mParentHasRollover: boolean to determine if parents have rollver class
// mParentRolloverClass: class name of rollover parent state
// mChildClass: class of child element that encloses content to show/hide
// mChildShowClass: class name that sets child display:block
// mChildHideClass: class name that sets child display:none;

function setMoreLinks() {
	if (!document.getElementsByTagName) return;
	// set up onclick handlers
	var parents = YAHOO.util.Dom.getElementsByClassName(mParentClass, '', $(mRoot));
	//alert(parents.length);
	for (i=0; i < parents.length; i++){
		// add onclick handler to parent elements
		parents[i].onclick = function(){
			showHideText(this);
		}
		// if rollover state, add it
		if (mParentHasRollover) {
			parents[i].onmouseover = function(){
				doRollovers(this);
			}
			parents[i].onmouseout = function(){
				doRollovers(this);
			}
		}
		// set parent elements to hidden display state (arrow up) if not explicitly set stay open
		if (mParentHideClass && mParentHideClass != "" && mParentShowClass && !YAHOO.util.Dom.hasClass(parents[i], mParentShowClass)){
			YAHOO.util.Dom.addClass(parents[i],mParentHideClass);
		}
	}
	// hide all child content blocks if not explicitly set to stay open
	var children = YAHOO.util.Dom.getElementsByClassName(mChildClass, '', $(mRoot));
	for (j=0; j < children.length; j++){
		if (!YAHOO.util.Dom.hasClass(children[j], mChildShowClass)){
			YAHOO.util.Dom.addClass(children[j],mChildHideClass);
		}
	}
	// display expand all/collapse all link
	YAHOO.util.Dom.replaceClass($('expander'), 'displayOff', 'displayOn');
	
	// set onclick action for expand all/collapse all button
	setShowHideAll();
}

// function to do rollovers for parent items
function doRollovers(pObj){
	if (YAHOO.util.Dom.hasClass(pObj, mParentRolloverClass)){
			YAHOO.util.Dom.removeClass(pObj, mParentRolloverClass);
	} else {
			YAHOO.util.Dom.addClass(pObj, mParentRolloverClass);
	}
}

// function to show/hide text blocks
function showHideText(pObj){
	node = pObj;
	foundChild = 0;
	// look through siblings
	while (node.nextSibling){
		node = node.nextSibling;
		if (node.nodeType == 1){
			if (YAHOO.util.Dom.hasClass(node, mChildClass)){
				foundChild = 1;
				break;
			}
		}
	}
	// if we had to back up and look through children
	/*
	possibleKids = YAHOO.util.Dom.getElementsByClassName(mChildClass, '', node);
		if (possibleKids.length > 0){
			alert(possibleKids[0].getAttribute('id'));
			break;
		} else {
			node = node.nextSibling;
		}
	}
	*/
	
	if (YAHOO.util.Dom.hasClass(node, mChildHideClass)){
		YAHOO.util.Dom.replaceClass(node, mChildHideClass, mChildShowClass);
		YAHOO.util.Dom.replaceClass(pObj, mParentHideClass, mParentShowClass);
	} else {
		YAHOO.util.Dom.replaceClass(node, mChildShowClass, mChildHideClass);
		YAHOO.util.Dom.replaceClass(pObj, mParentShowClass, mParentHideClass);
	}
}


// set up expand all / collapse all buttons
function setShowHideAll(){
	if (!document.getElementById || !$("expander")) return;
	var exLinks = $('expander').getElementsByTagName("a");
	if (exLinks.length > 0){
		exLinks[0].onclick = function(){
			//YAHOO.util.Dom.addClass(exLinks[0],"expand");
			return showHideAll(this);
		}
	}
}

// function to show/hide all
function showHideAll(theLink){
	parents = YAHOO.util.Dom.getElementsByClassName(mParentClass, '', $(mRoot));
	chilren = YAHOO.util.Dom.getElementsByClassName(mChildClass, '', $(mRoot));
	
	// if currently displaying "expand all"...
	if (YAHOO.util.Dom.hasClass(theLink, "expand")){
		for (i=0; i < parents.length; i++){
			YAHOO.util.Dom.replaceClass(parents[i], mParentHideClass, mParentShowClass);
		}
		for (i=0; i < chilren.length; i++){
			YAHOO.util.Dom.replaceClass(chilren[i], mChildHideClass, mChildShowClass);
		}
		// change button display to "collapse all"
		YAHOO.util.Dom.replaceClass(theLink, 'expand', 'collapse');
		
	// if currently displaying "collapse all"...
	} else {
		for (i=0; i < parents.length; i++){
			YAHOO.util.Dom.replaceClass(parents[i], mParentShowClass, mParentHideClass);
		}
		for (i=0; i < chilren.length; i++){
			YAHOO.util.Dom.replaceClass(chilren[i], mChildShowClass, mChildHideClass);
		}
		// change button display to "expand all"
		YAHOO.util.Dom.replaceClass(theLink, 'collapse', 'expand');
	}
	return false;
}




























