var MainApp =
{
  useGMap: false,
  UTC: null,
  YT: null,

  init: function()
  {
    var self = MainApp;

    setInterval( self.showUTCTime, 1000);
    setInterval( self.showUserTime, 1000);

    $("#accordionBox").hackordion({
      defaultid: 0
    });

    // Registramos los links para abrirlos con el ColorBox de jQuery
    $(".linkslide").colorbox({width:"80%", height:"90%", iframe: true});

    var searchBox = $('#search-box');
    
    searchBox.click( function( e){
      searchBox.css( 'color', 'black');
      searchBox.val( '');
      searchBox.unbind( 'click');
    });
    
    self.initMapFilters();

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

  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 type = null;
    var subtype = null;

    if (MainApp.allStations[0].checked)
    {
      type = 3;
    }
    else if (MainApp.privateStations[0].checked)
    {
      type = 2;
    }
    else if (MainApp.skiStations[0].checked)
    {
      type = 1;
    }

    if (MainApp.weatherStations[0].checked)
    {
      subtype = 'weather';
    }
    else if (MainApp.tempStations[0].checked)
    {
      subtype = 'temp';
    }
    else if (MainApp.windStations[0].checked)
    {
      subtype = 'wind';
    }
    else if (MainApp.snowStations[0].checked)
    {
      subtype = 'snow';
    }
    else if (MainApp.preStations[0].checked)
    {
      subtype = 'rain';
    }

    GMapConfig.showMarkers( type, subtype);
  },

  addMClass: function()
  {
    $("#month").addClass( 'validate[required]');
  },

  addDClass: function()
  {
    var month = $("#month");

    if (month.value() != "")
    {
      $("#day").addClass( 'validate[required]');
    }
    else
    {
      $("#day").attr( 'class', '');
      alert( 'Month ==0');
    }
  },

  showUTCTime: function()
  {
    var self = MainApp;

    if (!self.utcTime)
    {
      self.utcTime = $('#utc-content');
    }

    self.utcTime.html( self.getUTCTime( new Date()) + ' ' + MainApp.UTC);
  },

  getUTCTime: function( date)
  {
    var self = MainApp;
    return self.makeTime( date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
  },

  showUserTime: function()
  {
    var self = MainApp;

    if (!self.localTime)
    {
      self.localTime = $('#yt-content');
    }

    var curDateTime = new Date();
    var curHour = curDateTime.getHours();
    var curMin = curDateTime.getMinutes();
    var curSec = curDateTime.getSeconds();

    self.localTime.html( self.makeTime( curHour, curMin, curSec) + ' ' + MainApp.YT);
  },

  makeTime: function( hours, minutes, seconds)
  {
    hours = (hours < 10 ? '0' + hours : hours);
    minutes = (minutes < 10 ? '0' + minutes : minutes);
    seconds = (seconds < 10 ? '0' + seconds : seconds);

    return hours + ':' + minutes + ':' + seconds;
  },

  makeVote: function()
  {
    var val = 0;
    var el = document.form.voto;

    for (var i = 0; i < el.length; i++)
    {
      if (el[i].checked == true)
      {
        val = document.form.voto[i].value;
      }
    }

    divEncuesta = $('#wait_msg');
    divEncuesta.html( '<div class="reload"><img src="images/loading.gif" alt="waiting" />Waiting for vote...</div>');

    $.ajax({
      type: 'get',
      url: 'admin/lib/vote.php?porcessPoll&val=' + val,
      dataType: 'html',
      success: function( text)
      {
        divEncuesta.html( text);
      }
    });
  }
};

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

