
MWS = {
	MWS_Version: "1.0",
	MWS_ArrService: null,
	MWS_TimerRefreshInterval: 10,
    	MWS_TimerRunning: false,
	MWS_Count:0,
	MWS_CurServiceID: "",

	MWS_lonlat: null,
	MWS_SessionID: "000000000",
	MWS_x: 0,
	MWS_y: 0,
	MWS_zoom: null,
	MWS_scale: null,
	MWS_Form: null,
	MWS_MapObj: null,

	MWS_ServiceBaseUrl: "",
	MWS_DefaultMapExtent: null,

	MWS_RefreshX: 1,
	MWS_RefreshY: 1,

	MWS_PopUp: null,
	MWS_ExternalFrameDocumentObj: null,

	MWS_WaitingDiv: '',
	MWS_WaitingImage: '',
	SiteReferrer: '',

	MWS_addService: function (id, name, type) {
		var validService = 0;

		if (this.MWS_Count==0)
			this.MWS_ArrService=new Array(1);

		if (type=="event")
			validService=1;
		else if (type=="time")
			validService=1;
		else if (type=="context")
			validService=1;

		if (validService==1)
		{
			this.MWS_ArrService[this.MWS_Count]=new MWS_Service(id, name, type);
			this.MWS_Count++;
		}
	},


	MWS_Select: function (webServiceID) {
		if (webServiceID!='')
		{
			var svc = this.MWS_getService(webServiceID);
			
			if (svc != null)
			{
				if (svc.Type=="time")
				{
					this.MWS_TBSelect(webServiceID);
				}
				else if (svc.Type=="event")
				{
					this.MWS_EBSelect(webServiceID);
				}
				else
				{
					var fn = svc.StartUpFunction;
					if (fn!=null)
						fn();
				}
			}
		}
	},

	MWS_EBSelect: function (webServiceID) {
		if (webServiceID!='')
		{
			this.MWS_CurServiceID = webServiceID;
			var svc = this.MWS_getService(webServiceID);
			if (svc!=null)
			{
				var fn = svc.StartUpFunction;
				if (fn!=null)
					fn();
			}
			
		}
	},

	MWS_TBSelect: function (webServiceID) {
		if (webServiceID!='')
		{
			if (!this.MWS_TimerRunning)
			{
				this.MWS_TimerRunning = true;
				MWSTimer_ServiceRunner();
			}
			
			var svc = this.MWS_getService(webServiceID);
			if (svc!=null)
			{
				var fn = svc.StartUpFunction;
				if (fn!=null)
					fn();

				var div = OpenLayers.Util.getElement(this.MWS_getServiceDiv(webServiceID, 0));
				if (div!=null)
				{
					div.innerHTML=svc.ResultsInitText;
					eval("MWS.MWS_Form."+webServiceID+"_Monitor.value='"+svc.TimerButtonText+"';");
				}
				else
					alert("Init text div not defined!");

			}
		}
	},

	MWS_MapClick: function (webServiceID) {
		var webServiceUrlFunction;

		if (webServiceID!='')
		{
			var fn = this.MWS_getServiceProperty
				(webServiceID, "event", "MapClickFunction");

			if (fn!=null)
			{
				fn(webServiceID, this.MWS_x, this.MWS_y,
					this.MWS_SessionID,this.MWS_zoom,this.MWS_scale);
			}
		}
	},

	MWS_getService: function (webServiceID){
		var theService = null;

		for( var i = 0; i < this.MWS_ArrService.length; i++) 
			if (this.MWS_ArrService[i].ID == webServiceID)
				theService = this.MWS_ArrService[i];
		return theService;
	},

	MWS_getServiceProperty: function (webServiceID, webServiceType, propertyName){
		var propertyValue = null;
		for( var i = 0; i < this.MWS_ArrService.length; i++) 
		if ((this.MWS_ArrService[i].ID == webServiceID) &&
			(this.MWS_ArrService[i].Type == webServiceType))
		{
			propertyValue = this.MWS_ArrService[i].getProperty(propertyName);

		}
		return propertyValue;
	},

	MWS_setServiceProperty: function (webServiceID, webServiceType, propertyName, propertyValue){
		for( var i = 0; i < this.MWS_ArrService.length; i++) 
		if ((this.MWS_ArrService[i].ID == webServiceID) &&
			(this.MWS_ArrService[i].Type == webServiceType))
		{
			this.MWS_ArrService[i].setProperty(propertyName, propertyValue);
		}
	},

	MWS_setServiceFunction: function (webServiceID, functionObject){
		for( var i = 0; i < this.MWS_ArrService.length; i++) 
			if (this.MWS_ArrService[i].ID == webServiceID)
				this.MWS_ArrService[i].setFunction(functionObject);
	},

	MWS_getServiceFunction: function (webServiceID, index){
		var serviceFunction = null;
		for( var i = 0; i < this.MWS_ArrService.length; i++) 
		if (this.MWS_ArrService[i].ID == webServiceID)
			serviceFunction = this.MWS_ArrService[i].getFunction(index);

		return serviceFunction;
	},


	MWS_setServiceDiv: function (webServiceID, Div){
		for( var i = 0; i < this.MWS_ArrService.length; i++) 
			if (this.MWS_ArrService[i].ID == webServiceID)
				this.MWS_ArrService[i].setDiv(Div);
	},

	MWS_getServiceDiv: function (webServiceID, index){
		var serviceDiv = null;
		for( var i = 0; i < this.MWS_ArrService.length; i++) 
		if (this.MWS_ArrService[i].ID == webServiceID)
			serviceDiv = this.MWS_ArrService[i].getDiv(index);

		return serviceDiv;
	},

	MWS_setServiceObject: function (webServiceID, objectname, serviceObject){
		for( var i = 0; i < this.MWS_ArrService.length; i++) 
			if (this.MWS_ArrService[i].ID == webServiceID)
				this.MWS_ArrService[i].setObject(objectname, serviceObject);
	},

	MWS_getServiceObject: function (webServiceID, objectname){
		var serviceObject = null;
		for( var i = 0; i < this.MWS_ArrService.length; i++) 
		if (this.MWS_ArrService[i].ID == webServiceID)
			serviceObject = this.MWS_ArrService[i].getObject(objectname);

		return serviceObject;
	},

	MWS_refreshMap: function (){
		this.MWS_MapObj.pan(this.MWS_RefreshX,this.MWS_RefreshY);

		this.MWS_RefreshX=0-this.MWS_RefreshX;
		this.MWS_RefreshY=0-this.MWS_RefreshY;
	},

	MWS_EBUrlFunction: function (webServiceID){
		var requestID = this.MWS_getRequestID(webServiceID, "event");
		this.MWS_setServiceProperty(webServiceID, "event", "serviceRequestID", (parseInt(requestID)+1));
		requestID = this.MWS_getRequestID(webServiceID, "event");

		url = this.MWS_ServiceBaseUrl;

		if (webServiceID!="")
			url += "&service="+webServiceID;

		url += "&sessionid="+this.MWS_SessionID+
			"&zoom="+this.MWS_MapObj.zoom+"&scale="+this.MWS_MapObj.getScale()+"&requestid="+
			this.MWS_SessionID+"_"+webServiceID+"_"+requestID+
			"&sitereferrer="+this.SiteReferrer;

		return url;
	},

	MWS_TBUrlFunction: function (webServiceID){
		var mav_lonlat = this.MWS_MapObj.getCenter();
		var requestID = this.MWS_getRequestID(webServiceID, "time");
		this.MWS_setServiceProperty(webServiceID, "time", "serviceRequestID", (parseInt(requestID)+1));
		requestID = this.MWS_getRequestID(webServiceID, "time");
		
		this.MWS_x = mav_lonlat.lon;
		this.MWS_y = mav_lonlat.lat;

		var url = this.MWS_ServiceBaseUrl+"&sessionid="+this.MWS_SessionID+
			"&zoom="+this.MWS_MapObj.zoom+"&scale="+this.MWS_MapObj.getScale()+"&requestid="+
			this.MWS_SessionID+"_"+webServiceID+"_"+requestID+
			"&sitereferrer="+this.SiteReferrer;

		if (webServiceID!="")
			url += "&service="+webServiceID;

		return url;
	},


	MWS_getRequestID: function (webServiceID, webServiceType)
	{
		var lastID = this.MWS_getServiceProperty(webServiceID, webServiceType, "serviceRequestID");
		return (parseInt(lastID));	
	},

	MWS_Init: function (mapID, serviceForm, serviceBaseUrl)
	{
		var d=new Date();
		sessionID=
			d.getUTCFullYear() + "_"+
			(d.getUTCMonth()+1) + "_"+
			d.getUTCDate() + "_"+
			d.getUTCHours() + "_"+
			d.getUTCMinutes() + "_"+
			d.getUTCSeconds() + "_"+
			d.getUTCMilliseconds() + "_"+
			Math.floor(Math.random()*1000000000001) ;

		this.MWS_SessionID = sessionID;
		this.MWS_Form = document.getElementById(serviceForm);
		this.MWS_ServiceBaseUrl = serviceBaseUrl;
		this.MWS_MapObj = mapID;
		//this.MWS_DefaultMapExtent = this.MWS_MapObj.getExtent();

		this.MWS_MapObj.events.register("click", this.MWS_MapObj, MWS_clickChecker);
		
		MWS_Services_Base();

		if(top.frames.length==0)
			this.SiteReferrer=document.referrer;
		else
			this.SiteReferrer=top.document.referrer;

	},

	MWS_CreateServicesList: function (cols) {
		servicesList = "";
		curCol = 1;
		colWidth = 100/cols;
		for( var i = 0; i < this.MWS_ArrService.length; i++) 
		if (this.MWS_ArrService[i].Type!="context")
		if (this.MWS_ArrService[i].DisplayInList==true)
		{
			if (curCol==1)
			{
				if (servicesList != "")
					servicesList+="</tr>\n";
					
				servicesList += '<tr>';
			}
			
			servicesList += "<td width=\""+colWidth+"%\">";
			servicesList += "<font size=-1><a href=\"javascript:";
			servicesList += "MWS.MWS_Select('"; 
			servicesList += this.MWS_ArrService[i].ID + 
				"');\">" + this.MWS_ArrService[i].serviceName + "</a></font>";
					
			servicesList += '</td>';

			curCol++;
			if (curCol > cols) 
				curCol=1;
		}
		
		servicesList+="</tr>\n";

		servicesList = "<table width=100% cellpadding=\"0\" cellspacing=\"0\">\n" + 
			servicesList + 
			"</table>\n";
			
		return servicesList;
	},

	
	MWS_showWaitingImage: function (show)
	{
		var div = OpenLayers.Util.getElement('progressBar');
		if (div!=null)
		{
			if (show)					    
				div.style.left = "0px"; 	
			else    
				div.style.left = "-500px"; 		
		}
	},

	//This method is called to reset the environment.  It can be called to reset
	//
	MWS_resetEnvironment: function ()
	{
		for( var i = 0; i < this.MWS_ArrService.length; i++) 
		if (this.MWS_ArrService[i].Type=="event")
		{
			svc = this.MWS_ArrService[i];
			if (svc!=null)
			{
				try{
				svc.CleanUpFunction();
				}catch(e){}
			}
		}
	}
}

