﻿
Type.registerNamespace('SoftClub.UI');

if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7.0) {
  document.execCommand("BackgroundImageCache", false, true);
}


// -----------------------------------------
// ---      Class: SoftClub.UI._Page    ---
// -----------------------------------------

SoftClub.UI._Page = function () {

  SoftClub.UI._Page.initializeBase(this);

  // layout

  this._divPageTop = null;
  this._divPageTopNav = null;
  this._divPageFooter = null;
  this._divPageBody = null;
  this._divPageBodyContent = null;
  this._divPageBodyHeightFix = null;

  this._resizeHandler = null;

  // search

  this._tbSearch = null;
  this._imgSearchButton = null;

  this._typeSearchTextHandler = null;
  this._searchHandler = null;
}

SoftClub.UI._Page.prototype = {

  // === Constants ===

  pageBodyBorder: 1,

  // === Overrides ===

  initialize: function SoftClub$UI$_Page$initialize() {

    SoftClub.UI._Page.callBaseMethod(this, 'initialize');

    this._divPageBodyHeightFix = document.createElement('DIV');
    var style = this._divPageBodyHeightFix.style;
    style.overflow = 'hidden';
    style.borderWidth = '0px';
    style.height = '0px';
    style.margin = '0px';
    style.padding = '0px';

    this._resizeHandler = Function.createDelegate(this, this._onLayout);
  },

  dispose: function SoftClub$UI$_Page$dispose() {

    // layout

    this._divPageTop = null;
    this._divPageTopNav = null;
    this._divPageFooter = null;
    this._divPageBody = null;
    this._divPageBodyContent = null;
    this._divPageBodyHeightFix = null;

    if (this._resizeHandler != null) {
      $removeHandler(window, 'resize', this._resizeHandler);
      this._resizeHandler = null;
    }

    // search

    $clearHandlers(this._imgSearchButton);
    $clearHandlers(this._tbSearch);

    this._tbSearch = null;
    this._imgSearchButton = null;

    this._typeSearchTextHandler = null;
    this._searchHandler = null;

    SoftClub.UI._Page.callBaseMethod(this, 'dispose');
  },

  // === Public methods ===

  createLayer: function SoftClub$UI$_Page$createLayer(className, styles) {

    var e = Function._validateParams(arguments, [
      { name: "className", type: String, mayBeNull: true },
      { name: "styles", type: Object, optional: true }
    ]);

    if (e) throw e;

    var div = document.createElement('DIV');

    if (className != null)
      div.className = className;

    var s = div.style;

    for (var name in styles) {
      var value = styles[name];
      s[name] = value;
    }

    return div;
  },

  addLayer: function SoftClub$UI$_Page$addLayer(el) {
    var e = Function._validateParams(arguments, [{ name: "el", domElement: true}]);
    if (e) throw e;

    document.body.insertBefore(el, document.body.firstChild);
  },

  getClientBounds: function SoftClub$UI$_Page$getClientBounds() {

    var w, h;
    switch (Sys.Browser.agent) {
      case Sys.Browser.InternetExplorer:
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
        break;
      case Sys.Browser.Safari:
        w = window.innerWidth;
        h = window.innerHeight;
        break;
      case Sys.Browser.Opera:
        w = Math.min(window.innerWidth, document.body.clientWidth);
        h = Math.min(window.innerHeight, document.body.clientHeight);
        break;
      default:
        w = Math.min(window.innerWidth, document.documentElement.clientWidth);
        h = Math.min(window.innerHeight, document.documentElement.clientHeight);
        break;
    }

    return new Sys.UI.Bounds(0, 0, w, h);
  },

  Layout: function SoftClub$UI$_Page$InitLayout() {

    this._initLayout();
    this._layout();
    $addHandler(window, 'resize', this._resizeHandler);
    this._initSearch();
  },


  // === Handlers === 

  _onSearch: function SoftClub$UI$_Page$_onSearch(e) {

    Sys.Debug.trace('_onSearch');

    this._search();
  },

  _onTypeSearchText: function SoftClub$UI$_Page$_onTypeSearchText(e) {

    Sys.Debug.trace('_onTypeSearchText');

    if (e.charCode == Sys.UI.Key.enter) {
      e.stopPropagation();
      e.preventDefault();
      this._search();
    }
  },

  _onLayout: function SoftClub$UI$_Page$_onLayout() {

    this._layout();
  },

  // === Private methods ===

  _initSearch: function SoftClub$UI$_Page$_initSearch() {
    Sys.Debug.trace('_initSearch');

    this._tbSearch = $get('tbSearch');
    this._imgSearchButton = $get('imgSearchButton');

    this._searchHandler = Function.createDelegate(this, this._onSearch);
    this._typeSearchTextHandler = Function.createDelegate(this, this._onTypeSearchText);

    $addHandler(this._imgSearchButton, 'click', this._searchHandler);
    $addHandler(this._tbSearch, 'keypress', this._typeSearchTextHandler);
  },

  _search: function SoftClub$UI$_Page$_search() {

    Sys.Debug.trace('_search');

    var s = this._tbSearch.value.trim();

    if (s.length == 0) {
      alert('Введите строку поиска!');
      return;
    }

    window.location.href = '/games/default.asp?sval=' + s;
  },

  _initLayout: function SoftClub$UI$_Page$_initLayout() {

    this._divPageTop = $get('divPageTop');
    this._divPageTopNav = $get('divPageTopNav');
    this._divPageFooter = $get('divPageFooter');
    this._divPageBody = $get('divPageBody');
    this._divPageBodyContent = $get('divPageBodyContent');

    this._divPageBody.appendChild(this._divPageBodyHeightFix);
  },

  _layout: function SoftClub$UI$_Page$_layout() {

    var height =
      this._divPageTop.offsetHeight +
      this._divPageTopNav.offsetHeight +
      this._divPageFooter.offsetHeight;

    var bounds = this.getClientBounds();
    var contenHeight = this._divPageBodyContent.offsetHeight;
    var result;

    if ((height + contenHeight) <= bounds.height)
      result = Math.max((bounds.height - height - this.pageBodyBorder), contenHeight);
    else
      result = contenHeight + this.pageBodyBorder;

    if (this._divPageBody.offsetHeight != result)
      this._divPageBodyHeightFix.style.height = '' + (result - contenHeight) + 'px';
  }
};

