jQuery.extend('datamarket.mapselect',(function(){

  var onstate = {};
  
  // standard station icon
  var icon_on  = null;
  var icon_off = null;

  var self = {
    
    config : {
      onImgUrl:  'img/map/mm_20_yellow.png',
      onShdUrl:  'img/map/mm_20_shadow.png',
      offImgUrl: '/site_media/skin/vedurtorg/img/map/mm_20_blue.png',
      offShdUrl: 'img/map/mm_20_shadow.png',
      startPoint: {  // the center of iceland
        x : -18.984375,
        y : 64.97006438589436,
        z : 6
      }
    },
    
    // weather stations
    stations: {},

    makeIcon: function ( image, shadow ) {
      var icon = new GIcon();
      icon.image            = image;
      icon.iconSize         = new GSize( 12, 20 );
      icon.iconAnchor       = new GPoint( 6, 20 );
      icon.infoWindowAnchor = new GPoint( 5, 1 );
      if ( shadow ) {
        icon.shadow         = shadow;
        icon.shadowSize     = new GSize( 22, 20 );
      }
      else {
        icon.shadow         = '';
        icon.shadowSize     = null;
      }
      return icon;
    },

    selectStation: function ( id, _flip /* internal use only */ ) {
      var marker = self.stations[id].marker;
      if ( _flip ) {
        delete onstate[ id ];
        marker.setImage( this.config.offImgUrl );
      }
      else {
        onstate[ id ] = true;
        marker.setImage( this.config.onImgUrl );
      }
    },
    
    deselectStation: function ( id ) {
      self.selectStation( id, true );
    },
        
    stationSelected: function ( id ) {
      return !!onstate[ id ];
    },
    
    getSelectedMarkers: function () {
      var ret = [];
      for ( var id in self.stations ) {
        var st = self.stations[id];
        if ( this.stationSelected( id ) ) {
          ret.push( id );
        }
      }
      return ret;
    },
    
    addMarkers: function ( map ) {
      // batch add stations
      var batch = [];
      var counter = 0;
      for ( var id in self.stations ) {
        var st = self.stations[id];

        if ( !isNaN(st.lat) || !isNaN(st.lon) ) {
        
          var pos = new GLatLng( st.lat, st.lon );  // FIXME: remove negative when we have correct data
          var marker = new GMarker( pos, { 
            icon  : st.startSelected ? icon_on : icon_off,
            title : st.title || ''
            //hide  : !!self.stations[id].startHidden
          });

          self.stations[id].marker = marker;

          if ( st.startSelected ) {
            onstate[ id ] = true;
          }
          //marker.setImage( this.config.onImgUrl );

          marker.v_id = id;
          GEvent.addListener(marker, 'click', function() {
            self.selectStation( this.v_id, self.stationSelected( this.v_id ) );
            jQuery('#map').trigger('markerselect', { id:id, on:!!onstate[ id ] });
            this.redraw();
          });
          
        }
        batch.push( marker );
      }
      
      for (var i=0,l=batch.length; i<l; i++) {
        map.addOverlay( batch[i] );
      }
      
    },


    // Call this function when the page has been loaded
    init: function ( stations, conf ) {
      if ( GBrowserIsCompatible() ) {
        
        this.config = $.extend( this.config, conf );  // todo: move config vars info own object
        
        self.stations = $.extend( self.stations, stations || {} );
        
        icon_on  = self.makeIcon( this.config.onImgUrl,  this.config.onShdUrl );
        icon_off = self.makeIcon( this.config.offImgUrl, this.config.offShdUrl );
        
        var map = new GMap2( jQuery('#map')[0] );
        map.setMapType( G_PHYSICAL_MAP/*, { draggableCursor : "crosshair" }*/ );
        map.setCenter( new GLatLng( this.config.startPoint.y, this.config.startPoint.x ),  this.config.startPoint.z || 6);

        map.addControl( new GSmallMapControl() );
        // map.addControl( new GLargeMapControl() );
        // map.addControl( new GScaleControl() );

        new GKeyboardHandler( map );

        // map.enableScrollWheelZoom();

        GEvent.addListener(map, 'click', function(e) {
          jQuery('#map').trigger('change');
        });

        self.addMarkers( map );
        
        if ( self.plugins ) {
          for ( var id in self.plugins ) {
            var plugin = self.plugins[ id ];
            if ( self.plugins.hasOwnProperty(id) && jQuery.isFunction( plugin.init ) ) {
              plugin.parent = self;
              plugin.init( map, self );
            }
          }
        }
      
      }
      // debugging interface 
      window.GM = map;
    },
    
    plugins: {
      
    }

  };
  return self;

})() );

