/*
 * $Name:  $
 */


/*
 * Browser detection code
 */
 
function Browser ()
{
    var name = navigator.appName;
    if (name == "Netscape")
        this.name = "ns";
    else if (name == "Microsoft Internet Explorer")
        this.name = "ie";

    this.version = navigator.appVersion;
    this.vMajor  = parseInt(this.version);
    this.isNS    = (this.name =="ns" && this.vMajor >= 4);
    this.isNS4   = (this.name =="ns" && this.vMajor == 4);
    this.isNS6   = (this.name =="ns" && this.vMajor == 5);
    this.isIE    = (this.name =="ie" && this.vMajor >= 4);
    this.isIE4   = (this.version.indexOf ('MSIE 4')   >0);
    this.isIE5   = (this.version.indexOf ('MSIE 5')   >0);
    this.isDOM   = (document.createElement
                    && document.appendChild
                    && document.getElementsByTagName) ? true : false;

    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf ("win") > - 1)
        this.platform = "win";
    else if (ua.indexOf("mac") > -1)
        this.platform = "mac";
    else
        this.platform="other";
}
var browser = new Browser;

/*
 * Map control code
 */
function Map (id, level, sessionId)
{
    this.id        = id;
    this.level     = level;
    this.sessionId = sessionId;
    this.element   = document.getElementById (id);
    this.offset_x  = 0;
    this.offset_y  = 0;

    if (this.element) {
        var el = this.element;
        this.url = el.src;
        if (browser.isDOM && !browser.isIE) {
            while (el.offsetParent) {
                this.offset_x += el.offsetLeft;
                this.offset_y += el.offsetTop;
                el = el.offsetParent;
            }
        }
    }

    this.recenter = function (x, y)
    {
        if (this.element) {
            this.element.src = this.url + "&mapbrowse=center&x=" + (x - this.offset_x) + "&y=" + (y - this.offset_y) + "&rand=" + Math.random ();
        }
    };
    this.zoom = function (level)
    {
        if (this.element) {
            this.element.src = this.url + "&mapbrowse=zoom_" + level + "&rand=" + Math.random ();
            this.level = level;
        }
    };

    this.pan = function (dir)
    {
        if (this.element) {
            this.element.src = this.url + "&mapbrowse=pan_" + dir + "&rand=" + Math.random();
        }
    };

    this.recenterzoom = function (x, y)
    {
        if (this.element) {
           this.element.src = this.url + "&mapbrowse=center_zoom&x=" + (x - this.offset_x) + "&y=" + (y - this.offset_y) + "&rand=" + Math.random ();
            if (oMap.level < 10) {
                ++this.level;
                showZoomState ();
                //showVendor ();
            }
        }
    };

    this.identify = function (x, y)
    {
        url = window.location.protocol + "//"
            + window.location.host + window.location.pathname
            + '?template=identify&transaction=locMap&identifyIcon='
            + this.sessionId
            + '&x=' + (x - this.offset_x) + '&y=' + (y - this.offset_y);

        var width  = 255;
        var height = 255;
        infoWindow = window.open (url,
                   Math.floor (Math.random () * 100000),
                   "height=" + height + ",width=" + width + ",toolbar=no,scrollbars=no,resize=yes");
        infoWindow.moveTo ((screen.width - width) / 2, (screen.height - height) / 2);
    };
}

//var map;

function mqaMapClick (e)
{
    var x, y;
    if (browser.isIE) {
        if (!(event.button == 1 || (browser.platform == "mac" && event.button == 0))) return;
        x = event.offsetX;
        y = event.offsetY;
    } else if (browser.isDOM) {
        if (e.which != 1) return;
        x = e.pageX;
        y = e.pageY;
    }
	/*
	mqaMapZoomIn ();
	map.recenter (x, y)
	oMap.recenterzoom (x, y);
	 
    if (document.mapClick.clickAction[0].checked) {
        // Zoom
        mqaMapZoomIn ();
    } else if (document.mapClick.clickAction[1].checked) {
        // Recenter
        map.recenter (x, y)
    } else if (document.mapClick.clickAction[2].checked) {
        // Identify Icon
        if (document.mapClick.clickAction[2].value == 'identify') {
            oMap.identify (x, y);
        } else {
            oMap.recenterzoom (x, y);
        }
    }
    */
}

