var _mSvgForced = true;
var _mSvgEnabled = true;

function degMinSecToDecimal(degrees, minutes, seconds, northEast)
{
  return ((northEast)?(1):(-1)) * (degrees + ((minutes*60 + seconds)) * (1 / (60*60)));
}

Date.prototype.getTimestamp = function()
{
  return this.getUTCFullYear() +","+ this.getUTCMonth() +","+ this.getUTCDate() +","+ this.getUTCHours() +","+ this.getUTCMinutes();
}

var MVMap =
{
  gmap           : null,
  shadowLayer    : null,
  layerTimestamp : "",
  markerOpt      : null,
  markerMgr      : null,
  markers        : new Array(),
  cnf            : {},

  init : function( config )
  {
    if( !GBrowserIsCompatible() )
    {
      return;
    }
    window.onunload = function(){GUnload()};
    MVMap.GMapExtend();

    MVMap.layerTimestamp = new Date().getTimestamp();

    MVMap.gmap = new GMap2(document.getElementById("map"),{mapTypes:[G_NORMAL_MAP,G_HYBRID_MAP,G_SATELLITE_MAP]});

    MVMap.cnf = config;
    if( typeof(MVMap.cnf) == "boolean" )
    {
      MVMap.cnf = MVMap.cnf?{mapType:true, panZoom:true, mouseZoom:true}:{mapType:false, panZoom:false, mouseZoom:false};
    }
    else if( typeof(MVMap.cnf) == "undefined" )
    {
      MVMap.cnf = {};
    }
    if( typeof(MVMap.cnf.pinOff) == "undefined" ) MVMap.cnf.pinOff = "http://labs.google.com/ridefinder/images/mm_20_red.png";
    if( typeof(MVMap.cnf.pinOn) == "undefined" ) MVMap.cnf.pinOn = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
    if( typeof(MVMap.cnf.pinShad) == "undefined" ) MVMap.cnf.pinShad = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    if( typeof(MVMap.cnf.pinSize) == "undefined" ) MVMap.cnf.pinSize = [12,20] ;
    if( typeof(MVMap.cnf.shadSize) == "undefined" ) MVMap.cnf.shadSize = [22,20];
    if( typeof(MVMap.cnf.minInfo) == "undefined" ) MVMap.cnf.minInfo = false;

    if( typeof(MVMap.cnf.mapType) == "boolean" && MVMap.cnf.mapType ) MVMap.gmap.addControl(new GMapTypeControl());
    if( typeof(MVMap.cnf.panZoom) == "boolean" && MVMap.cnf.panZoom ) MVMap.gmap.addControl(new GLargeMapControl());
    if( typeof(MVMap.cnf.mouseZoom) == "boolean" && MVMap.cnf.mouseZoom ){MVMap.gmap.enableDoubleClickZoom();MVMap.gmap.enableContinuousZoom();MVMap.gmap.enableScrollWheelZoom();}

    MVMap.gmap.setCenter(new GLatLng(20, 0), 1);

    var icon = new GIcon();
    icon.image = MVMap.cnf.pinOff;
    icon.shadow = MVMap.cnf.pinShad;
    icon.iconSize = new GSize( MVMap.cnf.pinSize[0], MVMap.cnf.pinSize[1] );
    icon.shadowSize = new GSize( MVMap.cnf.shadSize[0], MVMap.cnf.shadSize[1] );
    icon.iconAnchor = new GPoint(6, 20);
    icon.infoWindowAnchor = new GPoint(5, 1);
    MVMap.markerOpt = {icon:icon,draggable: false};
    MVMap.markerMgr = new GMarkerManager(MVMap.gmap);

    MVMap.shadowLayer = MVMap.getShadowOverlay();
    MVMap.gmap.addOverlay( MVMap.shadowLayer );
    if( typeof(MVMap.cnf.autoRefresh) == "boolean" && MVMap.cnf.autoRefresh )
    {
      var autoUpdate = function(){ MVMap.refreshLayer(); window.setTimeout(autoUpdate,600000) } //10min
      autoUpdate();
    }
    else
    {
      GEvent.addListener(MVMap.gmap, "moveend", function(){MVMap.refreshLayer()}); // see if shadow needs refreshing
    }
    if( typeof(MVMap.cnf.defZoom) == "number" ){MVMap.gmap.setZoom(MVMap.cnf.defZoom);}

  },

  getShadowOverlay : function()
  {
    var tilelayer = new GTileLayer(new GCopyrightCollection(), 0, 17);
    tilelayer.getTileUrl = function(a,b)
    {
      b=this.maxResolution()-b;
      return "http://maps.google.com/mapsdt?id="+ escape("http://myvector.info/goog/kml.jsp?r="+ (new Date().getTimestamp()) ) +"&x="+a.x+"&y="+a.y+"&zoom="+b;
    }
    tilelayer.isPng = function() {return true;}
    return new GTileLayerOverlay(tilelayer);
  },

  refreshLayer : function()
  {
    if( MVMap.layerTimestamp != new Date().getTimestamp() )
    {
      MVMap.layerTimestamp = new Date().getTimestamp();

      MVMap.gmap.removeOverlay( MVMap.shadowLayer );

      MVMap.shadowLayer = MVMap.getShadowOverlay()
      MVMap.gmap.addOverlay( MVMap.shadowLayer );
    }
  },

  createMarker : function( mapMarker )
  {
    if( MVMap.gmap == null || !GBrowserIsCompatible() )
    {
      return;
    }

    MVMap.markerOpt.title = mapMarker.person
    var marker = new GMarker( new GLatLng(mapMarker.latitude, mapMarker.longitude),  MVMap.markerOpt);
    marker.mvInfo = "";
    if( mapMarker.avatarUrl!="" )
    {
      marker.mvInfo += "<div style=\"float:left; width:80px; height:80px; margin-right:5px;\"><img src=\""+mapMarker.avatarUrl+"\"></div>";
    }
    if( mapMarker.pin!=null )
    {
      MVMap.markerOpt.icon.image = mapMarker.pin;
    }

    if(!MVMap.cnf.minInfo)
    {
      var now = new Date();
      var localDate = new Date( now.valueOf()+(now.getTimezoneOffset()*60*1000)+mapMarker.timeZoneOffset );
      var initPreHl = "<strong class=\"mvbrand\">";
      var initPstHl = "</strong>";
      marker.mvInfo += "<p style=\"float:left;\">";
      if(mapMarker.startDate!= ""){ marker.mvInfo += "<div class=\"mvbrand\">"+mapMarker.startDate+"</div>"; }
      if(mapMarker.person   != ""){ marker.mvInfo += initPreHl+mapMarker.person+initPstHl+"<br>"; initPreHl=""; initPstHl="";  }
      if(mapMarker.label    != ""){ marker.mvInfo += initPreHl+mapMarker.label+initPstHl+"<br>"; initPreHl=""; initPstHl="";}
      if(mapMarker.location != ""){ marker.mvInfo += initPreHl+mapMarker.location+initPstHl+"<br>"; initPreHl="";initPstHl="";}

      marker.mvInfo += "<span class=\"mvbrand\">"+
                       ((mapMarker.city=="")?(""):(mapMarker.city) + ((mapMarker.country!="")?(", "):("<br>")) )+
                       ((mapMarker.country=="")?(""):(mapMarker.country+"<br>"))+
                       "</span>"+
                       "<span class=\"mvmy\">Local time: "+ ((mapMarker.timeZone=="null")?("Unknown"):(localDate.getHours() +":"+ (localDate.getMinutes()<10?"0":"")+localDate.getMinutes() +"</span><br>"))+
                       "</p><div class=\"clear\"></div>";
      GEvent.addListener(marker, "click", marker.showInfo );
    }

    if( typeof(mapMarker.domEventBindings) != "undefined" && mapMarker.domEventBindings != null)
    {
      //GEvent.addDomListener( getElt(mapMarker.domEventBindings.source), "dblclick", function(){marker.zoomInfo()} )
      GEvent.addDomListener( getElt(mapMarker.domEventBindings.source), "mouseover", function(){marker.highlight()} )
      GEvent.addDomListener( getElt(mapMarker.domEventBindings.source), "mouseout",  function(){marker.deHighlight()} )
      GEvent.addDomListener( getElt(mapMarker.domEventBindings.source), mapMarker.domEventBindings.event, function(){marker.showInfo(true)})
    }
    MVMap.markers[MVMap.markers.length] = marker;
    MVMap.gmap.addOverlay(marker);
  },

  clearMarkers : function()
  {
    for( var i = 0; MVMap.markers != null && i < MVMap.markers.length; i++ )
    {
      MVMap.gmap.removeOverlay( MVMap.markers[i]  );
    }
    MVMap.markers = new Array();
  },

  Marker : function( latitude, longitude, person, label, avatarUrl, location, address, city, country, timeZone, timeZoneOffset, domEventBindings )
  {
    this.latitude  = latitude;
    this.longitude = longitude;
    this.person    = person;
    this.avatarUrl = avatarUrl;
    this.location  = location;
    this.label     = label;
    this.address   = address;
    this.city      = city;
    this.country   = country;
    this.timeZone  = timeZone;
    this.timeZoneOffset  = timeZoneOffset;
    this.domEventBindings = domEventBindings;
    this.startDate = "";
    this.pin       = null;

    this.setDate = function( date )
    {
      this.startDate = date;
    };
  },

  DomEventBinding : function( source, event )
  {
    this.source  = source;
    this.event = event;
  },

  GMapExtend : function()
  {
    GMarker.prototype.showInfo = function(zoom)
    {
      this.openInfoWindowHtml( this.mvInfo );
      if(zoom && ! MVMap.gmap.getBounds().contains( this.getPoint() ) ){MVMap.gmap.setZoom(1);}
    }

    GMarker.prototype.zoomInfo = function()
    {
      MVMap.gmap.zoomIn(this.getPoint(),false)
      this.openInfoWindowHtml( this.mvInfo );
    }

    GMarker.prototype.highlight = function()
    {
      if( MVMap.gmap.getBounds().contains( this.getPoint() ) )
      {
        this.setImage(MVMap.cnf.pinOn);
      }
    }
    GMarker.prototype.deHighlight = function()
    {
      this.setImage(MVMap.cnf.pinOff);
    }
  }
}
