$(document).ready(function(){
    //--menu-----------------------------------------------------
    $('.menu').hover(
        function(){},
        function(){
           $(".side_menu").stop(true,true);
           $(".side_menu").fadeOut("fast");
        }
    );
    //-----------------------------------------------------------

    //--menu element---------------------------------------------
    $('.menu .menu_element').click(
        function(){
            var href;
            if($(this).attr('href')) href = $(this).attr('href'); else href = 0;
            if(href == 'submenu'){
               $(this).next(".submenu").stop(true, true);
               $(this).next(".submenu").slideToggle(350);
               return false;
            }
            else
            {
               $(".content").html('<table style="font-size:11px;" border=0 cellspacing=0 cellpadding=0><tr><td><img width=18 src="images/ajax-loader.gif" /></td><td style="padding-bottom:4px;">&nbsp;Notiek ielāde...</td></tr></table>');
               var rand=Math.floor(Math.random()*1111);
               $.get("async.php", { page: href, rand: rand, lang: lang }, function(data){
                  $(".content").html(data);
               });
            }
    });

    $('.menu .menu_element').hover(
        function(){
            $(this).removeClass("menu_element");
            $(this).addClass("menu_element_active");
            $(".side_menu").fadeOut("normal");
        },
        function(){
            $(this).removeClass("menu_element_active");
            $(this).addClass("menu_element");
            $(".side_menu").fadeOut("normal");

        }
    );
    //-----------------------------------------------------------

    //--submenu element------------------------------------------
    $('.menu .submenu .submenu_element').hover(
          function(){
            $(".side_menu").stop(true,true);
            $(".side_menu").fadeOut("1");
            $(".submenu_element").css('color','black');
            $(this).css('color','gray');
            var side_menu_top = $(this).position().top;
            var side_menu_left = $(this).position().left;
            var submenu_width = $(this).width();
            var side_menu_padding = $(this).css('paddingLeft');
            var real_padding = parseInt(side_menu_padding.substring(0, side_menu_padding.length-2));
            var side_menu_pos_x = side_menu_left + submenu_width + real_padding+1;
            $(this).next(".side_menu").animate({top:side_menu_top, left:side_menu_pos_x},1);
            $(this).next(".side_menu").slideDown(100);

          },
          function(e){
            $(this).css('color','black');
            //$(this).next(".side_menu").fadeOut("fast");

          }
    );

    $('.menu .submenu .submenu_element').click(
          function(){
            //window.location=$(this).attr('href');
            //$(".content").load("async.php");
            var href;
            if($(this).attr('href')) href = $(this).attr('href'); else href = 0;
            if(href != 'submenu'){
               $(".content").html('<table style="font-size:11px;" border=0 cellspacing=0 cellpadding=0><tr><td><img width=18 src="images/ajax-loader.gif" /></td><td style="padding-bottom:4px;">&nbsp;Notiek ielāde...</td></tr></table>');
               $.get("async.php", { page: href, lang: lang }, function(data){
                  $(".content").html(data);
               });
            }
          }
    );
    //-----------------------------------------------------------

    //--side menu------------------------------------------------
    $('.side_menu').hover(
          function(e) {
            $(this).prev(".submenu_element").css('color','white');
            $(this).stop(true,true);
            $(this).fadeIn(1);
          },
          function() {
            $(this).prev(".submenu_element").css('color','black');
            $(this).fadeOut("normal");
          }
    );
    //-----------------------------------------------------------

    //--side menu element----------------------------------------
    $('.side_menu_element').hover(
           function() {
             $(this).css('color','white');
           },
           function() {
             $(this).css('color','black');
           }
    );

    $('.side_menu_element').click(
        function(){
            $(".side_menu").fadeOut('slow');
            $(".content").html($(this).html());
        }
    );
});