var timeoutId = null;
var timeIntervalId = null;
var m_triggerTime = null; //this would be set from outside
var m_duration = null; //this would be set from outside
var m_maptipsImagePath = null; ///this would be set from outside
var prevX, prevY;

var tempMessage,tempHeader;
var tempStatus=false;
var mouseXX;
var mouseYY;

var tempMaptipHeader;
var tempMessageFontStyle;
var tempFooterFontStyle;
var tempHeaderFontStyle;
var tempFooter;
/*
var locationX;
var locationY;

var isIE = false;
var isNetscape = false;
var isFirefox = false;

function GetBrowserType()
{  	
	var userAgent = navigator.userAgent.toLowerCase();
	if(userAgent.indexOf("ie") != -1)	
		this.isIE = true;
	if(userAgent.indexOf("netscape") != -1)	    				
		this.isNetscape = true;
	if(userAgent.indexOf("firefox") != -1)  				
		this.isFirefox = true;
} */   	

function getmouseXXY(e) 
{    
    getXY(e);
    
	if(!e)
		e=window.event;
	
	if(e)
	{			
		if(e.pageX)
		{			  		    
			mouseXX = e.pageX;
			mouseYY = e.pageY;
		}
		else if (e.x)
		{		    
		   // if(isIE)
		    {
		        mouseXX = e.clientX;
			    mouseYY = e.clientY;
		    }/*
		    else
		    {
			    mouseXX = e.x;
			    mouseYY = e.y;
			}*/
		}
	}
}

function ShowMapTips()
{
    var divElem = document.getElementById("mapDiv");
    if(divElem  != null)
    {   
        divElem.onmouseover = eval("divOnMouseOver");        
        divElem.onmousemove = eval("divOnMouseMove");                        
        divElem.onmouseleave = eval("divOnMouseLeave");        
        divElem.onmousedown = eval("divOnMouseDown");  
    }
}

function divOnMouseDown(e)
{
    if(isLeftMouseButton(e))
    {
        //check whether left mouse is clicked
        //clear the timers && raise timeout Trigger event         
        ClearTimeInterval();
        ClearTimeOut();
        raiseEvent();
    }
}

function divOnMouseOver(e)
{         
    getmouseXXY(e);        
}

function divOnMouseMove(e)
{ 
    getmouseXXY(e);
    //window.status = "prevX : " + prevX + "! mouseXX:" +  mouseXX + " |prevY :" + prevY + "! mouseYY: "+ mouseYY;
    if(timeIntervalId  == null && (prevX != mouseXX || prevY != mouseYY))
    {
        SetTimeInterval();
        rasieMouseMoved();
        //window.status = window.status + " MOUSE MOVED";
    }
    
}

function divOnMouseLeave(e)
{
    ClearTimeInterval();
    ClearTimeOut();    
}

function ClearTimeInterval()
{        
    //clear the time intervals    
    if(timeIntervalId != null)
    {        
        clearInterval(timeIntervalId);        
    }
    //reset the ids    
    timeIntervalId = null
}

function ClearTimeOut()
{    
    //clear the time intervals
    if(timeoutId != null)
    {
        clearTimeout(timeoutId);
    }    
    //reset the ids
    timeoutId = null;
}

function SetTimeInterval()
{    
    timeIntervalId  = window.setInterval('hasMouseStopped()',1000);    
}

function hasMouseStopped()
{        
    if(prevX == mouseXX && prevY == mouseYY)
    {                   
        ClearTimeInterval();        
        SetTimeOut();       
    }  
    prevX = mouseXX;
    prevY = mouseYY;  
}

function SetTimeOut()
{ 
    timeoutId = window.setTimeout("raiseEvent()", m_triggerTime);
}

function raiseEvent()
{
    prevX = mouseXX;
    prevY = mouseYY; 
    timeoutId = null;
    ShowMapTipDialog();
}

