//class Map
//{
//Constructer Function


//delegate function for search result
var delegateFunction = null;
var ZoomStateListener = null;
var ZoomKeyPressListener = null;

//  Array of user defined GIS mouse click listeners to be set in client
var arrGISMouseClickListeners = new Array();

//  Array of GIS states to be set by client
var arrGISMouseClickStates = new Array();

// Specify the overview image rather than use ArcIMS
var strOverviewImageURL="";

function IsNull(obj)
		{
		    if(obj != null && typeof(obj) !='undefined')
            {
                return false;
            }
		    else
		    {
		        return true;		    
		    }
		}
		
function Map(region_name,divID,WebWizardVirtualDirectory,minEast,minNorth,maxEast,maxNorth,LayersToShow,sliderZoomFactor, bFullScreen, lon, lat, initZoomFactor, showMarker,searchLayerID,searchFieldName,searchFieldValue,SearchZoomScaleFactor)
{
	//public members
	this.region_name = region_name;
	this.VirtualDirectory = WebWizardVirtualDirectory;
	this.divMapContainer = document.getElementById(divID);
	this.mapMainDocument = null;
	this.mapFrame = null; // it represents the WebWizard mapFrame containing all the methods
	this.searchCallback = null; //this callback needs to be provided by the client
	this.othis = this;
	//this.MainIFrame=null;
	this.mapFrameEngine = null; // it represents the WebWizard mapFrameEngine containing all the methods
	this.mapFrameWindow = null;
	//delegates that need to be set by client to listen to Zoom events	
	 
	
	// private Functions
	/*
	function CreatIFrame(parentDiv,source,id,width,height) 
	{ 
		//parentDiv.innerHTML += '<IFRAME id="mapIFrame" name="map" src="http://localhost/Webwizard/Logon.aspx?LoginUser=GUEST&amp;ApplicationMode=Embedded&amp;RegionName=LYR1_Croydon3"	width="100%" height="100%"></IFRAME>';
		var htmlstring = '';
		htmlstring += '<IFRAME id="'+id+'"';
		htmlstring += 'name="'+id+ '"';
		htmlstring += 'src="' + source + '"';
		htmlstring += 'width="'+width+'" height="'+height+'"';
		//htmlstring += 'style="border-right: black thin solid;border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid"></IFRAME>';
		htmlstring += '></IFRAME>';
		parentDiv.innerHTML += htmlstring;
	}
	*/
	var MapFrameHeight=parseInt(frames['mapFrame'].frameElement.clientHeight);
	var MapFrameWidth=parseInt(frames['mapFrame'].frameElement.clientWidth);
	//Create mapFrame
	
	//CreatIFrame(this.divMapContainer,this.VirtualDirectory+'mapViewer.htm','mapFrame','100%','100%');
	
	
	//Create mapEngineFrame
	
	
	//var strurl = this.VirtualDirectory+'mapViewerEngine.aspx?ApplicationMode=Embedded&amp;RegionName=' + this.region_name + '&maxEast=' + maxEast + '&minEast=' +minEast+ '&minNorth=' +minNorth+ '&maxNorth=' +maxNorth+ '&LayersToShow=' +LayersToShow+ '&date=1/1/1-12:0:0&filter=All&MapFrameHeight='+MapFrameHeight+'&MapFrameWidth='+MapFrameWidth+'&bFirstLoad=true';
	
	//Initializing Code
	//var strurl = this.VirtualDirectory + 'mapViewerframes.aspx?ApplicationMode=Embedded&amp;RegionName=' + this.region_name + '&maxEast=' + maxEast + '&minEast=' +minEast+ '&minNorth=' +minNorth+ '&maxNorth=' +maxNorth+ '&LayersToShow=' +LayersToShow+ '&date=1/1/1-12:0:0&filter=All'+'&zoomFactor='+zoomFactor;
	//CreatIFrame(this.divMapContainer,this.VirtualDirectory+'mapViewerEngine.aspx?MapFrameHeight='+MapFrameHeight+'&MapFrameWidth='+MapFrameWidth+'&ApplicationMode=Embedded&amp;RegionName=' + this.region_name + '&maxEast=' + maxEast + '&minEast=' +minEast+ '&minNorth=' +minNorth+ '&maxNorth=' +maxNorth+ '&LayersToShow=' +LayersToShow+'&bFirstLoad=true','MapEngineFrame',0,0);
	
	var bEmbedded = false;
	if(!IsNull(document.getElementById("bEmbedded")))
	{
	    bEmbedded = document.getElementById("bEmbedded").value;
	}

	var strurl = this.VirtualDirectory+'mapViewerEngine.aspx?bEmbedded='+bEmbedded+'&MapFrameHeight='+MapFrameHeight+'&MapFrameWidth='+MapFrameWidth+'&ApplicationMode=Embedded&RegionName=' + this.region_name + '&maxEast=' + maxEast + '&minEast=' +minEast+ '&minNorth=' +minNorth+ '&maxNorth=' +maxNorth+ '&LayersToShow=' +LayersToShow+'&bFirstLoad=true'+'&zoomFactor='+sliderZoomFactor+'&FullScreen='+bFullScreen;

    if(searchLayerID!=null && searchFieldName != null && searchFieldValue != null)
    {
        var dpi = screen.deviceXDPI?screen.deviceXDPI:96;
        strurl += '&SearchLayerID='+searchLayerID+'&SearchFieldName='+searchFieldName+'&SearchFieldValue='+searchFieldValue+'&SearchZoomScaleFactor='+SearchZoomScaleFactor+'&dpi='+dpi;
    }
    else if(lon != null && lat != null && initZoomFactor != null && showMarker != null)
    {
        var dpi = screen.deviceXDPI?screen.deviceXDPI:96;
        strurl += '&lon=' + lon + '&lat=' + lat + '&initZoomFactor=' + initZoomFactor + '&dpi=' + dpi + '&showMarker=' + showMarker;        
    }
    
    
	frames['MapEngineFrame'].location.href = strurl;
	//document.getElementById('MapEngineFrame').src = strurl; 
	
	//CreatIFrame(this.divMapContainer,this.VirtualDirectory+'PostBack HTML.aspx','PostBack',0,0);
	
	
	this.IsInitialized();
	//this.mapMainDocument.onreadystatechange = this.OnReadyStateChange;

}

