/****************************************************************************************************
File Name		: GSECPointsWS.js
Author			: 
Created date	: 08/04/09(dd/mm/yy)
Purpose			: Used for fetching City Status Centers data through Webservice. Called in CountryG page.
Change Log		: 

****************************************************************************************************/

function clearGSECPoints(reset_all) {  // HIDE ALL GSEC MARKERS
	for (var id in myGSECMarkers) {
	//map.removeOverlay(myGSECMarkers[i])
		if (myGSECMarkers[id] instanceof GMarker) {
		  //alert("key: "+id+" value:"+myGSECMarkers[id]);
			var marker = myGSECMarkers[id];
			//marker.hide();
			map.removeOverlay(marker);
			myGSECMarkers[id] = null;
		}
	}
  
	if (reset_all) {
		var theRadios = document.getElementsByName("gsec_Radios");
		for (r=0; r < theRadios.length; r++) {
			//theRadios[r].disabled=true;
			theRadios[r].checked=false;
		}
		document.getElementById("GSECMapType").value = "";
	}
	//document.getElementById('statusDIV').innerHTML = "";
	messagesforall(document.getElementById("GSECMapType").value, 0);
	showLoading(false);
}

// Removes XML special chars - convert to plain HTML
function parseToHTML(xmlStr) { 
	/*htmlStr=xmlStr.replace('&lt;', '<', "gm"); 
	htmlStr=htmlStr.replace('&gt;', '>', "gm"); 
	htmlStr=htmlStr.replace('&quot;', '"', "gm"); 
	htmlStr=htmlStr.replace('&apos;', "'", "gm"); 
	htmlStr=htmlStr.replace('&amp;', '&', "gm");*/

	//The above lines are commented and below code is added by Suresh on 9/1/2008

	htmlStr=xmlStr.replace(/\&lt;/g,'\<', "gm");
	htmlStr=htmlStr.replace(/\&gt;/g,'\>', "gm");
	htmlStr=htmlStr.replace(/\&quot;/g,'\"', "gm");
	htmlStr=htmlStr.replace(/\&apos;/g,'\'', "gm");
	htmlStr=htmlStr.replace(/\&amp/g,'\&', "gm");
 return htmlStr; 
}

// GLOBALS?
var maptype, bible, nt, portion, jfilm;

function get_GSECPoints(radio_id)
{
	var stat = false;

	clear_polycontrol();
	clear_lg_pg();
	clear_StepsInfo();
	GPolygonControl.prototype.polygButtonClick.off();

	//alert(radio_id);
	if(radio_id){
		maptype = radio_id;
		var radio_input = document.getElementById(radio_id);
		radio_input.disabled=false;
		radio_input.checked=true;

		//  DISABLE OTHER GSEC RADIO BUTTONS
		var theRadios = document.getElementsByName("gsec_Radios");
		
		for (r=0; r < theRadios.length; r++) {
			//alert(' this-radio-id: '+theRadios[r].id);
			//if (theRadios[r].disabled==true) { 
			if (theRadios[r].id != radio_id) { 
				//alert('disable this: '+theRadios[r].id+'radioid	: '+radio_id);
				//theRadios[r].disabled=true;
			}
		}

		// CURRENT MAP REDRAW - OR DRAW NEW MAPTYPE
		var currentMap = document.getElementById("GSECMapType").value;
		//alert('currentMap: '+currentMap);
		
		var makeNewMap = (maptype != currentMap);
		//alert('maptype not equal currentMap:: \n'+ makeNewMap);
		
		document.getElementById("GSECMapType").value = maptype;
		currentMap = document.getElementById("GSECMapType").value;
		//alert('maptype is now: '+currentMap);
		
		if (!makeNewMap) {
		// Re-display markers already loaded
			//for (var id in myGSECMarkers) {
			//map.removeOverlay(myGSECMarkers[i])
				//if (myGSECMarkers[id] instanceof GMarker) {
				  //alert("key: "+id+" value:"+myGSECMarkers[id]);
					//var marker = myGSECMarkers[id];
					//if (marker.isHidden()) {
						//marker.show();
					//}
				//}
			//}
		}
		// New Map - Clear Markers. Leave radio enabled? Free memory used for this object???
		else {  // MAKE NEW MAP
			clearGSECPoints(reset_all = false);
			//myGSECMarkers = { "-1" : 0 };
		}

		if ( document.getElementById(radio_id).checked == true ) {
			stat = true;
		}
	}

	//calls partners.worldmap webservice if checked, else clears the plotted markers
	if ( stat )	
	{
		getGSECWebservice(radio_id);
	} 
	else 
	{
		clearGSECPoints(reset_all = false);
		document.getElementById('statusDIV').innerHTML = "";
		showLoading(false);

		for (var i = 0; i < GSECMarkers.length; i++) 
		{
			GSECMarkers[i].hide();
		}
	}
}

