function MWS_Services_ElectionMapper(searchDiv, searchResultsDiv, onClickCode){
	var options2 = {
		maxScale: 500,
		minScale: 500000000,
		minResolution: "auto",
		minExtent: new OpenLayers.Bounds(-1, -1, 1, 1),
		maxResolution: "auto",
		maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90),
		controls: [new OpenLayers.Control.MouseDefaults(),new OpenLayers.Control.PanZoomBar()]
	};

	MWS.MWS_ElectionMapObj = new OpenLayers.Map('electionmapPanel', options2);
	var mapavenue_wms2 = new OpenLayers.Layer.WMS( "MapAvenue Election Map",
		[MWS.GeoServerUrl],
		{layers: 'izongo0', format: "image/png"},{singleTile: true, ratio: 1}
		);
	MWS.MWS_ElectionMapObj.addLayers([mapavenue_wms2]);
	
	var contentText = '<div class="wrap"><div><div id="'+searchDiv+'" class="srchPanel"></div></div></div>';
	MWS.electionmapperSearchTab = new YAHOO.widget.Tab
		({ id:'electionmapperView', label: "Election Mapper", content: contentText, active:true });	

	MWS.electionmapperElectionLayerFilter="";
	MWS.electionmapperElectionID="";
	MWS.electionmapperElectionCountryCode="";
	MWS.electionmapperElectionRegionCode="";
	MWS.electionmapperElectionDistrictCode="";

	MWS.electionmapperElectionLayer = null;
	
	MWS.electionmapperPopUp = null;
	MWS.electionmapperItem = function ()
	{
		this.ID = 0;
		this.X = 0;
		this.Y = 0;
		this.Number = 0;
		this.Name = "";
		//this.Type = "";
		this.UniqueID = "";		
	}
	 
	MWS.electionmapperItems = new Array();

	MWS.MWS_addService("electionmapper", "Election Mapper", "event");
	var startUpFunction = function (){
		MWS.MWS_resetEnvironment();
		var serviceID = "electionmapper";

		var svc = MWS.MWS_getService(serviceID);
				
		MWS.ServiceClearTabs();	
		MWS.ServiceTabs.addTab(MWS.electionmapperSearchTab);
		//MWS.ServiceTabs.addTab(MWS.electionmapperSearchResultsTab);
		MWS.ServiceTabs.set('activeIndex', 0);			
		YAHOO.example.app.tabView.set('activeIndex', 1);	
		
		var div = OpenLayers.Util.getElement(MWS.MWS_getServiceDiv(serviceID, 0));

		var userLastKey = "";
		try{userLastKey = MWS.MWS_ExternalFrameDocumentObj.forms[0].userLastKey.value;}catch(e){}

		if (div!=null)
			div.innerHTML="<iframe width='100%' frameborder='0'" + 
		"name='frameelectionmapper' " +
		"src='mavlibs/utils/electionmapper/manager.php?"+
		"&sessionid="+MWS.MWS_SessionID+
		"&pmPageList=main"+"&pmPage=main"+
		"&electionmapper_country_code="+MWS.pageheaderLocation.country_code +
		"&electionmapper_country_name="+MWS.pageheaderLocation.country_name +
		"&electionmapper_extents="+MWS.MWS_MapObj.getExtent().toBBOX() +
		"' width='280' height='300'" +
		"></iframe>";

		var bounds = new OpenLayers.Bounds();
		bounds.extend(new OpenLayers.LonLat(-20,-30));
		bounds.extend(new OpenLayers.LonLat(40,40));

		MWS.MWS_ElectionMapObj.zoomToExtent(bounds);
		MWS.MWS_ElectionMapObj.zoomTo(3);	
	};
	
	var cleanUpFunction = function (){
		try{
		if (MWS.electionmapperElectionLayer!=null)
		{
			MWS.MWS_ElectionMapObj.removeLayer(MWS.electionmapperElectionLayer);
			MWS.electionmapperElectionLayer.destroy();
			MWS.electionmapperElectionLayer=null;
		}
		}catch(e){}
		//MWS.MWS_ElectionMapObj.removeLayers([MWS.electionmapperElectionLayer]);
	};

	var mapClickFunction = function (e){	
		var mav_lonlat = MWS.MWS_ElectionMapObj.getLonLatFromViewPortPx(e.xy);
		MWS.MWS_x = mav_lonlat.lon;
		MWS.MWS_y = mav_lonlat.lat;
		MWS.MWS_zoom = MWS.MWS_ElectionMapObj.zoom;
		MWS.MWS_scale = MWS.MWS_ElectionMapObj.getScale();
		
		MWS.electionmapperClickDiv(MWS.MWS_x,MWS.MWS_y);				
	};
	
	var t = "<iframe />";
	var svc = MWS.MWS_getService("electionmapper");
	svc.StartUpFunction = startUpFunction;
	svc.CleanUpFunction = cleanUpFunction;
	MWS.MWS_ElectionMapObj.events.register("click", MWS.MWS_ElectionMapObj, mapClickFunction);
	//svc.MapClickFunction=mapClickFunction;
	
	svc.ResultsInitText = t;

	MWS.MWS_setServiceDiv("electionmapper", searchDiv);
	MWS.MWS_setServiceDiv("electionmapper", searchResultsDiv);

	MWS.electionmapperCountryClicked= function (extents)
	{
		var coords = extents.split(",");	
		var bounds = new OpenLayers.Bounds();
		bounds.extend(new OpenLayers.LonLat(coords[0],coords[1]));
		bounds.extend(new OpenLayers.LonLat(coords[2],coords[3]));

		MWS.MWS_ElectionMapObj.zoomToExtent(bounds);
	}

	MWS.electionmapperLoadLayerInMap = function (frm, extents, country_code)
	{
		MWS.electionmapperElectionID = 
			frm.electionmapper_electionid.options[frm.electionmapper_electionid.selectedIndex].value;

		//Update Legend Div
		var url = 'mavlibs/utils/electionmapper/webservice.php?'+
			'request=partycolors&country='+country_code+'&electionid='+MWS.electionmapperElectionID;
		OpenLayers.loadURL(url, '', MWS.MWS_ElectionMapObj, MWS.electionmapperParseLegend);
			
		url = MWS.MapServerUrl1 + 'map=/data/prod/appdata/mapfile/elections/elections.map&';
		url += "electionid=" + MWS.electionmapperElectionID + "&admin1=" + country_code.toUpperCase() + "&";	
		var coords = extents.split(",");

		MWS.electionmapperElectionLayerFilter=
			"<Filter><And><PropertyIsEqualTo>"+
			"<PropertyName>electionid</PropertyName><Literal>"+MWS.electionmapperElectionID+"</Literal>"+
			"</PropertyIsEqualTo>"+
			"<PropertyIsEqualTo>"+
			"<PropertyName>voterank</PropertyName><Literal>1</Literal>"+
			"</PropertyIsEqualTo></And></Filter>";

		try{
		if (MWS.electionmapperElectionLayer!=null)
		{
			MWS.MWS_ElectionMapObj.removeLayer(MWS.electionmapperElectionLayer);
			MWS.electionmapperElectionLayer.destroy();
			MWS.electionmapperElectionLayer=null;
		}
		}catch(e){}
		MWS.electionmapperElectionLayer=null;
		//http://www.africonmap.com/cgi-bin/mapserv.exe?map=E:\data\prod\appdata\mapfile\elections\elections.map&
		MWS.electionmapperElectionLayer = new OpenLayers.Layer.WMS( "Election Map",
			url,
			{
			//layers: 'izongo:vw_results_admin2,izongo:vw_results_admin2', 
			layers: 'election_result,election_bnd,outline', 
			format: "image/png",
			//filter: MWS.electionmapperElectionLayerFilter,
			//styles:'izongo_election_admin2,izongo_election_admin2a',
			transparent: true
			},{singleTile: true, ratio: 1});
		//MWS.electionmapperElectionLayer.setTileSize(new OpenLayers.Size(768,768));

		MWS.electionmapperElectionLayer.setIsBaseLayer(false);
		//MWS.electionmapperElectionLayer.setVisibility(false);
		//MWS.electionmapperElectionLayer.displayInLayerSwitcher = false;
		MWS.MWS_ElectionMapObj.addLayers([MWS.electionmapperElectionLayer]);

		var bounds = new OpenLayers.Bounds();
		bounds.extend(new OpenLayers.LonLat(coords[0],coords[1]));
		bounds.extend(new OpenLayers.LonLat(coords[2],coords[3]));

		MWS.MWS_ElectionMapObj.zoomToExtent(bounds);
				
	}
		
	/*
	** This function is called to parse the results from the real estate search.
	** These are the search results displayed in left panel.
	** Function also displays real estate markers on map.
	*/
	MWS.electionmapperParseLegend = function (resp)
	{
		var legend="<b>Legend:</b><br />";
		if (resp.responseXML!=null)
		{
			var theXML = resp.responseXML.documentElement;
			var r = theXML.getElementsByTagName('ResultSet')[0].childNodes;
			
			var el = theXML.getElementsByTagName('Election')[0];

			var electionid = MWS_Utils.getNodeText(el, 'electionid', "");
			var electiondate = MWS_Utils.getNodeText(el, 'electiondate', "");
			var electionname = MWS_Utils.getNodeText(el, 'electionname', "");
			var comments = MWS_Utils.getNodeText(el, 'comments', "");
			var notes = MWS_Utils.getNodeText(el, 'notes', "");
			var admin1 = MWS_Utils.getNodeText(el, 'admin1', "");
			var electiontype = MWS_Utils.getNodeText(el, 'electiontype', "");
			var countertype = MWS_Utils.getNodeText(el, 'countertype', "");
			var data_source = MWS_Utils.getNodeText(el, 'data_source', "");

			for (var i = 0; i < r.length; i++)
			{
				var oResult = r[i];
				if(r[i].firstChild)
				{
					var partycolor_weak = MWS_Utils.getNodeText(r[i], 'partycolor_weak', "");
					var partycolor_strong = MWS_Utils.getNodeText(r[i], 'partycolor_strong', "");
					var partycolor = MWS_Utils.getNodeText(r[i], 'partycolor', "");
					var partyname = MWS_Utils.getNodeText(r[i], 'partyname', "");
					var partyid = MWS_Utils.getNodeText(r[i], 'partyid', "");
					var admin1 = MWS_Utils.getNodeText(r[i], 'admin1', "");
					legend +=partyname + "("+partyid+"):<br />\n";
					legend +="<table border=0 width='100%'>";
					legend += "<tr><td>Leads With 44% or less</td><td bgcolor='#"+partycolor_weak+"' width='15%'></td></tr>"+
						"<tr><td>Leads With 45% to 59%</td><td bgcolor='#"+partycolor+"' width='15%'></td></tr>"+
						"<tr><td>Leads With 60% or more</td><td bgcolor='#"+partycolor_strong+"' width='15%'></td></tr>"+
						"</table><br />\n";
				}
			} 
		}	
		
		var div = OpenLayers.Util.getElement("electionlegendPanel");
		if (div!=null)
		{
			div.innerHTML=
			'<b>'+electionname+'</b><br />'+legend;
			
			if (data_source!= "")
			{
				div.innerHTML += '<br />Data Source: ' + data_source;
			}
		}
		
	};
	
