/* vim: set autoindent shiftwidth=2 tabstop=8: */
var Bloka = (function () {
  function initContainer(path) {
    var container = $('main_container').setStyle({visibility: 'visible', display: 'none'});
    container.positionAtCenter().appear({duration: 4});
    [['perl', 61], ['widget', 66], ['library', 69], ['for', 44], ['the', 34], ['web', 52]].each(function(args) {
      var el = $(args[0]);
      el.signalConnect('mouseover', function() {
        var [id, width] = arguments[0];
        if (this.downEffect)
          this.downEffect.cancel();
        this.upEffect = new Effect.Morph(this, {
          style: {width: width * 2 + 'px', height: '74px'},
          duration: 0.2, 
          beforeStart: this.writeAttribute.bind(this, {src: path + id + '2.jpg'})
        });
        }.bind(el, args)).signalConnect('mouseout', function() {
          var [id, width] = arguments[0];
          if (this.upEffect)
            this.upEffect.cancel();
          this.downEffect = new Effect.Morph(this, {
            style: {width: width + 'px', height: '37px'},
            duration: 0.2,
            afterFinish: this.writeAttribute.bind(this, {src: path + id + '.jpg'})
          });
        }.bind(el, args));
    });
  }

  var urlTemplate = new Template('url(#{url})');

  return {
    initialize: function(path) {
      initContainer.call(this, path);

      var pageTracker = _gat._getTracker('UA-2846284-2');
      pageTracker._initData();
      pageTracker._trackPageview();
    },
    Menus: {
      About: {
        complete: function(json) {
          if (json.data)
            IWL.createHtmlElement(json.data, document.body);
          Bloka.Menus.stopRequest($(json.extras.parent));
        }
      },
      IWLDemo: {
        complete: function(json) {
          if ($('iwl_demo_contentbox'))
            return Bloka.Menus.stopRequest($(json.extras.parent));
          if (json.data)
            IWL.createHtmlElement(json.data, document.body);
          Bloka.Menus.stopRequest($(json.extras.parent));
        }
      },

      request: function(element) {
        if (!element.menu || !element.menu.parentItem) return;
        var item = element.menu.parentItem;
        item.style.backgroundImage = urlTemplate.evaluate({url: IWL.Config.IMAGE_DIR + '/loader/loader10x10.gif'});
      },
      stopRequest: function(element) {
        if (!element.menu || !element.menu.parentItem) return;
        var item = element.menu.parentItem;
        item.style.backgroundImage = 'none';
      }
    }
  }
})();
