document.onmousemove = getMouseMove;
document.onkeydown = getKeyDown;
var eventClientX=0;
var eventClientY=0;
var eventShiftKey;
var eventCtrlKey;
var popDivX=0;
var popDivY=0;

var that;

var currentPopupOffsetX = 0;
var currentPopupOffsetY = 0;

var wikiString


//-----------------------------------------------------
//-----------------------------------------------------
function getMouseMove(e)
{
  try
    {
      eventClientX =  e.clientX+document.body.scrollLeft;
      eventClientY =  e.clientY+document.body.scrollTop;
    }
  catch(e)
    {
      eventClientX =  event.clientX+document.body.scrollLeft;
      eventClientY =  event.clientY+document.body.scrollTop;
    }

  popDiv.style.position = "absolute";
  popDiv.style.left = eventClientX + currentPopupOffsetX;
  popDiv.style.top = eventClientY + currentPopupOffsetY;

  dx=(eventClientX-mzjzLineBoxLeft)/4
  dy=(eventClientY-mzjzLineBoxTop)/4
  
 // AdjustLineBoxDisplay();
//along the top  
  for (i=0;i<5;i++)
  {
  eval('cursorDiv_'+i+'0.style.left = mzjzLineBoxLeft+dx*i');
  eval('cursorDiv_'+i+'0.style.top = mzjzLineBoxTop');
  }
// along the bottom  
  for (i=0;i<5;i++)
  {
  eval('cursorDiv_'+i+'4.style.left = mzjzLineBoxLeft+dx*i');
  eval('cursorDiv_'+i+'4.style.top = eventClientY');
  }
//down the far side  
  for (i=1;i<4;i++)
  {
  eval('cursorDiv_4'+i+'.style.left = eventClientX');
  eval('cursorDiv_4'+i+'.style.top = mzjzLineBoxTop+dy*i');
  }
//down the near side  
  for (i=1;i<4;i++)
  {
  eval('cursorDiv_0'+i+'.style.left = mzjzLineBoxLeft');
  eval('cursorDiv_0'+i+'.style.top = mzjzLineBoxTop+dy*i');
  }

}
//-----------------------------------------------------
function getKeyDown(e)
{
   try
    {
      eventShiftKey =  e.shiftKey;
      eventCtrlKey =  e.ctrlKey;
    }
  catch(e)
    {
      eventShiftKey =  event.shiftKey;
      eventCtrlKey =  event.ctrlKey;
    }
// alert('getKeyDown'+eventShiftKey+eventCtrlKey);
  
}
//-----------------------------------------------------
//-----------------------------------------------------
var sortedPtD = new Array();

var gKeyColumnCount = 0;
var gKeyRowCount = 0;

var gCategoryColumnCount = 0;
var gCategoryRowCount = 0;

var NumSelected = 0;
var selected = new Array();

