/*
 * Image Rollovers using event listeners
 *
 * This function will loop through all the images in a page
 * and add rollovers by adding onmouseover and onmouseout events.
 * 
 *
 * Usage:
 * Add an "rollover" class to the image tag.
 * The rollover image must be in the same directory as the default
 * image and suffixed with "_over".
 */
function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className.split(' ').has("rollover")) {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_over'+ftype);
		
			//Preload the rollover
			aImages[i].setAttribute('hsrc', hsrc);
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			//Attach the rollover event
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}	
			
			//Attach the rolloff event
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_over'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

/*
 * Add Link Titles
 *
 * This function will loop through all the elements in a page and add
 * a title attribute to all anchor tags.  For anchor tags with a className
 * of "external" the title will be "New Window:" followed by the link text.
 * For all anchor tags just the link text will be used for the title.  
 *
 * To override this functionality, just specify a title in the anchor tag.  
 * Anchors with defined titles will be ignored.
 * 
 */
addTitles = function(sContextPath) {
	var allLinks = document.getElementsByTagName("a");
	for (i=0; i<allLinks.length; i++) {
		var linkClass=allLinks.item(i).className		
		if (allLinks.item(i).title.length == 0) { 
			var innerHTML = allLinks.item(i).innerHTML;
			innerHTML = innerHTML.replace('&amp;','&');			
			if(linkClass.split(' ').has("external"))
			{allLinks.item(i).title = "New Window: " + innerHTML}			
			else if(linkClass.split(' ').has("pdf"))
			{allLinks.item(i).title = "New Window: " + innerHTML}		
			else{allLinks.item(i).title = innerHTML}					
		}
	}
}

/*
	Accessible pop-ups, snatched from http://www.alistapart.com/articles/popuplinks 
	and http://v2studio.com/k/code/lib. Note that the functions and extensions 
	originally declared in Caiao's lib.js have been stripped down and included in 
	this file to trim file size.
 */

var _POPUP_FEATURES = '';

if (!Array.prototype.push) Array.prototype.push = function() {
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}

Array.prototype.find = function(value, start) {
    start = start || 0;
    for (var i=start; i<this.length; i++)
        if (this[i]==value)
            return i;
    return -1;
}

Array.prototype.has = function(value) {
    return this.find(value)!==-1;
}

function map(list, func) {
    var result = [];
    func = func || function(v) {return v};
    for (var i=0; i < list.length; i++) result.push(func(list[i], i, list));
    return result;
}

function filter(list, func) {
    var result = [];
    func = func || function(v) {return v};
    map(list, function(v) { if (func(v)) result.push(v) } );
    return result;
}

function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}

function hasClass(elem, className) {
    return getElem(elem).className.split(' ').has(className);
}

function getElementsByClass(className, tagName, parentNode) {
    parentNode = !isUndefined(parentNode)? getElem(parentNode) : document;
    if (isUndefined(tagName)) tagName = '*';
    return filter(parentNode.getElementsByTagName(tagName),
        function(elem) { return hasClass(elem, className) });
}

function listen(event, elem, func) {
    elem = getElem(elem);
    if (elem.addEventListener)  // W3C DOM
        elem.addEventListener(event,func,false);
    else if (elem.attachEvent)  // IE DOM
        elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
        // for IE we use a wrapper function that passes in a simplified faux Event object.
    else throw 'cannot add event listener';
}

function mlisten(event, elem_list, func) {
    map(elem_list, function(elem) { listen(event, elem, func) } );
}

function W3CDOM_Event(currentTarget) {
    this.currentTarget  = currentTarget;
    this.preventDefault = function() { window.event.returnValue = false }
    return this;
}

function isUndefined(v) {
    var undef;
    return v===undef;
}

function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features, offsite) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    var sHref = src.getAttribute('href')
    if (offsite) {sHref = offSiteURL + sHref;}
	return raw_popup(sHref, src.getAttribute('target') || '_blank', features);
}

function event_popup(e) {
    // to be passed as an event listener
    // pops up a window grabbing the url from the event source's href
	// added Safari filtering
	if (checkIt('safari')) {
		var a = e.currentTarget;
	    var href = a.href;
	    link_popup(a);
	    e.preventDefault();
	    a.href = '#';
	    setTimeout(function(){ a.href = href }, 0);
	}
	else 
	{
		link_popup(e.currentTarget);
    	e.preventDefault();
	}
}

function event_popup_features(features,offsite) {
    // generates an event listener similar to event_popup, but allowing window features
	// added Safari filtering
	if (checkIt('safari')) {
		return function(e) { 
	        var a = e.currentTarget;
	        var href = a.href;
	        link_popup(a, features, offsite);
	        e.preventDefault();
	        a.href = '#';
	        setTimeout(function(){ a.href = href }, 0);
    	}
	}
	else 
	{
		return function(e) { link_popup(e.currentTarget, features, offsite); e.preventDefault() }
	}
}


var detect = navigator.userAgent.toLowerCase();
var thestring;
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