function rasieMouseMoved()
{           
    //window.status = window.status  + "MOUSE MOVED";
    return;
    HidePopupDialog();
}

function HidePopupDialog()
{
    if(popupDialog  != null)
    {
        popupDialog.Hide();
    }    
}

function getServerProtocol()
{
    var protocol = window.location.protocol;
    var theHost = window.location.hostname;
    var port = window.location.port;
    
    if (port)
    {
        port=":"+ port;
    }
    
    var path = window.location.pathname;
    
    path = path.substring(0, path.indexOf("/", 1));
    
    var newUrl = protocol + "//" + theHost + port ;

    return newUrl;	
}

var popupDialog = null;
/*
    This method displays MapTip dialog box.
*/
function ShowMapTipDialog()
{            
    if(popupDialog == null)
    {
        popupDialog = new JSBalloon();    
        popupDialog.imagePath = m_maptipsImagePath;        
        JSBalloonPath = getServerProtocol() + m_maptipsImagePath;          
        //alert(JSBalloonPath );
    }
    
    popupDialog.autoHide=true;
	popupDialog.transShow=false;
	popupDialog.autoAway=false;
	popupDialog.showCloseBox=true;
    popupDialog.autoHideInterval = m_duration;
        
    getmouseXXY();
    //alert("mouseYY:" + mouseYY + "-mouseXX:" + mouseXX);
    var top =  mouseYY;
    var left = mouseXX;
        
    var mapTipObj = eval("maptipObject");    
    
    if(mapTipObj  != null)
    {     
        mapTipObj.GetInfo(mouseX, mouseY); 
         
         //alert( mapTipObj.Message + " - " +  mapTipObj.Header + " - " + mapTipObj.Footer);
            tempMaptipHeader=  mapTipObj.Heading;            
                  
            if(mapTipObj.MessageFontStyle != null && mapTipObj.MessageFontStyle != undefined)
            {
                tempMessageFontStyle=mapTipObj.MessageFontStyle;
            }
            if(mapTipObj.FooterFontStyle != null && mapTipObj.FooterFontStyle != undefined)
            {            
	            tempFooterFontStyle=mapTipObj.FooterFontStyle;
	        }
	        if(mapTipObj.HeaderFontStyle != null && mapTipObj.HeaderFontStyle != undefined)
            {
	            tempHeaderFontStyle=mapTipObj.HeaderFontStyle;
	        }
	
            //popupDialog.footer = (mapTipObj.footer == undefined ? '<a href=#>more info</a>' : mapTipObj.footer);
            tempFooter = (mapTipObj.footer == undefined ? '' : mapTipObj.footer);
            //alert(popupDialog.footer);
                
        
    }    
}
function ShowResultTipDialog(searchId,featureId)
{ 
         
    if(popupDialog == null)
    {
        popupDialog = new JSBalloon();    
        popupDialog.imagePath = m_maptipsImagePath;        
        JSBalloonPath = getServerProtocol() + m_maptipsImagePath;          
    }
     
    
    popupDialog.autoHide=true;
	popupDialog.transShow=false;
	popupDialog.autoAway=false;
	popupDialog.showCloseBox=true;
    popupDialog.autoHideInterval = m_duration;
        
    getmouseXXY();
    
    var top =  mouseYY;
    var left = mouseXX;
  
        
    var mapTipObj = eval("resulttipObject");    
    
    if(mapTipObj  != null)
    {     
            mapTipObj.GetResultTip(searchId,featureId);
                    
            //alert( mapTipObj.Message + " - " +  mapTipObj.Header + " - " + mapTipObj.Footer);
          
            tempMaptipHeader=  mapTipObj.Heading;            
                  
            if(mapTipObj.MessageFontStyle != null && mapTipObj.MessageFontStyle != undefined)
            {
                tempMessageFontStyle=mapTipObj.MessageFontStyle;
            }
            if(mapTipObj.FooterFontStyle != null && mapTipObj.FooterFontStyle != undefined)
            {            
	            tempFooterFontStyle=mapTipObj.FooterFontStyle;
	        }
	        if(mapTipObj.HeaderFontStyle != null && mapTipObj.HeaderFontStyle != undefined)
            {
	            tempHeaderFontStyle=mapTipObj.HeaderFontStyle;
	        }
	
            //popupDialog.footer = (mapTipObj.footer == undefined ? '<a href=#>more info</a>' : mapTipObj.footer);
            tempFooter = (mapTipObj.footer == undefined ? '' : mapTipObj.footer);
            //alert(popupDialog.footer);
                
        
    }
         
}
/*
    This method is invoked when Maptips tool button is clicked.
*/
function DisplayMapTips(pnlElemId)
{   
   
   
    if(window.ShowLayer)
    {
        ShowLayer(pnlElemId, null, null);
    }
    else
    {
        if(pnlElemId != null)
        {
            var pnlElem = document.getElementById(pnlElemId);        
            if(pnlElem != null)
            {
                pnlElem.style.display = "block"; 
                  xZIndex(pnlElem, highZ++);             
            }
        }
    }
    
    SetDrawMode(DrawMode.MAPTIP);
}