function MWS_clickChecker (e) {
	if (MWS.MWS_CurServiceID!="")
	{
		var mav_lonlat = MWS.MWS_MapObj.getLonLatFromViewPortPx(e.xy);
		MWS.MWS_x = mav_lonlat.lon;
		MWS.MWS_y = mav_lonlat.lat;
		MWS.MWS_zoom = MWS.MWS_MapObj.zoom;
		MWS.MWS_scale = MWS.MWS_MapObj.getScale();

		MWS.MWS_MapClick(MWS.MWS_CurServiceID);
	}
}

function MWS_Service(serviceID, serviceName, serviceType)
{
	this.ID= serviceID;
	this.serviceName= serviceName;
	this.ObjectID= null;
	this.Objects= null;
	this.ObjectNames= null;
	this.Type="event";
	this.RefreshInterval = 20;
	this.ParseFunction= null;
	this.UrlFunction= null;
	this.Functions=null;
	this.Divs=null;
	this.Parameters= "";
	this.ResultsInitText= null;
	this.TimerButtonText= "Start";
	this.TimerRunning= false;
	this.ConnectionState=0;
	this.UpdateAsHTML= 0;
	this.RequestID=0;
	this.DisplayInList=false;

	this.StartUpFunction=null;
	this.CleanUpFunction=null;
	this.TimeRefreshFunction=null;
	this.MapClickFunction=null;
	this.IgnoreCleanUp=false;
	
	if (serviceType!=null)
		this.Type=serviceType;
		
	this.setFunction = function (functionObject){
		if (this.Functions==null)
		{
			this.Functions = new Array(1);
			this.Functions[0]=functionObject;
		}
		else
		{
			this.Functions[this.Functions.length]=functionObject;
		}
	};

	this.getFunction = function (index){
		var theFunction = null;
		if (this.Functions!=null)
			theFunction = this.Functions[index];
		return theFunction;
	};

	this.setDiv = function (divObject){
		if (this.Divs==null)
		{
			this.Divs = new Array(1);
			this.Divs[0]=divObject;
		}
		else
		{
			this.Divs[this.Divs.length]=divObject;
		}
	};

	this.getDiv =  function (index){
		theDiv = null;
		if (this.Divs!=null)
			theDiv = this.Divs[index];
		return theDiv;
	};

	this.getProperty = function (propertyName){
		var propertyValue = null;
		
		if (propertyName == "UrlFunction")
			propertyValue = this.UrlFunction;

		else if (propertyName == "MapClickFunction")
			propertyValue = this.MapClickFunction;

		else if (propertyName == "TimeRefreshFunction")
			propertyValue = this.TimeRefreshFunction;

		else if (propertyName == "StartUpFunction")
			propertyValue = this.StartUpFunction;

		else if (propertyName == "CleanUpFunction")
			propertyValue = this.CleanUpFunction;

		else if (propertyName == "serviceType")
			propertyValue = this.Type;

		else if (propertyName == "serviceRefresh")
			propertyValue = this.RefreshInterval;

		else if (propertyName == "serviceParseFunction")
			propertyValue = this.ParseFunction;

		else if (propertyName == "serviceParameters")
			propertyValue = this.Parameters;

		else if (propertyName == "serviceMapObj")
			propertyValue = this.MapObj;

		else if (propertyName == "ResultsInitText")
			propertyValue = this.ResultsInitText;

		else if (propertyName == "serviceData")
			propertyValue = this.Data;

		else if (propertyName == "serviceRequestID")
			propertyValue = this.RequestID;

		else if (propertyName == "serviceTimerRunning")
			propertyValue = this.TimerRunning;

		else if (propertyName == "serviceUpdateAsHTML")
			propertyValue = this.UpdateAsHTML;

		else if (propertyName == "serviceConnectionState")
			propertyValue = this.ConnectionState;

		else if (propertyName == "serviceDivs")
			propertyValue = this.Divs;

		else
			propertyValue = null;
		return propertyValue;
	};

	this.setProperty = function (propertyName, propertyValue){
		if (propertyName == "UrlFunction")
			this.UrlFunction = propertyValue;

		else if (propertyName == "MapClickFunction")
			this.MapClickFunction = propertyValue;

		else if (propertyName == "TimeRefreshFunction")
			this.TimeRefreshFunction = propertyValue;

		else if (propertyName == "StartUpFunction")
			this.StartUpFunction = propertyValue;

		else if (propertyName == "CleanUpFunction")
			this.CleanUpFunction = propertyValue;

		else if (propertyName == "serviceType")
			this.Type = propertyValue;

		else if (propertyName == "serviceRefresh")
			this.Refresh = propertyValue;

		else if (propertyName == "serviceParseFunction")
			this.ParseFunction = propertyValue;

		else if (propertyName == "serviceParameters")
			this.Parameters = propertyValue;

		else if (propertyName == "serviceMapObj")
			this.MapObj = propertyValue;

		else if (propertyName == "serviceTimerRunning")
		{
			this.TimerRunning=propertyValue;
			if (this.TimerRunning)
				this.TimerButtonText= "Stop";
			else
				this.TimerButtonText= "Start";
		}

		else if (propertyName == "serviceUpdateAsHTML")
			this.UpdateAsHTML=propertyValue;

		else if (propertyName == "serviceConnectionState")
			this.ConnectionState=propertyValue;

		else if (propertyName == "ResultsInitText")
		{
			this.ResultsInitText = "";
			if (this.Type=="time")
			{
				this.ResultsInitText = "<input type='button' name='"+
					this.ID+"_Monitor' " + 
					"value='' onClick=\"MWSTimer_ButtonClicked('"+
					this.ID+"',this);\" />"+
					this.ResultsInitText;
			}

			this.ResultsInitText += propertyValue;
		}

		else if (propertyName == "serviceData")
			this.Data = propertyValue;

		else if (propertyName == "serviceRequestID")
			this.RequestID = propertyValue;

		else if (propertyName == "serviceDivs")
			this.Divs = propertyValue;

		else
			propertyValue = null;
	};
	
	this.setObject = function (objectname, serviceObject){
			if (this.Objects==null)
			{
				this.Objects = new Array(1);
				this.ObjectNames = new Array(1);

				this.Objects[0] = serviceObject;
				this.ObjectNames[0] = objectname;
			}
			else
			{
				this.Objects[this.Objects.length] = serviceObject;
				this.ObjectNames[this.Objects.length] = objectname;
			}
	};

	this.getObject = function (objectname){
		var serviceObject = null;
		for( var j = 0; j < this.ObjectNames.length; j++) 
		if (this.ObjectNames[j] == objectname)
		{
			serviceObject = this.Objects[j];
		}
		return serviceObject;
	};
}

