//
//  Trigger.js
//  Version 2.1
//  written by Stefan Habel
//  Last modified: 06.04.2008
//


//
//
//
window.TriggerElement = function ( elementId, action )
{

    this.elementId = elementId;
    this.action    = action;
    this.element   = null;
    this.title     = "";

    this.Init();

}


//
//
//
window.TriggerElement.prototype.Init = function ()
{
    this.element = document.getElementById(this.elementId);
    if (!this.element) {
        console.error("Error initializing Trigger object: Element \"" + this.elementId + "\" could not be found.");
        return;
    }

    // set the cursor style to be the hand pointer for elements that are not
    // button elements
    if (this.element.tagName != "BUTTON") {
        this.element.style.cursor = "pointer";
        this.element.style.MozUserSelect = "none";
    }
}


//
//
//
window.TriggerElement.prototype.GetElementId = function ()
{
    return this.elementId;
}


//
//
//
window.TriggerElement.prototype.GetElement = function ()
{
    return this.element;
}


//
//
//
window.TriggerElement.prototype.GetTitle = function ()
{
    if (this.title == "" && this.element) {
        var node = this.element.firstChild;
        while (node && this.title == "") {
            // check if the node is a text node
            if (node.nodeType == 3) {
                var trimmedText = node.nodeValue.replace(/^\s*/, "").replace(/\s*$/, "");
                if (trimmedText != "")
                    this.title = trimmedText;
                else
                    node = node.nextSibling;
            } else
                node = node.firstChild;
        }
    }
    return this.title;
}


//
// Updates element pointers to respect changes in the DOM tree.
//
window.TriggerElement.prototype.Update = function ()
{
    this.element = document.getElementById(this.elementId);
}


//
//
//
window.TriggerElement.prototype.AddContent = function ( id )
{
    console.info("window.TriggerElement.prototype.AddContent() has been called.");
    if (!IsIn(id, this.ids))
        this.ids.push(id);
}


//
//
//
window.TriggerElement.prototype.ToggleClassName = function ( state, suffix )
{
    var className = this.element.className;
    if (state) {
        if (className.indexOf(suffix) == -1) {
            var selectedHovered = suffix == "_sel" && className.indexOf("_hov") > -1;
            if (selectedHovered)
                className = className.replace(/_hov/, "");
            className += suffix;
            if (selectedHovered)
                className += "_hov";
        }
    } else {
        if (className.indexOf(suffix) > -1)
            className = className.replace(suffix, "");
    }
    this.element.className = className;
}


//
// Displays the trigger element as a selected or normal element.
//
// \param selected  flag that states whether to display the element as selected or normal element
//
window.TriggerElement.prototype.SetSelected = function ( selected )
{
    this.ToggleClassName(selected, "_sel");
}


// type enumeration
window.TriggerElement.Action = {
    TOGGLE: 0,
    SHOW: 1,
    HIDE: -1,
    EXPAND: 1,
    COLLAPSE: -1
};