function SetClientSideValues()
{
    if(m_triggerTime == null)
    {
        var mapTipObj = eval("maptipObject");    
        if(mapTipObj  != null)
        {
            m_triggerTime = parseInt(mapTipObj.TriggerTime) * 1000; //convert to milliseconds  1 sec = 1000 milliseconds
            m_duration = parseInt(mapTipObj.DisplayDuration) * 1000; //convert to milli seconds 
            m_maptipsImagePath = mapTipObj.ImagePath;
        }
    }
}
/*
refreshes mapTip layers based on the xml document received after all XML HTTP calls made from the client
---Sample of XML which the method parses is 
<MAPTIPLAYERS>
    <LAYER id="" name="" />
    <LAYER id="" name="" />
    <LAYER id="" name="" />
    <LAYER id="" name="" />
</MAPTIPLAYERS>
*/
function RefreshMapTipLayers(xmlDoc)
{                   
    //get the MapTipLayer node
    var mapTipNode = xmlDoc.getElementsByTagName("MAPTIPLAYERS");    
    var currentMapTipLayer = null;    
    //fetch the current maptip layer
    if(mapTipNode && mapTipNode.length > 0)    
    {                
        currentMapTipLayer = getXMLAttributeValue(mapTipNode.item(0), "currentlayer");        
    }
    
    //get all the maptip layers and populate in the dropdown list.
    var mapTipNodeList = xmlDoc.getElementsByTagName("LAYER");    
    
    if(mapTipNodeList != null && mapTipNodeList.length > 0)
	{	    	    
	    var optionsList = new Array();
	    
        for( i=0; i<mapTipNodeList.length; i++)
		{		    
			var layerId = getXMLAttributeValue(mapTipNodeList.item(i),"id");
			var name= getXMLAttributeValue(mapTipNodeList.item(i),"name");
			var option = createOption(name, layerId);
			//set the current maptip layer as selected.
			if(currentMapTipLayer != null && layerId != null && layerId == currentMapTipLayer)
			{
			    option.selected = true;
			}
		
			if(option != null)
			{
                optionsList.push(option);
            }
		}		
		//add maptips to the dropdown.
		AddMapTipDropDownItems(optionsList);	    
	}
	else
	{
	    ClearMapTipDropdownItems();
	}
}

