var markers = Array();

function gmap_init() 
{
    if (GBrowserIsCompatible()) 
    {
        GoogleMapsControlsInit();
        var map = new GMap2(document.getElementById("map_canvas"));        
        map.setMapType(G_PHYSICAL_MAP);
        map.addControl(new ZoomControl());
        map.addControl(new TL_Corner());
        map.addControl(new BL_Corner());  
        map.addControl(new BR_Corner());  
        map.addControl(new TR_Corner());
             
        return map;
    }
}

function gmap_createMarker(latlng, content, itemid)
{
    var blueIcon = new GIcon(G_DEFAULT_ICON);
    blueIcon.image = "/img/maps/map_point.png";
    
    blueIcon.iconSize = new GSize(32, 42);
    blueIcon.iconAnchor = new GPoint(16, 42);
    blueIcon.infoWindowAnchor = new GPoint(16, 17);      
    blueIcon.shadowSize = new GSize(62, 40);
     
    markerOptions = { icon:blueIcon };
      
    var marker = new GMarker(latlng, markerOptions);   
    markers[itemid] = marker;
    GEvent.addListener(marker,"click", function() 
    {   
        map.openInfoWindowHtml(latlng, content);
    });
    return marker;
}

function gmap_point(itemid)
{
    var selectedPosX = 0;
    var selectedPosY = 0;
    var theElement = document.getElementById('map_canvas');
              
    while(theElement != null)
    {
        selectedPosX += theElement.offsetLeft;
        selectedPosY += theElement.offsetTop;
        theElement = theElement.offsetParent;
    }
    
    var winHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE    
    winHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'    
    winHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible    
    winHeight = document.body.clientHeight;
  }
                            		      
    
    
    $j.scrollTo(selectedPosY - 50, 600); 
    
    //window.scrollTo(selectedPosX ,selectedPosY - (winHeight / 2) + (document.getElementById('map_canvas').offsetHeight / 2));

    GEvent.trigger(markers[itemid], "click");
}




function gmap_article(id)
{
}


function ZoomControl() { }
function TL_Corner () { } 
function TR_Corner () { } 
function BL_Corner () { } 
function BR_Corner () { } 

function GoogleMapsControlsInit()
{

    TL_Corner.prototype = new GControl();
    TL_Corner.prototype.initialize = function(map)     
    {
        var container = document.createElement("div");
        container.style.width = "10px";
        container.style.height = "10px";
        container.style.background = "url(/img/maps/map_corner_top_left.png)";
        
        map.getContainer().appendChild(container);
        return container;
    }
    
    TL_Corner.prototype.getDefaultPosition = function() 
    {
      return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
    }




    TR_Corner.prototype = new GControl();
    TR_Corner.prototype.initialize = function(map)     
    {
        var container = document.createElement("div");
        container.style.width = "10px";
        container.style.height = "10px";
        container.style.background = "url(/img/maps/map_corner_top_right.png)";
        
        map.getContainer().appendChild(container);
        return container;
    }
    
    TR_Corner.prototype.getDefaultPosition = function() 
    {
      return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0, 0));
    }



    BL_Corner.prototype = new GControl();
    BL_Corner.prototype.initialize = function(map)     
    {
        var container = document.createElement("div");
        container.style.width = "10px";
        container.style.height = "10px";
        container.style.background = "url(/img/maps/map_corner_bottom_left.png)";
        
        map.getContainer().appendChild(container);
        return container;
    }
    
    BL_Corner.prototype.getDefaultPosition = function() 
    {
      return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(0, 0));
    }




    BR_Corner.prototype = new GControl();
    BR_Corner.prototype.initialize = function(map)     
    {
        var container = document.createElement("div");
        container.style.width = "156px";
        container.style.height = "32px";
        container.style.background = "url(/img/maps/map_corner_bottom_right.png)";
        container.style.cursor = "pointer";
        
        var caption = document.getElementById('OpenGoogleMapsLinkCaption').value; 
        
        var captionDiv = document.createElement("div");
        captionDiv.appendChild(document.createTextNode(caption));
        
        captionDiv.style.color = "#002A4F";
        captionDiv.style.marginLeft = "38px"; 
        captionDiv.style.marginTop = "8px";
        captionDiv.style.textDecoration = "underline";
        
        container.appendChild(captionDiv);
        
        GEvent.addDomListener(container, "click", function() 
        {                         
            var ll = map.getCenter().toUrlValue();  
            var z = map.getZoom(); 
          
            var url = "http://maps.google.com/maps?ll=" + ll + "&z=" + z + "&t=p"; 
            window.open(url);      
            
           // document.location = url; 
                                
        });
        
        map.getContainer().appendChild(container);
        return container;
    }
    
    BR_Corner.prototype.getDefaultPosition = function() 
    {
      return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(0, 0));
    }


    ZoomControl.prototype = new GControl();

      ZoomControl.prototype.initialize = function(map) {
      var container = document.createElement("div");

      container.style.width = "29px";
      container.style.height = "57px";
      container.style.background = "url(/img/maps/map_plusminus_border.png)"; 
                
      var zoomInDiv = document.createElement("div");
      zoomInDiv.style.cursor = "pointer";
      container.appendChild(zoomInDiv);
      
      var imgIn = document.createElement("img"); 
      imgIn.src = "/img/maps/map_plus.png";  
      zoomInDiv.appendChild(imgIn);
      zoomInDiv.style.marginLeft = "2px";
      zoomInDiv.style.marginTop = "2px";
      
      GEvent.addDomListener(zoomInDiv, "click", function() {
        map.zoomIn();
      });

      var imgOut= document.createElement("img"); 
      imgOut.src = "/img/maps/map_minus.png";
      
      var zoomOutDiv = document.createElement("div");            
      zoomOutDiv.style.cursor = "pointer";
      zoomOutDiv.style.marginLeft = "2px";
       
      container.appendChild(zoomOutDiv);
      zoomOutDiv.appendChild(imgOut);
      GEvent.addDomListener(zoomOutDiv, "click", function() {
        map.zoomOut();
      });

      map.getContainer().appendChild(container);
      return container;
    }

    // By default, the control will appear in the top left corner of the
    // map with 7 pixels of padding.
    ZoomControl.prototype.getDefaultPosition = function() 
    {
      return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(14, 9));
    }
    

}
