/**
 * Clase para la aplicacion.
 */ 
var MainApp =
{
  useGMap: false,
  unitModeMetric: null,
  unitModeEnglish: null,

  init: function()
  {
    var self = MainApp;

    $(".linkslide").colorbox({width:"80%", height:"90%", iframe:true});
    $(".linkinfo").colorbox({width:"90%", height:"90%", iframe:true});

    // Cada 1 segundo se ejecuta el onTimer.
    setInterval( MainApp.onTimer, 1000);

    // Detectamos los links de cambio de tipo de temperatura/velocidad

    $('#unitModeMetric').click( self.changeToMetricMode);
    $('#unitModeEnglish').click( self.changeToEnglishMode);

    self.initMapFilters();

    //FIXME: Tratar de que funcione la carga asincronica del GMap!!!
    //self.loadGMapAPI();

    if (self.useGMap)
    {
      GMapConfig.init( self.changeMapFilter, self);
    }
  },

  initMapFilters: function()
  {
    this.allStations = $('#all_stations');
    this.privateStations = $('#private_stations');
    this.skiStations = $('#ski_stations');
    this.weatherStations = $('#weather_stations');
    this.tempStations = $('#temp_stations');
    this.windStations = $('#wind_stations');
    this.snowStations = $('#snow_stations');
    this.preStations = $('#pre_stations');

    this.allStations.click( this.changeMapFilter);
    this.privateStations.click( this.changeMapFilter);
    this.skiStations.click( this.changeMapFilter);

    this.weatherStations.click( this.changeMapFilter);
    this.tempStations.click( this.changeMapFilter);
    this.windStations.click( this.changeMapFilter);
    this.snowStations.click( this.changeMapFilter);
    this.preStations.click( this.changeMapFilter);
  },

  changeMapFilter: function()
  {
    var self = MainApp;
    var type = null;
    var subtype = null;

    if (self.allStations.attr( 'checked'))
    {
      type = 3;
    }
    else if (self.privateStations.attr( 'checked'))
    {
      type = 2;
    }
    else if (self.skiStations.attr( 'checked'))
    {
      type = 1;
    }

    if (self.weatherStations.attr( 'checked'))
    {
      subtype = 'weather';
    }
    else if (self.tempStations.attr( 'checked'))
    {
      subtype = 'temp';
    }
    else if (self.windStations.attr( 'checked'))
    {
      subtype = 'wind';
    }
    else if (self.snowStations.attr( 'checked'))
    {
      subtype = 'snow';
    }
    else if (self.preStations.attr( 'checked'))
    {
      subtype = 'precipitation';
    }

    GMapConfig.showMarkers( type, subtype);
  },

  onTimer: function()
  {
    var utc_time = $('#utc_time');
    var text = Utils.getUTCTime( new Date()) + ' UTC';
    
    utc_time.text( text);
  },

  changeToMetricMode: function()
  {
    location.href = "?unitmode=metric";
  },

  changeToEnglishMode: function()
  {
    location.href = "?unitmode=english";
  }
};

$(document).ready( MainApp.init);