//calls the web service 
function getGSECWebservice(radio_id)
{
	var t0 = new Date().getTime();
	document.getElementById('statusDIV').innerHTML = "Loading..."; 
	showLoading(true);

	var url = 'webservice_string.php';
	//var url = 'webservice_string_new.php';
	var mapbounds = map.getBounds();
	var zLevel = map.getBoundsZoomLevel(map.getBounds());  
	//alert('mapbounds = '+mapbounds);
	var flag = '34';

	var params = 'flag='+flag+'&mapbounds='+mapbounds+'&radio_id='+radio_id+'&z='+zLevel;
	var aj = new Ajax.Request(  
					url, 
						{  
						method: 'GET',  
						parameters: params,  
						onComplete: function(oReq) 
							{ 
								//alert('responseText = '+oReq.responseText);
								/*if(GSEC_moveend_listener != undefined)
								{	  
									GEvent.removeListener(GSEC_moveend_listener);
								}*/

								var res = oReq.responseText.substring(0,1); 
								//alert('res = '+res);
								if (res == '<')
								{
									//getGSECWebservice();
									document.getElementById('statusDIV').innerHTML = "";
									showLoading(false);
								}
								else
								{
									getGSECMap(oReq.responseText, t0);
								}

								/*GSEC_moveend_listener = GEvent.addListener(map, "moveend", function() 
								{
									activation=1;	    
									get_GSECPoints(document.getElementById("GSECMapType").value);
								});*/
							}
						}  
					); 
}

var GSECrecords;
var pinID;
var GSECBounds = new GLatLngBounds();
var GSECMarkers = [];

function getGSECMap(ws, t0)
{
	pinID = 1;

	var records1 = ws;
	GSECrecords = records1.split('#');

	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(24, 24);
	baseIcon.iconAnchor = new GPoint(12, 24);
	baseIcon.infoWindowAnchor = new GPoint(12, 12);
	baseIcon.image = "gmap/icons/0_People_dk_red.png";
	  
	//if there are records to point on the map
	//alert('GSECrecords='+GSECrecords);
	if ( GSECrecords != "" )
	{
		for (i=0; i < GSECrecords.length; i++)
		{	
			var values = GSECrecords[i].split("|");
			
			var oid			= values[0];
			var ylat		= values[1];
			var xlon		= values[2];
			var Population	= values[3];
			var PlaceName	= values[4];
			var GSEC		= values[5];
	
			var isLoaded = myGSECMarkers[oid];
			if (!isLoaded) {
				var point = new GLatLng(parseFloat(ylat), parseFloat(xlon));
				myGSECMarkers[oid] = new createGSECMarker(oid, point, PlaceName, Population, GSEC, maptype);
			}

			//var point = new GLatLng(parseFloat(ylat), parseFloat(xlon)); 
			//map.addOverlay(createGSECMarker(oid, point, baseIcon, PGName));
			
			pinID++; 
		}
		//setting map view
		//map.setZoom(map.getBoundsZoomLevel(GSECBounds));
		//map.setCenter(GSECBounds.getCenter());
		
		for (var id in myGSECMarkers) {			  
		  // Performance boost to hide all then show?
			if (myGSECMarkers[id] instanceof GMarker) {
			  //alert("key: "+id+" value:"+myGSECMarkers[id]);
				var marker = myGSECMarkers[id];
				map.addOverlay(marker);
			}
		}

		var t1 = new Date().getTime();
		//var secs = ((t1 - t0) / 1000).toFixed(1);
		gsecsecs = ((t1 - t0) / 1000).toFixed(1);
		gsecpoints = GSECrecords.length;
		//document.getElementById('statusDIV').innerHTML =GSECrecords.length + " City Status points loaded ("+ secs +" secs)";
		messagesforall(document.getElementById("GSECMapType").value, GSECrecords.length);
		showLoading(false);
	}
	else
	{
		//document.getElementById('statusDIV').innerHTML = "City Status Records found: 0";
		var t1 = new Date().getTime();
		gsecsecs = ((t1 - t0) / 1000).toFixed(1);
		gsecpoints = 0;
		messagesforall(document.getElementById("GSECMapType").value, 0);
		showLoading(false);
	}
}