var lastZitHighlighted;
//-----------------------------------------------------
//-----------------------------------------------------
function HighlightZit(SpotID,IsHighlightFromSpot){

  DropZits(SpotID);
  currentSpot = PtD[SpotID];
  
  spotImage = currentSpot["Spot"]
  DotPosition = spotImage.indexOf('.')
  if (DotPosition != -1)   
    spotHighlight = spotImage.slice(0,DotPosition)+'_'+spotImage.slice(DotPosition,spotImage.length)
  else
    spotHighlight = spotImage

  keyDisplaySuppressed = currentSpot["Type"]=="leafMap"  && mzjsLeafMapKeySuppression;

  document.all['img_'+SpotID].src = httpSpotImageLocation + spotHighlight;  
  eval('img_'+SpotID).style.cursor = 'hand';
  
  
    
  if (!keyDisplaySuppressed)
      {
       if (typeof PtD[SpotID]["Dp"] == 'undefined')
           {
           document.all['x'+SpotID].style.color = 'yellow';
           document.all['x'+SpotID].style.background ="#888888";
           }
       else
           {
           dpIndex=PtD[SpotID]["Dp"].slice(2)
    //       alert('dpIndex:'+dpIndex)
    //       alert('dp[dpIndex][0]:'+dp[dpIndex][0])
           for (duplicate in dp[dpIndex])
               {
    //           alert('duplicate:'+duplicate)
    //           alert('dp[dpIndex][duplicate]:'+dp[dpIndex][duplicate])
    //           alert('PtD[dp[dpIndex][duplicate]]["Popup"]:'+PtD[dp[dpIndex][duplicate]]["Popup"])
                document.all['x'+dp[dpIndex][duplicate]].style.color = 'yellow';
                document.all['x'+dp[dpIndex][duplicate]].style.background ="#888888";
               }
           }    
      }


  
  document.all['m'+SpotID].style.visibility='visible';
  document.all['m'+SpotID].style.zIndex=100;
  NumSelected++;
  selected[NumSelected]=SpotID;

  if ((currentSpot.Gp =='yes')&& IsHighlightFromSpot) HighlightClass(currentSpot.Cl);
}
//-----------------------------------------------------
function UnhighlightZit(SelectedID,SpotID)
{
  keyDisplaySpressed = false;
	
  if (SpotID != "x" && SpotID != SelectedID) 
    {
    
    if (typeof PtD[SpotID]["Type"]=='undefined')
        ptType = 'undefined'
    else
        ptType = PtD[SpotID]["Type"]
        
        
    if (ptType =='undefined')
      keyColor='black'
    else
      if (typeof mzjsColorOfType[ptType]=='undefined')
        keyColor='blue'
      else
        keyColor=mzjsColorOfType[ptType]
    
    
    document.images['img_'+SpotID].src = httpSpotImageLocation + PtD[SpotID]["Spot"];
    
    keyDisplaySpressed = (PtD[SpotID]["Type"]=="leafMap")  && mzjsLeafMapKeySuppression;
    
    if ((PtD.length > 0) && !keyDisplaySpressed)
      {
      document.all['x'+SpotID].style.color =keyColor;
      }


  if (!keyDisplaySuppressed)
      {
       if (typeof PtD[SpotID]["Dp"] == 'undefined')
           {
           document.all['x'+SpotID].style.color = keyColor;
           document.all['x'+SpotID].style.background ="#ffffff";
           }
       else
           {
           dpIndex=PtD[SpotID]["Dp"].slice(2)
    //       alert('dpIndex:'+dpIndex)
    //       alert('dp[dpIndex][0]:'+dp[dpIndex][0])
           for (duplicate in dp[dpIndex])
               {
    //           alert('duplicate:'+duplicate)
    //           alert('dp[dpIndex][duplicate]:'+dp[dpIndex][duplicate])
    //           alert('PtD[dp[dpIndex][duplicate]]["Popup"]:'+PtD[dp[dpIndex][duplicate]]["Popup"])
                document.all['x'+dp[dpIndex][duplicate]].style.color = keyColor;
                document.all['x'+dp[dpIndex][duplicate]].style.background ="#ffffff";
               }
           }    
      }
      
      
      
      
    document.all['m'+SpotID].style.zIndex=PtD[SpotID]["zIndex"];
    if (PtD[SpotID]["Spot"].charAt(1) == '_')
      {
      document.all['m'+SpotID].style.visibility = 'hidden'
      }
  return 1
    }
  else
    {
    //Current spot - do not remove it from the selected list.
    return 0
    }
}
//-----------------------------------------------------
function HighlightClass(className)
{
  for (i=0;i<PtD.length;i++)
  {
    currentSpot = PtD[i]
    NotGroup = true; //(currentSpot.Gp != 'yes')
    if (NotGroup && (currentSpot.Cl == className))
    {
      document.all['img_'+ i].src = httpSpotImageLocation + currentSpot["Highlight"];  
      document.all['img_'+ i].style.cursor = 'hand';  
      document.all['x'+ i].style.color = 'red'; 
      document.all['m'+ i].style.zIndex=100;
      document.all['m'+ i].style.visibility='visible';
  
      NumSelected++;
      selected[NumSelected]=i;
    }
  }
}
//-----------------------------------------------------
function DropZits(SelectedID)
{
//  alert("DropZits:NumSelected:"+NumSelected);
  for (i = NumSelected; i > 0; i--) 
  {   
    if (UnhighlightZit(SelectedID,selected[i])==1)
    {
      //Remove this Spot from the selected array
      selected[i]="x";
      NumSelected--;
    }
  }  
}

