/*************************************************************************
 * $Id: identify.js ,v 1.2 2009/09/29 12:30:40 
 
 * author: Najima Muhammad 
 
 * modifications by : Najima Muhammad 
 
 * Copyright (c) 2009, Keltron Consultancy services.
 
**************************************************************************/

var coords,store;
var identifyPopup;
var highlitelayer="";
var webAddress="";
var bName="";

/******* PASS XY POINTS TO GET THE INFORMATION **********************/
function handleMapClick(evt){
	
	queryFlag=1;
	coords=map.getLonLatFromViewPortPx(evt.xy);
	var scale=Math.round(map.getScale());
	var params='x='+coords.lon+'&y='+coords.lat+"&scale="+scale;		
	store = new dojo.data.ItemFileReadStore({url: '/igsg/identify/?'+params});
	//alert("fjgj");
	var request = store.fetch({onComplete: gotitems,onError:showIdeError});
	
}
/******* ALERT ERROR MESSAGE IF ANY **********************/
function showIdeError(er)
{
	//alert(er);
}

/******* FETCH JSON TO SHOW THE RESULT **********************/
function gotitems(items, request)
{
	var names = new Array();
	var gid = new Array();
	var locality = new Array();
	var officeContent="";
	var placeContent="";
	var panchayathContent="";
	var roadContent="";
	var outerContent="";
	var identifyGovtOffFlag=0;
	for(var i=0;i<items.length;i++)
	{
		var layer=store.getValue(items[i],'layer');
		gid[i]=store.getValue(items[i],'gid');
		if(layer=='LandMarkBuild')
		{
			
			bName=store.getValue(items[i],'name');
			
			officeContent+="<br> <div class=popTable style=background-color:#f7f0f8;><table><tr  ><td><a href='#' onclick=getOfficesInBuiding("+gid[i]+");>"+bName+"</a></td></tr></table></div>";
			
			if (identifyPopup )
			{	
				map.removePopup(identifyPopup);
				identifyPopup.destroy();
				identifyPopup=null;	
			}
			identifyPopup = new OpenLayers.Popup.FramedCloud("junction",coords,null,officeContent,null,true,cancelMe);
		    	coords.popup = identifyPopup;
		    	map.addPopup(identifyPopup);
		}
		else if(layer=='PlaceName')
		{
			var name1=store.getValue(items[i],'name');
			placeContent="<br><div class=popTable><table width=100% height=15px ><tr  bgcolor=#f7f0f8><th>Place</th></tr><tr  bgcolor=#eaf1fd  ><td>"+name1+"</td></tr><table></div>";
			if (identifyPopup)
			{	
				map.removePopup(identifyPopup);
				identifyPopup.destroy();
				identifyPopup=null;
			}
			identifyPopup = new OpenLayers.Popup.FramedCloud("junction",coords,null,placeContent,null,true,cancelMe);
            		coords.popup = identifyPopup;
            		map.addPopup(identifyPopup);
            		i=items.length;
            		
		}
		else if(layer=='RoadsOfKerala')
		{
			var name1=store.getValue(items[i],'name');
			if(name1 )
			{
				roadContent="<br><div class=popTable style=background-color:#eaf1fd;><span>"+name1+"</span></div>";
            			if (identifyPopup)
				{	
					map.removePopup(identifyPopup);
					identifyPopup.destroy();
					identifyPopup=null;
				}
				identifyPopup = new OpenLayers.Popup.FramedCloud("junction",coords,null,roadContent,null,true,cancelMe);
            			coords.popup = identifyPopup;
            			map.addPopup(identifyPopup);
            		}
            		else
            		{
            			i=items.length;
            		}
            		i=items.length;
		}
		else if(layer=='PanchayathBoundary')
		{
			var panchayat=store.getValue(items[i],'panchayat');
			if(panchayat==null)
				panchayat="----";
			var district=store.getValue(items[i],'district');
			if(district==null)
				district="----";
			var municipality=store.getValue(items[i],'municipali');
			if(municipality==null)
				municipality="----";
			var corporation=store.getValue(items[i],'corporatio');
			if(corporation==null)
				corporation="----";
			panchayathContent="<br><div class=popTable ><table width=100% height=70% ><tr bgcolor=#f7f0f8 ><td><b>Panchayath: </b></td><td>"+panchayat+"</td></tr><tr bgcolor=#eaf1fd ><td><b>Municipality: </b></td><td> "+municipality+"</td></tr><tr  bgcolor=#f7f0f8 ><td><b>Corporation: </b></td><td> "+corporation+"</td></tr><tr bgcolor=#eaf1fd ><td><b>District: </b></td><td> "+district+"</td></tr><table></div>";
			if (identifyPopup)	
			{
				map.removePopup(identifyPopup);
				identifyPopup.destroy();
				identifyPopup=null;
			}
			identifyPopup = new OpenLayers.Popup.FramedCloud("junction",coords,null,panchayathContent,null,true,cancelMe);
            		coords.popup = identifyPopup;
            		map.addPopup(identifyPopup);
            		i=items.length;
		}
		else if(layer=='Outerarea')
		{
			var name1=store.getValue(items[i],'name');			
			outerContent="<br><div class=popTable style=background-color:#f7f0f8;><span>"+name1+"</span></div>";
			if (identifyPopup)	
			{
				map.removePopup(identifyPopup);
				identifyPopup.destroy();
				identifyPopup=null;
			}
			identifyPopup = new OpenLayers.Popup.FramedCloud("junction",coords,null,outerContent,null,true,cancelMe);
            		coords.popup = identifyPopup;
            		map.addPopup(identifyPopup);
            		i=items.length;
		}
		
	}
	
}

