// Copyright (c) 2009 - 2010 Erik van den Berg (http://www.planitworks.nl/jeegoocontext)
// Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
// and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
//
// Contributors:
// Denis Evteev
// Roman Imankulov (www.netangels.ru)
//
// Version: 1.3
// Requires jQuery 1.3.2+
(function(b){var d;var h;var f=function(i,j){return{width:i-b(window).width()-b(window).scrollLeft(),height:j-b(window).height()-b(window).scrollTop()}};var c=function(k){if(h[d.activeId].currentHover){var i=k?h[d.activeId].currentHover.nextAll(":not(."+h[d.activeId].separatorClass+"):visible:first"):h[d.activeId].currentHover.prevAll(":not(."+h[d.activeId].separatorClass+"):visible:first");if(i.length==0){i=h[d.activeId].currentHover.parent().find("> li:visible");i=(k?b(i[0]):b(i[i.length-1]))}i.mouseover()}else{var l=b("#"+d.activeId+", #"+d.activeId+" ul").filter(function(){return(b(this).is(":visible")&&b(this).parents(":hidden").length==0)});if(l.length>0){var j=b(l[l.length-1]).find("> li:visible");b(j[(k?0:(j.length-1))]).mouseover()}}};var e=function(){for(cm in h){b(h[cm].allContext).removeClass(d.activeClass)}};var a=function(){if(d.activeId){b("#"+d.activeId).add("#"+d.activeId+" ul").hide()}clearInterval(d.keyUpDown);d.keyUpDownStop=false;if(h[d.activeId]){h[d.activeId].currentHover=null}d.activeId=null;b(document).unbind(".jeegoocontext");b(window).unbind("resize.jeegoocontext")};var g=function(i){if(d.activeId&&h[d.activeId].onHide){if(h[d.activeId].onHide.apply(b("#"+d.activeId),[i,h[d.activeId].context])==false){return false}}e();a()};b.fn.jeegoocontext=function(m,l){if(!d){d={}}if(!h){h={}}if(l&&l.menuClass){d.menuClass=l.menuClass}if(!d.menuClass){d.menuClass="jeegoocontext"}if(l&&l.activeClass){d.activeClass=l.activeClass}if(!d.activeClass){d.activeClass="active"}h[m]=b.extend({hoverClass:"hover",submenuClass:"submenu",separatorClass:"separator",operaEvent:"ctrl+click",fadeIn:200,delay:300,keyDelay:100,widthOverflowOffset:0,heightOverflowOffset:0,submenuLeftOffset:0,submenuTopOffset:0,autoAddSubmenuArrows:true,startLeftOffset:0,startTopOffset:0,keyboard:true},l||{});h[m].allContext=this.selector;b("#"+m).find("li")[h[m].livequery?"expire":"unbind"](".jeegoocontext")[h[m].livequery?"livequery":"bind"]("mouseover.jeegoocontext",function(s){var v=h[m].currentHover=b(this);clearTimeout(h[m].show);clearTimeout(h[m].hide);b("#"+m).find("*").removeClass(h[m].hoverClass);var n=v.parents("li");v.add(v.find("> *")).add(n).add(n.find("> *")).addClass(h[m].hoverClass);var o=true;if(h[m].onHover){if(h[m].onHover.apply(this,[s,h[m].context])==false){o=false}}if(!h[m].proceed){h[m].show=setTimeout(function(){h[m].proceed=true;v.mouseover()},h[m].delay);return false}h[m].proceed=false;v.parent().find("ul").not(v.find("> ul")).hide();if(!o){s.preventDefault();return false}var r=v.find("> ul");if(r.length!=0){var u=v.offset();var p=f((u.left+v.parent().width()+h[m].submenuLeftOffset+r.width()+h[m].widthOverflowOffset),(u.top+h[m].submenuTopOffset+r.height()+h[m].heightOverflowOffset));var t=r.parent().parent().width();var q=u.top-v.parent().offset().top;r.css({left:(p.width>0&&!h[m].ignoreWidthOverflow)?(-t-h[m].submenuLeftOffset+"px"):(t+h[m].submenuLeftOffset+"px"),top:(p.height>0&&!h[m].ignoreHeightOverflow)?(q-p.height+h[m].submenuTopOffset)+"px":q+h[m].submenuTopOffset+"px"});r.fadeIn(h[m].fadeIn)}s.stopPropagation()})[h[m].livequery?"livequery":"bind"]("click.jeegoocontext",function(n){if(h[m].onSelect){if(h[m].onSelect.apply(this,[n,h[m].context])==false){return false}}a();b(h[m].context).removeClass(d.activeClass);n.stopPropagation()});var i=document.createElement("div");i.setAttribute("oncontextmenu","");var k=h[m].event;if(!k){k=(typeof i.oncontextmenu!="undefined")?"contextmenu.jeegoocontext":h[m].operaEvent+".jeegoocontext"}else{k+=".jeegoocontext"}if(k.indexOf("+")!=-1){var j=k.split("+",2);h[m].modifier=j[0]+"Key";k=j[1]}return this[h[m].livequery?"livequery":"bind"](k,function(q){if(typeof h[m].modifier=="string"&&!q[h[m].modifier]){return}h[m].context=this;var o=b("#"+m);var n,s;if(h[m].openBelowContext){var r=b(this).offset();n=r.left;s=r.top+b(this).outerHeight()}else{n=q.pageX;s=q.pageY}n+=h[m].startLeftOffset;s+=h[m].startTopOffset;var p=f((n+o.width()+h[m].widthOverflowOffset),(s+o.height()+h[m].heightOverflowOffset));if(!h[m].ignoreWidthOverflow&&p.width>0){n-=p.width}if(!h[m].openBelowContext&&!h[m].ignoreHeightOverflow&&p.height>0){s-=p.height}if(h[m].onShow){if(h[m].onShow.apply(o,[q,h[m].context,n,s])==false){return false}}a();d.activeId=m;b("#"+d.activeId).add("#"+d.activeId+" ul").hide();e();b(h[m].context).addClass(d.activeClass);o.find("li, li > *").removeClass(h[m].hoverClass);if(h[m].autoAddSubmenuArrows){o.find("li:has(ul)").not(":has(span."+h[m].submenuClass+")").prepend('<span class="'+h[m].submenuClass+'"></span>');o.find("li").not(":has(ul)").find("> span."+h[m].submenuClass).remove()}o.css({left:n+"px",top:s+"px"}).fadeIn(h[m].fadeIn);if(h[m].openBelowContext){b(window).bind("resize.jeegoocontext",function(){b("#"+m).css("left",b(h[m].context).offset().left+h[m].startLeftOffset+"px")})}b(document).bind("mouseover.jeegoocontext",function(u){if(b(u.relatedTarget).parents("#"+m).length>0){clearTimeout(h[m].show);var t=b(u.relatedTarget).parent().find("li");t.add(t.find("> *")).removeClass(h[m].hoverClass);h[d.activeId].currentHover=null;h[m].hide=setTimeout(function(){t.find("ul").hide();if(h[m].autoHide){g(u)}},h[m].delay)}}).bind("click.jeegoocontext",g);if(h[m].keyboard){b(document).bind("keydown.jeegoocontext",function(u){switch(u.which){case 38:if(d.keyUpDownStop){return false}c();d.keyUpDown=setInterval(c,h[d.activeId].keyDelay);d.keyUpDownStop=true;return false;case 39:if(h[d.activeId].currentHover){h[d.activeId].currentHover.find("ul:visible:first li:visible:first").mouseover()}else{var v=b("#"+d.activeId+", #"+d.activeId+" ul:visible");if(v.length>0){b(v[v.length-1]).find(":visible:first").mouseover()}}return false;case 40:if(d.keyUpDownStop){return false}c(true);d.keyUpDown=setInterval(function(){c(true)},h[d.activeId].keyDelay);d.keyUpDownStop=true;return false;case 37:if(h[d.activeId].currentHover){b(h[d.activeId].currentHover.parents("li")[0]).mouseover()}else{var t=b("#"+d.activeId+" li."+h[d.activeId].hoverClass);if(t.length>0){b(t[t.length-1]).mouseover()}}return false;case 13:if(h[d.activeId].currentHover){h[d.activeId].currentHover.click()}else{g(u)}break;case 27:g(u);break;default:break}}).bind("keyup.jeegoocontext",function(t){clearInterval(d.keyUpDown);d.keyUpDownStop=false})}return false})};b.fn.nojeegoocontext=function(){this.unbind(".jeegoocontext")}})(jQuery);
