var SERVICE_WINDOW_WIDTH = 600;
var SERVICE_WINDOW_HEIGHT = 300;

function ServiceWindow (EventManager) {

	this.resultsAction = function(serviceElements) {
		var serviceTitle = serviceElements.getElementsByTagName('results')[0].getAttribute('title');
		
		window.processElements[serviceTitle] = serviceElements;
		window.Events[serviceTitle] = EventManager;
		
		var serviceWindow = window.open('services/ServiceWindow.html', serviceTitle, 'width='+SERVICE_WINDOW_WIDTH+',height='+SERVICE_WINDOW_HEIGHT+',toolbar=0,status=0,menubar=0,location=0,scrollbars=yes,resizable=1');

	}

	var MySelf = this;

	this.spawn = function(serviceElements) {
		//var serviceWindow = window.open('services/ServiceWindow.html');
		if(!window.processElements) {
			window.processElements = new Array();
		}

		loadDefaultsFromMapBook(serviceElements, null, EventManager);

		var windowsToOpen = serviceElements.getElementsByTagName('window');
		for(var i = 0; i < windowsToOpen.length; i++) {
			var url = windowsToOpen[i].firstChild.nodeValue;
			if(url) {
				window.open(url);
			}
		}
		var root = serviceElements.getElementsByTagName('return')[0];
		var showWarnings = true;
		if(!SERVICE_SHOW_WARNINGS || (root && root.getAttribute('warnings') && root.getAttribute('warnings').match(/false/i))) {
			showWarnings = false;
		}

		if(!serviceElements.getElementsByTagName('results')[0]) { 
			if(showWarnings) { alert('No active layer(s) to query!'); }
			return true;
		}
		if(serviceElements.getElementsByTagName('row').length <= 0) {
			if(showWarnings) { alert('Nothing found for active layer(s)!'); }
			return true; 
		}
		if(serviceElements.getElementsByTagName('error')[0]) {
			var errorString = serviceElements.getElementsByTagName('error')[0].firstChild.nodeValue;
			alert(errorString);
			return true;
		}
		MySelf.resultsAction(serviceElements);

	}

	this.callService = function(serviceXMLObj) {

		var url = serviceXMLObj.getElementsByTagName('url')[0].firstChild.nodeValue+'?';
		var inputs = serviceXMLObj.getElementsByTagName('input');
		for(var i = 0; i < inputs.length; i++) {
			url = url + inputs[i].getAttribute('name') +'='+inputs[i].getAttribute('value')+'&';
		}

		logDebugMessage('Service Call', false, url);
		var serviceType = serviceXMLObj.getAttribute('type');
		if(!serviceType || serviceType.match(/default/i)) {
			window.open(url);
			var tmpParent = document.createElement('parent');
			var tmpResults = document.createElement('results');
			tmpParent.appendChild(tmpResults);
			tmpResults.setAttribute('title', serviceXMLObj.getAttribute('title'));
			EventManager.triggerEvent('servicereturn', tmpParent);
		} else {
			var http = getHTTPObject();
			var method = 'GET';
			var postInfo = null;
			if(url.length > 1000) {
				method = 'POST';
				var urlArr = url.split('?');
				url = urlArr[0];
				postInfo = urlArr[1];
			}
			
			http.open(method, url, true);
			if(method == 'POST') {
				http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
			}
			
			http.onreadystatechange = function () { 
				if(http.readyState == 4) {
					var results = null;
					if(http.responseXML) {
						results = http.responseXML.getElementsByTagName('return')[0];
						if(!results) {
							results = http.responseXML.getElementsByTagName('results')[0];
						}
					}
					if(!results) {
						var ret = document.createElement('return');
						
						results = document.createElement('return');
						ret.setAttribute('title', serviceXMLObj.getAttribute('title'));
						results.appendChild(ret);
						
					}
					results.setAttribute('title', serviceXMLObj.getAttribute('title'));
					EventManager.triggerEvent('servicereturn', results);
				}
			};
			http.send(postInfo);
		}
	}

}
