﻿	// TODO: Merge functionality with new graphs.

	GraphMapServices = function(){};

	requestComplete                          = 4;
	httpStatusOk                             = 200;
	GraphMapServices.prototype.dataStoreURL  = '/Buoyager/System/DataStore.aspx?containerID=';
	GraphMapServices.prototype.CreateRequest = function() 
	{
		try {
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch(e) {
			// Internet Explorer
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e){
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		return xmlHttp;
	}
	
	GraphMapServices.prototype.BindMap = function(id, newMap) 
	{
		var
		map           = document.getElementById(id),
		graphID       = map.name.substring(0, map.name.length - 3),
		graph         = document.getElementById(map.name.substring(1, map.name.length - 3));
		if(typeof(hideLoadingIcon) != "undefined") {
			hideLoadingIcon(graphID);
		}
		map.innerHTML = newMap;
	}

	GraphMapServices.prototype.UnbindMap = function(id)
	{
		document.getElementById(id).innerHTML = "";
	}
	
	GraphMapServices.prototype.CreateHandler = function(mapState, xmlHttp) 
	{
		return function()
		{
			if(xmlHttp.readyState == requestComplete) {
				if(xmlHttp.status == httpStatusOk) {
					mapState.SetMap(xmlHttp.responseText);
				}
				else {
					mapState.SetRequestFailed();
				}
			}
		}
	}

	GraphMapServices.prototype.PostRequest = function(mapState) {
		var
		xmlHttp = this.CreateRequest();
		
		xmlHttp.onreadystatechange = this.CreateHandler(mapState, xmlHttp);
	    xmlHttp.open("GET", this.dataStoreURL + mapState.id,true);
		xmlHttp.send(null);
	}
	
	function
	MapState(id, mapServices)
	{
		this.id            = id;
		this.map           = "";
		this.bound         = false;
		this.requestIssued = false;
		this.mapServices   = mapServices;
	}
	
	MapState.prototype.hasMap       = function() { return this.map.length > 0; }
	MapState.prototype.Bind         = function() 
	{
		this.bound = true;
		if(this.hasMap()) {
			this.mapServices.BindMap(this.id, this.map);
		}
		else if(!this.requestIssued) {
			this.mapServices.PostRequest(this);
			this.requestIssued = true;
		}
	}
	MapState.prototype.Unbind       = function() 
	{
		if(this.isBound()) {
			this.mapServices.UnbindMap(this.id);
			this.bound = false;
		}
	}
	MapState.prototype.isBound      = function() 
	{
		return this.bound;
	}
	MapState.prototype.SetMap       = function(map) 
	{
		this.map = map;
		if(this.isBound()) {
			this.mapServices.BindMap(this.id, this.map);
			this.requestIssued = false;
		}
	}
	MapState.prototype.SetRequestFailed = function() 
	{
		this.requestIssued = false;
		this.bound         = false;
	}
	
	function 
	MapStates(mapServices)
	{
		this.mapServices = mapServices;
	}

	MapStates.prototype.data        = new Array();

	MapStates.prototype.AddMapState = function(newMapState) 
	{
		this.data[newMapState.id]   = newMapState;
	}

	MapStates.prototype.GetMapState = function(id) 
	{
		if(typeof(this.data[id]) == "undefined") {
			this.AddMapState(new MapState(id, this.mapServices));
		}
		return this.data[id];
	}

	var
	mapStates = new MapStates(new GraphMapServices());
		
	function ConnectMap(id, parameter)
	{
		if(typeof(showLoadingIcon) != "undefined") {
			showLoadingIcon(parameter);
		}
		mapStates.GetMapState(id).Bind();
	}
	
	function DisconnectMap(id, eventDetails)
	{
		var target;
		
		if(eventDetails) {
			target = eventDetails.relatedTarget;
		}
		else {
			target = window.event.toElement;
		}
		
		if(target && (target.id.length > 0)) {
			mapStates.GetMapState(id).Unbind();
		}
	}
