var cfg = {
  animationTime: 1000,
  speedIn: 5000,
  speedOut: 2500,
  minSize: 1,
  maxSize: 6,
  titleHeight: 50,
  titleWidth: 100,
  maxHeight: $(document).height(),
  maxWidth: $(document).width(),
  numberOf: 12
};

var animation = {
  counter: -1,
  list: [],
  init: function() {
    this.create();
    var that = this;
    window.setInterval(function() {
      that.counter = ++that.counter % cfg.numberOf;
      that.play();
    }, cfg.animationTime);
  },
  create: function() {
    var title;
    for (var i = 0; i < cfg.numberOf; i++) {
      title = $('<div class="title' + (i % cfg.maxSize) + '"/>').hide();
      this.list.push(title);
      $('#background').append(title);
    }
  },
  play: function() {
    var size = this.rand(cfg.minSize, cfg.maxSize),
        title = this.list[this.counter];
    title
      .css({
        top: this.rand(0, (cfg.maxHeight - cfg.titleHeight)),
        left: this.rand(0, cfg.maxWidth - (size * cfg.titleWidth)),
        opacity:'0',
        display:'block'
      })
      .animate({ opacity:'1' }, cfg.speedIn)
      .animate({ opacity:'0' }, cfg.speedOut);
    
    window.setTimeout(function() {
      title.hide();
    }, (cfg.speedIn+cfg.speedOut));
  },
  rand: function(min, max) {
    if (min > max) { return 0; }
    if (min == max) { return min; }
    return (min + parseInt(Math.random() * (max - min+1)));
  }
};

function rPosition(e, mx, my) {
  var offset = $(e).offset(),
      x = mx - offset.left,
      y = my - offset.top;
  return {'x': x, 'y': y};
}

$(function() {
  var player = $('#player');
  var playerbar = $('#playerbar');
  var playbutton = $('#playbutton');
  var global_lp = 0;

  playerbar.hide();
  playbutton.hide();
  animation.init();

  /*
   * Button hover adds 'hover'-class
   */
  $('.btn').hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });

  /*
   * Call overlay for footer-buttons
   */
  $('button[rel]').overlay({
    start: { top:20 }     
  });
  
  /*
   * Play/Pause Button
   */
  playbutton
    .click(function() {
      if ($(this).hasClass('active')) {
        player.jPlayer('pause');
      }
      else {
        player.jPlayer('play');
        playerbar.fadeIn(200);
      }
      $(this).hide().toggleClass('active').fadeIn(200);
      return false;
    });
  /*
   * Init player
   */
	player.jPlayer({
      swfPath: 'src/media',
      ready: function () {
        playbutton.fadeIn(200);
        this.element.jPlayer('setFile', 'src/media/LosingFame2009.mp3');
      }
    })
	.jPlayer('onProgressChange', function(lp,ppr,ppa,pt,tt) {
      var lpInt = parseInt(lp, 10);
      var ppaInt = parseInt(ppa, 10);
      global_lp = lpInt;

      var myPlayedTime = new Date(pt);
      var ptMin = ((myPlayedTime.getMinutes() < 10) ? '0' : '') + myPlayedTime.getMinutes();
      var ptSec = ((myPlayedTime.getSeconds() < 10) ? '0' : '') + myPlayedTime.getSeconds();
      $('#play_time').text(ptMin + ':' + ptSec);

      var myTotalTime = new Date(tt);
      var ttMin = ((myTotalTime.getMinutes() < 10) ? '0' : '') + myTotalTime.getMinutes();
      var ttSec = ((myTotalTime.getSeconds() < 10) ? '0' : '') + myTotalTime.getSeconds();
      $('#total_time').text(ttMin + ':' + ttSec); 
      
      $('#player_progress_play_bar').css('width', ppaInt+'%');
      $('#player_progress_load_bar').css('width', lpInt+'%');
    })
	.jPlayer('onSoundComplete', function() {
      $(this).play();
    });

  $('#player_progress_load_bar').live('click', function(e) {
    var pos = rPosition(this, e.pageX, e.pageY);
    player.jPlayer('playHead', (pos.x/3)*(100.0/global_lp));
    playbutton.addClass('active');
    return false;
  });
  
});


