Limb.Class('modalWindow',
{
  __construct: function(container)
  {
    if (container && jQuery(container)[0])
      this.container = jQuery(container);
    else
      this.container = jQuery('body');
  },
  _build: function()
  {
    var that = this;
    this._buildOverlay();

    this.window = jQuery('.modalWindow');
    if (!this.window.is('div'))
    {
      this.container.append('<div class="modalWindow"></div>');
      this.window = jQuery('.modalWindow');
      jQuery(document).bind('keydown',{that:this}, this.keyPressed);
    }
    else
      this.window.empty();

    this.window.prepend("<a href='#' class='close_icon' title='Закрыть'><img src='images/1x1.gif' alt='Закрыть'/></a>").show();
    this.windowOverlay.show();
  },
  _buildOverlay: function()
  {
    this.windowOverlay = jQuery('.modalWindowOverlay');
    if (!this.windowOverlay.is('div'))
    {
      this.container.append('<div class="modalWindowOverlay"></div>');
      this.windowOverlay = jQuery('.modalWindowOverlay');
    }
    else
      this.windowOverlay.show();
  },

  loadByUrl: function (url,id)
  {
    if(!url)
      return;
    this.name = "ajax";
    var that = this;
    this._build();
    if (id)
    {
      this.id = id;
      this.window.attr('id', this.id);
    }
    this.window.load(url,
      function()
      {
        that.window.css('background-image','none');
        that.window.find('.hideModalWindow, .close_icon').click(function(){that.hide();return false;});
      });
  },
  loadById: function (target,id)
  {
    if(!target)
      return;
    this.name = "static";
    this.target = jQuery(target);
    this.prevTarget = this.target.prev();
    if (!this.prevTarget[0])
      this.parentTarget = this.target.parent();
    this._build();
    var that = this;
    if (id)
    {
      this.id = id;
      this.window.attr('id', this.id);
    }
    this.target.clone(true).appendTo(this.window).show();
    this.target.remove();
    this.window.css('background-image','none').find('.hideModalWindow, .close_icon.close_icon').click(function(){that.hideById();return false;});;
  },

  messageBlock: function (msg, parentNode)
  {
    parentNode = parentNode || this.container;
    var htmlMessage = "<a href='javascript:void(0)' class='close_icon' onclick='jQuery(this.parentNode).remove();' title='Закрыть'><img src='images/1x1.gif' alt='Закрыть' /></a>";
    htmlMessage += "<a href='javascript:void(0)' class='button'>OK</a>";

    jQuery(parentNode).append("<div class='message_block'><div class='msg'>" + msg + "</div>"+ htmlMessage + "</div>");

    var messageBlock = jQuery('.message_block');
    messageBlock.find('.button').eq(0).click(function(){jQuery(this.parentNode).remove();return false;});
    messageBlock.css('margin-top', (-1)*messageBlock.height()/2);
    return messageBlock;
  },

  confirmBlock: function(msg, target)
  {
    var html = "<div class='confirm_block'>";
    html += "<a href='javascript:void(0)' class='close_icon' title='Закрыть' onclick='jQuery(this.parentNode).remove();jQuery(\".modalWindowOverlay\").hide();'><img src='images/1x1.gif' alt='Закрыть' /></a>";

    if (!target)
      var link = "javascript:void(0);";
    else
      var link = target.href;

    html += "<div class='msg'>" + msg + "</div>";
    html += "<a href='" + link + "' class='button' id='ok_button'>OK</a>";
    html += "<a href='#' class='button' id='cancel_button'>Отмена</a>";
    html += "</div>";

    this._buildOverlay();

    var elem = jQuery(html).appendTo(this.container);

    elem.css('margin-top', (-1)*elem.height()/2);

    elem.find('.button').eq(0).click(function(){jQuery(this.parentNode).remove();jQuery('.modalWindowOverlay').hide();});
    elem.find('.button').eq(1).click(function(){jQuery(this.parentNode).remove();jQuery('.modalWindowOverlay').hide();return false;});

    return elem;
  },

  hideById: function()
  {
    if (this.prevTarget[0])
      this.prevTarget.after(this.target.clone(true));
    else
      this.target.clone(true).prependTo(this.parentTarget);
    this.hide();
    this.window.empty();
  },

  hide: function()
  {
    if(this.id)
    {
      this.window.removeAttr("id");
      this.id = null;
    }
    this.window.hide();
    this.windowOverlay.hide();
  },
  keyPressed: function (event)
  {
    if(event.which == 27)
      if (event.data.that.name = "static")
        event.data.that.hideById();
      else
        event.data.that.hide();
  }
});

