/* Author : Sourav Biswas */

//google.load("maps", "2", {"other_params" :"sensor=true"});
//google.load("maps", "2", {"callback" :initialize});

/* Path Configuration */
var baseUrl = null;

//var mapserverurl = "http://184.168.119.199:23180/cgi-bin/mapserv.exe?";
var mapserverurl = "http://184.168.119.199/cgi-bin/mapserv.exe?";
var dataurl = "map=/DATA/PROJECT/179_SDP/MAP/";

//var mapserverurl = "http://72.167.115.25:5280/cgi-bin/mapserv.exe?";
//var dataurl = "map=/DATA/project_data/179_SubDistrictProject/MapServer/";

//var mapserverurl = "http://cyberswiftgis.servehttp.com:88/cgi-bin/mapserv.exe?";
//var dataurl = "map=/MS4W/ms4w/Apache/htdocs/dps/";

/* parameter for main Body */
var headerHeight = 80; // Page Header
var footerHeight = 30; // Page Footer
var bodyMargin = 5; // Inside margin of container body

/* Parameter for inner Container */
var borderCompensation = 6;
var otherCompensation = 15; // Padding + border
var totalCompensation = (30+24+13); // Header+Footer+shadow

/* Map Object */
var CS_Gmap = null;

/* Marker Array */
var markerArray = new Array();
var boudaryArray = new Array();
var thematicBoundaryArray = new Array();

/* Legends Array*/
var legendsArray = new Array();

/* Slider Object Data */
var sliderObj = null;
var transparencyLevel = 0.7;

/* Default Legend Parameter */
var legendColourParams = 	new Array(['255,108,118','0,0,0'],
									  ['254,189,75','0,0,0'],
									  ['254,247,46','0,0,0'],
									  ['39,251,114','0,0,0'],
									  ['108,111,205','0,0,0']);

function initializeHeight(baseURL){
	baseUrl = baseURL;
	var screenSize = new BrowserScreenSize();
	
	try{
		/* Initialize Page height */
		screenSize.setObjectHeight(document.getElementById('middle'),-(headerHeight+footerHeight+bodyMargin));
		try{
			screenSize.setObjectHeight(document.getElementById('leftPaneContainer'),-(headerHeight+footerHeight+bodyMargin+totalCompensation+otherCompensation));
			screenSize.setObjectHeight(document.getElementById('bodyContainer'),-(headerHeight+footerHeight+bodyMargin+totalCompensation+otherCompensation+borderCompensation));
			screenSize.setObjectHeight(document.getElementById('map_canvas'),-(headerHeight+footerHeight+bodyMargin+totalCompensation+otherCompensation+borderCompensation));
		}catch(e){}
	}catch(e){
		alert(e);
	}
}

