/* Copyright (c) 2007-2008 Onlyweb Studio | http://www.onlyweb.ru/ */
/*

Author: Miha Zimin 

Big thanks to http://jquery.org/ [джей квири]

From Russia with love!

*/


/**
 *
 * Используем эту функцию из prototype.js для
 * кода based on jQuery
 *
 **/
var newClass = function() {
	return function() {
		return this.init.apply(this, arguments);
	}
}


/**
 *
 * Необходимо для установки значения по умолчанию
 *
 **/
var defaultValue = function(ref, val) {
  return (ref == undefined) ? val : ref;
}

/**
 *
 * Используется на странице services
 *
 **/
var Services = newClass();
Services.prototype = {
  init: function() {
    var t = this;
    // Инициализация плагина history
    $.historyInit(function(hash) {
      t.pageLoad(hash);
    });
  },

  listItemClick: function(e) {
    var t = this;
    e = jQuery.event.fix(e);
    var el = $(e.target).find('.ServiceHeader');
    return t.handleClick(el.attr('id') + '_');
  },

  spanClick: function(e) {
    var t = this;
    e = jQuery.event.fix(e);
    var el = $(e.target);
    return t.handleClick(el.attr('id') + '_');
  },

  handleClick: function(hash) {
    var t = this;
    // "Загружаем" страницу с этим id
    if (t.currentHash != hash) {
      $.historyLoad(hash);
    }
    return false;
  },

  pageLoad: function(hash) {
    var t = this;
    t.currentHash = hash;
    // Если передан hash
		if(hash) {
      hash = hash.replace(/_$/, '');
      var ref = $('#' + hash);
      var text = $('#' + hash + '_text');
      $('.ServiceHeader').addClass('ServiceReference');
      ref.removeClass('ServiceReference');
      $('.ServiceText').css('display', 'none');
      text.fadeIn();
		} else {
      $('.ServiceText').css('display', 'none');
		}
  }
}




/*
var Articles = newClass();
Articles.prototype = {

  init: function() {
    var t = this;
    t.articlesContainer = $('#articles');
    t.articles = [];
    t.articlesContainer.find('.Article').each(function() {
      t.articles.push($(this));
    });
    t.referenceContainer = $('#article_references');
    t.references = [];
    t.referenceContainer.find('.ArticleRef').each(function() {
      t.references.push($(this)); 
    });
    t.animationIsFinished = true;
    $.historyInit(function(hash) {
      t.pageLoad(hash);
    });
  },

  referenceClick: function(e) {
    var t = this;
    e = jQuery.event.fix(e);
    var el = $(e.target);
    return t.handleClick(el.attr('id').replace('ref', ''));
  },

  handleClick: function(hash) {
    var t = this;
    if (t.currentHash != hash && t.animationIsFinished) {
      $.historyLoad(hash);
    }
    return false;
  },

  pageLoad: function(hash) {
    var t = this;
    if (!hash) {
      t.currentHash = 'article_0_';
      return false;
    }
    if (t.currentHash == undefined) {
      t.currentHash = hash;
      for (var i = 0; i < t.articles.length; i++) {
        t.articles[i].css('display', 'none')
        .removeClass('Current');
        t.references[i].removeClass('Current');
      }
      t.currentArticleId = parseInt(hash.replace('article_', '').replace('_', ''));
      t.articles[t.currentArticleId].css('display', 'block')
      .addClass('Current');
      t.references[t.currentArticleId].addClass('Current');

      return false;
    }
    if (t.currentHash == hash) {
      return false;
    }
    t.currentHash = hash;

    t.currentArticleId = parseInt(t.articlesContainer
    .find('.Current').attr('id')
    .replace('article_', ''));

    var newArticle = $('#' + hash.replace(/_$/, ''));
    t.replaceCurrentArticle(newArticle);
  },

  replaceCurrentArticle: function(newArticle) {
    var t = this;
    var article = t.articles[t.currentArticleId];
    var articleRef = $('#' + article.attr('id') + '_ref');
    var newArticleRef = $('#' + newArticle.attr('id') + '_ref');
    article.css('display', 'block');
    article.removeClass('Current');
    articleRef.removeClass('Current');
    newArticle.css('left', '570px');
    newArticle.css('display', 'block');
    newArticle.addClass('Current');
    newArticleRef.addClass('Current');
    t.animationIsFinished = false;
    var counter = 0;
    var animationEnd = function() {
      if (++counter == 2) {
        t.animationIsFinished = true;
      }
    };
    newArticle.animate({
      'left': '-=570px'
    }, 400, 'swing', animationEnd);
    article.animate({
      'left': '-=570px'
    }, 400, 'swing', animationEnd);
  }
}
*/


