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();
  }
})

Limb.Class('topMenu',
{
  __construct: function()
  {
    this.tabs_container = jQuery('#top_menu');

    if (!this.tabs_container[0])
      return;

    this.tabs = [];
    this.tabs_content = [];
    this._findControls();
    this.activateTab(this.tabs[0].attr('class').split(' ').shift());
  },

  _findControls: function()
  {
    var that = this;
    this.tabs_container.find("dt > ul > li").each(function()
    {
      var className = this.className.split(' ').shift();
      var target = that.tabs_container.find('.' + className+'_content');
      if (target)
      {
        jQuery(this).bind("click", {that: that}, that.showTab);
        that.tabs.push(jQuery(this));
        that.tabs_content.push(target);
      }
    });
    return false;
  },

  showTab:function(event)
  {
    var that = event.data.that;
    that.activateTab(this.className)
    return false;
  },

  activateTab: function(className)
  {
    for(var i = 0; i < this.tabs.length; i++)
    {
      if(this.tabs[i].attr('class').indexOf(className) == -1)
      {
        this.tabs_content[i].hide();
        this.tabs[i].removeClass('active');
        this.tabs[i].addClass('not_active');
      }
      else
      {
        this.tabs_content[i].show();
        this.tabs[i].removeClass('not_active');
        this.tabs[i].addClass('active');
      }
    }
  }
});

