/*
 * 
 */

var TOOLBAR_ITERATIONS = 0;

function GTools(EventManager) {
	var ToolsParent;

	/*
	 * Some settings taken from the globals
	 */
	this.SHOW_TEXT = TOOLBAR_TEXT;
	this.SHOW_IMAGE = TOOLBAR_IMAGE;

	var ServiceLinkId = 'ServiceLink-'+TOOLBAR_ITERATIONS+'-';
	var ServiceToolId = 'ServiceTool-'+TOOLBAR_ITERATIONS+'-';

	var ToolsListId = 'ToolsList-'+TOOLBAR_ITERATIONS+'-';
	var ToolsListClassName = 'ToolList';
	TOOLBAR_ITERATIONS++;

	this.setType = function(cName) {
		ToolsListClassName = cName;
		var toolsList = document.getElementById(ToolsListId);
		if(toolsList) {
			toolsList.className = cName;
		}
	}
	
	var MySelf = this;

	var ToolIcons = new Array();

	function showNone(link) {
		if(link.highlightIcon) {
			link.highlightIcon.style.visibility = 'hidden';
			link.highlightIcon.style.width = '1px';
			link.highlightIcon.style.paddingLeft = '0px';
		}
		if(link.toolIcon) {
			link.toolIcon.style.visibility = 'hidden';
			link.toolIcon.style.width = '1px';
			link.toolIcon.style.paddingRight = '0px';
		}

	}
	
	function showUnselected(link) {
		if(link.toolIcon) {
			link.toolIcon.style.visibility = 'visible';
			link.toolIcon.style.width = 'auto';
			link.toolIcon.style.paddingRight = '1px';
		}
		if(link.highlightIcon) {
			link.highlightIcon.style.visibility = 'hidden';
			link.highlightIcon.style.width = '1px';
			link.highlightIcon.style.paddingLeft = '0px';
		}
	}

	function showSelected(link) {
		if(link.toolIcon && link.highlightIcon) {
			link.toolIcon.style.visibility = 'hidden';
			link.toolIcon.style.width = '1px';
			link.toolIcon.style.paddingRight = '0px';
		}
		if(link.highlightIcon) {
			link.highlightIcon.style.visibility = 'visible';
			link.highlightIcon.style.width = 'auto';
			link.highlightIcon.style.paddingLeft = '1px';
		}
	}

	this.clearToolSelection = function(title) {
		var links = document.getElementById(ToolsParent).getElementsByTagName('a');//toolsList.getElementsByTagName('a');
		for(var i = 0; i < links.length; i++) {
			if(links[i].className == 'Selected') {
				links[i].className = '';
			}
			showUnselected(links[i]);
					
		}
	}

	this.showSelectedTool = function(toolName) {
		var tool = document.getElementById(ServiceLinkId+toolName);	
		MySelf.clearToolSelection();
		if(tool) {
			showSelected(tool);
		}
	}

	function onclickServiceLink() {
		var title = this.title;
		var sel = this.selectable;
		if(sel) {
			EventManager.triggerEvent('changetool', title);
			if(TOOLS_SHOW_AS_SELECTED) {
				this.className = 'Selected';
			}
			showSelected(this);
		}
		if(this.callService) {
			this.callService();
		}

	}

	function onclickCallService() {
		EventManager.triggerEvent('serviceform', this.selfXML);
	}

	var KnownServices = new Array();
	var SelectToolAtStartup = null;

	this.addService = function(serviceXMLObj) {
		var title = serviceXMLObj.getAttribute('title');
		KnownServices[title] = serviceXMLObj;
		var toolsList = document.getElementById(ToolsListId);
		if(toolsList) {
			renderServiceInToolbar(title);
		}
	}

	function renderServiceInToolbar(title) {
		var serviceXMLObj = KnownServices[title];
		var rendered = serviceXMLObj.getAttribute(ToolsListId+'rendered');
		if(rendered && rendered == '1') { return true; }	/* Already rendered this one cheif! */
		serviceXMLObj.setAttribute(ToolsListId+'rendered','1');

		var toolsList = document.getElementById(ToolsListId);

		var newTool = document.createElement('li');
		var ServiceLink = document.createElement('a');
		ServiceLink.selfXML = serviceXMLObj;
		
		//document.getElementById(ToolsListId).appendChild(newTool);
		toolsList.appendChild(newTool);
		newTool.appendChild(ServiceLink);

		var action = '';
		var selectable = serviceXMLObj.getAttribute('selectable');
		if(selectable && selectable.match(/false/i)) {
			ServiceLink.selectable = false;
		} else {
			ServiceLink.selectable = true;
		}
		
		if(serviceXMLObj.getAttribute('type') && serviceXMLObj.getAttribute('type').match(/internal/i)) {
			action = 'javascript:'+serviceXMLObj.getAttribute('command');
//		} else if(!serviceXMLObj.getAttribute('type') || !serviceXMLObj.getAttribute('type').match(/internal/i)) {
		} else {
			action = 'javascript:function nil() {return false; }';
			ServiceLink.callService = onclickCallService;
		}
		ServiceLink.title = title;
		ServiceLink.href = action;
		ServiceLink.id = ServiceLinkId+title;
		ServiceLink.onclick = onclickServiceLink;
		var icon = serviceXMLObj.getAttribute('icon');
		if(!icon) { icon = SERVICE_DEFAULT_ICON; }
		var newToolIcon = document.createElement('img');
		newToolIcon.src = icon;
		newToolIcon.id = ServiceToolId+'Icon-'+title;

		ToolIcons[title] = icon;

		var altIcon = serviceXMLObj.getAttribute('highlight');
		if(MySelf.SHOW_IMAGE && altIcon) {
			ServiceLink.setAttribute('highlightIconSrc',altIcon);

			var highlightImg = document.createElement('img');
			highlightImg.src = altIcon;
			highlightImg.id = ServiceToolId+'Highlight-'+title;
			ServiceLink.appendChild(highlightImg);
			ServiceLink.highlightIcon = highlightImg;
		}
		if(MySelf.SHOW_IMAGE) {
			ServiceLink.appendChild(newToolIcon);
			ServiceLink.toolIcon = newToolIcon;
			ServiceLink.setAttribute('toolIconSrc',ToolIcons[title]);
		}
		if(MySelf.SHOW_TEXT) {
			ServiceLink.appendChild(document.createTextNode(title));
		}
		ServiceLink.title = title;

		newTool.id = ServiceToolId+title;
		if(serviceXMLObj.getElementsByTagName('help')[0] && serviceXMLObj.getElementsByTagName('help')[0].firstChild.nodeValue) {
			var helpButton = document.createElement('img');
			helpButton.src = HELP_DEFAULT_ICON;
			helpButton.onclick = function () {
				window.open(serviceXMLObj.getElementsByTagName('help')[0].firstChild.nodeValue);
				return false;
			}
			ServiceLink.appendChild(helpButton);
		}	

		
		if(TOOLS_SHOW_REMOVE && (!serviceXMLObj.getAttribute('locked') || !serviceXMLObj.getAttribute('locked').match(/true/i))) {
			var removeButton = createIcon(EventManager, REMOVE_ICON,'removeservice',title);
			ServiceLink.appendChild(removeButton);
		}
		

		showUnselected(ServiceLink);
	}
	
	this.setParent = function(p) {
		ToolsParent = p;
		var toolsList = document.createElement('ul');
		toolsList.setAttribute('id', ToolsListId);
		toolsList.className = ToolsListClassName;
		document.getElementById(ToolsParent).appendChild(toolsList);
		toolsList = null;
		for(var s in KnownServices) {
			renderServiceInToolbar(s);
		}
		if(SelectToolAtStartup) {
			this.selectTool(SelectToolAtStartup);
			SelectToolAtStartup = null;
		}
	}

	this.removeService = function(serviceTitle) {
		var serv = document.getElementById(ServiceToolId+serviceTitle);
		if(serv) {
			var p = serv.parentNode;
			p.removeChild(serv);
			if(KnownServices[serviceTitle]) {
				delete KnownServices[serviceTitle];
			}
		}
	}

	this.toolWaiting = function(serviceXMLObj) {
		if(serviceXMLObj.getAttribute('type') && serviceXMLObj.getAttribute('type').match(/search/i)) {
			var title = serviceXMLObj.getAttribute('title');
			var link = document.getElementById(ServiceLinkId+title);
			if(link) { 
				showNone(link);
				link.waitIcon = document.createElement('img');
				link.waitIcon.src = MAP_LOADING_ICON;
				link.insertBefore(link.waitIcon,link.childNodes[0]);
			}
		}
	}
	
	this.restoreTool = function(serviceXMLObj) {
		var title = serviceXMLObj.getAttribute('title');
		if(!title) { 
			var root = serviceXMLObj.getElementsByTagName('results')[0]; 
			title = root.getAttribute('title');
		}
		var link = document.getElementById(ServiceLinkId+title);
		if(link && link.waitIcon) {
			link.removeChild(link.waitIcon);
			showUnselected(link);
		}
	}

	this.selectTool = function(toolName) {
		var tool = document.getElementById(ServiceLinkId+toolName);	
		if(tool) {
			tool.onclick();
			if(tool.href) {
				var js = tool.href;
				js = js.replace('javascript:','');
				eval(js);
			}
		} else {
			SelectToolAtStartup = toolName;
		}
	}

	this.initializeEvents = function() {
		EventManager.addEventListener('addservice', ToolsListId, MySelf.addService);
		EventManager.addEventListener('removeservice', ToolsListId, MySelf.removeService);
		EventManager.addEventListener('changetool', ToolsListId, MySelf.clearToolSelection);
		EventManager.addEventListener('callservice', ToolsListId, MySelf.toolWaiting);
		EventManager.addEventListener('servicereturn', ToolsListId, MySelf.restoreTool);
		EventManager.addEventListener('selecttool', ToolsListId, MySelf.selectTool);
		EventManager.addEventListener('changetool', ToolsListId, MySelf.showSelectedTool);
	}
}