//Public Methods
//Map::IsInitialized() Checks if document is loaded and ready to be used
function IsInitialized()
{
		this.mapFrameWindow = frames['mapFrame'];
		this.mapFrameEngine = frames['MapEngineFrame'];
		if(this.mapFrameWindow && this.mapFrameWindow.bMapViewerWindowLoaded==true && this.mapFrameEngine && this.mapFrameEngine.bMapViewerEngineLoaded == true)
		{
		//alert('initialized');
		    this.mapFrameWindow.ZoomStateListener = this.OnZoomStateChange;
            this.mapFrameWindow.ZoomKeyPressListener = this.OnZoomKeyPress;
                       
            this.mapFrameWindow.arrGISMouseClickStates=arrGISMouseClickStates;
            this.mapFrameWindow.arrGISMouseClickListeners=arrGISMouseClickListeners;
                         
            this.mapFrameWindow.OverviewImageURL=strOverviewImageURL;
            
			return true;
		}
	
	return false;
}
Map.prototype.IsInitialized = IsInitialized;

function SetOverviewImage(strURL)
{
    strOverviewImageURL=strURL;   
}

Map.prototype.SetOverviewImage = SetOverviewImage;

//  Use this function to add custom GIS mouse click states and event handlers
function AddGISMouseClickState(GISMouseClickState, GISMouseClickListeners)
{
    var nIndex=arrGISMouseClickStates.length;
    arrGISMouseClickStates[nIndex]=GISMouseClickState;
	arrGISMouseClickListeners[nIndex]=GISMouseClickListeners;		
	
}
Map.prototype.AddGISMouseClickState = AddGISMouseClickState;

function GetCurrentGISState()
{
    return frames['mapFrame'].state;
}

function ChangeCursorType(type)
{
    var mapFrame = frames['mapFrame'];
    mapFrame.changeCursorType(type);
}
Map.prototype.ChangeCursorType = ChangeCursorType;


function ShowWaitCursor(bShow)
{
    var mapFrame = frames['mapFrame'];
    
    try
    {
        if(bShow==true)
        {
            mapFrame.changeCursorType('wait');
        }
        else
        {
            mapFrame.changeCursorType('auto');    
        }
        
    }
    catch(e)
     {}

}
Map.prototype.ShowWaitCursor = ShowWaitCursor;

function ShowGISProgressImage(bShow)
{
    try
    {
        if(bShow==true)
        {
            frames['mapFrame'].showLayer("loadLayer");
        }
        else
        {
             frames['mapFrame'].hideLayer("loadLayer");
        }
     }
     catch(e)
     {}
    
}
Map.prototype.ShowGISProgressImage = ShowGISProgressImage;


