﻿function AddLoadEvent(func)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function()
        {
            if (oldonload)
            {
                oldonload();
            }
            func();
        }
    }
}

/* Written by Dustin Diaz http://www.dustindiaz.com/ */
function AddEvent(obj, type, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(type, fn, false);
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn](window.event); }
		obj.attachEvent("on"+type, obj[type+fn]);
		EventCache.add(obj, type, fn);
	}
	else
	{
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

AddEvent(window, "unload", EventCache.flush);

/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/
function GetElementsByClassName(oElm, strTagName, strClassName)
{
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++)
    {
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className))
        {
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function HasClassName(objElement, strClass)
{
    // if there is a class
    if (objElement.className)
    {
        // the classes are just a space separated list, so first get the list
        var arrList = objElement.className.split(' ');

        // get uppercase class for comparison purposes
        var strClassUpper = strClass.toUpperCase();

        // find all instances
        for (var i = 0; i < arrList.length; i++)
        {
            // if class found
            if (arrList[i].toUpperCase() == strClassUpper)
            {
                // we found it
                return true;
            }
        }
    }
    
    // if we got here then the class name is not there
    return false;
}

function AddClassName(objElement, strClass, blnMayAlreadyExist)
{
    // if there is a class
    if (objElement.className)
    {
        // the classes are just a space separated list, so first get the list
        var arrList = objElement.className.split(' ');

        // if the new class name may already exist in list
        if (blnMayAlreadyExist)
        {
            // get uppercase class for comparison purposes
            var strClassUpper = strClass.toUpperCase();

            // find all instances and remove them
            for (var i = 0; i < arrList.length; i++)
            {
                // if class found
                if (arrList[i].toUpperCase() == strClassUpper)
                {
                    // remove array item
                    arrList.splice(i, 1);

                    // decrement loop counter as we have adjusted the array's contents
                    i--;
                }
            }
        }

        // add the new class to end of list
        arrList[arrList.length] = strClass;

        // add the new class to beginning of list
        //arrList.splice(0, 0, strClass);
      
        // assign modified class name attribute
        objElement.className = arrList.join(' ');
    }
    // if there was no class
    else
    {
        // assign modified class name attribute      
        objElement.className = strClass;   
    }
}

function RemoveClassName(objElement, strClass)
{
    // if there is a class
    if (objElement.className)
    {
        // the classes are just a space separated list, so first get the list
        var arrList = objElement.className.split(' ');

        // get uppercase class for comparison purposes
        var strClassUpper = strClass.toUpperCase();

        // find all instances and remove them
        for ( var i = 0; i < arrList.length; i++ )
        {
            // if class found
            if (arrList[i].toUpperCase() == strClassUpper)
            {
                // remove array item
                arrList.splice(i, 1);

                // decrement loop counter as we have adjusted the array's contents
                i--;
            }
        }

        // assign modified class name attribute
        objElement.className = arrList.join(' ');
    }
    // if there was no class
    // there is nothing to remove
}
		
//gets the next element node
function GetNextElement(node)
{
	if(node.nodeType == 1)
		return node;
		
	if(node.nextSibling)
		return GetNextElement(node.nextSibling);
		
	return null;
}
		
//gets the previous element node
function GetPrevElement(node)
{
	if(node.nodeType == 1)
		return node;
		
	if(node.previousSibling)
		return GetPrevElement(node.previousSibling);
		
	return null;
}
