/*
 * DisplayUtils
 * 
 *    A collection of miscellaneous web page frobs.
 *
 * Tom Coppeto
 * 10 May 2007
 */


function DisplayUtils() {

    this.FadeIn  = FadeIn;
    this.FadeOut = FadeOut;
    this.SetOpacity = SetOpacity;

    
    function SetOpacity(targetName, opacity) {
	
	if(navigator && navigator.userAgent) {
	    /* avoid mac anti-alias thing */
	    if ((navigator.userAgent.indexOf("Macintosh") >= 0) &&
		(navigator.userAgent.indexOf("Mozilla") >= 0) &&
		(navigator.userAgent.indexOf("Safari") < 0) &&
                (navigator.userAgent.indexOf("Firefox/3") < 0)) {
		return;
	    }		
	}

	target = document.getElementById(targetName);
	if (!target) {
	    return;
	}
    
	/* omni needs - .0001 for main image only */
	/* safari & opera ok */
	/* netscape, seamonkey, mozilla disable or .0001 */

	if (target.style) {
	    target.style.opacity = (opacity/100) -.00001;
	    target.style.filter = "alpha(opacity=" + opacity + ")";
	}
    }


    function FadeIn(targetName, opacity) {

	if(navigator && navigator.userAgent) {
	    /* avoid mac anti-alias thing */
	    if ((navigator.userAgent.indexOf("Macintosh") >= 0) &&
		(navigator.userAgent.indexOf("Mozilla") >= 0) &&
		(navigator.userAgent.indexOf("Safari") < 0) &&
                (navigator.userAgent.indexOf("Firefox/3") < 0)) {
		return;
	    }		
	}

	if (opacity <= 100) {
	    SetOpacity(targetName, opacity);
	    opacity += 2;
	    setTimeout(function() { FadeIn(targetName, opacity) }, 30);
	}
    }


    function FadeOut(targetName, opacity) {
	/* avoid mac anti-alias thing */
	if(navigator && navigator.userAgent) {
	    if ((navigator.userAgent.indexOf("Macintosh") >= 0) &&
		(navigator.userAgent.indexOf("Mozilla") >= 0) &&
		(navigator.userAgent.indexOf("Safari") < 0) &&
                (navigator.userAgent.indexOf("Firefox/3") < 0)) {
		return;
	    }
	}

	if (opacity < 5) {
	    opacity = 3;
	}

	SetOpacity(targetName, opacity);
	opacity -= 5;
	if (opacity >= 3) {
	    setTimeout(function() { FadeOut(targetName, opacity) }, 30);
	}
    }

   
    function SetInnerHTML(node, text) {
	if (node) {
	    if (node.childNodes[0]) {
		node.childNodes[0].nodeValue = text;
	    } else if (node.value) {
		node.value = text;
	    } else {
		node.innerHTML = text;
	    }
	}
    }
}



function getCookie(name) {
    var dc = document.cookie;
    if (!document.cookie) {
	return; 
    }

    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
	begin = dc.indexOf(prefix);
	if (begin != 0) {
	    return null;
	}
    } else
	begin += 2;

    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
	end = dc.length;
    }
    return (unescape(dc.substring(begin + prefix.length, end)));
}


function getElementsByClassName(className) {
    var all = document.all ? document.all : document.getElementsByTagName('*');
    var elements = new Array();

    for (var e = 0; e < all.length; e++) {
	if (all[e].className == className) {
	    elements[elements.length] = all[e];
	}
    }

    return (elements);
}


function createElement(element) {
    if ((navigator && navigator.userAgent && navigator.userAgent.indexOf("MSIE") >= 0)) {	
	    return (document.createElement(element));
    } else {
	    return (document.createElementNS("http://www.w3.org/1999/xhtml", element));
    }
}


function deleteNode(node) {

    if (node) {
	deleteChildren(node); 
	gEVENTS.removeBuiltinListener(node);
	if (typeof node.outerHTML !== 'undefined')
	    node.outerHTML = ''; //prevent pseudo-leak in IE
	else
	    if(node.parentNode) //if the node has a parent
		node.parentNode.removeChild(node); //remove the node from the DOM tree
	delete node; //clean up just to be sure
    }

    return;
}


