﻿function isInteger(q) {
  return (q.toString().search(/^-?[0-9]+$/) == 0);
}

function isUnsignedInteger(q) {
  return (q.toString().search(/^[0-9]+$/) == 0);
}

function getElement(id) {
	var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? document.layers[id]: null;
	if (el) el.css = (el.style)? el.style: el;
	return el;
}

function getCursorCoords(e) {
   var posX = 0, posY = 0;
   if( !e ) { e = window.event; }
   if( !e ) { return [ 0, 0 ]; }
   if( typeof( e.pageX ) == 'number' ) {
      posX = e.pageX;
      posY = e.pageY;
   }
   else {
      if( typeof( e.clientX ) == 'number' ) {
         posX = e.clientX;
         posY = e.clientY;
         if( document.body && ( document.body.scrollTop || document.body.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) ) {
               posX += document.body.scrollLeft;
               posY += document.body.scrollTop;
         }
         else {
            if( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) ) {
               posX += document.documentElement.scrollLeft;
               posY += document.documentElement.scrollTop;
            }
         }
      }
   }
   return [ posX, posY ];
}

function init(){
  preloadRegionMaps();
  for (nIdx = 0; nIdx < aRegions.length; nIdx++){
    var x = getElement('county_' + nIdx);
    if (x) { x.onmouseover = showTip; x.onmouseout = hideTip;x.onfocus = showTip; x.onblur = hideTip;}
    var x = getElement('countyText_' + nIdx);
    if (x) { x.onmouseover = showTip; x.onmouseout = hideTip;x.onfocus = showTip; x.onblur = hideTip;}
  }
}
var imgArray = new Array(); // moved global to avoid garbage collection //
function preloadRegionMaps(){
  for (nIdx = 0; nIdx < aRegions.length; nIdx++) {
    imgArray[nIdx] = new Image;
    imgArray[nIdx].src = '/images/pictures/maps/map_county_' + aRegions[nIdx][1] + '.gif';
  }
}

function showTip(e) {
  if( !e ) { e = window.event; }
  if( !e ) { return;}  
  var aTmp = this.id.split("_");
  var nID = parseInt(aTmp[1]);
  var bImage = false; // for flagging when the function is called from an image link not text or tab //
  if (aTmp[0] == 'county') {
    bImage = true;
  }
  else {
    bImage = false;
  }
  var nXOffset = -170;
  var nYOffset = -20;
  //var nID = parseInt(this.id.charAt(7));
  aPos = getCursorCoords(e);
  var px = document.layers? 0: "px";
  var x = getElement('mapTipsLayer');
  var z = getElement(this.id);
  if (x && x.css && isInteger(nID)){
    x.innerHTML = '<p>' + aRegions[nID][2] + '<\/p>';
    x.css.position = 'absolute';
    if (aPos[0] && aPos[1] && bImage) {
      x.css.left = aPos[0] + nXOffset + px;
      x.css.top = aPos[1] + nYOffset + px;
    }
    x.css.width = 120 + px;
    x.css.color = '#cd3200';
    x.css.borderLeft = '1px solid #000';
    x.css.borderTop = '1px solid #000';
    x.css.borderRight = '1px solid #000';
    x.css.borderBottom = '1px solid #000';
    x.css.paddingTop = 2 + px;
    x.css.paddingBottom = 2 + px;
    x.css.paddingLeft = 2 + px;
    x.css.paddingRight = 2 + px;
    x.css.backgroundColor = '#fff';
    if (bImage) { x.css.visibility = 'visible'; }
    else { x.css.visibility = 'hidden'; }
    if (z && z.getAttribute('title') && bImage) { z.setAttribute('title',''); }
    if (z && z.css && bImage) { z.css.cursor = 'pointer'; }
    var rgID = aRegions[nID][1];
    var imgRegion = getElement('countryMap');
    imgRegion.src = '/images/pictures/maps/map_county_' + aRegions[nID][1] + '.gif';
  }
}

function hideTip(e) {
  if( !e ) { e = window.event; }
  if( !e ) { return;}
  //var nID = parseInt(this.id.charAt(3));
  var aTmp = this.id.split("_");
  var nID = parseInt(aTmp[1]);
  var px = document.layers? 0: "px";
  var x = getElement('mapTipsLayer');
  var z = getElement(this.id);
  if (x && x.css){
    x.innerHTML = '&nbsp;';
    x.css.visibility = 'hidden';   
    if (z && z.getAttribute('title')) { z.setAttribute('title', aRegions[nID][0]); } 
    if (z && z.css) { z.css.cursor = 'normal'; }
    var imgRegion = getElement('countryMap');
    imgRegion.src = '/images/pictures/maps/map_county_0.gif';
  }
}

window.onload = init;