function GetGISMapMouseX()
{
    var mapFrame = frames['mapFrame'];
    mapFrame.getMapXY( mapFrame.mouseX, mapFrame.mouseY);
    
    return mapFrame.mapX;       
}
Map.prototype.GetGISMapMouseX = GetGISMapMouseX;

    
function GetGISMapMouseY()
{
    var mapFrame = frames['mapFrame'];
    mapFrame.getMapXY( mapFrame.mouseX, mapFrame.mouseY);
    
    return mapFrame.mapY;       
}
Map.prototype.GetGISMapMouseY = GetGISMapMouseY;


function GetGISPixelMouseX()
{
    var mapFrame = frames['mapFrame'];
    return mapFrame.window.mouseDown.X       
}
Map.prototype.GetGISPixelMouseX = GetGISPixelMouseX;

    
function GetGISPixelMouseY()
{
    var mapFrame = frames['mapFrame'];
    return mapFrame.window.mouseDown.Y     
}
Map.prototype.GetGISPixelMouseY = GetGISPixelMouseY;

function GetMapYFromPixelXY(PixelX,PixelY)
{
    var mapFrame = frames['mapFrame'];
    mapFrame.getMapXY(PixelX,PixelY);
    
    return mapFrame.mapY;
}

Map.prototype.GetMapYFromPixelXY = GetMapYFromPixelXY;

function GetMapXFromPixelXY(PixelX,PixelY)
{
    var mapFrame = frames['mapFrame'];
    mapFrame.getMapXY(PixelX,PixelY);
    
    return mapFrame.mapX;
}

Map.prototype.GetMapXFromPixelXY = GetMapXFromPixelXY;

function GetMapWidth()
{   
    var mapFrame = frames['mapFrame'];
 
    return  mapFrame.dMapWidth;
}

Map.prototype.GetMapWidth = GetMapWidth;


//Following are event listeners of Map
function OnZoomStateChange(max,min,current)
{
    if(ZoomStateListener != null)
    {
        ZoomStateListener(max,min,current);                
    }     
}
Map.prototype.OnZoomStateChange = OnZoomStateChange;

function OnZoomKeyPress(up,mapObj)
{
    if(ZoomKeyPressListener != null)
    {
      ZoomKeyPressListener(up,mapObj);                
    }
}
Map.prototype.OnZoomKeyPress = OnZoomKeyPress;


function getSliderState() 
{
	if(this.IsInitialized())
	{
		return this.mapFrameWindow.getSliderState();
	}
}
Map.prototype.getSliderState = getSliderState;

function Magnify(val)
{
    if(this.IsInitialized())
	{
        this.mapFrameWindow.zoomInterActive(val);
    }
}
Map.prototype.Magnify = Magnify;

function Zoom(val)
{
    if(this.IsInitialized())
	{
        this.mapFrameWindow.zoomUpdate(val);
    }
}
Map.prototype.Zoom = Zoom;

//date = 01032005155614 means 1st March 2005 15:56:14
function SetDate(date)
{
    if(this.IsInitialized())
	{
	    this.mapFrameWindow.SetDate(date);	    	    
	}
    
}
Map.prototype.SetDate = SetDate;

function SetExtent(minx,miny,maxx,maxy)
{
	if(this.IsInitialized())
	{
		this.mapFrameWindow.setExtent(minx,miny,maxx,maxy,true,true);
		this.mapFrameWindow.ClearExistingTiles();
		this.mapFrameWindow.refreshMap(true);
	}
}
Map.prototype.SetExtent = SetExtent;

//Map::Refresh() refresh the this isntant of Map
function Refresh(layers) 
{
	if(this.IsInitialized())
	{
		this.mapFrameWindow.RedrawMapOnly(true,this.VirtualDirectory,layers);
	}
}
Map.prototype.Refresh = Refresh;


function RefreshAllLayers()
{
    this.mapFrameEngine.bMapViewerEngineLoaded = false;
    var postBackFrame = frames['PostBack'];
     
	postBackFrame.document.forms['frmPostBack'].layerToRefresh.value = "All";
	postBackFrame.document.forms['frmPostBack'].submit();
	
}

Map.prototype.RefreshAllLayers=RefreshAllLayers;