//-----------------------------------------------------
function dispMap() {

    urlString = location.href
    qPosition = urlString.indexOf("?")
 
//   alert('urlString'+urlString)
    if (qPosition)
        {
    	restOfUrl = urlString.slice(qPosition+1)
//        alert('restOfUrl'+restOfUrl)
    	wikiLocation = restOfUrl.indexOf("wiki=")
    	if(wikiLocation)
    	    {
    	    restOfUrl = restOfUrl.slice(wikiLocation+1)
    	    ampLocation = restOfUrl.indexOf("&")
    	    if (ampLocation)
    	        wikiString = restOfUrl.slice(0,ampLocation)
    	    else
    	        wikiString = restOfUrl    	    
    	    }
//    	    alert('wikiString'+wikiString)
    	}
    else
        wikiString = ''

    
    var limitToKeyDisplay=200;
    var windowWidth = 1024
    
    if (document.body.offsetWidth > 0)
        {
        windowWidth = document.body.offsetWidth
        }
    else  
        if (window.outerWidth != 'undefined')
            {
            windowWidth = window.outerWidth + 11
            }
    if (windowWidth<555)
        {
        limitToKeyDisplay=35;
        }
    else
        {
        numofExtraColumns = Math.floor((windowWidth-555)/150) //div.keyColumn.width in 'styles.css' is 200
//        alert('numofExtraColumns'+numofExtraColumns)
        limitToKeyDisplay = mzjsKeyColumnLength + numofExtraColumns*mzjsKeyColumnLength
        }
    
//    alert('limitToKeyDisplay'+limitToKeyDisplay)
            
        
   txt =   ''
   
   
   txt +=  '<map name="hello99">'
   txt +=  '<area shape="rect" coords="0,0,900,900" style="background:blue" href="">'
   txt +=  '</map>'
   
   
   txt +=  '<div id="blobDiv" style="position:absolute;z-index:1;visibility:hidden;background:blue">&nbsp;</div>'
  
//along the top  
  for (i=0;i<5;i++)
  txt +=  '<div id="cursorDiv_'+i+'0" style="position:absolute;z-index:100;visibility:hidden;">+</div>'
// along the bottom  
  for (i=0;i<5;i++)
  txt +=  '<div id="cursorDiv_'+i+'4" style="position:absolute;z-index:100;visibility:hidden;">+</div>'
//down the far side  
  for (i=1;i<4;i++)
  txt +=  '<div id="cursorDiv_4'+i+'" style="position:absolute;z-index:100;visibility:hidden;">+</div>'
//down the near side  
  for (i=1;i<4;i++)
  txt +=  '<div id="cursorDiv_0'+i+'" style="position:absolute;z-index:100;visibility:hidden;">+</div>'


   
   txt +=  '<div id="xSpotDiv" style="position:absolute; visibility:hidden;">'
   txt +=  '<img src="'+httpSpotImageLocation+'x.gif">'
   txt +=  '</div>'
      
   txt +=  '<div style="position:relative; left:0; top:0; z-Index:0"'
   txt +=  'onMouseDown="mapMouseDown()">'
  
   txt +=  '<img name="ViewerImage" border="0" '
   txt +=  'src="'+ httpViewerImageLocation + 'viewer2.jpg" ' 
   txt +=  'style="position:absolute; left:2px; top:8px;" usemap="#hello99">'
   
   txt +=  '<div class="mapHeading" id="banner1" >'
   txt +=  ''+(mapTheme+MapLabel).replace(' ','&nbsp;')+ '</div>'
   
   txt +=  '<img name="MapImage" border="0" width="350" height="350" '
   txt +=  'src="'+httpMapImageLocation+MapIndexName+'.png" ' 
   txt +=  'style="position:relative;'
   txt +=  'left:'+(mzjsMapCornerLeft+1)+';'
   txt +=  'top:'+(mzjsMapCornerTop+1)+'; z-Index:1">'

   txt +=  '<div class="mapFooter" id="banner2" >'
   try
       {
       txt +=  'Spots shown ' + numofLocatedDataSpots + '...'
       numofHiddenSpots = (numofDataSpots - numofLocatedDataSpots)
       txt +=  'Spots hidden ' + numofHiddenSpots.toString() + '<br/>'
       txt +=  '<br>'
       if (numofHiddenSpots > 0)
           {
           document.getElementById("tooManySpotsToDisplay").style.visibility="visible"
           }
       }
   catch(E)
       {
       }
//       typeX = typeof numofLocatedDataSpots
//       alert('typeof numofLocatedDataSpots:'+typeX)
//       alert('numofLocatedDataSpots:'+numofLocatedDataSpots)
   txt +=  'Click on the map ... for other categories<br>'
   txt +=  'Click on the spots... for more information<br>'
   txt +=  '</div>'

   txt +=  '</div>'



   for (i=0;i<PtD.length;i++)
      {
      txt2 = ''
      txt2 +=  '<div ID="m'+i+'" style="position:absolute; '
      if (PtD[i]["Spot"].charAt(1) == '_')
          {
          txt2 +=  'visibility:hidden; '
          }
          
      xy = PtD[i].xy
      ampPos = xy.indexOf('&')
      x=parseInt(xy.slice(0,ampPos))
      y=parseInt(xy.slice(ampPos+1))

      txt2 +=  'left:' + (x+mzjsMapCornerLeft-mzjsSpotSizeAdjustmentX).toString() + ';'
      txt2 +=  'top: ' + (y+mzjsMapCornerTop-mzjsSpotSizeAdjustmentY).toString() + ';'
      if (PtD[i]['Type']=='leafMap')
        {
        txt2 +=  'z-Index:3;">'
        }
     else
        {
        txt2 +=  'z-Index:2;">'
        }
      txt2 +=  '<img name="img_'+i+'" class="spot" '
      txt2 +=  'src="' + '' +httpSpotImageLocation + PtD[eval(i)]["Spot"] +'" border=0 '
      txt2 +=  'onMouseOver="HighlightZit(\''+i+'\', true);popSummary(\''+i+'\')" '
      txt2 +=  'onMouseOut="killpop()" '
      txt2 +=  'onMouseDown="spotMouseDown(\'' + i + '\')" >'
      txt2 +=  '</div>'
      
      txt= txt + txt2;
      }

   
   txt += '</td></tr>'   
   txt += '</table>'

   document.all['mapDiv'].innerHTML = txt
   
   mapDiv.style.position = "absolute"; 
   mapDiv.style.left = mzjsViewerCornerLeft; 
   mapDiv.style.top = mzjsViewerCornerTop;  
   mapDiv.style.visibility = "visible";
   mapDiv.style.zIndex = 100;
   
   
//set up the right hand key
   keyDisplaySuppressionNoted = false;
   for (i=0;i<PtD.length;i++)
       {
        if (typeof PtD[i]["Type"]=='undefined')
            ptType = 'undefined'
        else
            ptType = PtD[i]["Type"]
        
        
        if (ptType =='undefined')
          keyColor='black'
        else
          if (typeof mzjsColorOfType[ptType]=='undefined')
            keyColor='blue'
          else
            keyColor=mzjsColorOfType[ptType]

//       if (PtD[i]['Type']!='sponsor')
       {
       xxxKeyItem= eval('x'+(i).toString())
       xxxKeyItem.style.visibility = "visible";
       xxxKeyItem.style.textdecoration = "none";
       xxxKeyItem.style.color = keyColor
       xxxKeyItem.style.color = keyColor
       xxxKeyItem.style.fontSize = 10
       xxxKeyItem.style.background = "#ffffff";
       xxxKeyItem.onmouseover = highlightDiv
       xxxKeyItem.onmouseout = unHighlightDiv        
       xxxKeyItem.onmousedown = mousedownDiv  
       }
       if ((numofSpots > limitToKeyDisplay)&&(ptType=='spot'))
           {
            xxxKeyItem.style.visibility = "hidden";
           if (!keyDisplaySuppressionNoted)
               {
               document.getElementById("tooManySpotsForKey").style.visibility="visible"
               keyDisplaySuppressionNoted =true
               }
           }
       }

 }
