(function($) {

  window.application = {
    linked_jackpot: 0,

    init: function() {
      application.bindMaximizeButton();
      application.activateExternalLinks();
      application.activateColorBox();
      application.activateLinkedJackpot();
      application.screenSizes();
      application.autoHideBanner();
    },

    windowHeight: function() {
      if(typeof(window.innerHeight) == 'number') {
        return window.innerHeight; 
      } else if( document.documentElement && document.documentElement.clientHeight ) {
        return document.documentElement.clientHeight;
      } else {
        return document.body.clientHeight;
      }
    },

    autoHideBanner: function() {
      if(application.windowHeight() > 820) return false;

      $('#wrapper').mouseenter(function() {
        $('header').animate({ marginTop: '-120px' });
      });
    },

    screenSizes: function() {
      if(application.windowHeight() < 700) {
        $('#content').css({ height: ( application.windowHeight() + 'px' ) });
      }
    },

    bindMaximizeButton: function() {
      $('#maximize').live('click', function(event) {
        event.preventDefault();

        $('#game-interface').toggleClass('maximized');

        return false;
      });
    },

    activateExternalLinks: function() {
      $("a[rel='nofollow external']").attr('target', '_blank');
    },

    activateColorBox: function() {
      $('footer .popup').colorbox({
        iframe: true,
        width:  '800px',
        height: '600px'
      });
    },

    //
    // Linked Jackpots
    //

    activateLinkedJackpot: function() {
      if(platform_id != 1) return false;

      application.getLinkedJackpot();

      setInterval(application.getLinkedJackpot, 20000);
    },

    getLinkedJackpot: function() {
      $.getJSON('http://content.slotpartners.com/platforms/'+platform_id+'/jackpots.json', function(response) {
        var result = String(response['platform_jackpot']['value']);

        application.setLinkedJackpot(result);
      });
    },

    setLinkedJackpot: function(target) {
      var target     = Number(target.replace(/[^0-9]+/ig, '')),
          difference = target - application.linked_jackpot
          factor     = 10;

      if(difference < factor || application.linked_balance > target) {
        application.linked_jackpot = target;
        $('#linked_jackpot span').html(application.currencyFormat(application.linked_jackpot));
      } else if(application.linked_jackpot < target) {
        var velocity = Math.round( difference / factor );

        setTimeout(function() {
          application.linked_jackpot = application.linked_jackpot + velocity;
          $('#linked_jackpot span').html(application.currencyFormat(application.linked_jackpot));
          application.setLinkedJackpot(target.toString());
        }, 50);
      }
    },

    currencyFormat: function(value) {
      var value = ( Number( value + '9' ) / 1000.0 ).toString();

      return ( '&euro;' + value.substring(0,value.length-1) ).replace('.', ',')
    }
  }

  $(application.init);

})(jQuery);