function initializeMap(baseURL){
	baseUrl = baseURL;
	var targetObj = null;
	var screenSize = new BrowserScreenSize();
	
	outputPanelObj = document.getElementById('output_panel');
	try {
		/* Initialize Page height */
		screenSize.setObjectHeight(document.getElementById('middle'),-(headerHeight+footerHeight+bodyMargin));
		screenSize.setObjectHeight(document.getElementById('leftPaneContainer'),-(headerHeight+footerHeight+bodyMargin+totalCompensation+otherCompensation));
		screenSize.setObjectHeight(document.getElementById('bodyContainer'),-(headerHeight+footerHeight+bodyMargin+totalCompensation+otherCompensation+borderCompensation));
		screenSize.setObjectHeight(document.getElementById('map_canvas'),-(headerHeight+footerHeight+bodyMargin+totalCompensation+otherCompensation+borderCompensation));
		
		//screenSize.setObjectHeight(document.getElementById('mapCanvas'),-200);
		
		/* Initialize Lightbox */
		screenSize.setObjectHeight(document.getElementById('lightboxMain'),0);
		screenSize.setObjectHeight(document.getElementById('lightboxContainer'),-65);
		
		/* Slider Object Operations */
		document.getElementById('sliderLocation').style.top = "-"+(screenSize.getHeight()-((headerHeight+footerHeight+bodyMargin+totalCompensation+otherCompensation+borderCompensation-300)+7))+"px";
		//alert("Top : "+document.getElementById('sliderLocation').style.top);
		
		sliderObj = new CS_Slider(document.getElementById('sliderLocation'),document.getElementById("slider-1"),document.getElementById("slider-input-1"),document.getElementById('slider-value'));
		sliderObj.setValue(70);
		sliderObj.setMinimum(0);
		sliderObj.setMaximum(100);
		sliderObj.setCallback("transparencyChanged()");
		sliderObj.recalculate();
		
		targetObj = document.getElementById('map_canvas');
		CS_Gmap = new CS_Map(targetObj, 22.0, 83,4);
	} catch (e) {
		alert("Initialization Failed:\nPlease Check your internet connection : "+e);
		CS_Gmap = null;
	}
}
/* Callback method for transparency change of Thematic Layer */
function transparencyChanged(){
	transparencyLevel = sliderObj.getValue() / 100;
	sliderObj.setValue(sliderObj.getValue());
	
	if(boudaryArray != null || thematicBoundaryArray != null){
		for(key in boudaryArray){
			if(boudaryArray.hasOwnProperty(key)){
				if(boudaryArray[key].isHidden()){
					boudaryArray[key].setTransparency(transparencyLevel);
					boudaryArray[key].hideLayer();
				}else{
					boudaryArray[key].setTransparency(transparencyLevel);
				}
			}
		}
		
		for(key in thematicBoundaryArray){
			if(thematicBoundaryArray.hasOwnProperty(key)){
				if(thematicBoundaryArray[key].isHidden()){
					thematicBoundaryArray[key].setTransparency(transparencyLevel);
					thematicBoundaryArray[key].hideLayer();
				}else{
					thematicBoundaryArray[key].setTransparency(transparencyLevel);
				}
			}
		}
	}else{
		alert("No boundary specified");
	}
}

/* Tools Operation */
var toolClass = null;
var measurementFlag = false;
var outputPanelObj;

function measurementTool(imageId,operation){
	//alert(measurementFlag);
	if(toolClass == null){
		toolClass = new CS_Tool(CS_Gmap.getMap(),document.getElementById('output_panel'))
	}
	
	if(!measurementFlag){
		imageId.src = baseUrl+"/assets/images/map/tool/pointer_over.gif";
		measurementFlag = true;
		//CS_addEventListener();
		toolClass.startMesure();
		//CS_Gmap.getMap().getDragObject().setDraggableCursor("crosshair");
	}else{
		imageId.src = baseUrl+"/assets/images/map/tool/pointer.gif";
		measurementFlag = false;
		//CS_removeEventListener();
		toolClass.stopMesure();
		//CS_Gmap.getMap().getDragObject().setDraggableCursor("");
	}
}

/* Marker Operation */
function showHideDairyMarker(data,stat,type,subCategory,typeOfJoin){
	//alert(type+":"+subCategory+"\n"+data);
	if(stat){
		if(markerArray[type+"_"+subCategory] == null && data == null){
			initializeDairyMarker(type,subCategory,typeOfJoin);
		}else if(markerArray[type+"_"+subCategory] == null && data != null){;
			markerArray[type+"_"+subCategory] = new CS_Markers(CS_Gmap,data[1],initializeLegendsAndSetBoundboxDairy());
			showHideMarkers(markerArray[type+"_"+subCategory],true);
		}else{
			showHideMarkers(markerArray[type+"_"+subCategory],true);
		}
	}else{
		showHideMarkers(markerArray[type+"_"+subCategory],false);
	}
	cotrolLegendDisplay(stat, 'dairy_marker', markerArray[(type+"_"+subCategory)].getLegendString());
	
	function initializeLegendsAndSetBoundboxDairy(){
		/* Initializing Legend Object */
		var markerList = data[0];
		var legend = new CS_MarkerLegends('dairy_marker','legends','Dairy',markerList);
		
		/* Setting the appropriate zoom level */
		if(subCategory == "MPCS"){
			setBoundingBoxOfMarker(CS_Gmap,"map_point_dairy","wkb_geometry","category","MPCS","false");
		}else if(subCategory == "CC"){
			setBoundingBoxOfMarker(CS_Gmap,"map_point_dairy","wkb_geometry","category","CC","true");
		}
		return legend.getLegendString();
	}
}