function deleteChildren(node) {

    if (node) {
	for (var x = node.childNodes.length - 1; x >= 0; x--) { 
	    var childNode = node.childNodes[x];
	    if (childNode.hasChildNodes()) {
		deleteChildren(childNode);
	    }

	    node.removeChild(childNode); //remove the child from the DOM tree
	    delete childNode; //clean up just to be sure
	}
    }
    
    return;
}


String.prototype.escapeHTML = function () {
    return (this.replace(/&/g,'&amp;').replace(/>/g,'&gt;').replace(/</g,'&lt;').replace(/\"/g,'&quot;'));
}


String.prototype.unescapeHTML = function () {
    return (this.replace(/&amp;/g,'&').replace(/&gt;/g,'>').replace(/&lt;/g,'<').replace(/&quot;/g,'\"'));
}	

var preferences = {
    option_have     : 1 << 0,
    option_pulldown : 1 << 1,
    option_ttNav    : 1 << 2,
    option_ttLinks  : 1 << 3,

    getOptions : function() {
	var options = [];
	var cookie = getCookie("options");
	var values = parseInt(cookie);	    

	if (values & preferences.option_have) {
	    if (values & preferences.option_pulldown) {
		options["pulldownMenu"] = true;
	    } else {
		options["pulldownMenu"] = false;
	    }

	    if (values & preferences.option_ttNav) {
		options["tooltipNav"] = true;
	    } else {
		options["tooltipNav"] = false;
	    }

	    if (values & preferences.option_ttLinks) {
		options["tooltips"] = true;
	    } else {
		options["tooltips"] = false;
	    }	    
	} else {
		options["pulldownMenu"] = true;
		options["tooltipNav"] = false;
		options["tooltips"] = true;
	}

        return (options);
    },

    
    navbar : function() {
	options = preferences.getOptions();
	if (options["pulldownMenu"]) {
	    return;
	}

	var nav = document.getElementById("navigation");
	for (var n = 0; n < nav.childNodes.length; n++) {
	    if (nav.childNodes[n].nodeName.toLowerCase() == "ul") {
		var ul = nav.childNodes[n];
		for (var i = 0; i < ul.childNodes.length; i++) {
		    menu = ul.childNodes[i];
		    for (var j = 0; j < menu.childNodes.length; j++) {
			child = menu.childNodes[j];
			if (child.nodeName.toLowerCase() == "ul") {
			    menu.removeChild(child);
			}
		    }
		}
	    }
	}
    }
}


preferences.navbar();
/*
 * External Links
 * 
 *    Opens up a new window when a link with an "external" attribute is present.
 *    The new way of doing target=_blank.
 *
 * Tom Coppeto
 * 12 May 2007
 */


function externalLinks() { 

    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") == "external") ||
	     anchor.getAttribute("rel") == "nofollow")) {
	    anchor.onclick = function(e) {
		window.open(this.href);
		return (false);
	    }

	    anchor.onkeypress = anchor.onclick;
	}
    } 

    return;
} 


addEvent(window, "load", externalLinks);
/*
 * Sweet Titles (c) Creative Commons 2005
 * http://creativecommons.org/licenses/by-sa/2.5/
 * Author: Dustin Diaz | http://www.dustindiaz.com
 */

/*
 * Modified for BBF web site.
 *
 * Tom Coppeto
 * May 2007
 */

