/** \class TeWMSGetCapabilitiesRequest
  * \brief Models the GetCapabilities request for a WMS.
  *
  *
  * This class can be used to request a Capabilities document from
  * a WMS server. You should create an instance of TeWMSGetCapabilitiesRequest
  * and then send it to an instance of TeWMS (see GetCapabilities method of
  * TeWMS class).
  *
  * \sa TeWMS
  *
  */
function TeWMSGetCapabilitiesRequest(serverURL, wmsVersion)
{
	var serverURL_ = serverURL;
	var version_ = wmsVersion;	
	
	/* initialize the member function references for the class prototype */
	//if(typeof(_tewmsgetcapabilitiesrequest_prototype_called) == 'undefined')
	//{
		//_tewmsgetcapabilitiesrequest_prototype_called = true;
		TeWMSGetCapabilitiesRequest.prototype.getServerURL = getServerURL;
		TeWMSGetCapabilitiesRequest.prototype.setServerURL = setServerURL;
		TeWMSGetCapabilitiesRequest.prototype.getVersion = getVersion;
		TeWMSGetCapabilitiesRequest.prototype.setVersion = setVersion;
		TeWMSGetCapabilitiesRequest.prototype.toString = toString;
		TeWMSGetCapabilitiesRequest.prototype.show = show;
	//}

	/** \brief Returns the server URL for the request.
	  */
	function getServerURL()
	{
		return makeUrlRequestGetCapabilities();
	}

	/** \brief Sets the server URL for the request.
	  */
	function setServerURL(serverURL)
	{
		serverURL_ = serverURL;
	}

	/** \brief Returns the version of the capabilities request.
	  */
	function getVersion()
	{
		return version_;
	}

	/** \brief Sets the version of capabilities request.
	  */
	function setVersion(wmsVersion)
	{
		version_ = wmsVersion;
	}

	/** \brief Returns instance properties converted to a string format.
	  */
	function toString()
	{
		var message  = "Server URL: ";
		    message += this.getServerURL();
			message += "\n";
			message += "Server Version: ";
			message += this.getVersion();
			
		return message;
	}

	/** \brief Shows an alert with instance properties converted to a string format.
	  */
	function show()
	{
		alert(this.toString());
	}
	/** \brief Format get query the HTTP Request Rules from OWS - WMS specifications.
	  */
	function makeUrlRequestGetCapabilities()
	{
		if(version_=="1.1.1")
		{
			var getQueryStart = "";
			var getQuerySeparator = "";
			var posQueryStart = serverURL_.toString().lastIndexOf("?");// Separator indicating start of query string.
			var posQuerySeparator = serverURL_.toString().lastIndexOf("&");// Separator between parameters in query string.
			if (posQueryStart == -1) {
				getQueryStart = "?";
			}else {
				if ( (posQueryStart < serverURL_.length-1 && posQuerySeparator == -1) || (posQuerySeparator > -1 && posQuerySeparator < serverURL_.length-1) ) {
					getQuerySeparator = "&";
				}
				else
				{
					getQuerySeparator = "";
				}
			}
			
			serverURL_ += getQueryStart + getQuerySeparator + "REQUEST=GetCapabilities&SERVICE=wms&VERSION=1.1.1";
		}
		return serverURL_;
	}
}

/* Outra maneira de garantir que so uma funcao esta sendo
criada por metodo é a seguinte:
minhaclasse.prototype.bar = function()
{   alert('bar'); }
*/