function showHideTourismMarker(data,stat,type,subCategory){
	//alert(type+":"+subCategory+"\n"+data);
	if(stat){
		if(markerArray[type+"_"+subCategory] == null && data == null){
			initializeTourismMarker(type,subCategory);
		}else if(markerArray[type+"_"+subCategory] == null && data != null){;
			markerArray[type+"_"+subCategory] = new CS_Markers(CS_Gmap,data[1],initializeLegendsAndSetBoundboxTourism());
			showHideMarkers(markerArray[type+"_"+subCategory],true);
		}else{
			showHideMarkers(markerArray[type+"_"+subCategory],true);
		}
	}else{
		showHideMarkers(markerArray[type+"_"+subCategory],false);
	}
	cotrolLegendDisplay(stat, 'tourism_marker', markerArray[(type+"_"+subCategory)].getLegendString());
	
	function initializeLegendsAndSetBoundboxTourism(){
		//alert("Initializing Legends");
		var markerList = data[0];
		var legend = new CS_MarkerLegends('tourism_marker','legends','Tourism',markerList);
		
		if(subCategory == "Leisure"){
			setBoundingBoxOfMarker(CS_Gmap,"map_point_tourism","wkb_geometry","type","Leisure","true");
		}else if(subCategory == "Pilgrim"){
			setBoundingBoxOfMarker(CS_Gmap,"map_point_tourism","wkb_geometry","type","Pilgrim","true");
		}
		return legend.getLegendString();
	}
}

function showHideAgricultureMarker(data,stat,type,subCategory){
	//alert(type+":"+subCategory+"\n"+data);
	if(stat){
		if(markerArray[type+"_"+subCategory] == null && data == null){
			initializeAgricultureMarker(type,subCategory);
		}else if(markerArray[type+"_"+subCategory] == null && data != null){;
			markerArray[type+"_"+subCategory] = new CS_Markers(CS_Gmap,data[1],initializeLegendsAndSetBoundboxTourism());
			showHideMarkers(markerArray[type+"_"+subCategory],true);
		}else{
			showHideMarkers(markerArray[type+"_"+subCategory],true);
		}
	}else{
		showHideMarkers(markerArray[type+"_"+subCategory],false);
	}
	cotrolLegendDisplay(stat, 'agriculture_marker', markerArray[(type+"_"+subCategory)].getLegendString());
	
	function initializeLegendsAndSetBoundboxTourism(){
		var markerList = data[0];
		var legend = new CS_MarkerLegends('agriculture_marker','legends','Tourism',markerList);
		
		if(subCategory == "Agri inputs shop"){
			setBoundingBoxOfMarker(CS_Gmap,"map_point_agri_font","wkb_geometry","category","Agri inputs shop","true");
		}else if(subCategory == "Coop Bank"){
			setBoundingBoxOfMarker(CS_Gmap,"map_point_agri_font","wkb_geometry","category","Coop Bank","true");
		}
		return legend.getLegendString();
	}
}

function showHideTownFontMarker(data,stat,type,subCategory){
	//alert(type+":"+subCategory+"\n"+data);
	if(stat){
		if(markerArray[type+"_"+subCategory] == null && data == null){
			initializeTownFontMarker(type,subCategory);
		}else if(markerArray[type+"_"+subCategory] == null && data != null){;
			markerArray[type+"_"+subCategory] = new CS_Markers(CS_Gmap,data[1],initializeLegendsAndSetBoundboxTownFont());
			showHideMarkers(markerArray[type+"_"+subCategory],true);
		}else{
			showHideMarkers(markerArray[type+"_"+subCategory],true);
		}
	}else{
		showHideMarkers(markerArray[type+"_"+subCategory],false);
	}
	cotrolLegendDisplay(stat, 'town_font_marker', markerArray[(type+"_"+subCategory)].getLegendString());
	
	function initializeLegendsAndSetBoundboxTownFont(){
		var markerList = data[0];
		//alert(markerList);
		var legend = new CS_MarkerLegends('town_font_marker','legends','Town Font',markerList);
		
		setBoundingBoxOfMarker(CS_Gmap,"map_point_town_font","wkb_geometry","","","false");
		
		return legend.getLegendString();
	}
}