var sweetTitles = { 
    xCord : 0,			// @Number: x pixel value of current cursor position
    yCord : 0,			// @Number: y pixel value of current cursor position
    obj : Object,		// @Element: That of which you're hovering over
    tip : Object,		// @Element: The actual toolTip itself
    active : 0,			// @Number: 0: Not Active || 1: Active
    oOID : 0,
    tipElements : ['a', 'abbr', 'acronym', 'div', 'span', 'tr', 'th', 'area', 'tbody', 'select', 'ul', 'table', 'li'],


    init : function() {
	var options = preferences.getOptions();

	if (!document.getElementById || !document.createElement ||
	    !document.getElementsByTagName) {
		return;
	    }

	this.tip = createElement("div");
	this.tip.id = "toolTip";
	document.getElementsByTagName("body")[0].appendChild(this.tip);
        this.tip.style.top = "0";
        this.tip.style.visibility = "hidden";

	for (var i = 0; i < this.tipElements.length; i++) {
	    var current = document.getElementsByTagName(this.tipElements[i]);
	    for (var j = 0; j < current.length; j++) {

		if ((current[j].className == "egg") && options["tooltips"]) {
		    var parent = current[j].parentNode;
		    if (parent.nodeName.toLowerCase() == "td") {
			parent = parent.parentNode;
		    }

		    parent.setAttribute("tip", "egg");
		    addEvent(parent, "mouseover", this.tipOver);
		    addEvent(parent, "mouseout",  this.tipOut);
		} else if (current[j].title) {
		    if (current[j].className && (current[j].className.indexOf("ntt") >= 0)) {
			current[j].removeAttribute("title");
			continue;
		    }

		    current[j].setAttribute("tip", current[j].title.escapeHTML());
		    current[j].removeAttribute("title");
		    
		    if ((this.isChild(current[j], "navigation") ||
			 this.isChild(current[j], "shortcuts")) &&
			options["tooltipNav"]) {
			addEvent(current[j], "mouseover", this.tipOver);
			addEvent(current[j], "mouseout",  this.tipOut);
		    } else if ((!this.isChild(current[j], "navigation") &&
				!this.isChild(current[j], "shortcuts")) &&
			       options["tooltips"]) {
			addEvent(current[j], "mouseover", this.tipOver);
			addEvent(current[j], "mouseout",  this.tipOut);
		    }
		}
	    }
	}

	/* disable alt tooltips on IE */
	if ((navigator && navigator.userAgent && navigator.userAgent.indexOf("MSIE") >= 0)) {
	    var current = document.getElementsByTagName("img");
	    for (var j = 0; j < current.length; j++) {
		if (current[j].alt) {
		    current[j].setAttribute("alt", "");
		}
	    }
	}
    },

    isChild : function(e, parent) {
	if (!e) {
	    return (false);
	}
	
	if (e.id == parent) {
	    return (true);
	}

	if (e.parentNode) {
	    return this.isChild(e.parentNode, parent);
	} else {
	    return (false);
	}
    },

    updateXY : function(e) {
	var currentX = 0;
	var currentY = 0;

	if (self.pageYOffset) { // all except Explorer 
	    currentY = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict 
	    currentY = document.documentElement.scrollTop;
	} else if (document.body) { // all other Explorers
	    currentY = document.body.scrollTop;
	}

	if (self.pageXOffset) { // all except Explorer 
	    currentX = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollLeft) { // Explorer 6 Strict 
	    currentX = document.documentElement.scrollLeft;
	} else if (document.body) { // all other Explorers
	    currentX = document.body.scrollLeft;
	}

	if (document.captureEvents) {
	    sweetTitles.xCord = e.pageX;
	    sweetTitles.yCord = e.pageY;
	} else if (window.event.clientX) {
	    sweetTitles.xCord = window.event.clientX + currentX;
	    sweetTitles.yCord = window.event.clientY + currentY;
	}
    },

    tipOut: function() {
	if (window.tID) {
	    clearTimeout(tID);
	}

	if (window.oID) {
	    clearTimeout(oID);
	}

	if (window.opacityID) {
	    clearTimeout(opacityID);
	}

	sweetTitles.tipFadeOut(95);
    },
	
    checkNode : function() {
	var trueObj = this.obj;
	if (this.tipElements.inArray(trueObj.nodeName.toLowerCase())) {
	    return trueObj;
	} else {
	    return trueObj.parentNode;
	}
    },

    tipOver : function(e) {
	if (window.tID) {
	    clearTimeout(tID);
	}

	if (window.oID) {
	    clearTimeout(oID);
	}

	if (window.opacityID) {
	    clearTimeout(opacityID);
	}

	sweetTitles.obj = this;
	tID = window.setTimeout("sweetTitles.tipShow()", 1500);
	sweetTitles.updateXY(e);
    },

    tipUpdate : function(node) {
	var style;
	if (this.tip.style) {
	    style = this.tip.style;
	} else {
	    style = this.tip;
	}

	if (style.visibility == "visible") {
	    if (this.obj.id == node) {
		tID = window.setTimeout("sweetTitles.tipShow()", 0);
	    }
	}
    },

    tipShow : function() {		
	var scrX   = Number(this.xCord);
	var scrY   = Number(this.yCord);
	var tp     = parseInt(scrY+15);
	var lt     = parseInt(scrX+10);
	var anch   = this.checkNode();
	var access = "";

	if (anch.getAttribute("tip") == "egg") {
	    var egg = sweetTitles.eggHunt(anch);
	    if (egg) {
		this.tip.innerHTML = egg.innerHTML.trim();
	    }
        } else {
	    var s = anch.getAttribute("tip");
	    this.tip.innerHTML = s;
	}
	
	var style;
	if (this.tip.style) {
	    style = this.tip.style;
	} else {
	    style = this.tip;
	}

	var t = this.tip.innerHTML.length/25 + 5;
	if (this.tip.innerHTML.length < 8) {
	    style.width = "5em";
	} else if (this.tip.innerHTML.length < 20) {
	    style.width = "10em";
	} else if (this.tip.innerHTML.length < 150) {
	    style.width = "15em";
	} else if (this.tip.innerHTML.length < 200) {
	    style.width = "20em";
	} else if (this.tip.innerHTML.length < 350) {
	    style.width = "25em";
	} else if (this.tip.innerHTML.length < 900) {
	    style.width = "30em";
	} else {
	    style.width = "40em";
	}

	if ((navigator && navigator.userAgent && navigator.userAgent.indexOf("MSIE") >= 0)) {
	    if (this.tip.innerHTML.length < 8) {
		style.width = "7em";
	    } else if (this.tip.innerHTML.length < 20) {
		style.width = "13em";
	    } else if (this.tip.innerHTML.length < 150) {
		style.width = "20em";
	    } else if (this.tip.innerHTML.length < 200) {
		style.width = "25em";
	    } else if (this.tip.innerHTML.length < 350) {
		style.width = "35em";
	    } else if (this.tip.innerHTML.length < 900) {
		style.width = "40em";
	    } else {
		style.width = "50em";
	    }
	}

	this.oOID = window.setTimeout("sweetTitles.tipOut()", t * 1000);

	if (parseInt(document.documentElement.clientWidth+document.documentElement.scrollLeft) < parseInt(this.tip.offsetWidth + lt)) {
	    style.left = parseInt(lt - (this.tip.offsetWidth + 10)) + "px";
	} else {
	    style.left = lt + "px";
	}

	if (parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) < parseInt(this.tip.offsetHeight+tp)) {
	    style.top = parseInt(tp-(this.tip.offsetHeight+10)) + "px";
	} else {
	    style.top = tp + "px";
	}

	style.visibility = "visible";

	/* work around for problems with mozilla opacity on mac */
	if(navigator && navigator.userAgent) {
	    if ((navigator.userAgent.indexOf("Macintosh") >= 0) &&
		(navigator.userAgent.indexOf("Mozilla") >= 0) &&
		(navigator.userAgent.indexOf("Safari") < 0) &&
                (navigator.userAgent.indexOf("Firefox/3") < 0)) {
		return;
	    }
	}

	style.opacity = "0";
	this.tipFadeIn(5);
    },

    tipFadeIn: function(opac) {
	var style;
	if (this.tip.style) {
	    style = this.tip.style;
	} else {
	    style = this.tip;
	}

	var passed = parseInt(opac);
	var newOpac = parseInt(passed+10);
	if (newOpac < 90) {
	    style.opacity = "." + newOpac;
	    style.filter = "alpha(opacity:"+newOpac+")";
	    opacityID = window.setTimeout("sweetTitles.tipFadeIn('"+newOpac+"')", 30);
	} else { 
	    style.opacity = ".95";
	    style.filter = "alpha(opacity:95)";
	}
    },
    
    tipFadeOut: function(opac) {
	var style;
	if (this.tip.style) {
	    style = this.tip.style;
	} else {
	    style = this.tip;
	}

	window.clearTimeout(this.oOID);
	/* work around for problems with mozilla opacity on mac */
	if(navigator && navigator.userAgent) {
	    if ((navigator.userAgent.indexOf("Macintosh") >= 0) &&
		(navigator.userAgent.indexOf("Mozilla") >= 0) &&
		(navigator.userAgent.indexOf("Safari") < 0)) {
		    style.visibility = "hidden";
		    return;
		}
	}

	var passed = parseInt(opac);
	var newOpac = parseInt(passed-10);
	if (newOpac > 10) {
	    style.opacity = "." + newOpac;
	    style.filter = "alpha(opacity:"+newOpac+")";
	    window.setTimeout("sweetTitles.tipFadeOut('"+newOpac+"')", 20);
	}
	else { 
	    style.visibility = "hidden";
	}
    },

    eggHunt: function(node) {
	for (var i = 0; i < node.childNodes.length; i++) {
	    if (node.childNodes[i].className == "egg") {
		return (node.childNodes[i]);
	    }

	    var egg = sweetTitles.eggHunt(node.childNodes[i]);
	    if (egg) {
		return (egg);
	    }
	}

	return (null);
    }
};


