/** (C) CopyRight 2009. Andry V Trushin
 ** JavaScript Framework. Events & States
 **/
var uag = window.navigator.userAgent.toLowerCase();

browser = {
	safari: /safari/.test( uag ) && !/chrome/.test( uag ),
	chrome: /chrome/.test( uag ),
	opera: /opera/.test( uag ),
	msie: /msie/.test( uag ) && !/opera/.test( uag ),
	firefox: /firefox/.test( uag ),
    version: (uag.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1]
};

function H( id )
{
    this.id = id;
    this.f = [];

    this.add = function( f )
    {
        this.f[ this.f.length ] = f;
    }
    this.fire = function( p )
    {
        for( var i = 0; i < this.f.length; i++ )
        	try
        	{
            	this.f[ i ]( p );
            }
            catch( e )
            {
				alert( 'EVENT: ' + this.id + '\nPARAM: ' + this.fire.arguments.toString() + '\n\n' + e.number + '\n' + e.description  + '\n\n' + this.f[ i ]);
            }
    }
}

var E = new function()
{
    this.h = [];

    this.hook = function( id )
    {
    	if( this.h[ id ] == null )
            this.h[ id ] = new H( id );
    }
    this.set = function( id, f )
    {
        if( f != null )
		{
			if( this.h[ id ] == null )
				this.hook( id );
            this.h[ id ].add( f );
		}
    }
    this.fire = function( id, p )
    {
        this.h[ id ].fire( p );
    }
}

var S = new function()
{
    this.s = [];

    this.set = function( id, value )
    {
        var state = this.s[ id ];
        this.s[ id ] = value;
        if( state != null )
            E.fire( "changeState", { "state":id, "oldvalue":state, "value":value });
    }
    this.get = function( id )
    {
        return this.s[ id ];
    }
}
E.hook( "changeState" );

E.hook( "onLoad" );
E.hook( "onMouseMove" );
E.hook( "onClick" );
E.hook( "onKeyDown" );
E.hook( "onMouseDown" );
E.hook( "onResize" );
E.hook( "showPopup" );

document.onclick = function( event )
{
    E.fire( "onClick", event );
}
/*
document.onselectstart = function( event )
{
    var el = ( browser.msie  ? window.event.srcElement : event.target );
    if( el.tagName == 'INPUT' )
        return true;
    else
        return ( getParentDiv( el ).getAttribute( 'SELECT' ) == "true" );
}
*/
document.onmousedown = function( event )
{
    E.fire( "onMouseDown", event );
}

var onmm = function( event )
{
    E.fire( "onMouseMove", event );
}
window.onload = function( event )
{
    E.fire( "onLoad", event );
    document.body.onmousemove = onmm;
}
document.ondragstart = function()
{
    return false;
}
document.onkeydown = function( event )
{
   	E.fire( "onKeyDown", event );
}
window.onresize = function( event )
{
    E.fire( "onResize", null );
}
document.oncontextmenu = function( event )
{
    var el = ( browser.msie  ? window.event.srcElement : event.target );

    while( el.tagName != "BODY" )
    {
        var pu = el.getAttribute( "POPUP" );
        if( pu != null )
        {
        	var id = el.getAttribute( "ID" );
            E.fire( "showPopup", { "id":id, "popup":pu });
            return false;
        }
        el =  el.parentNode;
    }
    return true;
}
var toUrl = function( url )
{
    document.location.href = url;
}
E.set( "toUrl", toUrl );
var toNewUrl = function( url )
{
    window.open( url );
}
E.set( "toNewUrl", toNewUrl );