function showHideMilkRouteMarker(data,stat,type,subCategory){
	//alert(type+":"+subCategory+"\n"+data);
	if(stat){
		if(markerArray[type+"_"+subCategory] == null && data == null){
			initializeMilkRouteMarker(type,subCategory);
		}else if(markerArray[type+"_"+subCategory] == null && data != null){;
			markerArray[type+"_"+subCategory] = new CS_Markers(CS_Gmap,data[1],initializeLegendsAndSetBoundboxMilkRoute());
			showHideMarkers(markerArray[type+"_"+subCategory],true);
		}else{
			showHideMarkers(markerArray[type+"_"+subCategory],true);
		}
	}else{
		showHideMarkers(markerArray[type+"_"+subCategory],false);
	}
	cotrolLegendDisplay(stat, 'milk_route_marker', markerArray[(type+"_"+subCategory)].getLegendString());
	
	function initializeLegendsAndSetBoundboxMilkRoute(){
		var markerList = data[0];
		//alert(markerList);
		var legend = new CS_MarkerLegends('milk_route_marker','legends','Milk Route Marker',markerList);
		
		setBoundingBoxOfMarker(CS_Gmap,"map_point_milk_route","wkb_geometry","","","false");
		
		return legend.getLegendString();
	}
}

function showHideMarkers(markerArray,stat){
	if(markerArray != null){
		if(stat){
			//alert("Displaying Marker \n"+markerArray);
			markerArray.showMarker(CS_Gmap);
		}else{
			markerArray.hideMarker();
		}
	}else{
		//alert("Marker Array is Vacant");
	}
}

function showSchoolDetails(id){	
	var lightboxMain = null;
	var lightboxContainer = null;
	
	try{
		lightboxMain = document.getElementById('lightboxMain');
		lightboxContainer = document.getElementById('lightboxContainer');
		lightboxMain.style.display = "block";
		showSchoolDetailsAjax(id,lightboxContainer);
	}catch(e){
		alert("Error Here : "+e);
	}
}

function hideSchoolDetails(){
	//alert("Called to close");
	document.getElementById('lightboxMain').style.display = "none";
}

function legendSliderDisplay(status, legendId, htmlText){
	//alert(status+":"+legendId+" \n "+htmlText);
	if(isAllLayerHidden() && isAllThematicLayerHidden()){
		sliderObj.hideSlider();
	}else{
		//alert("You are here");
		sliderObj.showSlider();
	}
	cotrolLegendDisplay(status, legendId, htmlText);
}

function cotrolLegendDisplay(status, legendId, htmlText) {
	var tempMessage = "";

	var legendPaneObj = document.getElementById('legendPanel');
	var legendContainerObj = document.getElementById('legendContainer');
	
	var tempArray = null;
	var pushFlag = true;

	var i = 0, len;
	if (status) {
		// alert("Status : "+status);
		tempArray = new Array();
		tempArray[0] = legendId;
		tempArray[1] = htmlText;

		len = legendsArray.length;
		if (len > 0) {
			for (i = 0; i < len; i++) {
				if (legendsArray[i][0] == legendId) {
					pushFlag = false;
				}
			}
		}
		if(pushFlag){
			legendsArray.push(tempArray);
		}
	} else if (!status) {
		// alert("Status : "+status);
		len = legendsArray.length;
		if (len > 0) {
			for (i = 0; i < len; i++) {
				if (legendsArray[i][0] == legendId) {
					//alert("Got the position ['"+legendsArray[i][0]+"/"+legendId+"']: "+i);
					legendsArray.splice(i, 1);
					len = legendsArray.length;
				}
			}
		}
	}

	len = legendsArray.length;
	if (len > 0) {
		// alert("Length [>0] : "+len);
		tempMessage = "";
		for (i = 0; i < len; i++) {
			tempMessage += legendsArray[i][1];
		}
		legendPaneObj.style.display = "block";
		legendContainerObj.innerHTML = "";
		legendContainerObj.innerHTML = tempMessage;
	} else {
		// alert("Length [0] : "+len);
		legendPaneObj.style.display = "none";
		legendContainerObj.innerHTML = "";
	}
}