/*
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='javascript:' class='close_icon' title='Закрыть'><img src='images/1x1.gif' alt='Закрыть'/></a><div class='bgr'></div>").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,className)
  {
    if(!url)
      return;
    this.name = "ajax";
    var that = this;
    this._build();
    if (className)
    {
      this.className = className;
      this.window.addClass(this.className);
    }
    this.window.load(url,
      function()
      {
        that.window.find('.hideModalWindow, .close_icon').click(function(){that.hide();return false;});
      });
  },
  loadById: function (target,className)
  {
    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 (className)
    {
      this.className = className;
      this.window.addClass(this.className);
    }
    this.target.appendTo(this.window); //.show();
    this.window.find('.hideModalWindow, .close_icon.close_icon').click(function(){that.hideById();return false;});;
  },

  messageBlock: function (msg, parentNode, callback)
  {
    parentNode = parentNode || this.container;
    var htmlMessage = "<span class='title'><img src='images/1x1.gif' alt='Сообщение' /></span>";
    htmlMessage += "<a href='javascript:void(0)' class='close_icon' onclick='jQuery(this.parentNode).remove();jQuery(\".modalWindowOverlay\").hide();' title='Закрыть'><img src='images/1x1.gif' alt='Закрыть' /></a>";
    htmlMessage += "<a href='javascript:void(0)' class='button' id='ok_button'>OK</a>";

    this._buildOverlay();

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

    var messageBlock = jQuery('#message_block');
    var button = messageBlock.find('.button').eq(0);
    button.focus();
    button.click(
      function()  {
        jQuery(this.parentNode).remove();
        jQuery('.modalWindowOverlay').hide();
        if(typeof(callback) === 'function')
          callback();
        return false;

      }
    );
    return messageBlock;
  },

  greetingsNewMedal: function ()
  {
    parentNode = this.container;
    var htmlMessage = "<span class='title'><img src='images/1x1.gif' alt='Сообщение' /></span>";
    htmlMessage += "<a href='javascript:void(0)' class='close_icon' onclick='jQuery(this.parentNode).remove();jQuery(\".modalWindowOverlay\").hide();' title='Закрыть'><img src='images/1x1.gif' alt='Закрыть' /></a>";

    htmlcontent = '<h1><img src="images/1x1.gif" alt="Поздравляем!" /></h1>'+
                  '<h2><img src="images/1x1.gif" alt="У вас новая медаль!" /></h2>'+
                  '<div class="medals_case"><img src="tmp_medal_small.png" alt="Медаль!" /></div>'+
                  '<div class="medals_info">'+
                    '<h4>Корреспондент</h4>'+
                    'Теперь ваши друзья буду думать про вас, что вы настоящий фотограф. Ваши работы буду смотреть с большим интересом! <br/><br/>'+
                    '<strong>Вы загрузили на сайт 25 фотографий!</strong>'+
                  '</div>'+
                  '<div class="medals_other_info">'+
                  '</div>';

    this._buildOverlay();

    jQuery(parentNode).append("<div class='modalWindow' id='greetingsNewMedal_popup'><div class='msg'>"+htmlcontent+"</div>"+ htmlMessage + "<div class='bgr'></div></div>");

    return false;
  },
  
  greetingsNewAchievement: function ()
  {
    parentNode = this.container;
    var htmlMessage = "<span class='title'><img src='images/1x1.gif' alt='Сообщение' /></span>";
    htmlMessage += "<a href='javascript:void(0)' class='close_icon' onclick='jQuery(this.parentNode).remove();jQuery(\".modalWindowOverlay\").hide();' title='Закрыть'><img src='images/1x1.gif' alt='Закрыть' /></a>";

    this._buildOverlay();

    jQuery(parentNode).append("<div class='modalWindow' id='greetingsNewAchievement_popup'><div class='msg'>ggggggggggggggggggg</div>"+ htmlMessage + "<div class='bgr'></div></div>");

    return false;
  },
  
  confirmBlock: function(msg, target)
  {
    var html;
    html = this.getHtmlConfirmForm(msg, target);
    this._buildOverlay();

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

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

  confirmBlockCallback: function(msg, yes_function, no_function)
  {
    var html;
    html = this.getHtmlConfirmForm(msg, false);
    this._buildOverlay();

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

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

  getHtmlConfirmForm: function(msg, target)
  {
    var html = "<div class='modalWindow' id='confirm_block'>";
    html += "<span class='title'><img src='images/1x1.gif' /></span>";
    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;

    html += "<div class='msg'>" + msg + "</div><div class='button_panel'>";
    html += "<a href='" + link + "' class='button' id='yes_button'>OK</a>";
    html += "<a href='javascript:void(0)' class='button' id='cancel_button'>Отмена</a></div>";
    html += "<div class='bgr'></div></div>";
    return html;
  },

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

  hide: function()
  {
    if(this.className)
    {
      this.window.removeClass(this.className);
      this.className = 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('tabs',
{
  __construct: function(id)
  {
    this.tabs_container = jQuery(id);

    if (!this.tabs_container[0])
      return;

    this.tabs = [];
    this.tabs_content = [];

    this._findControls();
    this.activateTab(this.tabs[0].attr('id'));
  },

  _findControls: function()
  {
    var that = this;
    this.tabs_container.find("li").each(function(){
      var content_id = this.id.substring(0, this.id.indexOf("_tab"));
      var target = jQuery('#' + content_id);
      if (target)
      {
        jQuery(this).bind("click", {that: that}, that.showTab);
        that.tabs.push(jQuery(this));
        that.tabs_content.push(target);
      }
    });
    return false;
  },

  showTab:function(event)
  {
    var that = event.data.that;
    that.activateTab(this.id)
    return false;
  },

  activateTab: function(id)
  {
    for(var i = 0; i < this.tabs.length; i++)
    {
      if(this.tabs[i].attr('id') != id)
      {
        this.tabs_content[i].hide();
        this.tabs[i].removeClass('active');
        this.tabs[i].addClass('not_active');
      }
      else
      {
        this.tabs_content[i].show();
        this.tabs[i].removeClass('not_active');
        this.tabs[i].addClass('active');
      }
    }
  }
});

Limb.Class('dropDownMenu',
{
  __construct: function(id)
  {
    this.menu = jQuery('#user_menu');

    if (!this.menu[0])
      return;

    this.submenu = [];
    this.controls = [];

    this._findControls();
    this.setCloseEvent();
  },

  _findControls: function()
  {
    var that = this;
    this.menu.find("li.submenu > a").each(function(){
      var control = jQuery(this);
      var target = control.parent('li.submenu').find('.dropMenu');
      control.click(function(){that.initEvent(this);return false;});
      that.controls.push(control);
      that.submenu.push(target);
    });
    return false;
  },

  initEvent:function(that)
  {
    this.activateSubmenu(that);
    return false;
  },

  activateSubmenu: function(control)
  {
    var parentTarget = jQuery(control).parent('li.submenu');
    for(var i = 0; i < this.submenu.length; i++)
    {
      if(this.menu.find('li').index(this.submenu[i].parent('li')[0]) != this.menu.find('li').index(parentTarget[0]))
      {
        this.hideSubmenu(this.submenu[i]);
      }
      else
      {
        if (this.submenu[i].css('display')== "none")
          this.showSubmenu(this.submenu[i]);
        else
          this.hideSubmenu(this.submenu[i]);
      }
    }
  },
  showSubmenu:function (submenu)
  {
    submenu.show();
    submenu.parent('li').removeClass('not_active');
    submenu.parent('li').addClass('active');
  },
  hideSubmenu:function (submenu)
  {
    submenu.hide();
    submenu.parent('li').removeClass('active');
    submenu.parent('li').addClass('not_active');
  },
  setCloseEvent: function()
  {
    var that = this;
    jQuery('body').click(function(event){
      if(!event) event = window.event;
      var target =  event.target;
      if (jQuery(target).parents('#user_menu')[0] || target.id == "user_menu")
        return;
      for(var i = 0; i < that.submenu.length; i++)
      {
        that.hideSubmenu(that.submenu[i]);
      }
    });
  }
});

Limb.Class('newsBlock',
{
  __construct: function()
  {
    this.tabs_container = jQuery('#news');

    if (!this.tabs_container[0])
      return;

    this.tabs = [];
    this.tabs_content = [];

    this._findControls();
    this.activateTab(this.tabs[0].attr('class'));
  },
  _findControls: function()
  {
    var that = this;
    this.tabs_container.find("li").each(function(){
      var target = that.tabs_container.find('#' + this.className);
      if (target)
      {
        jQuery(this).bind("click", {that: that}, that.showTab);
        that.tabs.push(jQuery(this));
        that.tabs_content.push(target);
      }
    });
    return false;
  },

  showTab:function(event)
  {
    var that = event.data.that;
    that.activateTab(this.className)
    return false;
  },
  activateTab: function(className)
  {
    for(var i = 0; i < this.tabs.length; i++)
    {
      if(this.tabs[i].attr('class') != className)
      {
        this.tabs_content[i].hide();
        this.tabs[i].removeClass('active');
        this.tabs[i].addClass('not_active');
      }
      else
      {
        this.tabs_content[i].show();
        this.tabs[i].removeClass('not_active');
        this.tabs[i].addClass('active');
      }
    }
  }
});

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


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

  dropDownMenu = new dropDownMenu();
  try
  {
    jQuery("#registration_form, #login_form").jLook();
    if(logind_live==0)
    {
      jQuery('#attention_login').html(i18n['can_not_connect_to_server']).show();
      jQuery('#attention').html(i18n['can_not_connect_to_server']).show();
      /*return false;*/
    }
  }
  catch (err) {}
  try
  {
     jQuery("#friends_form").jLook();

  }
  catch (err) {}
  try
  {
     jQuery("#search_form").jLook();
     jQuery("#search_form .title").toggle(
       function () {
        jQuery(this).parent().addClass("collapse");
       },
       function () {
        jQuery(this).parent().removeClass("collapse");
       }
     );
  }
  catch (err) {}
});

