(function($) {
  $(document).ready(function() { 
    function UpdateGameClock() {
      var datestring = ZeroPad(GameTime.getDate(), 2) + "." + ZeroPad(GameTime.getMonth() + 1, 2) + "." + GameTime.getFullYear();
      var timestring = ZeroPad(GameTime.getHours(), 2) + ":" + ZeroPad(GameTime.getMinutes(), 2) + ":" + ZeroPad(GameTime.getSeconds(), 2);
      $('#game-clock .game-clock-time').html(datestring + " " + timestring);
      GameTime.setSeconds(GameTime.getSeconds() + 1);
    }
    function ZeroPad(value, count) {
      var zeros = ""; 
      for (i = 0; i < count; i++) {
        zeros += "0";
      } 
      return String(zeros + value).slice(-1 * count);
    }

    /* Menu anchor button(s) */
    var icons = {
      'toolsHomeTrigger'      : { primary: 'ui-icon-home',   secondary: '' },
      'toolsServersTrigger'   : { primary: '',               secondary: 'ui-icon-circle-arrow-s' },
      'toolsLoginTrigger'     : { primary: 'ui-icon-locked', secondary: '' },
      'toolsProfileTrigger'   : { primary: 'ui-icon-person', secondary: 'ui-icon-circle-arrow-s' },
      'toolsRegisterTrigger'  : { primary: 'ui-icon-pencil', secondary: '' },
    };
    $("button,input[type='button'],input[type='submit']").each(function() {
      var disabled = $(this).hasClass('toolsDisabled');
      var hasText = ! $(this).hasClass('toolsLinkNoText');
      if (icons[$(this).attr('id')] == undefined) {
        $(this).button({ disabled: disabled, text: hasText });
      } else {
        $(this).button({ 
          disabled: disabled,
          text: hasText,
          icons: {
            primary  : icons[$(this).attr('id')].primary,
            secondary: icons[$(this).attr('id')].secondary,
          }
        });
      }
    }).show();

    /* Menu drop-down(s) */
    $("#toolsServers,#toolsProfile").popup();
    $("#toolsServers ul, #toolsAvailable ul").menu();
    $("#toolsProfile ul").menu({ 
      select: function(event, ui) { $(this).hide(); } 
    });
    $('#toolsServers').attr('right','0');

    /* Look for changes in the selected tool */
    $('#toolsSelect').change(function() {
      document.location = '/' + this.value;
    });

    /* Update the game clock. */
    UpdateGameClock();
    setInterval(UpdateGameClock, 1000);

    /* Turn back on visibility of objects that were hidden while initializing */
    $('#mainBody').show();
    /* Mobile pages need a little hand holding */
    $(window).trigger('resize');
  });

  $(window).resize(function() { 
    var minWidth = 550, currentWidth = $(window).width();
    $('#toolsTopMenu .ui-button:not(#toolsServersTrigger)').button({ text: currentWidth  > minWidth });
    /*
    $('.left, .right').css('width', currentWidth  <= minWidth ? '20px' : '40px');
    $('.left').css('background-position', currentWidth  <= minWidth ? '-20px 0' : '0 0');
    $('#toolsFooter .left').css('background-position', currentWidth  <= minWidth ? '-20px -20px' : '0 -20px');
    $('#toolsBody').css('padding-left', currentWidth  <= minWidth ? '0' : '25px');
    $('#toolsBody').css('padding-right', currentWidth  <= minWidth ? '0' : '25px');
    $('#toolsMenuArrow').css('display', currentWidth  <= minWidth ? 'none' : 'inline');
    $('#game-clock').css('right', currentWidth  <= minWidth ? '15px' : '50px');
    */
  });
})(jQuery);