function GetAllLayers()
{
	var layerList = ''; //comma seperate list to be returned
	if(this.IsInitialized())
	{
		//if(this.mapFrameEngine.frmMap!= null && this.mapFrameEngine.frmMap.strLayerInfo != null)
		if(this.mapFrameWindow!= null && this.mapFrameWindow.strAllLayers != null)
		{
			layerList = this.mapFrameWindow.strAllLayers;
		}
	}	
	return layerList;
}
Map.prototype.GetAllLayers = GetAllLayers;

//Map::SetState()
//Sets the state of the map as pan, zoomin or zoomout
Map.PAN = 'pan';
Map.ZOOMIN = 'zoomIn';
Map.ZOOMOUT = 'zoomOut';
Map.INFO = 'info';
Map.SELECT = 'select';



function SetState(state,cursorUrl)
{
	if(this.IsInitialized())
	{
		this.mapFrameWindow.setState(state,cursorUrl);
	}	
}

Map.prototype.SetState = SetState;


function GetCurrentMapImageAsSingleTile(mapImg)
{
	if(this.IsInitialized())
	{			    
		var mapFrame = this.mapFrameWindow.window;
		var tileWidth = mapFrame.mapObj.scrollLeft+mapFrame.MapFrameWidth;
		var tileHeight = mapFrame.tileHeight=mapFrame.mapObj.scrollTop+mapFrame.MapFrameHeight;
		
		m = mapFrame.getMouse();
		var TileX = mapFrame.mapObj.scrollLeft;
		var TileY = mapFrame.mapObj.scrollTop;
		var TileW = mapFrame.MapFrameWidth;
		var TileH = mapFrame.MapFrameHeight;
		var rand=Math.random();
		
		//request the image
        var imgQuery =this.VirtualDirectory + "ImageServices/ImageServer.aspx?";
        imgQuery += "ImageRect=rect,"+TileX+","+TileY+","+TileW+","+TileH	+ "&WSize=size,"+ m.Wp + "," + m.Hp+"&rand="+rand;
	    imgQuery += "&DynLabels=false";
	    imgQuery += "&SaveImage=true";

	    var SyncRequest = new WebWizardHTTPRequest("text");
		var result = SyncRequest.SendRequest(imgQuery, 'dummy_contents',false);		
        mapImg.src = this.VirtualDirectory + "images/CopyToClipboardImg.jpg";
	}	
}
Map.prototype.GetCurrentMapImageAsSingleTile = GetCurrentMapImageAsSingleTile;



function SetFeatureInfoListener(listenerfunction)
{
    if(this.IsInitialized())
	{
		this.mapFrameWindow.SelectedFeaturesInfoListener = listenerfunction;
	}
}
Map.prototype.SetFeatureInfoListener = SetFeatureInfoListener

