CTideStations.prototype = new CXMLHTTP();
CTideStations.prototype.constructor = CTideStations;

function CTideStations( zoom, westLng, eastLng, northLat, southLat )
{
	var m_XMLHTTP = null;
	var m_Response = "";
	var nCode = 0;
	var nMesg = "";
	
	var names = new Array(	"zoom",
							"westLng",
							"eastLng",
							"northLat",
							"southLat" );
	
	var values = new Array(	zoom,
							westLng,
							eastLng,
							northLat,
							southLat );
							
	this.m_FormID = "id_tide";
	this.ClassName = "CTideStations";
			
	// this callback does not have access to 'this'.
	// it's as though it were static

	function _onReadyStateChange()
	{
		var nCode;
		var sMesg;
			
		//alert( "m_XMLHTTP.readyState = " + m_XMLHTTP.readyState );
		
		if( m_XMLHTTP.readyState == 4 )
		{
			//alert( "_onReadyStateChange\n\n" + m_XMLHTTP.responseText );
		
			m_Response = m_XMLHTTP.responseText;
			//var elem = document.getElementById( "id_tide_stations" );
			//window.status = "cliff " + m_Response;//nMesg;
			nCode = g_GetCode( m_Response );
			sMesg = g_GetMesg( m_Response );
			
			// assume it's not the xml
			/*if( nCode != undefined )
			{
				window.status = "nCode != undefined " + sMesg;
				return;
			}
			*/
			// if you don't reset it, it will be what it was last time
			/*
			window.status = "";
			
			
			// didn't get xml
			if( m_Response.indexOf( "<" ) != 0 )
			{
				window.status = "nCode: " + nCode + " sMesg: " + sMesg;
				return;
			}
			*/
			// IE
			//elem.innerHTML = "asdfa0000fafd"; //m_Response;
			// NS
			//buildTideDisplay( elem, m_Response );			
			//elem.textContent = m_Response;
			
			//alert( "CTideStations _onReadyStateChange Response = " + m_Response );
			buildMarkers( m_Response );
		}				
	}			
	/*
	<station ID="8447386" name="Fall River">
	<metadata>
	<location>
	<lat>41.705000</lat>
	<long>-71.163300</long>
	<state>MA</state>
	</metadata>
	</station>

	*/
	function buildMarkers( xml )
	{
		var doc;
		
		if( m_Response.length == 0 )
		{
			//alert( "CTideStations::buildMarkers xml is empty" );
			return;
		}
		
		doc = loadDOMFromString( xml );
		if( null == doc )
		{
			alert( "doc is null" );
			return;
		}
		
		//alert( xml );
			
		var latlng;
		var lat;
		var lng;
		var id;
		var name;
		var state;
		var nodes = doc.getElementsByTagName( "station" );
		//alert( "CTideStations there are " + nodes.length + " nodes" );
		for( var i=0; i<nodes.length; i++ )
		{
			id = nodes[i].getAttribute("ID");
			name = nodes[i].getAttribute("name");
			state = nodes[i].getElementsByTagName( "state" )[0].childNodes[0].nodeValue;
			lat = nodes[i].getElementsByTagName( "lat" )[0].childNodes[0].nodeValue;
			lng = nodes[i].getElementsByTagName( "long" )[0].childNodes[0].nodeValue;
			
			//alert( lat + " " + lng );
			
			if( null == state ||
				state.length < 1 )
				state = "";
				
			var marker = createMarker(id, name, state, lat, lng);          	
          	map.addOverlay( marker );
		}
		/*
			time = nodes[i].getElementsByTagName( "timeStamp" )[0].childNodes[0].nodeValue;
			h = parseFloat( nodes[i].getElementsByTagName( "pred" )[0].childNodes[0].nodeValue );
		*/
		
		
	}
	
	// if this is not done in a function then
	// every marker you click on will nav you to the first one
	// created.  NO IDEA WHY
	//
	function createMarker(id, name, state, lat, lng)
	{
		var point = new GLatLng( lat, lng );
			
		var ico = new GIcon(G_DEFAULT_ICON);
        ico.image = "/img/ico/markers/green_T.png";
		var markerOptions = { icon:ico };
        // Set up our GMarkerOptions object
		var marker = new GMarker(point, markerOptions);
		
		// need to initialize size with table if drawing a table
		// it can be vertically different and the br's don't allocate the 
		// same amount of room.
		GEvent.addListener(marker, "click", function() {
            	marker.openInfoWindowHtml( "<div id=\"id_tide_station_" + id + "\">" + paddingTable( (MAX_PREVIEW_TIDE * 4), id + " " + name + " " + state) + "</div>");
            	//<p>" + id + " " + name + " " + state + "</p>" +
            	//marker.openInfoWindowHtml( "<h8 title=\"click here to get tide forcast\" onmouseover=\"this.style.cursor='pointer'\" onclick=\"getTides('id_tide_station_" + id + "', " + id + ")\">" + id + " " + name + " " + state + "</h8><br>" +
            	//"&nbsp;&nbsp;<img src=\"/img/ico/chevron_down.gif\" onclick=\"getTides('id_tide_station_" + id + "', " + id + ")\"onmouseover=\"this.style.cursor='pointer'\"></img>&nbsp;<br>" +
            	
            	// using <h11/> IE would not populate, said "unknown runtime error", took a day to realize
            	
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.text = "var tide = new CTide( 'id_tide_station_" + id + "', " + id + ", '" + name + "', '" + state + "' )";
		document.body.appendChild(script);

            //	"var elem = document.getElementById( 'id_tide_station_" + id + "\' );" +
	// 	"var tide = new CTide( elem, " + id + " );" +
	
		});
		return marker;
	}
		
	m_XMLHTTP = this.getXMLHTTP();
	//m_XMLHTTP.onreadystatechange = _onReadyStateChange;
	
	//alert( "http://www.hazmap.net/processor/tide_stations.php?" + 
	//			"zoom=" + values[0] + "&westLng=" + values[1] + "&eastLng=" + values[2] + "&northLat=" + values[3] + "&southLat=" + values[4] );
	
	var req = "http://www.hazmap.net/processor/tide_stations.php";
	var args = "zoom=" + values[0] + "&westLng=" + values[1] + "&eastLng=" + values[2] + "&northLat=" + values[3] + "&southLat=" + values[4];
	
	//alert( "pre submit" );
	this.submit3( req, args, _onReadyStateChange );
	
	//var elem = document.getElementById( 'debugit' );
	//elem.innerText = req + "?" + args;
	//alert( "post submit\n" + req + "?" + args );
	/*
	try
	{
		//if(xmlhttp.obj.onreadystatechange == null)
		if ((m_XMLHTTP.readyState == 4) && (m_XMLHTTP.status == 200))
		{
			alert( "m_XMLHTTP.responseText = " + m_XMLHTTP.responseText );
		}
	}
	catch (e)
	{
		alert( e );
	}
	*/
}