/*
    Clears the items in the maptip object and adds passed option items to it.
*/
function ClearMapTipDropdownItems()
{
    var mapTipObj = eval("maptipObject");
    if(mapTipObj != null)
    {
        var drpElem = document.getElementById(mapTipObj.LayerElemId);    
        if(drpElem != null)
        {        
            var count = drpElem.options.length;
            
            for(i=count-1; i>=0; i--)
            {            
                drpElem.options[i] = null;
            }
            var option = createOption(mapTipObj.NoMapTipLayersMsg, "-1");
            //create blank maptips option
            drpElem.options.add(option);
             
                         //Refresh the dropdown
            RefreshDropdownControl(mapTipObj.LayerElemId);
        }
    }
}
/*
    Returns the attribute value in the given node.
*/
function getXMLAttributeValue(node,attributeName)
{    
	if(node && node.attributes)
	{	    
		attrNode=node.attributes.getNamedItem(attributeName);
		if(attrNode)
			return attrNode.nodeValue;
	}
}
/*
    Create an option element and sets passed value and text to it.
*/
function createOption(itemText, itemValue)
{
    var option = document.createElement("OPTION");
    if(option != null)
    {
        option.text = itemText;
        option.value = itemValue;        
    }
    return option;
}
/*
    Clears the items in the maptip object and adds passed option items to it.
*/
function AddMapTipDropDownItems(items)
{
    var mapTipObj = eval("maptipObject");
    if(mapTipObj != null)
    {
        var drpElem = document.getElementById(mapTipObj.LayerElemId);    
        if(drpElem != null)
        {        
            var count = drpElem.options.length;
            
            for(i=count-1; i>=0; i--)
            {            
                drpElem.options[i] = null;
            }
            
            for(i=0; i<items.length; i++)
            {
                if(items[i] != null)
                {
                    drpElem.options.add(items[i]);
                }
            }
                        //Refresh the dropdown
            RefreshDropdownControl(mapTipObj.LayerElemId);        
        }
    }
}
/*
    Hides maptip dialog box.
*/
function HideMapTips(divElemId)
{
    //This removed because HideLayer function adds a smartnavigation attribute to it and will not work when repositioning for RTL.
    /*if(window.HideLayer)
    {
        HideLayer(divElemId, null, null);
    }
    else
    {        
       } 
      */ 
       var drpElem = document.getElementById(divElemId);
        if(drpElem != null)
        {
            //drpElem.style.visibility = "hidden";
            drpElem.style.display = "none";
        }
    
    //Reset the mapcontrol draw mode.
    SetDrawMode(DrawMode.NONE);
    //clear the timeout.
    clearTimeout(timeoutId);
}
/*
    Client event which is raised when the maptip layer is changed. Makes an AJAX call and updates the Current MapTip layer.
*/
function MaptipLayer_OnChange(drpElem)
{
    var mapTipObj = eval("maptipObject");
    if(mapTipObj != null)
    {
        var mapTipObj = PageMethods;
        if(!sessionExpired)
         { 
            if(drpElem != null && mapTipObj  != null)
            {   
                    //set the current maptip layer                          
                    mapTipObj.SetCurrentMapTipLayer(drpElem.value); 
            }
        }
        else
        {
            HandleSessionExpired();
        }	
	}
}
/*
    DisplayInfo
*/

function DisplayInfo(locationX, locationY, layerId)
{
    //alert(locationX + " , " + locationY);    
    if(window.ShowInfoWindow)
    {
        ShowInfoWindow(locationX, locationY, layerId);
    }
}