SoftClub.UI._Page.registerClass('SoftClub.UI._Page', Sys.Component);


// -----------------------------------------
// --- Class: SoftClub.UI.ImageRollover ---
// -----------------------------------------

SoftClub.UI.ImageRollover = function () {

  SoftClub.UI.ImageRollover.initializeBase(this);

  this._appLoadHandler = null;

  this._rolloverImages = null;
}

SoftClub.UI.ImageRollover.prototype = {

  // === Constants ===

  rolloverClassName: 'rollover',
  rolloverAttrOverName: 'srcOver',
  rolloverAttrOutName: 'srcOut',
  rolloverSufixName: '_over',

  // === Overrides ===

  initialize: function SoftClub$UI$ImageRollover$initialize() {
    SoftClub.UI.ImageRollover.callBaseMethod(this, 'initialize');

    this._appLoadHandler = Function.createDelegate(this, this._onAppLoad);
    Sys.Application.add_load(this._appLoadHandler);

    this._rolloverImages = [];
  },

  dispose: function SoftClub$UI$ImageRollover$dispose() {

    if (this._rolloverImages != null) {
      var l = this._rolloverImages.length;

      for (var i = 0; i < l; i++) {

        if (this._rolloverImages[i][2])
          $clearHandlers(this._rolloverImages[i][0]);

        Array.clear(this._rolloverImages[i]);
      }

      Array.clear(this._rolloverImages);
    }

    if (this._appLoadHandler != null) {
      Sys.Application.remove_load(this._appLoadHandler);
      this._appLoadHandler = null;
    }

    SoftClub.UI.ImageRollover.callBaseMethod(this, 'dispose');
  },

  // === Methods ===

  initImageState: function SoftClub$UI$ImageRollover$initImageState(img) {
    this._initImageState(img, false);
  },

  over: function SoftClub$UI$ImageRollover$over(img) {

    var e = Function._validateParams(arguments, [{ name: "img", domElement: true}]);
    if (e) throw e;

    img.src = img.getAttribute(this.rolloverAttrOverName);
  },

  out: function SoftClub$UI$ImageRollover$out(img) {

    var e = Function._validateParams(arguments, [{ name: "img", domElement: true}]);
    if (e) throw e;

    img.src = img.getAttribute(this.rolloverAttrOutName);
  },

  _initImageState: function SoftClub$UI$ImageRollover$_initImageState(img, clearHandler) {

    var src = img.src;
    var index = src.lastIndexOf('.');
    var srcOver = src.substring(0, index) + this.rolloverSufixName + src.substring(index);

    var over = new Image();
    over.src = srcOver;

    img.setAttribute(this.rolloverAttrOutName, src);
    img.setAttribute(this.rolloverAttrOverName, srcOver);

    Array.add(this._rolloverImages, [img, over, clearHandler]);
  },

  _onAppLoad: function SoftClub$UI$ImageRollover$_onAppLoad() {

    var images = document.images;
    var l = images.length;

    for (var i = 0; i < l; i++) {
      var img = images[i];
      if (Sys.UI.DomElement.containsCssClass(img, this.rolloverClassName)) {
        this._initImageState(img, true);
        $addHandlers(img, { mouseover: this._onRolloverOver, mouseout: this._onRolloverOut }, this);
      }
    }
  },

  _onRolloverOver: function SoftClub$UI$ImageRollover$_onRolloverOver(e) {
    this.over(e.target);
  },

  _onRolloverOut: function SoftClub$UI$ImageRollover$_onRolloverOut(e) {
    this.out(e.target);
  }

};

SoftClub.UI.ImageRollover.registerClass('SoftClub.UI.ImageRollover', Sys.Component);

// -----------------------------------------
// ---          Runtime code             ---
// -----------------------------------------

var $page = $create(SoftClub.UI._Page);
var $rollover = $create(SoftClub.UI.ImageRollover);

