﻿/**
 * jQuery Fade Menu
 * 
 * Author: M.Rambod
 * Last Revision: 3/16/2010
 *
 **/
        
        var menuOpacity = 1;
        var shadowDiameter = 5;
        var loaderGifURL = "http://www.ikco.com/Base%20Icons/load-small-icon.gif"; //For Product Menu Loading Effect
        var currentRequest = "";
        
        //For Product Menu
        $(function() {
            $(".MenuDiv .CarsTitles a").mouseenter(function(e) {
                var parentDiv = $(this).parents(".MenuDiv");
                var thumbnailTD = $(parentDiv).find(".CarsThumbnail");
                var imageURL = $(this).attr("rel");
                var img = new Image();
                if ($(thumbnailTD).find("IMG").attr("src") != imageURL) {
                    currentRequest =  imageURL;
                    $(parentDiv).css({opacity:menuOpacity}).filter(":animated").stop();
                    $(thumbnailTD)
                        .html("")
                        .css({ background: "url(" + loaderGifURL + ") no-repeat center center" });
                    $(img).load(function() {
                        if ($(this).attr("src")==currentRequest)
                        {
                            $(this).hide();
                            $(thumbnailTD).css({ background: "" })
                             .html(this);
                            $(this).fadeIn();
                        }
                    });
                    $(img).error(function() {
                        $(thumbnailTD).css({ background: "" });
                    });
                    $(img).attr('src', imageURL);
                }
                e.stopPropagation();
            });
        });

        // For All Menus
        $(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 divLeft = pos.left - $(divname).outerWidth() + $(this).width();
                    var divTop = pos.top + 20;
                   
                    if (divLeft < 0) divLeft = 0;
                    // For Product Menu
                        $(divname).find(".CarsTitles a:first").trigger("mouseenter");
                    // End of Product Menu
                    $(divname)
                    .css({ left: divLeft, top: divTop })
                    .fadeTo(0, 0)
                    .animate({ opacity: menuOpacity }, "normal");
                    
                    //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("normal");
             $(".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; });
                }
            });
        });
