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