if ((top.EventServiceInit == "undefined") || (top.EventServiceInit != true))
{

	/**
	 *
	 * EventService CLASS definition.
	 *
	 */

	function EventService ()
	{
		this.eventTree = new Array();
		EventService.theInstance = this;
	}

	function EventEntry(eventId, handler) {
		this.eventId = eventId;
		this.handler = handler;
	}

	EventService.prototype.subscribe = function (
		 publisher,
		 eventName,
		 eventId,
		 handlerReference )
	{
		if ( publisher == null )
			publisher = "-";
		if ( eventName == null )
			eventName = "-";
		if ( eventId == null )
			eventId = "-";

		eventN = "ALL" + "." + publisher + "." + eventName + "." + eventId;

		if ( top.logger)
			top.logger.logMsg("DEBUG","EVENT SERVICE - SUBSCRIBE: ",eventN);

		objEvtEntry = new EventEntry( eventN, handlerReference);
		this.eventTree[this.eventTree.length] = objEvtEntry;
	 }

	/**
	 * Unsubscribe the specified handler by deleting all corresponding event entry
	 */
	EventService.prototype.unsubscribe = function(handlerReference)
	{
		if (handlerReference != null) {
			for (count=0; count < this.eventTree.length; count++) {
				//check whether event entry is already unsubscribed
				if (this.eventTree[count] != null) {
					var evtEntry = this.eventTree[count];
					if (handlerReference == evtEntry.handler) {
						this.eventTree[count] = null;
					}
				}
			}
		}
	}

	EventService.prototype.publish = function (
		publisher,
		eventName,
		eventId,
		payLoad )
	{
		var objXMLWrap = new XmlWrapper();
		payLoad = objXMLWrap.wrapXml(payLoad);
		objXMLWrap = null;

		if ( publisher == null)
			publisher = "-";
		if ( eventName == null)
			eventName = "-";
		if (eventId == null)
			eventId = "-";

		eventN = "ALL" + "." + publisher + "." + eventName + "." + eventId;

		if ( top.logger){
				top.logger.logMsg("DEBUG","EVENT SERVICE - PUBLISH: ",eventN);
				top.logger.logMsg("DEBUG","EVENT SERVICE - PAYLOAD: ",payLoad);
			}

		var count = 0;
		for (count=0; count < this.eventTree.length; count++) {
			//check whether event entry is already unsubscribed
			if (this.eventTree[count] != null) {
				myEvtEntry = ""+this.eventTree[count].eventId;
				if (this.EventCompare(myEvtEntry,eventN)){
					if ( top.logger)
								top.logger.logMsg("DEBUG","EVENT SERVICE - HANDLER:"," Matched for Event : " + myEvtEntry);
					if (this.eventTree[count].handler != null)
						this.eventTree[count].handler.handle(publisher, eventName, eventId,  payLoad);
				}
			}
		}
	}

	EventService.prototype.EventCompare = function (pEvtOne, pEvtTwo)
	{
		var arrEvtOne = pEvtOne.split(".");
		var arrEvtTwo = pEvtTwo.split(".");
		var arrLength = arrEvtOne.length;

		if( arrLength != arrEvtTwo.length)
			return false;
		var i = 0;
		for ( i = 0; i < arrLength; i++)
		{
			if(( arrEvtOne[i] != arrEvtTwo[i]) &&( arrEvtOne[i] != "-"))
					return false;
		}
		return true;
	}

	EventService.prototype.unsubscribeMethod = function (elementId) {
	}


	/**
	 * Factory class to create the instance of EventService or EventServiceProxy
	 */

	function EventServiceFactory() {
	}

	/**
	 * Returns the instance of EventServiceProxy if the current window is a dialog. Returns
	 * an instance of EventService otherwise
	 * @returns an instance of EventService or EventServiceproxy
	 */

	EventServiceFactory.getEventService = function()
	{
		if (Utils.isDialog(window) == false) { //@todo xiaow
			return new EventService();
		} else {
			return new EventServiceProxy();
		}
	}



	/**
	 * EventServiceProxy is the local representive for EventService in dialog windows.
	 *
	 * It sends all event service requst of local components in dialog window to the
	 * EventService instance in the main browser.
	 *
	 * The EventServiceProxy class has to maintain the same interface as the EventService class
	 */

	function EventServiceProxy() {

		if (window.dialogArguments.evtSvc == null) {
			top.logger.logMsg("ERROR","EventServiceProxy()","Can not find the main EventService instance.");
		}
		this.evtSvc = window.dialogArguments.evtSvc;
	}

	EventServiceProxy.prototype.subscribe = function(
		 publisher,
		 eventName,
		 eventId,
		 handlerReference )
	{
		this.evtSvc.subscribe(publisher, eventName, eventId, handlerReference);
	}

	EventServiceProxy.prototype.unsubscribe = function(handlerReference)
	{
		this.evtSvc.unsubscribe(handlerReference);
	}

	EventServiceProxy.prototype.publish = function (
		publisher,
		eventName,
		eventId,
		payLoad )
	{
		this.evtSvc.publish(publisher, eventName, eventId, payLoad);
	}


	//////////////////////////////////////

	top.EventServiceInit = true;
	top.eventService = EventServiceFactory.getEventService();
}