function MWSTimer_ButtonClicked(webServiceID, obj){
	var TimerRunning = MWS.MWS_getServiceProperty(webServiceID, "time", "serviceTimerRunning");
	
	if (TimerRunning) 
	{
		TimerRunning=false;
		obj.value='Start';
	} 
	else 
	{
		var currentTime = new Date();
		var hours = currentTime.getHours();
		var minutes = currentTime.getMinutes();
		var seconds = currentTime.getSeconds();

		TimerRunning=true;
		obj.value='Stop';
		var mav_lonlat = MWS.MWS_MapObj.getCenter();

		MWS.MWS_x = mav_lonlat.lon;
		MWS.MWS_y = mav_lonlat.lat;
		MWS.MWS_zoom = MWS.MWS_MapObj.zoom;
		MWS.MWS_scale = MWS.MWS_MapObj.getScale();
	}

	MWS.MWS_setServiceProperty(webServiceID, "time", "serviceTimerRunning",TimerRunning);
}

/*
OpenLayers.Layer.WMS.prototype.getURL = function (bounds) {
        bounds = this.adjustBounds(bounds);
        
        var imageSize = this.getImageSize(); 
        var newParams = {
            'BBOX': this.encodeBBOX ?  bounds.toBBOX() : bounds.toArray(),
            'WIDTH': imageSize.w,
            'HEIGHT': imageSize.h,
            'RESOLUTION': this.map.getResolution(),
            'SCALE': this.map.getScale(),
            'ZOOM2': this.map.zoom
        };
        var requestString = this.getFullRequestString(newParams);
        return requestString;
    }

OpenLayers.Layer.WMS.prototype.getURL = function (bounds) {
        bounds = this.adjustBounds(bounds);
        
        var imageSize = this.getImageSize(); 
        var newParams = {
            'BBOX': this.encodeBBOX ?  bounds.toBBOX() : bounds.toArray(),
            'WIDTH': imageSize.w,
            'HEIGHT': imageSize.h,
            'RESOLUTION': this.map.getResolution(),
            'SCALE': this.map.getScale(),
            'ZOOM': this.map.zoom
        };
        var requestString = this.getFullRequestString(newParams);
        return requestString;
    }

OpenLayers.Layer.WMS.prototype.addTile = function (bounds,position) {
	//alert(position);
        return new OpenLayers.Tile.Image(this, position, bounds, 
                                         null, this.tileSize);
    }
*/