
function WGMap( elementName ){
  this.map = new GMap2( document.getElementById( elementName ), { mapTypes:[G_NORMAL_MAP, G_SATELLITE_MAP] });
  this.map.addControl( new GSmallMapControl() );
  this.map.addControl( new GMapTypeControl() );
  this.map.addControl( new GOverviewMapControl() );
  this.map.enableDoubleClickZoom();
  this.map.enableContinuousZoom();
  this.map.enableScrollWheelZoom(); 
  this.plot = wgPlot;
  this.focusOn = function ( spot ){ this.map.setCenter( spot.position, spot.zoom /*, G_PHYSICAL_MAP G_HYBRID_MAP G_SATELLITE_MAP*/ ); };
}

function wgPlot( spot ){
  var marker = new GMarker( spot.position, spot );
  // info vindu til valgt spot
  GEvent.addListener( marker, "click", function() {
    marker.openInfoWindowHtml( spot.excerpt );
    } );
  this.map.addOverlay( marker );
}

function WGIconFactory(){
  this.icons = { level:[] };
  this.sizes = { 'normal':{ 'icon':new GSize( 20.0, 20.0 ), 'shadow':new GSize( 31.0, 20.0 )},
                 'big':{    'icon':new GSize( 40.0, 40.0 ), 'shadow':new GSize( 61.0, 40.0 )}};
  this.points = { 'normal':{ 'icon':new GPoint( 10.0, 10.0 ), 'info':new GPoint( 10.0, 10.0 )},
                  'big':{    'icon':new GPoint( 20.0, 20.0 ), 'info':new GPoint( 20.0, 20.0 )}};
  this.isValidSize = function ( size ){ return /big|normal/.test( size ); };
  // images
  this.images = { 'level':[ 'http://windsurfguiden.com/wp-content/uploads/2009/08/spot-40x40-transparent.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/spot-40x40-green.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/spot-40x40-blue.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/spot-40x40-red.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/spot-40x40-black.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/spot-40x40-transparent.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/ok.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/smile.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/djevel.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/spot-40x40-transparent.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/sol.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/maane.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/sky.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/regn.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/sno.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/transparent.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/liteN.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/liteNE.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/liteE.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/liteSE.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/liteS.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/liteSW.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/liteW.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/liteNW.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/passeN.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/passeNE.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/passeE.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/passeSE.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/passeS.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/passeSW.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/passeW.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/passeNW.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/myeN.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/myeNE.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/myeE.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/myeSE.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/myeS.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/myeSW.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/myeW.png',
                            'http://windsurfguiden.com/wp-content/uploads/2009/08/vind/myeNW.png'
                            ],
                  'meaning':[ 'Backgrund',
                              ' nybegynnere, ',
                              ' viderekomne, ',
                              ' avanserte, ',
                              ' eksperter',
                              ' 0-5 m/s, ',
                              ' 5-8 m/s, ',
                              ' 8-13 m/s, ',
                              ' over 13 m/s',
                              ,
                              ' lit eller helt klar himmel',
                              ' lit eller helt klar himmel (natt)',
                              ' helt overskyet',
                              ' bare regn',
                              ' kun sno eller sludd',
                              ' Det flys med vinden: ',
                              ,,,,,' 5-8 m/s, ',,,
                              ,,,,,' 8-13 m/s, ',,,
                              ,,,,,' over 13 m/s',,
                            ] };
  this.isValidLevel = function ( level ) { return level > 0 && level < this.images.level.length; };
  this.icon = wgIconForSpot;
  this.createIcon = wgCreateIcon;
  this.legend = wgLegend
}

function wgIconForSpot( spot, size ){
  if(  !this.isValidLevel( spot.level )
    || !this.isValidSize( size )){
    return null;
  }
  if(  this.icons.level[spot.level] == null ){
    this.icons.level[spot.level] = {};
  }
  if( this.icons.level[spot.level][size] == null ){
    this.icons.level[spot.level][size] = this.createIcon( spot.level, size );
  }
  return this.icons.level[spot.level][size];
}

function wgCreateIcon( level, size ){
  if(  !this.isValidLevel( level )
    || !this.isValidSize( size )){
    return null;
  }
  var i = new GIcon( G_DEFAULT_ICON );
  i.image = this.images.level[ level ];
  i.iconSize = this.sizes[size].icon;
	if( level < 16 ){
  	i.shadow = this.images.level[0];
  }
	else {
		i.shadow = this.images.level[15];
	}
  i.shadowSize = this.sizes[size].shadow;
  i.iconAnchor = this.points[size].icon;
  i.infoWindowAnchor = this.points[size].info;
  return i;
}

function wgLegend(){
  document.write( '<b>Symbolforklaring: </b>' ); 
  for( var i = 0; i < arguments.length; i++ ){
		var x = arguments[i];
    document.write( '<img src="' + this.images.level[x] + '" width=20 height=20 style="vertical-align:text-top;"/>' + this.images.meaning[x] );
  }
}

var icf = new WGIconFactory();

function Spot( navn, lat, lon, zoom, excerpt, level, size ){
  this.title = navn;
  this.position = new GLatLng( lat, lon );
  this.zoom = zoom;
  this.excerpt = excerpt;
  this.level = level;
  this.icon = icf.icon( this, size );
  this.show = function() { return navn + ', ' + this.position.show() + ', ' + zoom + ', \'' + excerpt + '\', ' + level + ', ' + this.icon.show(); };
}


