﻿

// -----------------------------------------
// ---         Структура меню            ---
// -----------------------------------------

var menuContent = {

  News: {
    shift: 0,
    width: 111,
    items: [
      ['Новости', '/news/default.asp?sec=1'],
      ['Статьи', '/news/default.asp?sec=2'],
      ['Поиск', '/news/search.asp'],
      ['Подписка', '/news/subscribe.asp']
	  ]
  },

  Catalog: {
    shift: 0,
    width: 118,
    items: [
      ['Игры для PC', '/games/section.asp?platform=PC'],
      ['Игры для PS2', '/games/section.asp?platform=PS2'],
      ['Игры для PS3', '/games/section.asp?platform=PS3'],
      ['Игры для PS3 Move', '/games/ps3-move/default.aspx'],
      ['Игры для PSP', '/games/section.asp?platform=PSP'],
      ['Игры для Xbox 360', '/games/section.asp?platform=X360'],
      ['Игры для Wii', '/games/section.asp?platform=Wii'],
      ['Игры для 3DS', '/games/3ds/default.aspx'],
      ['Игры для PS Vita', '/games/ps-vita/default.aspx'],
    //['Аксессуары','/games/hardlist.aspx'],
      ['Поиск', '/games/default.asp'],
      ['График выхода', '/games/releases.aspx']
	  ]
  },

  User: {
    shift: 0,
    width: 113,
    items: [
    //  ['Регистрация игр', '/users/default.asp'],
      ['Личный кабинет', '/personal/default.aspx'],
      ['Поддержка', '/users/support.asp'],
      ['FAQ', '/users/support-faq.aspx'],
      ['Акции', '/users/actions.aspx'],
      ['Предзаказы', '/users/preorders.aspx'],
      ['Где купить', '/users/shops.aspx'],
      ['Вакансии', '/users/job.asp']
	  ]
  },

  Partner: {
    shift: 0,
    width: 111,
    items: [
    //['О компании', '/partners/default.asp'],
    //['Контакты', '/partners/contacts.asp'],

      ['Прессе', '/partners/press.asp'],
      ['Стать дилером', '/partners/newpartner.asp']
	  ]
  },

  Shop: {
    shift: 0,
    width: 68, /* ширина картинки - 2 */
    items: [
	  ]
  }
};

// -----------------------------------------
// ---     Class: SoftClub.UI.TopMenu   ---
// -----------------------------------------

Type.registerNamespace('SoftClub.UI');


SoftClub.UI.TopMenu = function () {

  SoftClub.UI.TopMenu.initializeBase(this);

  this._structure = null;

  this._overHandler = null;
  this._outHandler = null;
  this._loadHandler = null;

  this._showTimerHandler = null;
  this._hideTimerHandler = null;

  this._vTimeout = null;
  this._hTimeout = null;
  this._curentImgName = null;

  this._showName = null;
  this._hideName = null;
}

