/*
jQuery menu

Example:
$(document).ready(function()
{
    $('#jmenu').jmenu({animation:'fade',duration:100});
});

(c) 2010 Sawanna Team (http://sawanna.org)
*/

var jmenu={
    effect: 'fade',           /* default animation effect */
    duration: 400,         /* default duration */
    set: function (settings)
    {
       try
        {
            if (settings.animation == 'show') { this.effect='show'; }
            if (settings.animation == 'slide') { this.effect='slide'; }
            if (settings.animation == 'fade') { this.effect='fade'; }
        } catch (e) {}

        try
        {
            this.duration=settings.duration;
        } catch (e) {}
    },
    fix_pos:function(elem)
    {
        var offset = $(elem).offset();
        // ширина окна
        var winWidth = $(document).width();
        // ширина меню
        var menuWidth = $('#navmenu').width();
        // крайнелевая позиция за которую нельзя выходить
        var maxLeftWidth;
        maxLeftWidth = menuWidth + (winWidth-menuWidth)/2;
        // ширина блока меню
        var mWidth = 260;
        
        
        if ($(elem).children('div').length)
        {
          el_width = $(elem).children('div').width();
          if (offset.left + mWidth > maxLeftWidth)
          {
            el_left =  offset.left + mWidth - maxLeftWidth+6.5;
          }
          else
            el_left = 0;
         
          $(elem).children('div').eq(0).css({marginTop:0,marginLeft:-el_left});
        } else
        {
            $(elem).children('div').eq(0).css({'top':$(elem).offset().top+$(elem).height(),'left':$(elem).offset().left});
        }
    },
    show:function(elem)
    {
        if (this.effect=='fade') { $(elem).children('div').eq(0).stop(1,1).fadeIn(this.duration); }
        else if (this.effect=='slide') {$(elem).children('div').eq(0).stop(1,1).slideDown(this.duration); }
        else if (this.effect=='show') { $(elem).children('div').eq(0).stop(1,1).show(); }
    },
    hide: function(elem)
    {
      if (this.effect=='fade')
        $(elem).children('div').eq(0).stop(1,1).fadeOut(this.duration);
      else if (this.effect=='slide')
        $(elem).children('div').eq(0).stop(1,1).slideUp(this.duration);
      else if (this.effect=='show')
        $(elem).children('div').eq(0).stop(1,1).hide();
    }
}

jQuery.fn.jmenu=function(settings)
{
    jmenu.set(settings);

    $(this).find('li').each(function()
    {
      jmenu.fix_pos(this);
      $(this).hover(
          function()
          {
            jmenu.show(this);
          },
          function()
          {
            jmenu.hide(this);
          }
      );
    });
}
