/**
*   @file
*   This file requires the following files to be included prior:
*
*   htmlelements/Mixin_DomInsertable.js
*   utilities/rbrowserdetector.js
*
*   This also requires that your base class inherits from Mixin_DomInsertable.
*/

if(!gBrowserDetector)
    var gBrowserDetector = new RBrowserDetector();

/**
*
*   Deriving from this class
*   promises that your child class can
*   handle several types of events:
*
*   @param onClick      The javascript to fire when clicked.
*   @param onMouseOver  The javascript to fire when moused over.
*   @param onMouseDown  The javascript to fire when the mouse is pressed on it.
*   @param onMouseOut   The javascript to fire when the mouse is moved off.
*
*/
function Mixin_HandlesEvents(onClick, onMouseOver, onMouseDown, onMouseOut)
{
    this.SetOnClick(onClick);
    this.SetOnMouseOver(onMouseOver);
    this.SetOnMouseDown(onMouseDown);
    this.SetOnMouseOut(onMouseOut);
}


/**
*   This function will register an event regardless of browser.
*
*   @param handler      This handler is the name of the handler you wish to register.
*                       (i.e., "onmousover", "onclick", etc)
*   @param jsToExecute  This is the JavaScript to execute when the event is
*                       triggered.  It can be either a function or a string to be
*                       evaluated as javascript.
*
*/
Mixin_HandlesEvents.prototype.SetEventHandler = function(handler, jsToExecute)
{
    var ourNode = this.GetDomNode();

    // if jsToExecute is a function set the listener to that function else set it to try to eval the string
    if('function'==typeof(jsToExecute))
    {
        if(gBrowserDetector.gecko)
        {
            handler = handler.substr(2);
            ourNode.addEventListener(handler, jsToExecute, false);
        }
        else
        {
            var toEval = "ourNode." + handler + " = jsToExecute;";
            eval(toEval);
        }
    }
    else
    {
        if(gBrowserDetector.gecko)
        {
            handler = handler.substr(2);
            ourNode.addEventListener(handler, function(event){ eval(jsToExecute); }, false);
        }
        else
        {
            var toEval = "ourNode." + handler + " = function(){eval(jsToExecute); };";
            eval(toEval);
        }
    }

}

/**
*   This function will register the onClick event.
*
*   @param onClick  The string that represents the code to run or a function to run.
*
*/
Mixin_HandlesEvents.prototype.SetOnClick = function(onClick)
{
    this.SetEventHandler("onclick", onClick);
}


/**
*   This function will register the onMouseOver event.
*
*   @param onMouseOver  The string that represents the code to run or a function to run..
*
*/
Mixin_HandlesEvents.prototype.SetOnMouseOver = function(onMouseOver)
{
    this.SetEventHandler("onmouseover", onMouseOver);
}

/**
*   This function will register the onMouseDown event.
*
*   @param onMouseDown  The string that represents the code to run or a function to run..
*
*/
Mixin_HandlesEvents.prototype.SetOnMouseDown = function(onMouseDown)
{
    this.SetEventHandler("onmousedown", onMouseDown);
}

/**
*   This function will register the onMouseOut event.
*
*   @param onMouseOut  The string that represents the code to run or a function to run..
*
*/
Mixin_HandlesEvents.prototype.SetOnMouseOut = function(onMouseOut)
{
    this.SetEventHandler("onmouseout", onMouseOut);
}

/**
*   This function will register the onMouseUp event.
*
*   @param onMouseUp  The string that represents the code to run.
*
*/
Mixin_HandlesEvents.prototype.SetOnMouseUp = function(onMouseUp)
{
    this.SetEventHandler("onmouseup", onMouseUp);
}