/*
 * 
 */

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+'-';

	TOOLBAR_ITERATIONS++;
	
	var ToolsList = document.createElement('ul');
	ToolsList.className = 'ToolList';

	this.setType = function(cName) {
		ToolsList.className = cName;
	}
	
	var MySelf = this;

	var ToolIcons = new Array();
	var HighlightIcons = 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 = 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);
		}
	}

	this.addService = function(serviceXMLObj) {
		var title = serviceXMLObj.getAttribute('title');
		var newTool = document.createElement('li');

		var ServiceLink = document.createElement('a');

		var action = '';
		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)) {
			action = 'javascript:function nil() {return false; }';
			ServiceLink.callService = function () { EventManager.triggerEvent('serviceform', serviceXMLObj	); }
		}
		ServiceLink.href = action;
		ServiceLink.id = ServiceLinkId+title;
		ServiceLink.onclick = function () {
			EventManager.triggerEvent('changetool', title);
			if(TOOLS_SHOW_AS_SELECTED) {
				this.className = 'Selected';
			}
			if(this.callService) {
				this.callService();
			}
			showSelected(this);

		}
		var icon = serviceXMLObj.getAttribute('icon');
		if(!icon) { icon = SERVICE_DEFAULT_ICON; }
		var newToolIcon = document.createElement('img');
		newToolIcon.src = icon;
		newToolIcon.id = 'ServiceIcon-'+title;

		ToolIcons[title] = icon;

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

			var highlightImg = document.createElement('img');
			highlightImg.src = altIcon;
			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.appendChild(ServiceLink);

		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 f() {
				window.open(serviceXMLObj.getElementsByTagName('help')[0].firstChild.nodeValue);
				return false;
			}
			ServiceLink.appendChild(helpButton);
		}	

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

		showUnselected(ServiceLink);
	}
	
	this.setParent = function(p) {
		ToolsParent = p;
		ToolsParent.appendChild(ToolsList);
	}

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

	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);
		}
	}
}
