var TOOLS;
$(function()
{

    function serMenu(menu)
    {
        var self = this;

        this.flag = false;
        this.trigger = $('#trigger_'+menu);
        this.menu = $('#'+menu);
        this.to = 100;


        this.initHideFn = function()
        {
          self.flag=false;
          setTimeout(self.hideFn, self.to);
        };

        this.initShowFn = function()
        {
          self.flag = true;
        };

        this.hideFn = function()
        {
          if(!self.flag)
          {
            self.menu.fadeOut(0);
          }
        };

        this.showFn = function()
        {
          self.flag = true;
          self.menu.slideDown(0);
        };

        this.init = function()
        {
          this.trigger.mouseover(this.showFn).mouseout(this.initHideFn);
          this.menu.mouseover(this.initShowFn).mouseout(this.initHideFn);
          this.menu.css("width", this.trigger.parent('li').width());
          this.menu.css("left", this.trigger.parent('li').position().left);
        }

        this.init();
    }


    TOOLS = {
      setActiveFieldsetByInput: function(el, mode)
        {
          if(mode)
          {
            var tn = el.attr("tagName").toLowerCase();
            el.parents("fieldset").addClass("actived");
            if(el.attr("type")=="text" || tn=="textarea" || tn=="select")
               $(this).addClass("actived");
          }
          else{
            el.parents("fieldset").removeClass("actived");
            el.removeClass("actived");
          }
        },
        prepareErrors: function()
        {
            $(".f small").each(function(){
                el = $(this);
                el.text()!="" ? el.addClass("block") : el.removeClass("block");
            });
        }
    }


    if($.fn.tooltip)
    {
        $(".half img[title]").tooltip({
            tip: '#tooltip',
            offset: [-5, 50],
            position: 'top left',
            delay: 0,
            slideOffset: 0,
            slideInSpeed: 0,
            slideOutSpeed: 0
        });
        $("a[title]").tooltip({
            tip: '#tooltip',
            offset: [-5, 50],
            position: 'top left',
            delay: 0,
            slideOffset: 0,
            slideInSpeed: 0,
            slideOutSpeed: 0
        });
        $("img[alt='Pomoc']").tooltip({
            tip: '#tooltip',
            offset: [-5, 50],
            position: 'top left',
            delay: 0,
            slideOffset: 0,
            slideInSpeed: 0,
            slideOutSpeed: 0
        });
    }


    $(".error_globe, .ok_globe").wrap($("<div>").addClass("error_center"));
    $(".no-active").click(function(e){e.preventDefault();});

    if($(".top_nav .search").length)
    {
      var el = $(".top_nav .search");
      el.val(el.attr("title")).css("color", "#999");

      el.focus(function(){
        var el = $(this);
        if(el.val()==el.attr("title"))
           el.val('').css("color", "");
      }).blur(function(){
        var el = $(this);
        if(el.val()=="")
           el.val(el.attr("title")).css("color", "#999");
      });
    }

    $.each(["hm1", "hm2", "hm3"], function()
    {
      if($("#"+this).length && $("#trigger_"+this).length)
      {
        new serMenu(this);
      }
    });

});