MWS.electionmapperClickDiv = function( x,y ) {
		DetailBubble = OpenLayers.Class(OpenLayers.Popup.AOMFramedCloud, {'autoSize': true});

		var feature = new OpenLayers.Feature(MWS.electionmapperResultsLayer, new OpenLayers.LonLat(x,y));
		feature.closeBox = true;
		feature.popupClass = DetailBubble;
		//feature.data.popupContentHTML = "<div class='olFramedCloudPopupContent'><div style='{position: relative; font-size: 8pt;}'><table><tr><td>TESTING TESTING <BR>TESTINGdfdfdv  dfd dfdfdfasdf <BR>TESTINGdfdfdv  dfd dfdfdfasdf <BR>TESTINGdfdfdv dfdfdfdfdfdfdfdfdfdfdffdfdfdfdf LAST LINE :-)</td></tr></table></div></div>";
		feature.data.popupContentHTML = "<div class='olFramedCloudPopupContent'><div style='{position: relative; font-size: 8pt;}'></div></div>";
		feature.data.overflow = "hidden";

		feature.data.popupContentHTML = 
			MWS.electionmapperCreatePopUpContent(x,y);
		MWS.electionmapperRemovePopUp();
		MWS.electionmapperPopUp = feature.createPopup(true);
		//MWS.electionmapperCreatePopUpContent(id);
		MWS.MWS_ElectionMapObj.addPopup(MWS.electionmapperPopUp);
		MWS.electionmapperPopUp.show();
		//OpenLayers.Event.stop(evt);
	}

MWS.electionmapperRemovePopUp = function() {
	if (MWS.electionmapperPopUp!=null)
	{
		try{
		MWS.MWS_ElectionMapObj.removePopup(MWS.electionmapperPopUp);
		MWS.electionmapperPopUp.destroy();
		MWS.electionmapperPopUp = null;
		}catch(e){}
	}
}
//This function creates the html that will be displayed in the popup
MWS.electionmapperCreatePopUpContent = function(x,y) {
		var html = "<div style='width:250px;'>" + 
			"<div class='reListingHdr'>" + 
			x + ' at time item.Time headed in the direction ' + y +
			' north, travelling at a speed of item.Speed km/h<br>'+
			"</div>" +
			"</div>";			
				
		//This is for Detailed Popup
		html = "<iframe  width='100%' frameborder='0'" + 
			"name='frameelectionmapper' " +
			"src='mavlibs/utils/electionmapper/manager.php?"+
			"&sessionid="+MWS.MWS_SessionID+
			"&electionmapper_x="+x+"&electionmapper_y="+y+
			"&electionmapper_electionid="+MWS.electionmapperElectionID+
			
			"&pmPageList=detail"+"&pmPage=detail"+
			"' width='200' height='200'" +
			"></iframe>";	
		return html;
}

}