
var isIE = (document.all && !window.opera)?true:false;

/*
* Constructor for the popup object .Although first argument is the
* invoking POI.This constructor will be called from the POI constructor.
*/
function MQPopup(ParentContainer)
{
   var popupWidth = "251px";
   this.UId = getguid();

   //create the container div
   var oDiv = document.createElement("div");
   oDiv.id = "popupcontainer" + this.UId;
   oDiv.align='right';
   oDiv.style.position = "absolute";
   oDiv.style.align = "right";
   oDiv.style.textAlign="left";
   oDiv.style.margin = "10px";
   oDiv.style.width = "267px";
   oDiv.style.visibility = "visible";
   oDiv.style.backgroundImage = 'url("images/poi-icon-off.png")';
   oDiv.style.backgroundRepeat = 'repeat';
   var brow = "-NS";
   if (__mqcontainer.browser.isIE) {
      brow = "-IE";
   } else if (__mqcontainer.browser.isNS) {
      brow = "-NS";
   }


   //create the top section
   var oSubDiv = document.createElement("div");
   oSubDiv.id = "popuptop" + this.UId ;
   oSubDiv.className = "mqPopuptop" +brow;
   oSubDiv.style.width = popupWidth;
   oSubDiv.style.height = "14px";
   oSubDiv.style.marginTop = "0px";
   oDiv.appendChild(oSubDiv);


   //create the middle section
   oSubDiv = document.createElement("div");
   oSubDiv.id = "popupmiddle" + this.UId ;
   oSubDiv.className = "mqPopupmiddle" +brow;
   oSubDiv.style.width = popupWidth;
   oSubDiv.style.minHeight = "35px";
   oDiv.appendChild(oSubDiv);

   oSubDiv = document.createElement("div");
   oSubDiv.id = "popupbottom" + this.UId ;
   oSubDiv.className = "mqPopupbottom" +brow;
   oSubDiv.style.width = popupWidth;
   oSubDiv.style.height = "19px";
   oDiv.appendChild(oSubDiv);


   //create the pointer arrow
   oSubDiv = document.createElement("div");
   oSubDiv.id = "popuppointer" + this.UId;
   oSubDiv.className = "mqPopuppointer" +brow;
   oSubDiv.style.position = "absolute";
   oSubDiv.style.top = "14px";
   oSubDiv.style.height = "31px";
   oSubDiv.style.width = "17px";
   oSubDiv.style.marginTop = "0px";
   oDiv.appendChild(oSubDiv);
   oDiv.style.left = "10px";
   oDiv.style.top = "10px";
   oDiv.style.visibility = "hidden";
   oDiv.style.zIndex = 2;
   var parentObj = (ParentContainer)? document.getElementById(ParentContainer) : document.getElementsByTagName("body").item(0);
   try
   {
      parentObj.appendChild(oDiv);
   }
   catch(e)
   {
      alert("An exception occurred in the script. Error name: " + e.name
      + ". Error message: " + e.message);
   }

   this.popupDiv = oDiv;
   this.popupId = "";
   this.timeOutId = null;
   this.getTimeOutId = function(){return this.timeOutId;}
   this.setTimeOutId = function(strTimeOutId)
   {
      if (this.timeOutId)
      {
         clearTimeout(this.timeOutId);
      }
      this.timeOutId = strTimeOutId;
   }

   this.getState = function()
   {
      return (this.popupId);
   }

   /*
   * This is a function to set the popup over x,y position
   * having content as provided in html String.
   */
   this.show = function (strX,strY,strHtmlText)
   {
      if(strHtmlText == undefined || strHtmlText == null)
      return;

      var OdivLeft = parseInt(strX) - 10;
      var OdivTop = parseInt(strY) - 34 ;
      var popupmidDiv;
      var brow = "-NS";
      if (__mqcontainer.browser.isIE) {
         brow = "-IE";
      } else if (__mqcontainer.browser.isNS) {
         brow = "-NS";
      }

      strHtmlText="<div style='margin:10px;text-align:left;'>"+strHtmlText+"</div>";
      getElementById("popupmiddle" + this.UId );
      if (getElementById)
      {
         popupmidDiv = getElementById("popupmiddle" + this.UId );
         popupmidDiv.innerHTML = '';
         popupmidDiv.innerHTML = strHtmlText;
      }
      else if (document.all)
      {
         popupmidDiv = document.all["popupmiddle" + this.UId ];
         popupmidDiv.innerHTML = strHtmlText;
      }
      else if (document.layers)
      {
         popupmidDiv = document.layers["popupmiddle" + this.UId ];
         htmlText2 = '';
         if(strHtmlText != '')
         {
            htmlText2 = '<P>' + strHtmlText + '</P>';
         }
         popupmidDiv.document.open();
         popupmidDiv.document.write(htmlText2);
         popupmidDiv.document.close();
      }

      var closeDiv = document.createElement("div");
      closeDiv.className = "mqPopupclose";
      currentpopup = this;

      closeDiv.onclick = function()
      {
         currentpopup.popupDiv.style.visibility = "hidden";
         currentpopup.popupId = "";
      }

      popupmidDiv.appendChild(closeDiv);

      this.popupDiv.onmouseout = function (evt)
      {
         var relatedObj;
         if (!evt) var evt = window.event;
         if (evt.relatedTarget) relatedObj = evt.relatedTarget;
         else if (evt.toElement) relatedObj = evt.toElement;
         var idStr = (relatedObj)?relatedObj.id : "";
         var parentIdstr = (relatedObj && relatedObj.offsetParent)? relatedObj.offsetParent.id : "";
         if (idStr.indexOf('popup')== -1 && parentIdstr.indexOf('popup')== -1)
         {
            currentpopup.hide(1000);
         }
      }

      this.popupDiv.onmouseover = function (evt)
      {
         currentpopup.setTimeOutId(null);
      }

      this.popupDiv.style.zindex = 2;
      this.popupDiv.style.position = "absolute";
      this.popupDiv.style.left = OdivLeft+ "px";
      this.popupDiv.style.top = OdivTop+ "px";
      this.popupDiv.style.visibility = "visible";
   }


   this.showPopUpOverLocColl = function(strX,strY,strTargetPOIId,MQLocationCollection_locCol,iTolerance,strMapId)
   {

      var size = eval("__mqcontainer.map." +strMapId).MQMap.locations.getSize();
      var transaction = eval("__mqcontainer.map." +strMapId).MQMap.locations.getTransaction();
      var showLink = transaction=="locMap"?false:true;
      var htmlString = "";
      var targetPOI;

      this.popupId = strTargetPOIId;

      var func = eval("__mqcontainer.map." + strMapId).MQMap.onRollover;
      var popupEnabled = eval("__mqcontainer.map." + strMapId).MQMap.rolloverPopups;
      var displayEnabled  = eval("__mqcontainer.map." + strMapId).MQMap.popupContentDisplay;
      for (var i=0; i < size ;i++)
      {
         var currentLocation = MQLocationCollection_locCol.getAt(i);
         if ( currentLocation.poiLayer && strTargetPOIId == currentLocation.poiLayer.id )
         {
         targetPOI=currentLocation;
         break;
         }
      }
      seperatorFlag = false;
      var locCollection = new MQLocationCollection();
      for (var i=0; i < size ;i++)
      {
         var currentLocation = MQLocationCollection_locCol.getAt(i);
         if ( isAcceptable(parseInt(targetPOI.getX()),parseInt(targetPOI.getY()),parseInt(currentLocation.getX()),parseInt(currentLocation.getY()),iTolerance) )
         {
            if(func)
               locCollection.add(currentLocation);
            if (seperatorFlag)
               htmlString = htmlString + "<br/><div><img src='images/seperator.gif' style='position:absolute;left:20px'/> </div> <br/>";
            if(!func)
            {
            htmlString  = htmlString +  currentLocation.htmlStr(showLink);
            }
            seperatorFlag = true;
         }
      }
      if(func)
         htmlString  = eval(func)(strX,strY,locCollection);

      if( popupEnabled ) {
         if( !displayEnabled ) {
            htmlString ="";
         }
         this.show(strX, strY, htmlString);
      }
   }

   this.showOnMouseover = function(strX,strY,strPOIId,MQLocationCollection_locCol ,strMapId)
   {
      var size = eval("__mqcontainer.map." + strMapId).MQMap.locations.getSize();
      var transaction = eval("__mqcontainer.map." +strMapId).MQMap.locations.getTransaction();
      var showLink = transaction=="locMap"?false:true;
      var curLoc = null;
      var htmlStr ="";
      for (var i=0; i < size ;i++)
      {
         curLoc = mq.locations.getAt(i);
         if (curLoc.poiLayer && strPOIId == curLoc.poiLayer.id )
         {
         break;
         }
      }

      this.popupId  = strPOIId;

      var func = eval("__mqcontainer.map." + strMapId).MQMap.onRollover;
      var popupEnabled = eval("__mqcontainer.map." + strMapId).MQMap.rolloverPopups;
      var displayEnabled  =eval("__mqcontainer.map." + strMapId).MQMap.popupContentDisplay;

      if(func)
      {
         htmlStr = eval(func)(strX,strY,curLoc);
      }
      else if(curLoc)
      {
         htmlStr = curLoc.htmlStr(showLink);
      }

      if( popupEnabled ) {
         if( !displayEnabled ) {
            htmlStr ="";
         }
         this.show(strX, strY, htmlStr);
      }
   }

   this.hide = function(nTime)
   {
      var mapId = "";
      if (this.popupId && this.popupId != "")
      mapId= this.popupId.substring(0,this.popupId.indexOf('poi-icon'));
      else
      mapId = this.popupDiv.id;
      var timeoutID = setTimeout("hidePopUp('" + eval("'" + mapId + "'")+"')",nTime);
      this.setTimeOutId(timeoutID);
   }

   return (this);
}


function isAcceptable( mouseX, mouseY, poiX, poiY, tolerance)
{
   var diffX = mouseX - poiX;
   var diffY = mouseY - poiY;
   diffX = (diffX > 0) ? diffX : -diffX;
   diffY = (diffY > 0) ? diffY : -diffY;
   if ( (diffX < tolerance) && (diffY < tolerance) )
      return (true);
   else
      return (false);
}


function hidePopUp(currMapId)
{
   var currentPopup  = null;

   if (! currMapId)
      return ;

   if(eval("__mqcontainer.map." + currMapId))
   {
      currentPopup = eval("__mqcontainer.map." + currMapId).MQMap.popup;
      currentPopup.popupId = "";
      currentPopup.popupDiv.style.visibility="hidden";
      currentPopup.setTimeOutId(null);
   }
   /* in this case currMapId would be the id of the popupcontainer(not associated with any map) */
   else
      document.getElementById(currMapId).style.visibility="hidden";
}