function mqaMapInit (id, level, sessionId)
{
    var oMap = new Map (id, level, sessionId);
    if (oMap.element) {
        if (browser.isIE) {
            oMap.element.onmouseup = mqaMapClick;
        } else if (browser.isDOM) {
            oMap.element.addEventListener ("mouseup", mqaMapClick, true);
        }
        showZoomState (oMap);
        //showVendor ();
    }
    return oMap;
}

function mqaMapZoom (level, oMap)
{
    if (! oMap) {
        alert ('you must call mqaMapInit(mapurl) before you can call this function');
    } else {
        oMap.zoom (level);
        showZoomState (oMap);
        //showVendor ();
    }
}

function mqaMapZoomIn (oMap)
{	
    if (oMap.level < 10)
        mqaMapZoom (oMap.level + 1, oMap);
}
function mqaMapZoomOut (oMap)
{
    if (oMap.level > 1)
        mqaMapZoom (oMap.level - 1, oMap);
}

function showZoomState (oMap)
{
    for (var i = 1; i <= 10; ++i) {
        var image = i == oMap.level ? 'tick_on.gif' : 'tick.gif';
        var elt = document.getElementById(oMap.id + "_zoom" + i);
        var sImgID = oMap.id + "_zoom" + i;
        eval ("document." + sImgID + ".src='" + imgFolder + image + "'");
        //elt.src = imgFolder + image;
    }
}

function showVendor ()
{
    if (! document.vendor) return;

    if ((oMap.level <= 10) && (oMap.level >= 6))
    {
      var vendImage = 'navteq.gif';
    } else {
      var vendImage = 't.gif';
    }
    eval ("document.vendor.src='images/" + vendImage + "'");
}

function mqaMapPan (dir, oMap)
{
    if (! oMap) {
        alert ('you must call mqaMapInit(mapurl) before you can call this function');
    } else {
        oMap.pan (dir);
    }
}

function getAbsoluteTop(obj)
{
  if (obj.offsetParent)
  {
    return obj.offsetParent.offsetTop + getAbsoluteTop(obj.offsetParent);
  } else
  {
    return obj.offsetTop
  }
}

function getAbsoluteLeft(obj)
{
  if (obj.offsetParent)
  {
    return obj.offsetParent.offsetLeft + getAbsoluteLeft(obj.offsetParent);
  } else
  {
    return obj.offsetLeft
  }
}

function mapMessage(sText)
{
  document.getElementById('mapLoadMessage').style.top = getAbsoluteTop(document.getElementById('mapImageParent')) + 'px'
  document.getElementById('mapLoadMessage').style.left = getAbsoluteLeft(document.getElementById('mapImageParent')) + 'px'
  document.getElementById('mapLoadMessage').style.width = document.getElementById('mapImageParent').offsetWidth;
  document.getElementById('mapLoadMessage').style.height = document.getElementById('mapImageParent').offsetHeight;
  document.getElementById('mapLoadMessageCell').innerHTML = sText;
  document.getElementById('mapLoadMessage').style.visibility = 'visible';		
  document.getElementById('mapImageParent').style.visibility = 'hidden';
}

function displayMap()
{
  document.getElementById('mapLoadMessage').style.visibility = 'hidden';		
  document.getElementById('mapImageParent').style.visibility = 'visible';	
}

var mapTries = 0;

function verifyMap()
{			
  var mapObj;
  
  // Determine Map ID
  if (document.getElementById('homeMap')) mapObj = document.getElementById('homeMap');
  if (document.getElementById('overviewMap')) mapObj = document.getElementById('overviewMap');
	  
  if (mapObj)
  {  	  	
	if ((new Browser).platform != 'mac')
	{

	if (!mapObj.complete)
	{
	  	// Display map not available if it never completes loading.
		if (mapTries < 20) {
			mapTries++;
			mapMessage('Loading map.');			
			setTimeout('verifyMap()', 1000);			
		} else {
			mapMessage('Map not available.');
		}
	} else {					
		displayMap();  
	}
	} else {
		displayMap();
	}
  }
}