//To create push pins(markers)
function createGSECMarker(oid, point, PlaceName, Population, GSEC, maptype) {

	theIcon = getIconImage(GSEC, Population, maptype);

	var marker = new GMarker(point, {icon:theIcon, title:'"'+ PlaceName +'"'});
	
	//appending marker to the list of points(GSECBounds)
	GSECBounds.extend(marker.getPoint());
    
	click_listener = GEvent.addListener(marker, "click", function()
	{
		movendstop(); showLoading(true);
		var url = 'webservice_string.php';
		var flag = '38';
		var params = 'flag='+flag+'&oid='+oid+'&radio_id='+maptype;
		var aj_more = new Ajax.Request(  
						url, 
							{  
							method: 'GET',  
							parameters: params,  
							onComplete: function(oReq_more) 
								{ 
									//alert('responseText = '+oReq_more.responseText);
									var res = oReq_more.responseText.substring(0,1); 
									if (res == '<')
									{
										document.getElementById('statusDIV').innerHTML = "";
										showLoading(false);
									}
									else
									{
										var record1 = oReq_more.responseText;
										record = record1.split('###');
										var tabs = [];  // info Tabs container for InfoWindows
										var tab_labels = [];
										var htmls = [];	

										//alert('record='+record);
										if ( record != "" )
										{
											for (i=0; i < record.length; i++)
											{	
												var values = record[i].split("|");				
												var oid = values[0];  // needed to manage these marker points
												
												// Location Tab		
												tab_labels.push('Location');    
												var html = values[2]; 
												html = parseToHTML(html);
												htmls.push(html);
												
												// Linguistic Tab		
												tab_labels.push('Linguistic');
												var html = values[1]; 
												html = parseToHTML(html);
												htmls.push(html);
														
												// Missions Tab		
												tab_labels.push('Cultural');
												var html = values[3]; 
												html = parseToHTML(html);
												htmls.push(html);
											}										
									  
											// Create Tabbed Info Window
											for (var j=0; j < htmls.length; j++) {
												htmls[j] = '<div class="infowindow" style="width:440px">' + htmls[j] + '</div>';
												tabs.push(new GInfoWindowTab(tab_labels[j],htmls[j]));
											}

											document.getElementById('statusDIV').innerHTML = "";
											showLoading(false);
											marker.openInfoWindowTabsHtml(tabs);
											setTimeout("showLoading(false);", 10000); 
                                            setTimeout("movendstart();", 1000);
											if(oReq_more.responseText.length>1)
		                                    {
		                              
		                                      }
										}
										else
										{
											document.getElementById('statusDIV').innerHTML = "No City Status Info found";
											showLoading(false);
										}
									}
								}
							}  
						);
		//map.openInfoWindowHtml(point, pinOverContent);
		GEvent.removeListener(click_listener);
	});
    
	GSECMarkers.push(marker);
	return marker;
}