//global function
function parseXMLResult(xmltext)
{
    var doc;
    // code for IE
    if (window.ActiveXObject)
    {
       doc=new ActiveXObject("Microsoft.XMLDOM");
       doc.async="false";
       doc.loadXML(xmltext);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else
    {
       var parser=new DOMParser();
       doc=parser.parseFromString(xmltext,"text/xml");
    }// documentElement always represents the root node
    var x=doc.documentElement;
    if(x.childNodes[0])
    var s = x.childNodes[0].nodeValue;
    return s;
}

function GetLayerFieldValues(region, layerToBeSearched,fieldName, filter)
{
	if(this.IsInitialized())
	{
	    var ArcIMS_ServiceName = region.split("_")[1];
		var SyncRequest = new WebWizardHTTPRequest("text");
		var strParams = "arcIMSServiceName=" + ArcIMS_ServiceName + "&layerToBeSearched=" + layerToBeSearched + "&fieldName=" + fieldName + "&filter=" + filter;
		var result = SyncRequest.SendRequest(this.VirtualDirectory+"LayerInfoService.asmx/GetLayerFieldValues", strParams,false);
		
		var xmlDocument = new ActiveXObject( "Microsoft.XMLDOM" );
		xmlDocument.loadXML(result);
		result = xmlDocument.text;
		return result;
	}
}
Map.prototype.GetLayerFieldValues = GetLayerFieldValues;

function ASynch_GetLayerFieldValues(region, layerToBeSearched, fieldName, filter, responseListener )
{
	if(this.IsInitialized())
	{
	    var ArcIMS_ServiceName = region.split("_")[1]
		var ASyncRequest = new WebWizardHTTPRequest("text");
		var strParams = "arcIMSServiceName=" + ArcIMS_ServiceName + "&layerToBeSearched=" + layerToBeSearched + "&fieldName=" + fieldName + "&filter=" + filter;
		ASyncRequest.ResponseListner = this.ASynch_ResponseListener;
		var result = ASyncRequest.SendRequest(this.VirtualDirectory+"LayerInfoService.asmx/GetLayerFieldValues", strParams,true);
		
		delegateFunction = responseListener;
		return result;
	}
}
Map.prototype.ASynch_GetLayerFieldValues = ASynch_GetLayerFieldValues;


function ASynch_ResponseListener(response,state,status)
{	
	try
	{	
		if(response != null)
		{
		    var result = parseXMLResult(response);
		    delegateFunction(result);
		}
	
	}			
	catch(strError)
	{
	}
}
Map.prototype.ASynch_ResponseListener = ASynch_ResponseListener;


//GoTo
Map.PanTo = 'pan';
Map.ZoomTo = 'zoom';
Map.ZoomToScale = 'ZoomToScale';
function GoTo(fieldName,fieldValue,containinglayer,goFunction,scale,dpi)
{
	if(this.IsInitialized())
	{
		this.mapFrameWindow.showLayer("loadLayer");
		var postBackFrame = frames['PostBack'];
		postBackFrame.document.getElementById('txtSearchString').value='';
		postBackFrame.document.getElementById('SearchFunc').value=goFunction;
		postBackFrame.document.getElementById('SearchQuery1').value= '(' + fieldName + "='" + fieldValue +"')";
		postBackFrame.document.getElementById('SearchQuery2').value='';
		postBackFrame.document.getElementById('SearchLayer').value=containinglayer;
		postBackFrame.document.getElementById('SearchWholeWord').value = false;
		postBackFrame.document.getElementById('SearchCaseSensitive').value = false;
		
		if(scale!=null && scale!="")
		{		    
		    postBackFrame.document.getElementById('dpi').value=dpi;
		    postBackFrame.document.getElementById('ZoomScale').value=scale;
		}
		
		postBackFrame.document.getElementById('frmPostBack').submit();
	}
}
Map.prototype.GoTo = GoTo;

//GoToLatLon
function GoToLonLat(lon,lat,bPan)
{
	if(this.IsInitialized())
	{
		this.mapFrameWindow.showLayer("loadLayer");
		var postBackFrame = frames['PostBack'];
		postBackFrame.document.getElementById('lon').value=lon;		
		postBackFrame.document.getElementById('lat').value=lat;
		
		if(bPan==true)
		{
		    postBackFrame.document.getElementById('SearchFunc').value='panToLonLatWithMarker';
		}
		else
		{
		    postBackFrame.document.getElementById('SearchFunc').value='PlaceMarkerAtLonLat';
		}
					
		postBackFrame.document.getElementById('frmPostBack').submit();
	}
}
Map.prototype.GoToLonLat = GoToLonLat;

function ToggleCoordinates(bShow)
{
	if(this.IsInitialized())
	{
		this.mapFrameWindow.showCoordinatesBar(bShow);
	}
}
Map.prototype.ToggleCoordinates = ToggleCoordinates;

function Move(direction,delta)
{
	if(this.IsInitialized())
	{
		this.mapFrameWindow.MoveMapInDirection(direction,delta);
	}
}
Map.prototype.Move = Move;


function SetProgressStartListener(ProgressStartListener)
{
	if(this.IsInitialized())
	{
		this.mapFrameWindow.ProgressStartListener = ProgressStartListener;
	}
}
Map.prototype.SetProgressStartListener = SetProgressStartListener;

function SetProgressEndListener(ProgressEndListener)
{
	if(this.IsInitialized())
	{
		this.mapFrameWindow.ProgressEndListener = ProgressEndListener;
	}
}
Map.prototype.SetProgressEndListener = SetProgressEndListener;

function SetSessionExpiredListener(SessionExpiredListener)
{
	if(this.IsInitialized())
	{
		this.mapFrameWindow.SessionExpiredListener = SessionExpiredListener;
	}
}
Map.prototype.SetSessionExpiredListener = SetSessionExpiredListener;


function SetZoomFactor(zoomFactor)
{
	if(this.IsInitialized())
	{
		this.mapFrameWindow.zoomFactor = zoomFactor;
	}
}
Map.prototype.SetZoomFactor = SetZoomFactor;

//} end class Map








