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

	}

	this.passthroughHandler = function(url) {
		alert('internal');
		window.open(url);
	}

	this.csvHandler = function(url) {
		return false;
	}

	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+'?';
		//alert(serviceXMLObj.getAttribute('selectable')); // Indicates whether the user can do something or its just clicked.
		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');
		var returnType = serviceXMLObj.getAttribute('return');
		var displayType = serviceXMLObj.getAttribute('display');

		if(!returnType) { returnType = 'xml'; }
		if(!displayType) { displayType = 'div'; }

		if(displayType.match(/window/i)) {
			window.open(url);
		} else if(returnType.match(/html/i)) {
			MySelf.passthroughHandler(url);
		} else if(returnType.match(/csv/i)) {
			MySelf.csvHandler(url);
		} else if(returnType.match(/xml/) && (!serviceType || serviceType.match(/default/i))) {
			var tmpParent = document.createElement('parent');
			var tmpResults = document.createElement('results');
			tmpParent.appendChild(tmpResults);
			tmpResults.setAttribute('title', serviceXMLObj.getAttribute('title'));
			EventManager.triggerEvent('servicereturn', tmpParent);
			tmpParent = null;
			tmpResults = null;
		} 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);
		}
	}

}
