var ActionChain = {

  add: function(actions) {
    this.actions = $merge(this.actions, actions);
  },

  apply: function(element) {
    element = $(element);
    for(var index in this.actions) {
      var action = this.actions[index];
      index = index.split(':');
      var selector = index[0];
      var method = index.length > 1 ? index[1] : 'domready';
      element.getElements(selector).addEvent(method, action);
    }
  }
};

ActionChain.add({

  '#menu ul': function() {
    var current = this.getElement('a[href=' + this.get('data-current') + ']');
    current = current || this.getElement('a');

    current.addClass('current');
  },

  '#banner': function() {
    var n = (Math.random() * 3).floor();
    new Element('img', { src: '/ls/content/start/images/banner_' + n + '.png' }).inject(this);
  },

  '.clips li:click': function() {
    var playerId = this.getParent('.videos').getElement('.player a').get('id');
    var player = $f(playerId)
    player.stop();

    var file = 'videos/film_' + this.get('data-video-id') + '_highres.flv';

    player.setClip(file);
    player.play();

    this.getParent('ul').getChildren().removeClass('playing');
    this.addClass('playing');
  },

  '#controls li:click': function() {
    $$('#scene > div').addClass('hide');

    var step = $('scene').getElement('.' + this.get('data-show'));
    step.removeClass('hide');

    $$('#controls li').removeClass('active');
    this.addClass('active');

    $$('.clips li').removeClass('playing');

    $$('#controls li').each(function(li, index) {
      if (li != this) {
        $f(index).stop();
      }
      else {
        $f(index).play(0);
      }
    }, this);

    // first
    step.getElement('.clips li').addClass('playing');
  },

  '#scene': function() {
    var search = window.location.search;

    if (search) {
      if (search.contains('step')) {
        var show = search.split('=')[1];

        $$('#controls li').each(function(li, index) {
          if (li.get('data-show') == show) {
            if (show != 'one') {
              (function() {
                li.fireEvent('click');
              }).delay(10, this);
            }
          }
        });
      }
    }
  },

  // LEGACY CONVERTED JQUERY

  '#themecode:focus': function() {
    if (this.get('value') == "Temakod") {
      this.set('value', '');
      this.removeClass("logintextgray");
    }
  },

  '#username, #passwordInfo:focus': function() {
    if (this.get('value') == "Användarnamn") {
      this.set('value', '');
      this.removeClass("logintextgray");
    }
    
    var field = $("passwordInfo");
    field.addClass("passwordHide");
    field = $("password");
    field.removeClass("passwordHide");
    this.focus();
  },

  '.loginBtn:click': function() {
    $("loginForm").submit();
    return false;
  }

});

window.addEvent('domready', function() {
  ActionChain.apply(document);
});

