/** base library functions **/
// Find a parent comparisson is done by the compare function (provided as argument)/
// compare function must accept an array of arguments
function findRightParent(oObject, sComparefunction, aArguments)
{

    fFunction = eval(sComparefunction);
    
    if(fFunction(oObject, aArguments) == true)
    {
        return oObject
    } else {
        if(oObject.parentNode != null)
        {
            return findRightParent(oObject.parentNode, sComparefunction, aArguments);
        }
    }
    
    return false;
}

// Find parent by tag (depends on findRightParent (comparisson function for findRightParent is hardcoded into this funtion)
function findRightParentByTag(oObject, sTag)
{
    return findRightParent(oObject, "checkOnTag", new Array(sTag))
}

// Find parent by class (depends on findRightParent (comparisson function for findRightParent is hardcoded into this funtion)
function findRightParentByClass(oObject, sClass)
{
    return findRightParent(oObject, "checkOnClass", new Array(sClass))
}

function checkOnTag(oObject, aArguments)
{
    var sTagName = aArguments[0];
    
    if(oObject.nodeName.toLowerCase() == sTagName.toLowerCase())
    {
        return true;
    } 
    
    return false;
}

function checkOnClass(oObject, aArguments)
{

    var aClasses;
    
    var sClassName = aArguments[0];
    
    // Make sure the function doesn't cause any problems when an invalid object is given.
    if(oObject["className"] != undefined)
    {
    
        // Multiple classes on a single item is supported, classes are to be separated by a space (" ")
        aClasses = oObject.className.split(" ")
        
        // Loop through the classes
        for(var iClassIndex = 0; iClassIndex < aClasses.length; iClassIndex++)
        {
        
            // Check for the right class
            if(aClasses[iClassIndex] == sClassName)
            {
                // Ok found one, bail out!
                return true;
            }
            
        }
    
    }
    
    // Function returns false when an invalid item is given OR when the correct object is not found!    
    return false;

}


// Dependency on "expandItem" css class
// add or removes "expandedState"
function expandObject(oObject)
{
    
    var oBaseObject = findRightParentByClass(oObject, "expandItem");
    
    
    // Akward code: removeCSSClass returns false if it hasn't found the class
    // Used here to see if we need to add or remove the class 
    if(removeCSSClass(oBaseObject, "expandedState") == false)
    {
        addCSSClass(oBaseObject, "expandedState")
    }

    return false;
}

// 
function onExpandLink()
{
    expandObject(this);
    return false;
}

function populateExpandlink (oLink)
{
    oLink.onclick = onExpandLink;
}

// Remove a certain css class
// returns false when the class's not found
function removeCSSClass(oObject, sClass)
{
    var sNewClasses = "";
    var bFound = false;
    var aClasses = oObject.className.split(" ");
    
    for(iClassCounter = 0; iClassCounter < aClasses.length; iClassCounter++)
    {
        if(sClass != aClasses[iClassCounter])
        {
            sNewClasses += " " + aClasses[iClassCounter];
        } else {
            bFound = true;
        }
    }
    
    oObject.className = sNewClasses;
    
    return bFound;
}


// Add a css class to an object (no check for duplicate classes)
function addCSSClass(oObject, sClass)
{

    oObject.className += " " + sClass;
}

function populateExpandlink (oLink)
{
    oLink.onclick = onExpandLink;
}

// getElementsByClass found on: http://www.dustindiaz.com/getelementsbyclass
function getElementsByClass(node,searchClass,tag) {
 var classElements = new Array();
 var els = node.getElementsByTagName(tag);
 var elsLen = els.length;
 var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
 for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}


function onloader()
{
    var aExpandLinks = getElementsByClass(document,"expandLink", "a");
    
    for(var iLinkCounter = 0; iLinkCounter < aExpandLinks.length; iLinkCounter++)
    {
        populateExpandlink(aExpandLinks[iLinkCounter]);
    }
    
}
window.onload = onloader;