/*
http://souravb:5480/cgi-bin/mapserv.exe?map=/ms4w/project_data/district_project/thanjavur.map&layers=map_thanjavur_villageresidential_region_region&mode=tile&tilemode=ve&tile=1233300

http://souravb:5480/cgi-bin/mapserv.exe?map=/ms4w/project_data/district_project/thematic_layer.map&layers=map_layer_blockrice_samba_season_yield_per_hec&mode=tile&tilemode=ve&tile=12333000&V1=1&V2=20000
http://72.167.115.25:5280/cgi-bin/mapserv.exe?map=/DATA/project_data/179_SubDistrictProject/MapServer/thematic_layer.map&layers=map_layer_blockrice_samba_season_yield_per_hec&mode=tile&tilemode=ve&tile=12333000&V1=1&V2=20000
http://cyberswiftgis.servehttp.com:88/cgi-bin/mapserv.exe?map=/MS4W/ms4w/Apache/htdocs/dps/thematic_layer.map&layers=map_layer_blocksugarcane_production_tonnes&mode=tile&tilemode=ve&tile=1233300&V1=1&V2=20000
http://localhost:88/cgi-bin/mapserv.exe?map=/MS4W/ms4w/Apache/htdocs/dps/thanjavur.map&layers=map_thanjavur_villageresidential_region_region&mode=tile&tilemode=ve&tile=1233300
*/

/* Layer Operation */
function controlLayerOperation(showHide,mapFile,layerName,layerType,zPriority){
	hideAllThematicLayer(showHide);
	//alert("Hide Complete");
	showDistrictLayer(document.getElementById('map_district_boundary_chkbx'));
	
	if(showHide && mapFile != "" && layerName != ""){
		showHideThematicLayer(showHide,mapFile,layerName,layerType,zPriority);
	}
	
	function showDistrictLayer(obj){
		try{
			obj.checked = true;
			showHideLayer(true,'layers','map_layer_district','',4)
		}catch(e){}
	}
}

function showHideLayer(showHide,mapFile,layerName,layerType,zPriority){
	//alert("Called : "+showHide);
	var legendsAndBounds;
	var wardBoundaries = null;
	var url = mapserverurl+dataurl+mapFile+".map&"+
				"layers="+layerName+layerType+"&" +
				"mode=tile&"+ 
				"tilemode=gmap&"+ 
				"tile={X}+{Y}+{Z}";
	
	if(showHide && boudaryArray[layerName+layerType] == null){
		legendsAndBounds = getLegendsAndSetBounds();		
		boudaryArray[layerName+layerType] = new CS_LayersMapServer(CS_Gmap.getMap(),legendsAndBounds[0],transparencyLevel,legendsAndBounds[1],zPriority);		
	}else if(showHide && boudaryArray[layerName+layerType] != null){
		boudaryArray[layerName+layerType].showLayer();
	}else if(!showHide){
		boudaryArray[layerName+layerType].hideLayer();
	}
	legendSliderDisplay(showHide,layerName+layerType,boudaryArray[layerName+layerType].getLegend());
	
	function showBoundbox(map){
		setBoundingBoxOfLayer(layerName,"wkb_geometry",map);
	}
	
	function getLegendsAndSetBounds(){
		//var bounds = new GLatLngBounds(new GLatLng(10.136438369751,78.9547271728516),new GLatLng(11.1576414108276,79.5356674194336));
		var bounds = null;
		
		var colourParam;
		var legends = null;
		
		showBoundbox(CS_Gmap);		
		
		if(layerName == 'map_thanjavur_villageresidential_region_region'){
			colourParam = new Array(['255,0,0','55,0,0','Village Region']);
			legends = new CS_LayerLegends(0,0,colourParam,layerName,'legends','Village Residential Region',1);
			legends.setV1(' ');
			legends.setV2(' ');
		}else if(layerName == 'map_layer_waterbodies'){
			colourParam = new Array(['0,99,222','255,122,241','Waterbodies']);
			legends = new CS_LayerLegends(0,0,colourParam,layerName,'legends','Waterbody Layer',2);
			legends.setV1(' ');
			legends.setV2(' ');
		}else if(layerName == 'map_thanjavur_kumbakonam_region'){
			colourParam = new Array(['221,146,2','255,195,79','Kumbakonam']);
			legends = new CS_LayerLegends(0,0,colourParam,layerName,'legends','Kumbakonam Region',3);
			legends.setV1(' ');
			legends.setV2(' ');
		}else if(layerName == 'map_thanjavur_orathanadu_region'){
			colourParam = new Array(['121,3,213','177,79,252','Orathanadu']);
			legends = new CS_LayerLegends(0,0,colourParam,layerName,'legends','Orathanadu Region',4);
			legends.setV1(' ');
			legends.setV2(' ');
		}else if(layerName == 'map_thanjavur_papanasam_region'){
			colourParam = new Array(['116,213,3','176,255,82','papanasam']);
			legends = new CS_LayerLegends(0,0,colourParam,layerName,'legends','Papanasam Region',5);
			legends.setV1(' ');
			legends.setV2(' ');
		}else if(layerName == 'map_layer_district'){
			colourParam = new Array(['122,0,75','122,0,75','District Boundary']);
			legends = new CS_LineLegends(colourParam,layerName,'legends','District Boundaries',6);			
		}else if(layerName == 'map_layer_block' && layerType == ""){
			colourParam = new Array(['185,0,113','185,0,113','Block Boundary']);
			legends = new CS_LineLegends(colourParam,layerName,'legends','Block Boundaries',7);
		}else if(layerName == 'map_polyline_milk_route'){
			colourParam = new Array(['86,202,0','86,202,0','Milk Route']);
			legends = new CS_LineLegends(colourParam,layerName,'legends','Milk Route Line',8);
		}
		return [url,legends.getLegendString()];
	}
}