SoftClub.UI.TopMenu.prototype = {

  // === Constants ===

  controlPrefix: 'menu',
  controlPrefixLength: 4,

  attrItemName: 'menuItem',
  layerClassName: 'menuLayer',

  showDelay: 500,
  hideDelay: 500,

  // === Properties ===

  get_structure: function SoftClub$UI$TopMenu$get_structure() {
    return this._structure;
  },

  set_structure: function SoftClub$UI$TopMenu$set_structure(v) {
    this._structure = v;
  },

  // === Overrides ===

  initialize: function SoftClub$UI$TopMenu$initialize() {

    SoftClub.UI.TopMenu.callBaseMethod(this, 'initialize');

    this._overHandler = Function.createDelegate(this, this._onMouseOver);
    this._outHandler = Function.createDelegate(this, this._onMouseOut);

    this._showTimerHandler = Function.createDelegate(this, this._show);
    this._hideTimerHandler = Function.createDelegate(this, this._hide);

    this._loadHandler = Function.createDelegate(this, this._onAppLoad);
    Sys.Application.add_load(this._loadHandler);
  },

  dispose: function SoftClub$UI$TopMenu$dispose() {

    for (var name in this._structure) {

      var params = this._structure[name];

      if (params.img) {
        $clearHandlers(params.img);
        params.img = null
        params.layer = null;
      }
    }

    this._structure = null;

    this._overHandler = null;
    this._outHandler = null;

    this._showTimerHandler = null;
    this._hideTimerHandler = null;

    if (this._loadHandler != null) {
      Sys.Application.remove_load(this._loadHandler);
      this._loadHandler = null;
    }

    SoftClub.UI.TopMenu.callBaseMethod(this, 'dispose');
  },

  // === Handlers === 

  _onAppLoad: function SoftClub$UI$TopMenu$_onAppLoad() {

    for (var name in this._structure) {

      var params = this._structure[name];
      var img = $get(this._getImageId(name));

      img.setAttribute(this.attrItemName, name);
      $rollover.initImageState(img);
      this._applyHandlers(img, name);

      params.img = img;
      params.layer = null;
    }
  },

  _onMouseOver: function SoftClub$UI$TopMenu$_onMouseOver(e) {

    var name = this._getMenuName(e.target);
    this._startShowTimeout(name);
  },

  _onMouseOut: function SoftClub$UI$TopMenu$_onMouseOut(e) {

    var name = this._getMenuName(e.target);
    this._startHideTimeout(name);
  },

  // === Methods === 

  _show: function SoftClub$UI$TopMenu$_show(n) {

    var name = (typeof (n) == 'string') ? n : this._showName;
    var info = this._structure[name];

    if (info.layer == null) {
      var div = this._createLayer(info);
      this._applyHandlers(div, name);
      info.layer = div;
    }

    this._setTopLevelPosition(info);
    info.layer.style.display = 'block';

    this._curentImgName = name;

    if (this._sTimeout != null)
      this._clearShowTimeout();
  },

  _startShowTimeout: function SoftClub$UI$TopMenu$_startShowTimeout(name) {

    if (this._sTimeout != null && this._showName != name)
      this._clearShowTimeout();

    var img = this._structure[name].img;

    if (this._curentImgName == null) {
      this._showName = name;

      this._sTimeout = window.setTimeout(this._showTimerHandler, this.showDelay);

      if (this._hideName != null) {
        $rollover.out(this._structure[this._hideName].img);
        this._clearHideTimeout();
      }

      $rollover.over(img);
    }
    else {

      if (this._curentImgName == name) {
        this._clearHideTimeout();
      }
      else {
        $rollover.out(this._structure[this._curentImgName].img);

        if (name != this._curentImgName) {
          this._clearHideTimeout();
          this._hide(this._curentImgName);
        }

        $rollover.over(img);
        this._show(name);
      }
    }
  },

  _clearShowTimeout: function SoftClub$UI$TopMenu$_clearShowTimeout() {

    window.clearTimeout(this._sTimeout);
    this._sTimeout = null;
    this._showName = null;
  },

  _hide: function SoftClub$UI$TopMenu$_hide(n) {

    var name = (typeof (n) == 'string') ? n : this._hideName;
    var info = this._structure[name];

    if (info.layer != null)
      info.layer.style.display = 'none';

    $rollover.out(info.img);
    this._curentImgName = null;

    if (this._hTimeout != null)
      this._clearHideTimeout();
  },

  _startHideTimeout: function SoftClub$UI$TopMenu$_startHideTimeout(name) {

    if (this._sTimeout != null && this._showName == name) {
      this._clearShowTimeout();
      this._hide(name);
    }
    else {
      if (this._hTimeout != null)
        this._clearHideTimeout();

      this._hideName = name;
      this._hTimeout = window.setTimeout(this._hideTimerHandler, this.hideDelay);
    }
  },

  _clearHideTimeout: function SoftClub$UI$TopMenu$_clearHideTimeout() {

    window.clearTimeout(this._hTimeout);
    this._hTimeout = null;
    this._hideName = null;
  },

  _getImageId: function SoftClub$UI$TopMenu$_getImageId(targetName) {
    return this.controlPrefix + targetName;
  },

  _getMenuName: function SoftClub$UI$TopMenu$_getMenuName(el) {

    var o = el;

    if (o.tagName == 'A')
      o = o.parentNode.parentNode;
    else if (o.tagName == 'DIV' && Sys.UI.DomElement.containsCssClass(o, this.attrItemName))
      o = o.parentNode;

    return o.getAttribute(this.attrItemName);
  },

  _applyHandlers: function SoftClub$UI$TopMenu$_applyHandlers(el, name) {

    el.setAttribute(this.attrItemName, name);

    $addHandlers(el, {
      mouseover: this._overHandler,
      mouseout: this._outHandler
    }, this);
  },

  _createLayer: function SoftClub$UI$TopMenu$_createLayer(info) {

    var div = $page.createLayer(this.layerClassName, {
      display: 'none',
      width: info.width + 'px'
    });

    var items = info.items;
    var l = items.length;

    for (var i = 0; i < l; i++) {
      var child = this._createItem(items[i]);
      div.appendChild(child);
    }

    $page.addLayer(div);

    return div;
  },

  _createItem: function SoftClub$UI$TopMenu$_createItem(arr) {

    var div = $page.createLayer('menuItem');
    div.innerHTML = String.format('<a href="{1}">{0}</a>', arr[0], arr[1]);
    return div;
  },

  _setTopLevelPosition: function SoftClub$UI$TopMenu$_setTopLevelPosition(info) {

    // некорректно работает в IE8 :((
    // var b = Sys.UI.DomElement.getBounds(info.img);

    /* замена кода */
    var 
      img = $(info.img)
    , b = img.offset();

    b.x = b.left;
    b.y = b.top;
    b.height = img.height();
    /* /замена кода */

    var l = b.x + info.shift;
    var t = b.y + b.height - 1;

    var s = info.layer.style;
    s.top = t + 'px';
    s.left = l + 'px';
  }
}

SoftClub.UI.TopMenu.registerClass('SoftClub.UI.TopMenu', Sys.Component);

// -----------------------------------------
// ---          Runtime code             ---
// -----------------------------------------

var $topMenu = $create(SoftClub.UI.TopMenu, { structure: menuContent });

