/*
 * 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;
            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 (map.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 (e.which != 1) return;
    x = e.clientX;
    y = e.clientY;

    var agt = navigator.userAgent.toLowerCase();
    if (agt.indexOf('safari') == -1) {
        if (document.body && (document.body.scrollTop || document.body.scrollLeft)) {
            y += document.body.scrollTop;
            x += document.body.scrollLeft;
        } else if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) {
            y += document.documentElement.scrollTop;
            x += document.documentElement.scrollLeft;
        }
    }

   if(mq_ParamExists(document.mapClick)) {
      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) {
          if (document.mapClick.clickAction[2].value == 'identify') {
              // Identify Icon
              map.identify (x, y);
          } else {
              // Recenter and Zoom
              map.recenterzoom (x, y);
          }
      }
   } else {
      map.identify (x, y);
   }
}

function mqaMapInit (id, level, sessionId)
{
    map = new Map (id, level, sessionId);
    if (map.element) {
        add_listener (map.element, 'mouseup', mqaMapClick);
        showZoomState ();
        showVendor ();

        var dir= new Array ('nw','n','ne','e','se','s','sw','w');
        for (i=0; i<dir.length; i++) {
            add_listener (document.getElementById (dir[i]), 'mouseover', pan_over);
            add_listener (document.getElementById (dir[i]), 'mouseout', pan_out);
        }
    }
}

function mqaMapZoom (level)
{
    if (! map) {
        alert ('you must call mqaMapInit(mapurl) before you can call this function');
    } else {
         map.zoom (level);
         showZoomState ();
         showVendor ();
    }
}

function mqaMapZoomIn ()
{
    if (map.level < 10)
        mqaMapZoom (parseInt(map.level) + 1);
}
function mqaMapZoomOut ()
{
    if (map.level > 1)
        mqaMapZoom (parseInt(map.level) - 1);
}

function showZoomState ()
{
   for (var i = 1; i <= 10; ++i) {
       if (map.level == i) {
           document.getElementById ('zoom' + i).className = 'selected';
       } else {
           document.getElementById ('zoom' + i).className = 'zoom';
       }
   }
}

function showVendor ()
{
    if (! document.getElementById ('vendor')) return;

    if ((map.level <= 10) && (map.level >= 6))
    {
      var vendImage = 'navteq.gif';
    } else {
      var vendImage = 'spacer.gif';
    }
    document.getElementById ('vendor').src = '/images/general/' + vendImage;
}

function mqaMapPan (dir)
{
    if (! map) {
        alert ('you must call mqaMapInit(mapurl) before you can call this function');
    } else {
        map.pan (dir);
    }
}

/* changing styles of map directional panels */
function pan_out (e)
{
    pan_swap (e.currentTarget.id, 0);
}
function pan_over (e)
{
    pan_swap (e.currentTarget.id, 1);
}
function pan_swap (id, isOver)
{
    switch(id) {
        case "nw":
        case "ne":
        case "se":
        case "sw":
            var panside = new Array(id);
            break;
        case "e":
            var panside = new Array ('ne', 'e', 'se');
            break;
        case "w":
            var panside = new Array('nw', 'w', 'sw');
            break;
        case "s":
            var panside = new Array('sw', 's', 'se');
            break;
        case "n":
            var panside = new Array('nw', 'n', 'ne');
            break;
    }
    for (i = 0; i < panside.length; i++) {// roll through and change state
        if (isOver == 1) {
            document.getElementById (panside[i]).className = 'selected';
        } else {
            document.getElementById (panside[i]).className = '';
        }
    }
}