/* Show Hide Thematic layer */
function showHideThematicLayer(showHide,mapFile,layerName,layerType,zPriority){
	//alert("Called : "+showHide);
	var legendsAndBounds;
	var wardBoundaries = null;
	var url = mapserverurl+dataurl+mapFile+".map&"+
				"layers="+layerName+layerType+"&" +
				"mode=tile&"+ 
				"tilemode=gmap&"+ 
				"tile={X}+{Y}+{Z}";
	
	if(showHide && boudaryArray[layerName+layerType] == null){
		legendsAndBounds = getLegendsAndSetBounds();		
		thematicBoundaryArray[layerName+layerType] = new CS_LayersMapServer(CS_Gmap.getMap(),legendsAndBounds[0],transparencyLevel,legendsAndBounds[1],zPriority);		
	}else if(showHide && boudaryArray[layerName+layerType] != null){
		thematicBoundaryArray[layerName+layerType].showLayer();
	}else if(!showHide){
		thematicBoundaryArray[layerName+layerType].hideLayer();
	}
	legendSliderDisplay(showHide,layerName+layerType,thematicBoundaryArray[layerName+layerType].getLegend());
	
	function showBoundbox(map){
		setBoundingBoxOfLayer(layerName,"wkb_geometry",map);
	}
	
	function getLegendsAndSetBounds(){
		//var bounds = new GLatLngBounds(new GLatLng(10.136438369751,78.9547271728516),new GLatLng(11.1576414108276,79.5356674194336));
		var bounds = null;
		
		var colourParam;
		var legends = null;
		
		/* Zoom into specific boundbox */
		//showBoundbox(CS_Gmap);		
		
		var colourParamGreenBased = new Array(['184,249,184','50,50,50'],
											  ['149,237,149','50,50,50'],
											  ['91,210,91','50,50,50'],
											  ['59,190,59','50,50,50'],
											  ['30,159,30','50,50,50'],
											  ['50,50,50','150,150,150','No Data']);
											  //['0,125,0','50,50,50']);
		var commonBrownColourParam = new Array(	['184,249,184','50,50,50'],
												['234,153,114','150,150,150'],
												['225,138,96','150,150,150'],
												['216,117,69','150,150,150'],
												['196,89,36','150,150,150'],
												['171,58,2','150,150,150'],
												['50,50,50','150,150,150','No Data']);
		
		/* Rice Samba Total Tonnes */
		if(layerName == 'map_layer_block' && layerType == 'map_layer_block_attr_rice_samba'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(11460.08,58616.47,colourParam,layerName+layerType,'legends','Rice Samba [Tonnes]',9);
			legends.defaultInitialize();
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Rice Samba Yield per Hectare */
		else if(layerName == 'map_layer_block' && layerType == 'rice_samba_season_yield_per_hec'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(1560.17,5913.95,colourParam,layerName+layerType,'legends','Rice Samba [Tonnes/hectare]',10);
			legends.defaultInitialize();
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Rice Kar Yield per Hectare */
		else if(layerName == 'map_layer_block' && layerType == 'rice_kar_season_yield_per_hecta'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(2404.92,6729.16,colourParam,layerName+layerType,'legends','Rice Kar [Tonnes/hectare]',11);
			legends.defaultInitialize();
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Rice Kar Yield Total Tonnes */
		else if(layerName == 'map_layer_block' && layerType == 'rice_kar_season_production_tonn'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(1746.75,20290.96,colourParam,layerName+layerType,'legends','Rice Kar [Tonnes]',12);
			legends.defaultInitialize();
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Rice Navaroi Yield Yield Per Hectare */
		else if(layerName == 'map_layer_block' && layerType == 'rice_navaroi_season_yield_per_hect'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(5080,5936.74,colourParam,layerName+layerType,'legends','Rice Navaroi [Tonnes/hectare]',13);
			legends.defaultInitialize(3);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Rice Navaroi Total Tonnes */
		else if(layerName == 'map_layer_block' && layerType == 'rice_navarai_season_production_ton'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(340,3402.19,colourParam,layerName+layerType,'legends','Rice Navaroi [Tonnes]',14);
			legends.defaultInitialize(3);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Rice Suger Cane Yield Per Hectare */
		else if(layerName == 'map_layer_block' && layerType == 'sugarcane_yield_per_hectare'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(73.9,156.25,colourParam,layerName+layerType,'legends','Suger Cane [Tonnes/hectare]',15);
			legends.defaultInitialize(5);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Rice Suger Cane Total Tonnes */
		else if(layerName == 'map_layer_block' && layerType == 'sugarcane_production_tonnes'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(91616.74,431017.19,colourParam,layerName+layerType,'legends','Suger Cane [Tonnes]',16);
			legends.defaultInitialize(5);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Cattle Population */
		else if(layerName == 'map_layer_block' && layerType == 'cattle_per_sqkm'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(66.03,592.7,colourParam,layerName+layerType,'legends','Cattle [No/Sq Km]',17);
			legends.defaultInitialize(5);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Blackgram Yield Tones/Hect */
		else if(layerName == 'map_layer_block' && layerType == 'blackgram_yield_per_hectare'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(13.5,870,colourParam,layerName+layerType,'legends','Blackgram [Tonnes/hectare]',18);
			legends.defaultInitialize(5);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Blackgram Total Tones */
		else if(layerName == 'map_layer_block' && layerType == 'blackgram_production_tonnes'){
		
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(12.0974,6657.42,colourParam,layerName+layerType,'legends','Blackgram [Tonnes]',19);
			legends.defaultInitialize(5);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Cotton Tones/Hectare */
		else if(layerName == 'map_layer_block' && layerType == 'cotton_yield_per_hectare'){
		
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(71.0572,480.142,colourParam,layerName+layerType,'legends','Cotton [Tonnes/hectare]',20);
			legends.defaultInitialize(5);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Cotton Total Tones */
		else if(layerName == 'map_layer_block' && layerType == 'cotton_production_tonnes'){
		
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(11.2981,97.4411,colourParam,layerName+layerType,'legends','Cotton [Tonnes]',21);
			legends.defaultInitialize(5);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Greengram Tones/Hectare */
		else if(layerName == 'map_layer_block' && layerType == 'greengram_yield_per_hectare'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(11.2,317,colourParam,layerName+layerType,'legends','Greengram [Tonnes/hectare]',22);
			legends.defaultInitialize(5);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Greengram Total Tones */
		else if(layerName == 'map_layer_block' && layerType == 'greengram_production_tonnes'){
		
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(42.256,969.995,colourParam,layerName+layerType,'legends','Cotton [Tonnes]',23);
			legends.defaultInitialize(5);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Groundnut Tones/Hectare */
		else if(layerName == 'map_layer_block' && layerType == 'groundnut_yield_per_hectare'){
			
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(1706.25,3981.23,colourParam,layerName+layerType,'legends','Groundnut [Tonnes/hectare]',24);
			legends.defaultInitialize(5);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		/* Groundnut Total Tones */
		else if(layerName == 'map_layer_block' && layerType == 'groundnut_production_tonnes'){
		
			colourParam = colourParamGreenBased;
			legends = new CS_LayerLegends(861.281,14131.1,colourParam,layerName+layerType,'legends','Groundnut [Tonnes]',25);
			legends.defaultInitialize(5);
			url += "&V1="+legends.getV1()+"&V2="+legends.getV2()+"&V3="+legends.getV3()+"&V4="+legends.getV4()+"&V5="+legends.getV5()+"&V6="+legends.getV6();
		
		}
		
		return [url,legends.getLegendString()];
	}
}
/*
Nagashaki bari tar,ek chilo japani.
Ae she chumuk die,khete chilo chapani!!!
Hothath uthilo tar,haaf dhora hapaani.
Peyala ta chure fele,ki bhishon lafani!!!
Bheu bheu kede bole,nika kuru napani.
Ari goda gozamas,fama hitu tapani!!!!
*/
function isAllLayerHidden(){	
	if(boudaryArray != null){
		for(key in boudaryArray){
			if(boudaryArray.hasOwnProperty(key) && !boudaryArray[key].isHidden()){
				return false;
			}
		}
		return true;
	}else{
		return true;
	}
}

function isAllThematicLayerHidden(){	
	if(thematicBoundaryArray != null){
		for(key in thematicBoundaryArray){
			if(thematicBoundaryArray.hasOwnProperty(key) && !thematicBoundaryArray[key].isHidden()){
				return false;
			}
		}
		return true;
	}else{
		return true;
	}
}

function hideAllThematicLayer(showHide){
	//alert("Called hideAllLayer");
	if(showHide && !isAllThematicLayerHidden()){
		if(thematicBoundaryArray != null){
			for(key in thematicBoundaryArray){
				if(thematicBoundaryArray.hasOwnProperty(key)){
					//alert(key);
					thematicBoundaryArray[key].hideLayer();
					legendSliderDisplay(false,key,thematicBoundaryArray[key].getLegend());
				}
			}
		}
	}
}

function getBoundaryArraySize(){
	var size = 0;
	if(boudaryArray != null){
		for(key in boudaryArray){
			if(boudaryArray.hasOwnProperty(key)){
				size++;				
			}
		}
		return size;
	}else{
		return 0;
	}
}

/* Map Page Funtionality */

/* For Map Page Left Panel Menu Item Header */
function changeShowHideState(id){
	var obj = null;
	try{
		obj = document.getElementById(id);
		if(obj.style.display == "none"){
			obj.style.display = "block";
		}else{
			obj.style.display = "none";
		}
	}catch(e){
		alert("[Show Hide Left Panel] ID : '"+id+"' Doesnot Exists" );
	}
}
/* For Map Page Legend Display Panel Operation */
function expandCollapseLegendPane(imageId,divId,divId1) {
	//alert("Fired");
	var imgObj = document.getElementById(imageId);
	var panelDivObj = document.getElementById(divId);
	var panelDivObj1 = document.getElementById(divId1);

	//alert(panelDivObj.style.width+":"+panelDivObj1.style.width);
	
	if(panelDivObj.style.width == "200px"){
		try{
			startHiding(panelDivObj,panelDivObj1,200);
		}catch(e){alert(e);}
	} else {
		panelDivObj1.style.display = "block";
		startShowing(panelDivObj,panelDivObj1,0,200);
	}

	function startHiding(targetDiv,targetDiv1,size){		
		//alert(size+" : "+targetDiv.style.width+" : "+targetDiv1.style.width);			
		imgObj.src = baseUrl+"/assets/images/legendPanel/legendPaneSwitchExpand.png";
		imgObj.title = "Show Legend Panel";
		
		targetDiv.style.width = size+"px";
		targetDiv1.style.width = size+"px";
		
		while(size >= 0){
			size = parseInt(size/3);
			
			targetDiv.style.width = size+"px";
			targetDiv1.style.width = size+"px";
			
			if(size == 0 || size == 1){
				targetDiv1.style.display = "none";
				break;				
			}
		}
	}

	function startShowing(targetDiv,targetDiv1,size,maxSize){
		imgObj.src = baseUrl+"/assets/images/legendPanel/legendPaneSwitch.png";
		imgObj.title = "Hide Legend Panel";
		
		targetDiv.style.width = size+"px";
		targetDiv1.style.width = size+"px";
		
		while((maxSize-size) >= 0){
			size = parseInt(size + ((maxSize-size)/3) + 1);
			
			targetDiv.style.width = size+"px";
			targetDiv1.style.width = size+"px";
			
			if((maxSize-size) == 0){
				break;
			}
		}
	}
}