function stLoader() {
    sweetTitles.init();
}


Array.prototype.inArray = function (value) {

    for (var i = 0; i < this.length; i++) {
	if (this[i] == value) {
	    return true;
	}
    }
    return false;
};


String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}


addEvent(window, "load", stLoader);



/*
 * ParkDiv
 * 
 *    "Parks" a div box by preventing it from scrolling vertically off the
 *    window. Expands a "rubber band" div to park it.
 *
 * Tom Coppeto
 * 10 May 2007
 */

function ParkDiv() {
    var obj;
    var rubber;
    var initialY = 0;
    var margin   = 5;
    this.Top = Top;

    function Top(e) {	
	obj = document.getElementById(e);
	if (!obj) {
	    return;
	}

	var pos = findPos(obj);
	initialY = pos[1];
	if (initialY == 0) {
	    return;
	}

	rubber = document.getElementById('rubber');
	if (!rubber) {
	    return;
	}

	window.onscroll = rubberBand;
	rubberBand();
    }


    function rubberBand() { 
	var currentY = 0;

	if (self.pageYOffset) { // all except Explorer 
	    currentY = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict 
	    currentY = document.documentElement.scrollTop;
	} else if (document.body) { // all other Explorers
	    currentY = document.body.scrollTop;
	}

	if ((initialY - currentY) < margin) {
	    offset = currentY - initialY + margin;
	    rubber.style.height = offset + 'px';
	} else {
	    rubber.style.height = '0';
	}	
    }
    

    function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	    curleft = obj.offsetLeft;
	    curtop = obj.offsetTop;
	    while (obj = obj.offsetParent) {
		curleft += obj.offsetLeft;
		curtop  += obj.offsetTop;
	    }
	}

	return [curleft, curtop];
    }
}

if (navigator && navigator.userAgent) {
    if ((navigator.userAgent.indexOf("MSIE") < 0) &&
        (navigator.userAgent.indexOf("Firefox") < 0)) { 

	addEvent(window, "load", function() { var pd = new ParkDiv(); pd.Top('shortcuts')});
    }
}

function loge(user) {
    if (!user || (user == "logout")) {
	return (true);
    }

    try {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("msie") != -1) {
	    document.execCommand("ClearAuthenticationCache");
	}  else {
	    var xmlhttp = createXMLObject();
	    xmlhttp.open("GET", "/bin/logout", false, "logout", "logout");
	    xmlhttp.send(null);
	}
	
	return (true);
	    
    } catch(e) {
	alert(e)
    }

    
    function createXMLObject() {
	try {
	    if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	    } else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	} catch (e) {
	    xmlhttp=false;
	}

	return xmlhttp;
    }
}