/*
    Client MapTip object.
*/
function MapTip(layerDropDownId, maptipTriggerTime, maptipDisplayDuration, imagePath, msgNoMapTipLayer)
{
    this.LayerElemId = layerDropDownId;
    this.TriggerTime = maptipTriggerTime;
    this.DisplayDuration = maptipDisplayDuration;
    this.NoMapTipLayersMsg = msgNoMapTipLayer;
    this.ImagePath = imagePath;
    
    this.GetInfo = getInfo;
    
    this.GetResultTip = getResultTip;
    
    this.Heading;
    this.Message;
    this.HeaderFontStyle;    
    this.MessageFontStyle;
    this.FooterFontStyle;
    
    function getInfo(x, y)
    {
        var status = false;
        this.Heading = null;
        this.Message = null;
        
        var layerId;
        var drpElem = document.getElementById(this.LayerElemId);
        if(!sessionExpired)
        {
            if(drpElem != null)
            {   
                layerId = drpElem.value;        
                if(layerId != null && layerId.length > 0)
                {
                    this.Heading = getOptionText(drpElem, drpElem.selectedIndex);
                    PageMethods.GetMapTipHtml(layerId, parseInt(x), parseInt(y),HandleGetInfo);                
                }
            }
        }
        else
	    {
	        HandleSessionExpired();
	    }	
        return status;
    }    
    
     function getResultTip(searchId,featureId)
    {
        var status = false;
        this.Heading = null;
        this.Message = null;
        
        if(!sessionExpired)
        {
             PageMethods.GetResultTipHtml(searchId,featureId,HandleGetResultTip);                
        }
        else
	    {
	        HandleSessionExpired();
	    }	
        return status;
    }
    
    function HandleGetInfo(postData)
    {
         if(postData != null )
        {
            tempMessage = postData.ContentText;
            tempHeader = postData.HeaderText
            tempStatus= true;
        }
        if(tempStatus)        
        {
            popupDialog.title =  tempMaptipHeader;            
            popupDialog.message = tempMessage;            
            popupDialog.header = tempHeader;
            popupDialog.CloseButtonFunction = "HidePopupDialog()";
            
            if(tempMessageFontStyle != null && tempMessageFontStyle != undefined)
            {
                popupDialog.messageFontStyle=tempMessageFontStyle;
            }
            if(tempFooterFontStyle != null && tempFooterFontStyle != undefined)
            {            
	            popupDialog.footerFontStyle=tempFooterFontStyle;
	        }
	        if(tempHeaderFontStyle != null && tempHeaderFontStyle != undefined)
            {
	            popupDialog.headerFontStyle=tempHeaderFontStyle;
	        }
	
            popupDialog.footer   = (tempFooter == undefined ? '' : tempFooter);
            popupDialog.icon = 'Info';
            popupDialog.top = parseInt(mouseYY);
            popupDialog.left = parseInt(mouseXX);
            popupDialog.Show();           
        }
    }   
    
    function HandleGetResultTip(postData)
    {
         if(postData != null )
        {
            tempMessage = postData.ResultTipContentText;
            tempHeader = postData.ResultTipHeaderText;
            tempFooter = postData.ResultTipFooterText;
            tempStatus= true;
        }
        if(tempStatus)        
        {
        
            popupDialog.title =  tempHeader;            
            popupDialog.message = tempMessage;            
            popupDialog.header = '';
           
            popupDialog.CloseButtonFunction = "HidePopupDialog()";
            
            if(tempMessageFontStyle != null && tempMessageFontStyle != undefined)
            {
                popupDialog.messageFontStyle=tempMessageFontStyle;
            }
            if(tempFooterFontStyle != null && tempFooterFontStyle != undefined)
            {            
	            popupDialog.footerFontStyle=tempFooterFontStyle;
	        }
	        if(tempHeaderFontStyle != null && tempHeaderFontStyle != undefined)
            {
	            popupDialog.headerFontStyle=tempHeaderFontStyle;
	        }
	
            popupDialog.footer   = (tempFooter == undefined ? '' : tempFooter);
            popupDialog.icon = 'Info';
            popupDialog.top = parseInt(mouseYY);
            popupDialog.left = parseInt(mouseXX);
            popupDialog.Show();           
        }
    }   
     	
    function getOptionText(drpElem, selectedIndex)
    {
        var optionText = null;
        if(drpElem != null && selectedIndex != null)
        {
            var option = drpElem.options[selectedIndex];
            if(option != null)
            {
                optionText = option.text;
            }
        }
        
        return optionText;
    }
}

function ImageClose_Click()
{
}
//get the browser type.
//GetBrowserType();