/**
 * Application event handling
 */

// TABS

var ApplicationEventHandler = {
	/**
	 * Force display of lists when clicking on a tab
	 */
	tabSelect: function(args) {
		var context = args.tabId;
		if (context == undefined || context == null || context.length == 0) return;
		var eventHandler = null;
		var entity = '';

		switch(context) {
			case ApplicationContext.MYPROFILE:
				Entity.edit(ApplicationContext.MYPROFILE, EntityEA.entityName, $('eaid').value);
				break;
			case ApplicationContext.CASES:
				entity = EntityCase.entityName;
				EntityCase.list(context);
				return;
			case ApplicationContext.EAS:
				entity = EntityEA.entityName;
				break;
			default:
				return;
		}
		
		if (entity.length == 0) return;
		
		try {
			var target = context + Service.SCOPE_SEPARATOR + Widget.LISTER + Service.SCOPE_SEPARATOR + entity;
			if ($(target).getTextContent().length == 0) Entity.list(context, entity, eventHandler);
		} catch(err) {;}
	},
		
	init: function() {
		document.body.style.cursor = "";
		$('message').update('');
		
		// Disable tabs on initial load
		if (document.getElementById('subtabs')) {
			document.getElementById('subtabs').tabber.tabDisable(1);
			document.getElementById('subtabs').tabber.tabDisable(2);
			document.getElementById('subtabs').tabber.tabDisable(3);
		}
		
		// EA REGISTER CONTEXT
		
		try {
			Event.observe(ApplicationContext.REGISTER + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + EntityEA.entityName, 'click',  function(event) {		
				EntityEA.handleEvent(event, ApplicationContext.REGISTER, EntityEA.entityName);
				Event.stop(event);
			});
		} catch(err) {;}

		// EA REGISTERCONFIRM CONTEXT
		
		try {
			Event.observe(ApplicationContext.REGISTERCONFIRM + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + EntityEA.entityName, 'click',  function(event) {		
				EntityEA.handleEvent(event, ApplicationContext.REGISTERCONFIRM, EntityEA.entityName);
				Event.stop(event);
			});
		} catch(err) {;}

		// EA FORGOTTENPASSWORD CONTEXT
		
		try {
			Event.observe(ApplicationContext.FORGOTTENPASSWORD + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + EntityEA.entityName, 'click',  function(event) {		
				EntityEA.handleEvent(event, ApplicationContext.FORGOTTENPASSWORD, EntityEA.entityName);
				Event.stop(event);
			});
		} catch(err) {;}

		
		// MYPROFILE CONTEXT
		
		try {
			Event.observe(ApplicationContext.MYPROFILE + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + EntityEA.entityName, 'click',  function(event) {		
				EntityEA.handleEvent(event, ApplicationContext.MYPROFILE, EntityEA.entityName);
				Event.stop(event);
			});
		} catch(err) {;}

		
		// ADMIN CONTEXT

		try {
			Event.observe(Context.ADMIN + Service.SCOPE_SEPARATOR + 'access', 'click', function(event) {
				ApplicationService.handleEvent(event, Context.ADMIN);
				Event.stop(event);
			});		
		} catch(err) {;}

		try {
			Event.observe(Context.ADMIN + Service.SCOPE_SEPARATOR + 'home' + Service.SCOPE_SEPARATOR + 'register', 'click', function(event) {
				ApplicationService.handleEvent(event, Context.ADMIN);
				Event.stop(event);
			});		
		} catch(err) {;}

		// CASES CONTEXT
		
		try {
			Event.observe('casedec' + Service.SCOPE_SEPARATOR + 'menu', 'click', function(event) {
				// Got a case DEC menu event
				EntityCase.handleEvent(event, ApplicationContext.CASES);
			});
		} catch(err) {;}
		
		try {
			Event.observe('casear' + Service.SCOPE_SEPARATOR + 'menu', 'click', function(event) {
				// Got a case AR menu event
				EntityCase.handleEvent(event, ApplicationContext.CASES);
			});
		} catch(err) {;}

		try {
			Event.observe('casegenerate', 'click', function(event) {
				// Got a case AR menu event
				EntityCase.handleEvent(event, ApplicationContext.CASES);
				Event.stop(event);
			});
		} catch(err) {;}

		try {
			// Request a searcher interface for the case list
			var target = ApplicationContext.CASES + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + EntityCase.entityName;
			
			ApplicationService.searcher(target, ApplicationContext.CASES, ApplicationWidget.SIMPLE_SEARCHER, EntityCase.entityName);
		} catch(err) {;}

		try {
			Event.observe(ApplicationContext.CASES + Service.SCOPE_SEPARATOR + EntityCase.entityName, 'click', function(event) {
				// Got a case editor event				
				EntityCase.handleEvent(event, ApplicationContext.CASES);
			});
		} catch(err) {;}

		try {
			Event.observe(ApplicationContext.CASES + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + EntityCase.entityName, 'click', function(event) {
				// Got some case search criteria
				var element = Event.element(event);
				if (element.id.endsWith('search') || element.id.endsWith('clear')) EntityCase.search(ApplicationContext.CASES, event);
				if (!element.id.endsWith('closed') )
				Event.stop(event);
			});

			Event.observe(ApplicationContext.CASES + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + EntityCase.entityName, 'keypress', function(event) {
				// Got some case search criteria
				if (event.keyCode == Event.KEY_RETURN) {
					var element = Event.element(event);
					if (element.id.endsWith('terms')) EntityCase.search(ApplicationContext.CASES, event);
					Event.stop(event);
				}
			});
		} catch(err) {;}
	
		try {
			Event.observe(ApplicationContext.CASES + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + EntityCase.entityName, 'click', function(event) {
				// Got a case list refresh event
				EntityCase.refreshList(ApplicationContext.CASES);
				$(ApplicationContext.CASES + Service.SCOPE_SEPARATOR + Service.SEARCHER + Service.SCOPE_SEPARATOR + EntityCase.entityName + Service.SCOPE_SEPARATOR + 'terms').value = '';		
				Event.stop(event);
			});
		} catch(err) {;}
	
		try {
			Event.observe(ApplicationContext.CASES + Service.SCOPE_SEPARATOR + 'create' + Service.SCOPE_SEPARATOR + EntityCase.entityName, 'click',  function(event) {
				// Got a case create event
				Entity.create(ApplicationContext.CASES, EntityCase.entityName);
				Event.stop(event);
			});
		} catch(err) {;}
	
		try {
			Event.observe(ApplicationContext.CASES + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + EntityCase.entityName, 'click',  function(event) {
				// Got a case buttons event
				var element = Event.element(event);
				if (element.id.endsWith(Service.SCOPE_SEPARATOR + 'save')) {
					Entity.save(ApplicationContext.CASES, EntityCase.entityName, 'ApplicationService.contentSuccess');
				} else if (element.id.endsWith(Service.SCOPE_SEPARATOR + 'remove')) {
					if(confirm("Are you sure you want to delete this case?"))
						EntityCase.refreshList(ApplicationContext.CASES);
				} else if (element.id.endsWith(Service.SCOPE_SEPARATOR + 'duplicate')){
					Entity.duplicate(ApplicationContext.CASES, EntityCase.entityName, null, 'ApplicationService.contentSuccess');
				}else {
					Entity.handleEvent(event, ApplicationContext.CASES, EntityCase.entityName);
				}
				Event.stop(event);
			});
		} catch(err) {;}
		

		// EAs (Admin)
		
		try {
			Event.observe(ApplicationContext.EAS + Service.SCOPE_SEPARATOR + 'list' + Service.SCOPE_SEPARATOR + EntityEA.entityName, 'click', function(event) {
				Entity.refreshList(ApplicationContext.EAS, EntityEA.entityName);
				Event.stop(event);
			});
	
			Event.observe(ApplicationContext.EAS + Service.SCOPE_SEPARATOR + 'buttons' + Service.SCOPE_SEPARATOR + EntityEA.entityName, 'click',  function(event) {
				Entity.handleEvent(event, ApplicationContext.EAS, EntityEA.entityName);
				Event.stop(event);
			});
		} catch(err) {;}
	}
}

