﻿function CreateDelegate( instance, obj, method ) 
{
    return function() 
    {
        return method.call(instance, obj, arguments);
    }
}

function CustomEvent( eventName, instance )
{
    this.eventName = eventName;
    
    this.subscribedMethods = [];
    
    this.fire = function( args )
    {
        for( var i = 0; i < this.subscribedMethods.length; i++ )
        {
            if( typeof args != "array" )
                args = [ args ];
            this.subscribedMethods[ i ]( instance, args );
        }
    }
}

CustomEvent.prototype.subscribe = function( method )
{    
    this.subscribedMethods.push( method )
    //object.events[ object.events.length ] = method;
    
    
}

function addEvent( object, eventName, method, instance )
{
    var delegate = CreateDelegate( instance, object, method );
    
    if( !object.customObject ) // process HTML element
    {
        if( object.addEventListener )
        {
            object.addEventListener( eventName, delegate, false );
            return;
        }
        else if( object.attachEvent )
        {
            return object.attachEvent( "on" + eventName, delegate );
        }
    }
    else
    {
        object[ eventName ].subscribe( method );
    }
}

function getQueryStringValue( key )
{
    var queryString = window.location.search;
    
    if( queryString.length > 0 )
        queryString = queryString.substr( 1 ); // remove initial "?"
        
    var pairs = queryString.split( "&" );
    var keys = {};
    
    for( var i = 0, len = pairs.length; i < len; i++ )
    {
        var pair = pairs[ 0 ].split( "=" );
        
        if( pair[ 0 ] == key )
            return pair[ 1 ];
    }
    
    return false;
    //"?st=9/8/2007"
}
