if ( typeof(window.Map) == "undefined" )
{
  window.Map = {};
}

Map.Create = function( x,y,z,id,ui,maptype ) 
{
    if ( typeof(maptype) == "undefined" ) {
      maptype = google.maps.MapTypeId.TERRAIN;
    }

   var latlng    = new google.maps.LatLng(x,y);

   var options   = {
      zoom: z,
      center: latlng,
      mapTypeId: maptype,//google.maps.MapTypeId.TERRAIN,//HYBRID,
      disableDefaultUI : ui,
      scrollwheel : false
    }

   var self = {
    id      : id,
    z       : z,
    info    : null,
    markers : [],
    infos   : [],
    map     : new google.maps.Map(document.getElementById(id),options)
   };

   var map = {

     getMap : function() {
       return self.map;
     },

     move : function( x,y,z ) 
     {
       if ( typeof(z) == "undefined") 
       {
         z = self.z;
       }
       else
       {
         self.z = z;
       }

       var latlng = new google.maps.LatLng(x,y);
       self.map.setCenter( latlng  );
       self.map.setZoom(z);
    },

    marker : function(x,y,title,cb) {
      var myLatlng = new google.maps.LatLng(x,y);
      var m = new google.maps.Marker( {
          "position" : myLatlng, 
          "map"      : self.map, 
          "title"    : title
        });
      self.markers.push(m);

      if ( cb) {
        google.maps.event.addListener(
          m, 
          'click', 
          function() 
          { 
            cb();
          }
        );
     }

      return m; 
    },

    infoWindow : function( html, url) {
      var info = new google.maps.InfoWindow(
        {
          content: html,
          disableAutoPan :false
        });
      self.infos.push(info);
      return info ;
    },

    infoMarker : function( x,y,title,html,onclick ) {

      var marker = this.marker(x,y,title);
      var info   = this.infoWindow( html, onclick );

      google.maps.event.addListener(
        marker, 
        'click', 
        function() { onclick(); } 
      );

      google.maps.event.addListener(
        marker, 
        'mouseover', 
        function() 
        {   
          if ( self.info ) 
          { 
            self.info.close(); 
          }
          info.open(self.map,marker); 
          self.info = info; 
        }
      );

      google.maps.event.addListener(
        marker, 
        'mouseout', 
        function() { Map.info = null; info.close(); }
      );

      return { "marker" : marker, "info" : info };
    },

    openInfo : function( i )
    {
      if ( self.info )
      {
        self.info.close();
      }

      self.info = self.infos[i];

      if ( typeof(self.info) == "undefined" ) {
        alert(i);
        return;
      }
      self.info.open( self.map, self.markers[i] );
    }
  };

  return map;
};

/*
    var latlng = new google.maps.LatLng(x,y);
    var myOptions = {
      zoom: z,
      center: latlng,
      mapTypeId: maptype,//google.maps.MapTypeId.TERRAIN,//HYBRID,
      disableDefaultUI : ui,
      scrollwheel : false
    };
    Map.id  = id;
    Map.z   = z;
    Map.info= null;
    Map.markers = [];
    Map.infos = [];
    Map.map = new google.maps.Map(document.getElementById(id), myOptions);    
/*
  var stylez = [{
      featureType: "all",
      elementType: "all",
      stylers: [{
        hue:"#884400"//,
        //saturation:-100,
        //lightness:-00
      }]
    }];

  var styledMapOptions = {
      map: Map.map,
      name: "art"
  };

  var jayzMapType = new google.maps.StyledMapType(stylez,styledMapOptions);
  Map.map.mapTypes.set('art', jayzMapType);
  Map.map.setMapTypeId('art');

};

Map.Move = function( x,y,z ) 
{
    if ( typeof(z) == "undefined") 
    {
         z = Map.z;
    }
    else
    {
         Map.z = z;
    }

    var latlng = new google.maps.LatLng(x,y);
    Map.map.setCenter( latlng  );
    Map.map.setZoom(z);
};

Map.Marker = function(x,y,title)
{
    var myLatlng = new google.maps.LatLng(x,y);
    var m = new google.maps.Marker(
     {
      "position" : myLatlng, 
      "map"      : Map.map, 
      "title"    : title
     }
    );
    Map.markers[Map.markers.length] = m;
    return m; 
};

Map.InfoWindow = function( html, url)
{
   var info = new google.maps.InfoWindow(
    {
      content: html,
      disableAutoPan :false
    }
  );
  Map.infos[Map.infos.length] = info;
  return info ;
};

Map.InfoMarker = function( x,y,title,html,onclick )
{

  var marker = Map.Marker(x,y,title);
  var info   = Map.InfoWindow( html, onclick );

  google.maps.event.addListener(
      marker, 
      'click', 
      function() { onclick(); } 
  );

  google.maps.event.addListener(
      marker, 
      'mouseover', 
      function() 
      { 
        if ( Map.info ) 
        { 
          Map.info.close(); 
        }
        info.open(Map.map,marker); 
        Map.info = info; 
      }
  );

  google.maps.event.addListener(
      marker, 
      'mouseout', 
      function() { Map.info = null; info.close(); }
  );

  return { "marker" : marker, "info" : info };
};

Map.openInfo = function( i )
{
  if ( Map.info )
  {
    Map.info.close();
  }

  Map.info = Map.infos[i];

  if ( typeof(Map.info) == "undefined" ) {
    alert(i);
    return;
  }
  Map.info.open( Map.map, Map.markers[i] );
  
};
*/

