﻿/**
* jQuery Fade Menu
* 
* Author: M.Rambod
* Last Revision: 3/16/2010
*
**/

var menuOpacity = 1;
var shadowDiameter = 5;
var currentRequest = "";


$(function() {
    var timerID = 0;
    $(".MenuDiv").hide().disableTextSelect();
    $(".MenuHref").disableTextSelect();

    $(".MenuHref").mouseenter(function(e) {
        var divname = "#" + $(this).attr("rel");
        if ($(divname).is(":hidden")) {
            HideAllMenus(true);
            var pos = $(this).offset();
            var rev = $(this).attr("rev");
            var divLeft;
            if (rev == "rtl") {
                divLeft = pos.left - $(divname).outerWidth() + $(this).width();

            } else if (rev == "ltr") {
                divLeft = pos.left;

            } else {
                divLeft = pos.left - $(divname).outerWidth() + $(this).width();

            }
            var divTop = pos.top + 20;

            if (divLeft < 0) divLeft = 0;

            $(divname)
                    .css({ left: divLeft, top: divTop })
                    .fadeTo(0, 0)
                    .animate({ opacity: menuOpacity }, "fast");

            //Creating Shadow
//            var shadowDiv = $("<div class='MenuShadow'/>");

//            $(shadowDiv)
//                    .css({
//                        top: divTop + shadowDiameter,
//                        left: divLeft + shadowDiameter,
//                        width: $(divname).width() + "px",
//                        height: $(divname).height() + "px"
//                    })
//                    .insertAfter(divname)
//                    .fadeTo(0, 0)
//                    .animate({ opacity: 0.2 }, "normal");
            //End of Shadow
        }
    });

    $(".MenuHref").click(function(e) {
        $(this).trigger("mouseenter");
        e.stopPropagation();
    });

    $(".MenuDiv").add($(".MenuHref")).mouseenter(function(e) {
        clearTimeout(timerID);
    });

    $(".MenuDiv").add($(".MenuHref")).mouseleave(function(e) {
        timerID = setTimeout("HideAllMenus()", 2000);
    });

    $(".MenuDiv").click(function(e) {
        e.stopPropagation();
    });

    $(document).click(function(e) {
        HideAllMenus();
    });

});

// Hiding All Active Menus
function HideAllMenus(Quick) {
    if (!Quick) {
        $(".MenuDiv").fadeOut("fast");
        $(".MenuShadow").fadeOut("fast", function() { $(this).remove(); });
    } else {
        $(".MenuDiv").hide()
        $(".MenuShadow").hide().remove();
    }
}

//Shadow Resizing Extension
$.extend($.fn.resizeShadow = function() {
    if ($(this).is(".MenuDiv")) {
        $(".MenuShadow").css({
            "width": $(this).width() + "px",
            "height": $(this).height() + "px"
        });
    }
});

// Not Selectable Text Extension
$.extend($.fn.disableTextSelect = function() {
    return this.each(function() {
        if ($.browser.mozilla) {//Firefox
            $(this).css('MozUserSelect', 'none');
        } else if ($.browser.msie) {//IE
            $(this).bind('selectstart', function() { return false; });
        } else {//Opera, etc.
            $(this).mousedown(function() { return false; });
        }
    });
});