/******* GET OFFICES IN A BUILDING **********************/
function getOfficesInBuiding(id1)
{
	
	var nam=new Array();
	var id=new Array();
	var buildcontent="";
	var OfficesInBuidingstr= new dojo.data.ItemFileReadStore({url: '/igsg/officeBuilding/'+id1+'/'});
	var request = OfficesInBuidingstr.fetch({onComplete:function(items, request)
	{
		for(var i=0;i<items.length;i++)
		{
			nam[i]=OfficesInBuidingstr.getValue(items[i],'name');
			id[i]=OfficesInBuidingstr.getValue(items[i],'id');
			buildcontent+="<tr  ><td><a href='#' onclick=showIdentifyDetails("+id[i]+");>"+nam[i]+"</a></td></tr>";	
		}
		buildcontent="<br> <div class=popTable style=background-color:#f7f0f8;><table><tr><th>Building Name: "+bName+"</th></tr>"+buildcontent+"</table></div>";
		if (identifyPopup )
		{	
			map.removePopup(identifyPopup);
			identifyPopup.destroy();
			identifyPopup=null;	
		}
		identifyPopup = new OpenLayers.Popup.FramedCloud("junction",coords,null,buildcontent,null,true,cancelMe);
	    	coords.popup = identifyPopup;
	    	map.addPopup(identifyPopup);
	}});
	 
}

/******* SHOW THE OFFICE DETAILS IN A DIALOG BOX **********************/
function showIdentifyDetails(id)
{
	
	var params="layer=Office&gid="+id;
	//alert(params);
	var identifyStore= new dojo.data.ItemFileReadStore({url: '/igsg/show/?'+params});
	var request = identifyStore.fetch({onComplete:function(items, request)
		{
			for(var i=0;i<items.length;i++)
			{
				
				if(identifyStore.getValue(items[i],'gid')==id)
				{
					document.getElementById("official").innerHTML=' ';
					document.getElementById("serviceinfo").innerHTML=" ";
					document.getElementById("contact").innerHTML=" ";
					var name1=identifyStore.getValue(items[i],'name');
					var address=identifyStore.getValue(items[i],'address');
					var locality=identifyStore.getValue(items[i],'locality');
					var dist=identifyStore.getValue(items[i],'district');
					var tel=identifyStore.getValue(items[i],'telephone');
					var tel1=tel.split("|");
					var tel2="";
					for(var j=0,k=1;j<tel1.length-1;j++,k++)
					{
						if(tel1[j]==0||tel1[j]=="None")
							tel1[j]="------";
						tel2 =tel2+" "+tel1[j]+"<br>";
					}
					var fax=identifyStore.getValue(items[i],'fax');
					if(fax==0||fax=="None")
						fax="------";
					webAddress=identifyStore.getValue(items[i],'web');
					var type=identifyStore.getValue(items[i],'type');
					var department=identifyStore.getValue(items[i],'department');
					var subdept=identifyStore.getValue(items[i],'sub_department');
					if(subdept=='Default'||subdept=='default')
						subdept="--------";
					var designation=identifyStore.getValue(items[i],'head_designation');
					var services=identifyStore.getValue(items[i],'service');
					var service=services.split("|");
					var ser="";
					for(var j=0,k=1;j<service.length-1;j++,k++)
						ser =ser+" "+service[j]+"<br>";
					var juri=identifyStore.getValue(items[i],'jurisdiction');
					document.getElementById("official").innerHTML="<div class=popTable><table width=100% height=100% ><tr class=odd ><td><i>Department </i></td><td><b>"+department+"</b></td></tr><tr class=even ><td><i>Sub Department</i></td><td><b>"+subdept+"</b></td></tr><table></div>";
					document.getElementById("serviceinfo").innerHTML="<div div class=popTable >"+ser+"<div>";
					document.getElementById("contact").innerHTML="<div div class=popTable ><table width=100% height=100% ><tr class=odd  ><td>"+name1+",</td></tr><tr class=even ><td>"+address+"</td></tr><tr class=odd><td><i>Phone No. </i>:"+tel2+"</td></tr><tr  class=even ><td><i>Fax </i>:"+fax+"</td></tr><tr  class=odd ><td><i>Website</i> :  <a href='#' onclick=openWebsite(); >"+webAddress+"</a> </td></tr><table><div>";
					dijit.byId('OueryResult').titleNode.innerHTML="<span style=font-size:14px >"+name1+"</span>";
					dijit.byId('OueryResult').show();
					dijit.byId('OueryResult')._underlay.hide();
				}
			}
		}
	});
}
/******* OPEN THE PARTICULAR DEPARTMENT WEBSITE **********************/
function openWebsite()
{
	//window.open (webAddress,"mywindow","location=1,status=1,scrollbars=1,toolbar=1"); 
	window.open ("http://"+webAddress,"mywindow","location=1,status=1,scrollbars=1,toolbar=1");
}

/******* DELETE THE POPUP ***************/
function cancelMe()
{
	
	if(identifyPopup)
	{
		map.removePopup(identifyPopup);
		identifyPopup.destroy();
		identifyPopup=null;
	}
		
}

