$(document).ready(function() {
    /*********************************************/
    /*                                           */
    /*               THE MENU                    */
    /*                                           */
    /*********************************************/

    $(".first").hover(function(){
        //Dissapear the elements hovered before
        $(".first").each(function(){
            $(this).removeClass("hover");
        });
        $(".first").each(function(){
            $(this).addClass("link");
        });
        $(".second").each(function(){
            $(this).removeClass("hover");
        });
        $(".second").each(function(){
            $(this).addClass("link");
        });
        $(".third").each(function(){
            $(this).removeClass("hover");
        });
        $(".third").each(function(){
            $(this).addClass("link");
        });
        $(".firstHorizontal").each(function(){
            $(this).addClass("hidden");
        });
        $(".secondHorizontal").each(function(){
            $(this).addClass("hidden");
        });
        $(".secondLevel").each(function(){
            $(this).addClass("hidden");
        });
        $(".thirdLevel").each(function(){
            $(this).addClass("hidden");
        });
        $(".firstHelp").each(function(){
            $(this).removeClass("hidden");
        });
        $(".secondHelp").each(function(){
            $(this).removeClass("hidden");
        });

        $(this).removeClass("link");
        $(this).addClass("hover");
        var menu_id=$(this).attr("menu_id");

        //if there is a second level menu then appear it
        if($("#secondLevelParent"+menu_id).length>0){
            $(".firstHelp").each(function(){
                $(this).addClass("hidden");
            });

            $("#firstHorizontal"+menu_id).removeClass("hidden");
            $("#secondLevelParent"+menu_id).removeClass("hidden");
            //            $(".secondHelp").each(function(){
            //                $(this).addClass("hidden");
            //            });
            var shown=false;

            $("#secondLevelParent"+menu_id+" a.second").each(function(){
                var menu_id2=$(this).attr("menu_id");
                if($("#thirdLevelParent"+menu_id2).length>0 && !shown){
                    shown=true;
                    $(".secondHelp").each(function(){
                        $(this).addClass("hidden");
                    });
                    $("#secondHorizontal"+menu_id2).removeClass("hidden");
                    $("#thirdLevelParent"+menu_id2).removeClass("hidden");
                }
            });
        //            $("#secondHorizontal10").removeClass("hidden");
        //            $("#thirdLevelParent10").removeClass("hidden");
        }        
    });



    $(".second").hover(function(){
        //Dissapear the elements hovered before
        $(".second").each(function(){
            $(this).removeClass("hover");
        });
        $(".second").each(function(){
            $(this).addClass("link");
        });
        //88888888888888888888888888888888
//        $(".secondHorizontal").each(function(){
//            $(this).addClass("hidden");
//        });
//        $(".third").each(function(){
//            $(this).removeClass("hover");
//        });
//        $(".third").each(function(){
//            $(this).addClass("link");
//        });
//        $(".thirdLevel").each(function(){
//            $(this).addClass("hidden");
//        });
//        $(".secondHelp").each(function(){
//            $(this).removeClass("hidden");
//        });
        //88888888888888888888888888888
        $(this).removeClass("link");
        $(this).addClass("hover");
        var menu_id=$(this).attr("menu_id");

        //if there is a second level menu then appear it
        if($("#thirdLevelParent"+menu_id).length>0){
            $(".secondHelp").each(function(){
                $(this).addClass("hidden");
            });
            $("#secondHorizontal"+menu_id).removeClass("hidden");
            $("#thirdLevelParent"+menu_id).removeClass("hidden");
        }
    });

    $(".third").hover(function(){
        //Dissapear the elements hovered before
        $(".third").each(function(){
            $(this).removeClass("hover");
        });
        $(".third").each(function(){
            $(this).addClass("link");
        });

        $(this).removeClass("link");
        $(this).addClass("hover");
    });

    $(".headerContents").mouseleave(function(){

        //Dissapear the elements hovered before
        $(".first").each(function(){
            $(this).removeClass("hover");
        });
        $(".first").each(function(){
            $(this).addClass("link");
        });
        $(".second").each(function(){
            $(this).removeClass("hover");
        });
        $(".second").each(function(){
            $(this).addClass("link");
        });
        $(".third").each(function(){
            $(this).removeClass("hover");
        });
        $(".third").each(function(){
            $(this).addClass("link");
        });

        $(".firstHorizontal").each(function(){
            $(this).addClass("hidden");
        });
        $(".secondHorizontal").each(function(){
            $(this).addClass("hidden");
        });

        $(".secondLevel").each(function(){
            $(this).addClass("hidden");
        });
        $(".thirdLevel").each(function(){
            $(this).addClass("hidden");
        });

        $(".firstHelp").each(function(){
            $(this).removeClass("hidden");
        });
        $(".secondHelp").each(function(){
            $(this).removeClass("hidden");
        });
    });

    $(".thirdLevel").mouseleave(function(){
        //
        //        //Dissapear the elements hovered before
        //        $(".second").each(function(){
        //            $(this).removeClass("hover");
        //        });
        //        $(".second").each(function(){
        //            $(this).addClass("link");
        //        });
        $(".third").each(function(){
            $(this).removeClass("hover");
        });
        $(".third").each(function(){
            $(this).addClass("link");
        });
    //
    //        $(".secondHorizontal").each(function(){
    //            $(this).addClass("hidden");
    //        });
    //
    //        $(".thirdLevel").each(function(){
    //            $(this).addClass("hidden");
    //        });
    //
    //        $(".secondHelp").each(function(){
    //            $(this).removeClass("hidden");
    //        });
    });

});