//-----------------------------------------
 function highlightDiv(e)
   {
   this.style.background = "#888888";

   ii = this.id;
   thisSpot = ii.slice(1,ii.length)
   HighlightZit(thisSpot.valueOf(),true)
   popSummaryFromKey(thisSpot.valueOf())
   }
 function unHighlightDiv(e)
   {
   this.style.background = "#ffffff";

   killpop()
   }
 function mousedownDiv(e)
   {
   ii = this.id;
   thisSpot = ii.slice(1,ii.length)
   KeyMouseDown(thisSpot)
//   popSummaryFromKey(thisSpot.valueOf())
   }
//----------------------------------------------------
//----------------------------------------------------
function killpop()
{
   popDiv.innerHTML = ''; 
   popDiv.style.visibility = "hidden"
   keyPopDiv.innerHTML = ''; 
   keyPopDiv.style.visibility = "hidden"
}

//----------------------------------------------------
//----------------------------------------------------
function getPopUpTable(ZitID)
{
//--alert('getPopUpTable - get the popup on spot mouseover')
  var txt, regx;
     
   txt = ''
   txt += '<table border=0 cellpadding=2 cellspacing=0 bgcolor="green">'
   txt += '<tr><td>'
   txt += '<table width=100% border=0 cellpadding=2 cellspacing=0'
   txt += ' style="font-family:arial, verdana, helvetica;font-size:8pt;'
   txt += ' background-color:white;font-size:8pt;">'
   
   if (typeof PtD[ZitID]["Dp"] == 'undefined')
       {
       txtPopup = PtD[ZitID]["Popup"]
       }
   else
       {
       dpIndex=PtD[ZitID]["Dp"].slice(2)
//       alert('dpIndex:'+dpIndex)
//       alert('dp[dpIndex][0]:'+dp[dpIndex][0])
       txtPopup = ''
       for (duplicate in dp[dpIndex])
           {
//           alert('duplicate:'+duplicate)
//           alert('dp[dpIndex][duplicate]:'+dp[dpIndex][duplicate])
//           alert('PtD[dp[dpIndex][duplicate]]["Popup"]:'+PtD[dp[dpIndex][duplicate]]["Popup"])
            txtPopup += PtD[dp[dpIndex][duplicate]]["Popup"] + '////'
           }
       }    

   
   if (PtD[ZitID]["Type"]=='leafMap')
     txtPopup += '//..click zoom in'
   else if (PtD[ZitID]["Type"]=='parentMap')
     txtPopup += '//..click zoom out'
   else
     txtPopup += '//..click for info'
   
   regx = new RegExp('//','g')
   txtPopup = txtPopup.replace(regx, '<br/>');
   txt+= '<tr><td>' + txtPopup + '</td></tr>';

   txt+= '</table>';
   txt+= '</td></tr>';
   txt+= '</table>';
   
   return txt

}
//----------------------------------------------------
//----------------------------------------------------
function popSummary(ZitID)
{
   txt = getPopUpTable(ZitID)
   
   if (PtD[ZitID]['Type']=='spot')
     {
     currentPopupOffsetX = mzjsSpot_SpotPopupOffsetX;
     currentPopupOffsetY = mzjsSpot_SpotPopupOffsetY;
     }
   else if (PtD[ZitID]['Type']=='sponsor')
     {
     currentPopupOffsetX = mzjsSpot_SponsorPopupOffsetX;
     currentPopupOffsetY = mzjsSpot_SponsorPopupOffsetY;
     }
   else
     {
     currentPopupOffsetX = mzjsSpot_ZoomPopupOffsetX;
     currentPopupOffsetY = mzjsSpot_ZoomPopupOffsetY;
     }

   popDiv.style.position = "absolute";
   popDiv.style.left = eventClientX + currentPopupOffsetX;
   popDiv.style.top = eventClientY + currentPopupOffsetY;  

   document.all['popDiv'].innerHTML = txt
   popDiv.style.visibility = "visible"; 
   popDiv.style.zIndex = 100

//It would be nice to stop the popup going over the key but...
//popDiv.style.width is not changed by 'innerHTML = txt'
//It can be set but is otherwise undefined.
}

//----------------------------------------------------
//----------------------------------------------------

function popSummaryFromKey(ZitID)
{
    txt = getPopUpTable(ZitID)
    
    if (PtD[ZitID]['Type']=='sponsor')
        {
        currentPopupOffsetX = mzjsKey_SponsorPopupOffsetX;
        currentPopupOffsetY = mzjsKey_SponsorPopupOffsetY;
        }
    else
    	{
        currentPopupOffsetX = mzjsKey_SpotPopupOffsetX;
        currentPopupOffsetY = mzjsKey_SpotPopupOffsetY;
    	}
    	
          
    xy = PtD[ZitID].xy
    ampPos = xy.indexOf('&')
    x=parseInt(xy.slice(0,ampPos))
    y=parseInt(xy.slice(ampPos+1))

    keyPopDiv.style.position = "absolute";
    keyPopDiv.style.left =  (x+currentPopupOffsetX).toString();
    keyPopDiv.style.top =  (y+currentPopupOffsetY).toString();
     
    document.all['keyPopDiv'].innerHTML = txt
    keyPopDiv.style.visibility = "visible"; 
    keyPopDiv.style.zIndex = 100
}
//----------------------------------------------------
//----------------------------------------------------

