$("document").ready(function(){
    path = 'images/menu/';
    rotation_path = 'images/rotation/';
    awards_path= 'images/awards/';
    ext = '.gif';
    hover_prefix = '_hover';
    img = Array('about_lnk','montag_lnk','proekt_lnk','teplo_lnk','auto_lnk','poverka_lnk');

  	$("#img-awards img").not(".awards-active-img").hide();
    $("#img-rotation img").not(".rotation-active-img").hide();

    setInterval( "slideSwitchRotation()", 5000 );
    setInterval( "slideSwitchAwards()", 5000 );

    $(".main-menu").hover(function(){
        var id = $(this).find("img:first").attr("id");
        $(this).find("img").attr("src",path+img[id]+hover_prefix+ext);
    },
    function(){
               var id = $(this).find("img:first").attr("id");
               $(this).find("img").attr("src",path+img[id]+ext);
    });

    $(".main-menu, .left-menu, .catalog-menu").click(function(){

        $('.left-menu').css('color','black');
        $(this).css('color','#1E3964');

        var data = $(this).attr("href").split('?');

        $.ajax({
            type: 'GET',
            cache: false,
            url: 'content.php',
            data: data[1]+'&ajax=1',
            dataType: 'json',
            beforeSend: function(){
              $("#right-column").html('<img src="images/upload.gif">');
            },
            success: function(html){
              $("#right-column").empty().append(html.content);
              $("#title").empty().append(html.title);
            }
        });

       return false;

    });

    $("#right-column").ajaxStop(function(){
        $("ul.tabs li:first").addClass("active").show();
        $(".tab_content").hide();
        $(".tab_content:first").show();
        $(".tab_content .tab_content:first").show();

        if($(".tab_content .tab_content").length != 0){
            $(".tab_container .tab_content ul.tabs li:first").addClass("active");
        }


    });

    $(".slider-lnk").live('click', function(){
        $(this).find(".slider-content").slideToggle();
        return false;
    });

    $("ul.tabs li").live('click', function(){

        var tabs_id = $(this).find("a").attr("href");

        tabs_id = tabs_id.split('#');

        if(tabs_id[1].substr(0,3) != 'sub'){
            $("ul.tabs li").removeClass("active"); //Remove any "active" class
        }
        else{
             $(".tab_container .tab_content ul.tabs li").removeClass("active"); //Remove any "active" class
        }

        $(this).addClass("active"); //Add "active" class to selected tab

        if(tabs_id[1].substr(0,3) != 'sub'){
            $(".tab_content").hide(); //Hide all tab content
        }

        if($(".tab_content .tab_content").length != 0 && tabs_id[1].substr(0,3) != 'sub'){
            $(".tab_container .tab_content ul.tabs li:first").addClass("active");
            $(".tab_content .tab_content:first").show();
        }

        if(tabs_id[1].substr(0,3) != 'sub'){
            $(".tab_content").fadeOut('fast');
        }
        else{
            $(".tab_content .tab_content").fadeOut('fast');
        }
        $('#'+tabs_id[1]).fadeIn('fast');

        return false;
    });

    $("#map").click(function(){
      var img = $(this).attr("href");
      $("body").append('<div id="show-map"><div id="close-map">закрыть</div><img src="'+img+'" /></div>');
      $("#show-map").fadeIn('fast');
      return false;
    });
    $("body, #close-map").click(function(){
      $("#show-map").fadeOut('fast');
    })
});

    function slideSwitchRotation() {

      var active = $('#rotation #img-rotation img.rotation-active-img');

      if ( active.length == 0 ) active = $('#rotation #img-rotation').find('img:last');

      var next =  active.next().length ? active.next() : $('#rotation #img-rotation').find('img:first');

      active.fadeOut('slow', function(){
        active.removeClass('rotation-active-img');
        next.addClass('rotation-active-img').fadeIn('slow');

      });
    }

    function slideSwitchAwards(){
      var active = $('#awards #img-awards img.awards-active-img');

      if ( active.length == 0 ) active = $('#awards #img-awards img:last');

      var next =  active.next().length ? active.next() : $('#awards #img-awards img:first');

      active.fadeOut('slow', function(){
        active.removeClass('awards-active-img last-active');
        next.addClass('awards-active-img').fadeIn('slow');
      });

 }




