jQuery.fn.jetmenu = function(options){
var settings = {
indicator :true // indicator that indicates a submenu
,speed :300 // submenu speed
,delay :0 // submenu show delay
,hideClickOut :true // hide submenus when click outside menu
,align :"left" // menu alignment (left/right)
,submenuTrigger :"hover" // defines if submenu appears after hover/click
}
$.extend( settings, options );
var menu = $(".jetmenu");
$(menu).prepend("
MENU");
if(settings.indicator == true){
$(menu).find("a").each(function(){
if($(this).siblings(".dropdown, .megamenu").length > 0){
$(this).append("+");
}
});
}
start();
$(window).resize(function() {
if(settings.align == "right"){
fixSubmenuRight();
}
else{
fixSubmenuLeft();
}
});
function start(){
bindHover();
if(settings.align == "right"){
rightAlignMenu();
}
else{
fixSubmenuLeft();
}
}
function bindHover(){
if (navigator.userAgent.match(/Mobi/i) || window.navigator.msMaxTouchPoints > 0 || settings.submenuTrigger == "click"){
$(menu).find("a").on("click touchstart", function(e){
e.stopPropagation();
e.preventDefault();
$(this).parent("li").siblings("li").find(".dropdown, .megamenu").stop(true, true).fadeOut(settings.speed);
if($(this).siblings(".dropdown, .megamenu").css("display") == "none"){
$(this).siblings(".dropdown, .megamenu").stop(true, true).delay(settings.delay).fadeIn(settings.speed);
return false;
}
else{
$(this).siblings(".dropdown, .megamenu").stop(true, true).fadeOut(settings.speed);
$(this).siblings(".dropdown").find(".dropdown").stop(true, true).fadeOut(settings.speed);
}
window.location.href = $(this).attr("href");
});
$(menu).find("li").bind("mouseleave", function(){
$(this).children(".dropdown, .megamenu").stop(true, true).fadeOut(settings.speed);
});
if(settings.hideClickOut == true){
$(document).bind("click.menu touchstart.menu", function(ev){
if($(ev.target).closest(menu).length == 0){
$(menu).find(".dropdown, .megamenu").fadeOut(settings.speed);
}
});
}
}
else{
$(menu).find("li").bind("mouseenter", function(){
$(this).children(".dropdown, .megamenu").stop(true, true).delay(settings.delay).fadeIn(settings.speed);
}).bind("mouseleave", function(){
$(this).children(".dropdown, .megamenu").stop(true, true).fadeOut(settings.speed);
});
}
}
function rightAlignMenu(){
$(menu).children("li").addClass("jsright");
var items = $(menu).children("li");
$(menu).children("li:not(.showhide)").detach();
for(var i = items.length; i >= 1; i--){
$(menu).append(items[i]);
}
fixSubmenuRight();
}
function fixSubmenuRight(){
$(menu).children("li").removeClass("last");
var items = $(menu).children("li");
for(var i = 1; i <= items.length; i++){
if($(items[i]).children(".dropdown, .megamenu").length > 0){
var lastItemsWidth = 0;
for(var y = 1; y <= i; y++){
lastItemsWidth = lastItemsWidth + $(items[y]).outerWidth();
}
if($(items[i]).children(".dropdown, .megamenu").outerWidth() > lastItemsWidth){
$(items[i]).addClass("last");
}
}
}
}
function fixSubmenuLeft(){
$(menu).children("li").removeClass("fix-sub");
var items = $(menu).children("li");
var menuWidth = $(menu).outerWidth();
var itemsWidth = 0;
for(var i = 1; i <= items.length; i++){
if($(items[i]).children(".dropdown, .megamenu").length > 0){
if($(items[i]).position().left + $(items[i]).children(".dropdown, .megamenu").outerWidth() > menuWidth){
$(items[i]).addClass("fix-sub");
}
}
}
}
}