// 
// This code is used to support the map popups
//

NS4 = (document.layers) ? 1 : 0;
IE4 = (document.all) ? 1 : 0;
W3C = (document.getElementById) ? 1 : 0;

if (navigator.appName == "Microsoft Internet Explorer") IE = 1;
 else IE = 0;

//alert("NS4="+NS4+" IE4="+IE4+" W3C="+W3C+" IE7="+IE7);


function toCamelCase( sInput ) {
    var oStringList = sInput.split('-');
    if(oStringList.length == 1)   
        return oStringList[0];
    var ret = sInput.indexOf("-") == 0 ?
       oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
    for(var i = 1, len = oStringList.length; i < len; i++){
        var s = oStringList[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1)
    }
    return ret;
}


function getStyle(el, style) {
  if(!document.getElementById) return;
  
  var value = el.style[toCamelCase(style)];
  
  if(!value)
    if(document.defaultView)
      value = document.defaultView.
	getComputedStyle(el, "").getPropertyValue(style);
  
    else if(el.currentStyle)
      value = el.currentStyle[toCamelCase(style)];
  
  return value;
}


// Function show(evt, name)
// - evt is a pointer to the Event object passed when the event occurs
// - name is the ID attribute of the element to show


function show ( evt, name ) {
  if (IE4) {
    evt = window.event;  //is it necessary?
  }
  
  var currentX, //mouse position on X axis
    currentY,   //mouse position on X axis
    x,   //layer target position on X axis
    y,   //layer target position on Y axis
    docWidth,    //width of current frame
    docHeight,   //height of current frame
    layerWidth,  //width of popup layer
    layerHeight, //height of popup layer
    ele;     //points to the popup element
  
  // First let's initialize our variables
  if ( W3C ) {
    ele = document.getElementById(name);
    currentX = evt.clientX;
    currentY = evt.clientY;
    docWidth = document.width;
    docHeight = document.height;
    layerWidth = getStyle(ele, "width");
    layerHeight = getStyle(ele,"height");
    //alert("W3C: w="+layerWidth+" h="+layerHeight);
  } else if ( NS4 ) {
    ele = document.layers[name];
    currentX = evt.pageX;
    currentY = evt.pageY;
    docWidth = document.width;
    docHeight = document.height;
    layerWidth = ele.clip.width;
    layerHeight = ele.clip.height;   
  } else {  
    // meant for IE
    ele = document.all[name];
    currentX = evt.clientX;
    currentY = evt.clientY;
    docHeight = document.body.offsetHeight;
    docWidth = document.body.offsetWidth;
    layerWidth = ele.offsetHeigh;
    layerHeight = ele.offsetHeight;
    //alert("IE: w="+layerWidth+" h="+layerHeight);
  }

  // Then we calculate the popup element's new position.
  // Make sure we use parseInt so that strings are converted
  // to integers before doing math operations
  if ( ( currentX + parseInt(layerWidth) ) > docWidth ) {
    x = ( currentX - parseInt(layerWidth) );
  } else {
    x = currentX + 10;
  } 

  if ( ( currentY + parseInt(layerHeight) ) > docHeight ) {
    y = ( docHeight - parseInt(layerHeight) - 23);
  } else {
    y = currentY + 10;
  }
  
  // We now set its position and visibility
  if ( NS4 ) {
    //ele.xpos = parseInt ( x );
    //ele.ypos = parseInt ( y );
    ele.left = parseInt ( x );
    ele.top = parseInt ( y );
    ele.visibility = "show";
  } else if ( IE )  {
    ele.style.left = 10;
    ele.style.top = 100;
    ele.style.visibility = "visible";
  } else {
    // W3C & Mozilla
    ele.style.left = parseInt ( x ) + "px";
    ele.style.top = parseInt ( y ) + "px";
    ele.style.visibility = "visible";
  } 
}

function hide ( name ) {
  if (W3C) {
    document.getElementById(name).style.visibility = "hidden";
  } else if (NS4) {
    document.layers[name].visibility = "hide";
  } else {
    document.all[name].style.visibility = "hidden";
  }
}