function getIconImage(GSECstatus, population, maptype) 
{
	//var d = "gmap/icons/gsec/";
    var d = "gmap/icons/";  // directory for icons
	var myIcon = new GIcon();
    var status = parseInt(GSECstatus,10);  // convert to base 10 INT
	
	if (population  == 0) {
		myIcon.iconSize = new GSize(10, 10);
		myIcon.iconAnchor = new GPoint(5, 5);
		myIcon.infoWindowAnchor = new GPoint(5, 5);
		//alert('pop 0');
	}
	else if (population > 0 && population < 50000) {
		myIcon.iconSize = new GSize(14, 14);
		myIcon.iconAnchor = new GPoint(7, 7);
		myIcon.infoWindowAnchor = new GPoint(7, 7);
		//alert('pop < 50000');
	}
	else if (population > 50000 && population < 100000) {
		myIcon.iconSize = new GSize(18, 18);
		myIcon.iconAnchor = new GPoint(9, 9);
		myIcon.infoWindowAnchor = new GPoint(9, 9);	
		//alert('pop < 100000');
	}
	else if (population > 100000 && population < 250000) {
		myIcon.iconSize = new GSize(22, 22);
		myIcon.iconAnchor = new GPoint(11, 11);
		myIcon.infoWindowAnchor = new GPoint(11, 11);	
		//alert('pop < 250000');
	}
	else if (population > 250000 && population < 500000) {
		myIcon.iconSize = new GSize(26, 26);
		myIcon.iconAnchor = new GPoint(13, 13);
		myIcon.infoWindowAnchor = new GPoint(13, 13);	
	}
	else if (population > 500000 && population < 1000000) {
		myIcon.iconSize = new GSize(34, 34);
		myIcon.iconAnchor = new GPoint(17, 17);
		myIcon.infoWindowAnchor = new GPoint(17, 17);	
	}
	else if (population > 1000000 && population < 10000000) {
		myIcon.iconSize = new GSize(38, 38);
		myIcon.iconAnchor = new GPoint(19, 19);
		myIcon.infoWindowAnchor = new GPoint(19, 19);	
	}
	else if (population > 10000000) {
		myIcon.iconSize = new GSize(42, 42);
		myIcon.iconAnchor = new GPoint(21, 21);
		myIcon.infoWindowAnchor = new GPoint(21, 21);	
	}
	else {
		myIcon.iconSize = new GSize(10, 10);
		myIcon.iconAnchor = new GPoint(5, 5);
		myIcon.infoWindowAnchor = new GPoint(5, 5);
		//alert('pop??? :  ',population);
	}	
	
//alert('population: '+ population);
//alert('status: '+status+  '  pop: '+population);

	if (maptype == 'bible') {
		if (bible != "") {
			status = 5; 
		}
		else if (nt != "") {
			status = 2; 
		}
		else if (portion != "") {
			status = 3; 
		}
		else {
			status = 1; 
		}
		
	}
	else if (maptype == 'jfilm') {
		if (jfilm == 'Y') {
			status = 5; 
		}
		else {
			status = 1; 
		}
	}
	
	switch(status)
	{
	case 0:
		myIcon.image = d+"0_SGE_dk_red.png";
		break;    
	case 1:
		myIcon.image = d+"1_SGE_red.png";
		break;    
	case 2:
		myIcon.image = d+"2_SGE_orange.png";
		break;
	case 3:
	  myIcon.image = d+"3_Langs_yellow.png";
	  break;    
	case 4:
	  myIcon.image = d+"4a_SGE_bright_green.png";
	  break;    
	case 5:
	  myIcon.image = d+"5a_SGE_lt_green.png";
	  break;    
	case 6:
	  myIcon.image = d+"6_SGE_dk_green.png";
	  break;    
	case 7:
	  myIcon.image = d+"7_SGE_purple.png";
	  break;    
	case -1:
		myIcon.iconSize = new GSize(26, 26);
		myIcon.iconAnchor = new GPoint(13, 13);
		myIcon.infoWindowAnchor = new GPoint(13, 13);	
	  myIcon.image = d+"shaded_dot.png";
	  break;    
	default:
		myIcon.iconSize = new GSize(22, 22);
		myIcon.iconAnchor = new GPoint(11, 11);
		myIcon.infoWindowAnchor = new GPoint(11, 11);	
	  myIcon.image = d+"shaded_dot.png";
	}

//alert('myIcon.image: '+myIcon.image+' ('+myIcon.iconSize+')');
//alert('maptype: '+maptype+'\nstatus: '+status);

return myIcon;

} // END fcn CREATEMARKER
//end of gsec code

