// Copyright (c) WorldServe Internet Services
// All Rights Reserved
// For more information, see http://WorldServe.com/copyright
//

function firstUpper(string)
{
    return string.substring(0,1).toUpperCase() + string.substring(1).toLowerCase();
}

function br2nl(string)
{
    return string.replace(/ *<br *\/?> */, '\n');
}

// based on the PHP function
// (wish JavaScript did it intrinsically)
function strip_tags(string)
{
    return string.replace(/<([^>]+)>/g, '');
}

// (wish JavaScript had an equivalent for html_entity_decode)
function html_entity_decode(string)
{
var textarea = document.createElement('textarea');
    /*
    // THANK YOU http://www.locomotive.com.au/java/new/new42.htm !
    // original:
    // textarea.innerHTML = str.replace(/</g,"<").replace(/>/g,">");
    // not sure why the replace calls ?
    */
    textarea.innerHTML = string;

    return textarea.value;
}


function addEventHandler(element, type, handler)
{
    dean_addEvent(element, type, handler);
}

function redirTimer(URI, time)
{
    self.setTimeout(function() { self.location.href = URI; }, time);
}

function enhancedStatus(element)
{
    var message = null;

    if (element.title)
    {
	message = element.title;
    }
    else if (element.alt)
    {
	message = element.alt;
    }
    else if (element.tagName == 'A')
    {
	message = element.innerHTML;
    }

    if (message != null)
    {
	status = html_entity_decode(strip_tags(message));

	if (status != '')
	{
	    window.status = status;
	}
    }

    return status;
}

function enhancedMouseOver(event)
{
    event.preventDefault();
    event.stopPropagation();

    var element = this; //event.srcElement;

    enhancedStatus(element);
}

function enhancedMouseMove(event, x, y)
{
    event.preventDefault();
    event.stopPropagation();

    var element = this; //event.srcElement;

    enhancedStatus(element);
}

function enhancedMouseOut(event)
{
    return true;
}

function graphicMouseOver(event)
{
    event.preventDefault();
    event.stopPropagation();

    var img = this; //event.srcElement;

    img.src = img.hover;

    enhancedStatus(img);
}

function graphicMouseOut(event)
{
    var img = this; //event.srcElement;

    img.src = img.normal;

    return enhancedMouseOut(event);
}

var allLinks = new Array();
function getLinks()
{
    if (allLinks.length == 0)
    {
	var links;

        if (links = document.links)
        {
	    allLinks = links;
        }
        else
	{
	    a = collection2array(document.getElementsByTagName('a'));
	    area = collection2array(document.getElementsByTagName('area'));
	    allLinks = a.concat(area);
	}
    }

    return allLinks;
}

function collection2array(collection)
{
    var array = new Array();

    for (var i in collection)
    {
	object = collection[i];

	array.push(object);
    }

    return array;
}

function enhanceLinks()
{
    var links = getLinks();

    forEach(links, enhanceLink);
}

var linkEnhancer = new Array();
function enhanceLink(link)
{
    if ((link == null) ||
	(link == undefined) ||
	(link.href == undefined) ||
	(link.href == ''))
    {
	return false;
    }

    classNames = link.className;
    for (className in classNames.split(' '))
    {
	if (className == 0) // no splits
	{
	    className = classNames;
	}

	enhanceLinkClass = linkEnhancer[className];
	switch (typeof(enhanceLinkClass))
	{
	    case 'function':
		enhanceLinkClass(link);
		break;
	    
	    default:
		link.target = className;
	}
    }

    addEventHandler(link, 'focus', enhancedMouseOver);
    addEventHandler(link, 'mouseover', enhancedMouseOver);
    //addEventHandler(link, 'mouseout', enhancedMouseOut);
    if (link.tagName == 'AREA')
    {
	addEventHandler(link, 'mousemove', enhancedMouseMove);
    }

    return true;
}

function noEnhancement(link)
{
    // do nothing
    return true;
}

linkEnhancer['menu'] = noEnhancement;

function enhancePopup(link)
{
    link.href = 'JavaScript:popupTitle()';	// keep href behavior (cursor, etc.)
}

linkEnhancer['popup'] = enhancePopup;

function popupTitle()
{
    link = this; //event.srcElement;
    var popup = enhancedStatus(this);

    alert(popup);
}

function setID(element, id)
{
    var oldElement = document.getElementById(id);
    if(oldElement != undefined)
    {
	resetID(oldElement);
    }

    saveID(element, id);
    element.id = id;
}

function saveID(element, id)
{
    if ((element.id != undefined) && (element.id != id))
    {
	element.id_save = element.id;
    }
}

function resetID(element)
{
    if (element.id_save != undefined)
    {
	element.id = element.id_save;
    }
}

function tableMouseOver(event)
{
    row = this.parentNode; //event.srcElement.parentNode;
    row.id_save = row.id;
    row.id = 'hover';
    return true;
}

function tableMouseOut(event)
{
    row = this.parentNode; //event.srcElement.parentNode;
    row.id = row.id_save;
    return true;
}

function enhanceTables()
{
    var table = document.getElementById('enhancedtable');
    if (table)
    {
	var rows = table.getElementsByTagName('tr');
	for (var rowIdx in rows)
	{
	    if (rowIdx != 'length')
	    {
		var row = rows[rowIdx];
		if (row.getElementsByTagName('th').length < 1)	//skip Table Headers
		{
		    //row.onmouseover = tableMouseOver;
		    //row.onmouseout = tableMouseOut;
		    //row.onmouseover = function() { this.id = 'hover'; };
		    //row.onmouseout = function() { this.id = ''; };
		    addEventHandler(row, 'mouseover', tableMouseOver);
		    addEventHandler(row, 'mouseout', tableMouseOut);

		    var links = row.getElementsByTagName('a');	//check for links
		    if (links.length > 0)
		    {
			var link = links[0];
			//addEventHandler(row, 'mouseup', tableMouseUp);
			//addEventHandler(row, 'click', tableClick);
			//row.onmouseclick = link.onmouseclick;
			//row.onclick = link.onclick;
		    }
		}
	    }
	}
    }
}

function enhanceButton(link)
{
    if (link.id == 'close')
    {
	if (document.body.className == window.name)
	{
	    link.innerHTML = 'close';
	    link.title = 'close';

	    addEventHandler(link, 'click', blurClose);
	}
    }
}

function blurClose()
{
    // blur hides the window immediately, faster than close
    // gives user a better experience
    window.blur();
    window.close();
    return false;
}

linkEnhancer['button'] = enhanceButton;

//initialization code
var initFunctions = new Array();
var initialized = false; //flag to indicate whether target function has already been run
function initialize()
{
    if (!initialized)
    {
	// I hesitate to set this true at the top
	// but the loops might work better this way
	initialized = true;

	window.defaultStatus = 'Nicki Parrott';

	enhanceLinks();

	enhanceTables();

	for (initFunctionName in initFunctions)
	{
	    var initFunction = initFunctions[initFunctionName];
	    if (typeof(initFunction) == 'function')
	    {
		initFunction();
	    }
	}
	//var initFunctionName = 'enhance' + document.body.className.substring(0,1).toUpperCase() + document.body.className.substring(1);
	var initFunctionName = 'init' + firstUpper(document.body.className);
	if (eval('typeof(' + initFunctionName + ')') == 'function')
	{
	    eval(initFunctionName + '()');
	}
    }
}

addEventHandler(window, 'load', initialize);