Limb.Class('toggleBlock',
{
  __construct: function()
  {
  },
  start: function(target,trigger,options)
  {
    this.target = jQuery(target);
    if (!this.target[0])
      return;
    if(!options.noTitles)
    {
      this.showTitle = options.showTitle || "Показать";
      this.hideTitle = options.hideTitle || "Скрыть";
    }
    else
      this.noTitles = true;
    this.trigger = jQuery(trigger);
    this.timeout = options.timeout;
    if (options.parentNode)
      this.parentNode = jQuery(options.parentNode);
    var that = this;
    this.trigger.click(function(){that._behaviour(that);return false;});
  },
  _show: function(that)
  {
    var that = that;
    clearTimeout(that.timeoutCounter);
    that.target.show('slow');
    that.trigger.html(that.hideTitle);

    that.trigger.addClass('shown');
    if (that.parentNode[0])
      that.parentNode.addClass('opened');
    if (that.timeout)
      that.timeoutCounter =  setTimeout(function(){that._hide(that)}, that.timeout);
  },
  _hide: function(that)
  {
    var that = that;
    that.target.hide('slow', function(){if (that.parentNode[0]) that.parentNode.removeClass('opened');that.trigger.html(that.showTitle);});
    that.trigger.removeClass('shown');
  },
  _behaviour: function(that)
  {
    var that = that;
    if (that.target.css('display') == "none")
      that._show(that);
    else
      that._hide(that);
    return false;
  }
});

Limb.Class('ajaxLoader',
{
  __construct: function(container)
  {
    if (container)
      this.container = jQuery(container);
    else
      this.container = jQuery('body');
    this.build();
  },
  build: function()
  {
    this.container.append("<div class='ajax_loader' style='display:none;'>Загрузка ...</div>");
    this.ajaxLoader = jQuery('.ajax_loader');
  },
  show: function ()
  {
    this.ajaxLoader.show();
  },
  hide: function ()
  {
    this.ajaxLoader.hide();
  }
})

function showRegistrationUserFields()
{
  jQuery('.organization_fields').hide();
  jQuery('.organization_fields input').val('');
  jQuery('.organization_fields textarea').val('');
  jQuery('.personal_fields').show();
}

function showRegistrationOrganizationFields()
{
  jQuery('.personal_fields').hide();
  jQuery('.personal_fields input').val('');
  jQuery('.personal_fields textarea').val('');
  jQuery('.organization_fields').show();
}

function changeCode()
{
  var img = document.getElementById('captcha_value');
  img.src = "images/1x1.gif";
  ajaxLoader.show();
  img.setAttribute('src','/captcha.php?'+Math.random());
  ajaxLoader.hide();
}

function initDocumentStructure()
{
  var docWidth = document.documentElement.clientWidth;
  var body = jQuery('body');
  var wideWidthSite = 1246;
  var isWidehSite =(docWidth > wideWidthSite) ? true : false;

  if (isWidehSite)
    body.removeClass('thinSite');
  else
    body.addClass('thinSite');

}

function initMenu()
{
  var url = window.location.toString()
  var max = 0;
  var link = null;

  jQuery("#main_menu a").each(function(){
    if(url.indexOf(this.href) >= 0 && this.href.length > max)
    {
      link = this;
      max = this.href.length;
    }
  });

  if(link)
    jQuery(link).addClass('active');

  max = 0;
  link = null;

  jQuery("#user_menu a").each(function(){
    if(url.indexOf(this.href) >= 0 && this.href.length > max)
    {
      link = this;
      max = this.href.length;
    }
  });

  if(link)
    jQuery(link).parent('li').addClass('active');
}

function initCategoryList()
{
  var categoryList = new toggleBlock();
  categoryList.start('#category_list ul', "#category_list .title", {parentNode:'#category_list', noTitles: true});
  jQuery('body').click(function(event){
    if(!event) event = window.event;
    var target =  event.target;
    if (jQuery(target).parents('#category_list')[0] || target.id == "category_list")
      return;
    categoryList._hide(categoryList);
  });
}
function initCatalog()
  {
    jQuery('#catalog dt .hasSubsection').click(function(){
      var category = jQuery(this);
      var id = this.id.replace('category_','');
      var subsection = jQuery('#subsection_'+id);
      if (subsection[0])
      {
        subsection.toggle();
        return false;
      }
      ajaxLoader.show();
      jQuery.ajax({
          url: '/catalog/get_category_sub_folders/'+id+'?type=ajax',
          dataType: 'html',
          success: function(data)
          {
            category.parent('dt').after(data);
            ajaxLoader.hide();
          }
      });
      return false;
    })
  }

jQuery(document).ready(
function()
{
  initMenu();
  ajaxLoader = new ajaxLoader('#wrapper');
  modalWindow = new modalWindow();

  initCatalog();
  initCategoryList();

  jQuery('#search_form').jLook();

  initDocumentStructure();
  var currheight = null;
  if(!jQuery.browser.msie)
    jQuery(window).resize(initDocumentStructure);
  else
    window.onresize = function()
    {
      if(currheight != document.documentElement.clientHeight)
      {
        initDocumentStructure();
      }
      currheight = document.documentElement.clientHeight;
    }
});

