﻿
///////////////////////////////////////////////////////////////////////////////
// RcnClientFx
///////////////////////////////////////////////////////////////////////////////

var RcnClientFx = new _RcnClientFx();

function _RcnClientFx()
{
}

_RcnClientFx.prototype.GetRcnObjectById = function()
{
    if ( arguments.length )
    {
        var element = document.getElementById( this.elementId );
        return element.rcnObject;
    }
    return null;
}

_RcnClientFx.prototype.GetRcnObject = function(element)
{
    return element.rcnObject;
}

_RcnClientFx.prototype.AttachRcnObject = function(element, obj)
{
    element.rcnObject = obj;
}

_RcnClientFx.prototype.CopyPrototype = function(descendant, parent) 
{   
    var sConstructor = parent.toString();   
    var aMatch = sConstructor.match( /\s*function (.*)\(/ );   
    if ( aMatch != null ) 
    { 
        descendant.prototype[aMatch[1]] = parent;
    }   
    for (var m in parent.prototype) 
    {   
        descendant.prototype[m] = parent.prototype[m];   
    }   
    sConstructor = descendant.toString();   
    aMatch = sConstructor.match( /\s*function (.*)\(/ );   
    if ( aMatch != null ) 
    { 
        descendant.prototype.className = aMatch[1];
    }   
    descendant.baseClass = parent.prototype;
}  

_RcnClientFx.prototype.ShowPrototypes = function(src)
{
    var str = "";
    for (var m in src.prototype) 
    {   
        str += m + "\r\n";
    } 
    return str;
}


///////////////////////////////////////////////////////////////////////////////
// RcnObject
///////////////////////////////////////////////////////////////////////////////

function RcnObject()
{
    this.InitInstance();
}

RcnObject.prototype.ClassName = function()
{
    return this.className;
}

RcnObject.prototype.InitInstance = function()
{
    if(!this.className)
    {
        this.className = 'RcnObject';
        this.baseClass = null;
    }
}

///////////////////////////////////////////////////////////////////////////////
// RcnDictionary
///////////////////////////////////////////////////////////////////////////////
RcnClientFx.CopyPrototype(RcnDictionary, RcnObject);

function RcnDictionary()
{
    this.InitInstance();
}

RcnDictionary.prototype.InitInstance = function()
{
    RcnDictionary.baseClass.InitInstance.call(this);
}

RcnDictionary.prototype.Lookup = function(key) 
{
    return this[key];
}

RcnDictionary.prototype.Add = function()
{
    for (count=0; count < arguments.length; count += 2) 
    {
        this[arguments[count]] = arguments[count+1];
    }
}

RcnDictionary.prototype.Delete = function(key) 
{
    for (arg in arguments) 
    {
        this[arg] = null;
    }
}

///////////////////////////////////////////////////////////////////////////////
// RcnElement
///////////////////////////////////////////////////////////////////////////////

RcnClientFx.CopyPrototype(RcnElement, RcnObject);

function RcnElement()
{
    if ( arguments.length )
        this.InitInstance(arguments[0]);
}

RcnElement.prototype.InitInstance = function(id)
{
    RcnElement.baseClass.InitInstance.call(this);
    this.elementId = id;
    this.element = document.getElementById(this.elementId);
    if( this.element )
    {
        RcnClientFx.AttachRcnObject(this.element, this);
    }
}

RcnElement.prototype.ElementId = function()
{
    // get
    return this.elementId;
}

RcnElement.prototype.Element = function()
{
    // get
    return this.element;
}

///////////////////////////////////////////////////////////////////////////////
// RcnEvent
///////////////////////////////////////////////////////////////////////////////

RcnClientFx.CopyPrototype(RcnEvent, RcnObject);

function RcnEvent(src)
{
    if ( arguments.length )
        this.InitInstance(arguments[0]);
}

RcnEvent.prototype.InitInstance = function(src)
{
    RcnEvent.baseClass.InitInstance.call(this);
    this.src = src;
}

RcnEvent.prototype.Src = function()
{
    // get
    return this.src;
}

///////////////////////////////////////////////////////////////////////////////
// RcnEventPublisher
///////////////////////////////////////////////////////////////////////////////

RcnClientFx.CopyPrototype(RcnEventPublisher, RcnObject);

function RcnEventPublisher()
{
    this.InitInstance();
}

RcnEventPublisher.prototype.InitInstance = function()
{
    RcnElement.baseClass.InitInstance.call(this);
    this.eventDictionary = new RcnDictionary();
}

RcnEventPublisher.prototype.AttachEvent = function(eventObj, subscriber)
{
    var eventArr = this.eventDictionary.Lookup(eventObj.ClassName);
    if( eventArr == null )
    {
        eventArr = new Array();    
        this.eventDictionary.Add(eventObj.ClassName, eventArr);
    }
    eventArr.push(subscriber);
}

RcnEventPublisher.prototype.TrigEvent = function(eventObj)
{
    var eventArr = this.eventDictionary.Lookup(eventObj.ClassName);
    if( eventArr != null )
    {
        for( subscriber in eventArr )
        {
            eventArr[subscriber].HandleEvent(eventObj);
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
// RcnEventSubscriber
///////////////////////////////////////////////////////////////////////////////

RcnClientFx.CopyPrototype(RcnEventSubscriber, RcnObject);

function RcnEventSubscriber()
{
    this.InitInstance();
}

RcnEventSubscriber.prototype.InitInstance = function()
{
    RcnEventSubscriber.baseClass.InitInstance.call(this);
}

RcnEventSubscriber.prototype.HandleEvent = function(eventObj)
{
    alert("Override function 'HandleEvent' for event '" + eventObj.ClassName() + "'");
}

///////////////////////////////////////////////////////////////////////////////
// RcnElementEventSubscriber
///////////////////////////////////////////////////////////////////////////////

RcnClientFx.CopyPrototype(RcnElementEventSubscriber, RcnEventSubscriber);
RcnClientFx.CopyPrototype(RcnElementEventSubscriber, RcnElement);
RcnElementEventSubscriber.baseClass = RcnElement.prototype;
RcnElementEventSubscriber.baseClass2 = RcnEventSubscriber.prototype;

function RcnElementEventSubscriber()
{
    if( arguments.length )
        this.InitInstance(arguments[0]);
}

RcnElementEventSubscriber.prototype.InitInstance = function(id)
{
    RcnElementEventSubscriber.baseClass2.InitInstance.call(this);
    RcnElementEventSubscriber.baseClass.InitInstance.call(this, id);
}

///////////////////////////////////////////////////////////////////////////////
// RcnElementEventPublisher
///////////////////////////////////////////////////////////////////////////////

RcnClientFx.CopyPrototype(RcnElementEventPublisher, RcnEventPublisher);
RcnClientFx.CopyPrototype(RcnElementEventPublisher, RcnElement);
RcnElementEventPublisher.baseClass = RcnElement.prototype;
RcnElementEventPublisher.baseClass2 = RcnEventPublisher.prototype;

function RcnElementEventPublisher()
{
    this.InitInstance();
}

RcnElementEventPublisher.prototype.InitInstance = function(id)
{
    RcnElementEventSubscriber.baseClass2.InitInstance.call(this);
    RcnElementEventSubscriber.baseClass.InitInstance.call(this, id);
}

///////////////////////////////////////////////////////////////////////////////
// RcnElementEventPublisherSubscriber
///////////////////////////////////////////////////////////////////////////////

RcnClientFx.CopyPrototype(RcnElementEventPublisherSubscriber, RcnEventPublisher);
RcnClientFx.CopyPrototype(RcnElementEventPublisherSubscriber, RcnElementEventSubscriber);
RcnElementEventPublisherSubscriber.baseClass = RcnElementEventSubscriber.prototype;
RcnElementEventPublisherSubscriber.baseClass2 = RcnEventPublisher.prototype;

function RcnElementEventPublisherSubscriber()
{
    this.InitInstance();
}

RcnElementEventPublisherSubscriber.prototype.InitInstance = function(id)
{
    RcnElementEventPublisherSubscriber.baseClass2.InitInstance.call(this);
    RcnElementEventPublisherSubscriber.baseClass.InitInstance.call(this, id);
}





