(function($){

/*
*       li1  |  li1 |  li1
*       ------------------
*         li2 |
*         li2 | li3, li3, li3
*         li2 |
*
*/
    function CheckPhones()
    {
        try
        {
            var agent = navigator.userAgent.toLowerCase();

            if((agent.indexOf("series60") != -1) || (agent.indexOf("symbian") != -1) || (agent.indexOf("windows ce") != -1) || (agent.indexOf("blackberry") != -1))
                return true;
            if(typeof orientation != 'undefined')   // If the screen orientation is defined we are in a modern mobile OS
                return true;  
            if('ontouchstart' in document.documentElement) // If touch events are defined we are in a modern touch screen OS
                return true;
            if((navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPad") != -1))
                return true; 
            if(agent.indexOf("android") != -1) // If the user agent string contains "android" then it's Android
                return true;
            if(agent.indexOf("opera mobi") != -1) // If the user agent string contains "android" then it's Android
                return true;   
        }
        catch(e){}   
        return false;
    }
    
    function CheckIE7()
    {
        var rv = -1;
        if (navigator.appName == 'Microsoft Internet Explorer')
        {
            var ua = navigator.userAgent;
            var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null)
                rv = parseInt( RegExp.$1 );
        }  
        return ( rv == 7 ); 
    }

    window.HoverMenu = function(selector){ 
        var isTouchDevice = CheckPhones();
        var speed = 1.2;
        var menu = $(selector);
        var li1 = menu.children("li");
        
        try
        {
            var menu2 = li1.children("div:not(.grafic)");
            var li2 = menu2.find("ul").children("li");
            menu.append($("<div id=\"dim\" style=\"position: absolute; top: -1400px;\"></div>"));
            var dim = $("#dim");
            menu.find("li > div > ul > li > div > ul").each(function(){
                var maxWidth = 0;
                var submenu = $(this);
                submenu.find("a").each(function(){
                    var anchor = $(this);
                    dim.html(anchor.html());
                    var width = dim.width();
                    if(width > maxWidth)
                        maxWidth = width; 
                });
                maxWidth *= 1.02;
                submenu.children("li").width(maxWidth);
                var padding = parseInt(submenu.children("li").css("paddingLeft")) + parseInt(submenu.children("li").css("paddingRight")) + parseInt(submenu.css("marginRight")) + parseInt(submenu.css("marginLeft"));
                submenu.parent("div").css({"width" : maxWidth + padding + "px"});                   
            }); 
            
            
            if(CheckIE7())
            {
                menu.children("li").each(function(){
                    var submenu = $(this).children("div");
                    var ul = submenu.children("ul");
                    var maxWidth = 0;
                    ul.children("li").each(function(){
                        var li = $(this);
                        dim.html(li.children("a").html());
                        var width = dim.width();
                        if(width > maxWidth)
                            maxWidth = width; 
                    });                                      
                    maxWidth *= 1.02;
                    var padding = parseInt(ul.children("li").css("paddingLeft")) + parseInt(ul.children("li").css("paddingRight")) + parseInt(ul.children("li").children("a").css("textIndent"));
                    ul.width(maxWidth + padding + 15);               
                }); 
            }
            
                    
            dim.remove();  
        }
        catch(e){}
        
        
        function hideOtherMenusLevel1()
        {          
            var li = $("li.hover.level1");
            var div = li.children("div.submenu1"); 
                            
            div.stop();
            var h = div.height();
            div.css({"zIndex" : 40, "overflow" : "visible"})
                .animate({"height" : 0}, speed * h, 
                    function()
                    {
                        $(this).css({"display" : "none"}) 
                        li.removeClass("hover level1");  
                    });   
        }
        
        function hideOtherMenusLevel2()
        {          
            var li = $("li.hover.level2");
            var div = li.children("div.submenu2"); 
                            
            div.stop();
            var width = div.width();
            div.css({"zIndex" : 40})
                .animate({"width" : 0}, speed * width, 
                    function(){
                        $(this).css({"display" : "none"}); 
                        li.removeClass("hover level2");  
                    }); 
        }
        
        li1.each(function()
        {                   
            var li = $(this);
            var div = li.children("div.submenu1");

            if(div.length == 0)
                return true;
                
            var height = div.height();
            div.height(0);
            div.css({"overflow" : "visible", "display" : "none"});
            
            function showSubMenu()
            {   
                isOut = false;  
                li.addClass("hover level1");            
                div.stop();
                var h = div.height();
                div.css({"display" : "block", "zIndex" : 60, "overflow" : "visible"})
                    .animate({"height" : height}, speed * (height - h),
                        function()
                        {
                            $(this).css({"overflow" : "visible"});
                        }
                    ); 
            }
            
            function hideSubMenu()
            {                       
                div.stop();
                var h = div.height();
                div.css({"zIndex" : 40, "overflow" : "visible"})
                    .animate({"height" : 0}, speed * h, 
                        function()
                        {
                            $(this).css({"display" : "none"}) 
                            li.removeClass("hover");  
                        }); 
            }
            
            if(!isTouchDevice)
                li.hover(showSubMenu, hideSubMenu);
            else
            {
                li.children("a").click(function(){
                    if($(this).is(".hover > *"))
                    {
                        try
                        {
                            hideSubMenu();
                        }
                        catch(e)
                        {}
                        return true;
                    }
                    else
                    {
                        try
                        {
                            hideOtherMenusLevel1();
                            hideOtherMenusLevel2();
                        }
                        catch(e)
                        {}
                        showSubMenu();             
                        return false;
                    }    
                });
            }         
        });
        
        try{
        li2.each(function()
        {                
            var li = $(this); 
            var div = li.children("div.submenu2");
            if(div.length == 0)
                return true;
            var divParent = li.parent("ul").parent("div");  
            divParent.css({"display" : "block", "visibility" : "hidden"});  
            div.css({"display" : "block", "visibility" : "hidden"});
            
            var width = div.width();    
                               
            div.css({"display" : "none", "visibility" : "visible"});
            divParent.css({"display" : "none", "visibility" : "visible"});  
            
            div.width(0);
            
            function showSubMenu()
            {  
                li.addClass("hover level2");              
                div.stop(); 
                div.css({"display" : "block", "zIndex" : 60})
                   .animate({"width" : width}, width * speed); 
            }
            
            function hideSubMenu(e)
            {
                div.stop();
                div.css({"zIndex" : 40})
                   .animate({"width" : 0}, speed * width, 
                        function(){
                            $(this).css({"display" : "none"}); 
                            li.removeClass("hover level2");  
                        }); 
            } 
                          
            if(!isTouchDevice)
                li.hover(showSubMenu,hideSubMenu);
            else
            {
                li.children("a").click(function(){
                    if($(this).is(".hover > *"))
                    {
                        hideSubMenu();
                        return true;
                    }
                    else
                    {
                        hideOtherMenusLevel2();
                        showSubMenu();             
                        return false;
                    }  
                      
                });
            } 
        });    
        }
        catch(e)
        {}     
    };
    
})(jQuery, window);

$(document).ready(function(){
    window.HoverMenu("#hovermenu");
});