var Articles = newClass();
Articles.prototype = {

  init: function() {
    var t = this;
    t.articlesContainer = $('#articles');
    t.articles = [];
    t.articlesContainer.find('.Article').each(function() {
      t.articles.push($(this));
    });
    t.referenceContainer = $('#article_references');
    t.references = [];
    t.referenceContainer.find('.ArticleRef').each(function() {
      t.references.push($(this)); 
    });
    t.referenceWrappers = [];
    t.referenceContainer.find('.ArticleRefWrapper').each(function() {
      t.referenceWrappers.push($(this));
    });
    t.animationIsFinished = true;
    $.historyInit(function(hash) {
      t.pageLoad(hash);
    });
  },

  referenceClick: function(e) {
    var t = this;
    e = jQuery.event.fix(e);
    var el = $(e.target);
    return t.handleClick(el.attr('id').replace('ref', ''));
  },

  handleClick: function(hash) {
    var t = this;
    if (t.currentHash != hash) {
      $.historyLoad(hash);
    }
    return false;
  },

  pageLoad: function(hash) {
    var t = this;
    if (!hash) {
      t.currentHash = 'article_0_';
      return false;
    }
    if (t.currentHash == undefined) {
      t.currentHash = hash;
      for (var i = 0; i < t.articles.length; i++) {
        t.articles[i].css('display', 'none')
        .removeClass('Current');
        t.references[i].removeClass('Current');
        t.referenceWrappers[i].removeClass('Current');
      }
      t.currentArticleId = parseInt(hash.replace('article_', '').replace('_', ''));
      t.articles[t.currentArticleId].css('display', 'block')
      .addClass('Current');
      t.references[t.currentArticleId].addClass('Current');
      t.referenceWrappers[t.currentArticleId].addClass('Current');

      return false;
    }
    if (t.currentHash == hash) {
      return false;
    }
    t.currentHash = hash;

    t.currentArticleId = parseInt(t.articlesContainer
    .find('.Current').attr('id')
    .replace('article_', ''));

    var newArticle = $('#' + hash.replace(/_$/, ''));
    t.replaceCurrentArticle(newArticle);
  },

  replaceCurrentArticle: function(newArticle) {
    var t = this;
    var article = t.articles[t.currentArticleId];
    var articleRef = $('#' + article.attr('id') + '_ref');
    var articleRefWrapper = $('#' + article.attr('id') + '_ref_wrapper');
    var newArticleRef = $('#' + newArticle.attr('id') + '_ref');
    var newArticleRefWrapper = $('#' + newArticle.attr('id') + '_ref_wrapper');
    article.css('display', 'none');
    article.removeClass('Current');
    articleRef.removeClass('Current');
    articleRefWrapper.removeClass('Current');
    newArticle.css('display', 'block');
    newArticle.addClass('Current');
    newArticleRef.addClass('Current');
    newArticleRefWrapper.addClass('Current');
  }
}






var Onlyweb = {};

/**
 *
 * Возвращает состояние вертикальной полосы прокрутки (px)
 *
 * @return отступ сверху (в пикселях)
 *
 **/
Onlyweb.getScrollTop = function () {
  var returnValue = 0;
  if ( (document.documentElement) && (document.documentElement.scrollTop) ) {
    returnValue = document.documentElement.scrollTop;
  } else if ( (document.body) && (document.body.scrollTop) ) {
    returnValue = document.body.scrollTop;
  } else if ( window.pageYOffset ) {
    returnValue = window.pageYOffset;
  } else if ( window.scrollY ) {
    returnValue = window.scrollY;
  }
  returnValue = parseInt(returnValue);
  if ( (isNaN(returnValue) == true) || (returnValue < 0) ) {
    returnValue = 0;
  }
  return returnValue;
}


/**
 *
 * Заменяет e-mail, защищенный от спама на нормальный
 *
 * @param elId id элемента (ссылки), который необходимо поменять
 *
 **/
Onlyweb.changeEmail = function(elId) {
  var email = $('#' + elId);
  var realEmail = email.html().replace(/\(a\)/, '@');
  email.html(realEmail);
  email.attr('href', 'mailto:' + realEmail);
}
