﻿///////////////////////////////////////////////////////////////////
// Implements a basic class used by any other class that requires 
// event notification
//
// by Vasile Marita, www.tara-oasului.com
// If you use this code please let this info here, thanks
//
// Created on May 14, 2007
// 
///////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////
///    JS_ObjectEvent
//////////////////////////////////////////////////////////
function JS_ObjectEvent ( target, method, eventName )
{
	this.EventName	= eventName;
	this.Target			= target;
	this.CallBack		= method;
}

JS_ObjectEvent.prototype.toString = function ()
{
	return "[ JS_ObjectEvent Object. Event : " + this.EventName + ". Target : " + this.Target + ". CallBack : " + this.CallBack + " ]";
}

//////////////////////////////////////////////////////////
///    JS_Object
//////////////////////////////////////////////////////////

function JS_Object()
{
}

JS_Object.prototype.create = function ()
{
}

JS_Object.prototype.toString = function ()
{
	return "[ JS_Object Object ]";
}

JS_Object.prototype.checkConnected = function( target, eventName, targetMethod )
{
	for ( var i in this.ConnectionPoints )
	{
		var connection = this.ConnectionPoints [ i ];

		if ( connection.Target == target && connection.EventName	== eventName && connection.CallBack		== targetMethod )
			return true;
	}

	return false;
}

JS_Object.prototype.attachEventTarget = function( target, eventName, targetMethod )
{
	if ( ! this.ConnectionPoints )
		this.ConnectionPoints = new Array();

	if ( !this.checkConnected (target, eventName, targetMethod) )
		this.ConnectionPoints.push( new JS_ObjectEvent(target,targetMethod, eventName ) );
}

JS_Object.prototype.dettachEventTarget = function( target, eventName )
{
	if (!this.ConnectionPoints)
		return;

	for ( var i in this.ConnectionPoints )
	{
		if (this.ConnectionPoints[i].Target == target && this.ConnectionPoints[i].EventName == eventName )
		{
			var elem = this.ConnectionPoints[i];
			this.ConnectionPoints.splice(i, 1);
			delete elem;
			break;
		}
	}
}

JS_Object.prototype.detachAllEvents = function()
{
	if ( !this.ConnectionPoints )
		return;

	for ( var i in this.ConnectionPoints )
		delete this.ConnectionPoints [ i ];

	this.ConnectionPoints.splice ( 0, this.ConnectionPoints.length );
}

JS_Object.prototype.eventAttached = function ( eventName )
{
	if ( ! this.ConnectionPoints )
		return false;

	for (var i in this.ConnectionPoints )
	{
		if ( this.ConnectionPoints[i].EventName == eventName )
			return true;
	}
	
	return false;
}

JS_Object.prototype.invokeEvent = function ( eventName )
{
	if ( ! this.ConnectionPoints )
	{
		return;
	}
	var result = null;
	var paramExpr = "";

	if ( arguments.length > 1)
		for ( var p = 1 ; p < arguments.length ; p ++ )
			paramExpr += ( 'arguments['+p+']' + (p<arguments.length-1 ? ',' : '') );

	for ( var i in this.ConnectionPoints )
	{
		var connection_point = this.ConnectionPoints [ i ];

		if ( connection_point.EventName == eventName )
		{
			expr = '';
			indexParam = connection_point.CallBack.indexOf("(");

			if ( indexParam > 0 )
				expr = 'if (null != connection_point.Target.' + connection_point.CallBack.substr(0,indexParam)+') result = connection_point.Target.' + connection_point.CallBack + ';';
			else
				expr = 'if (null != connection_point.Target.' + connection_point.CallBack+') result = connection_point.Target.' + connection_point.CallBack + '(' + paramExpr + ');';

			try
			{
				eval( expr );
			}
			catch (error)
			{
			}
		}
	}
	return result;
}

