(function($){
 var openShema = function(){
   close($("dl.portfolio dt.p-active"), function(){
     $("a#shema-link").addClass("opened");
     $("div.hr").animate({"height": "550px"}, function(){$("dl.shema").animate({"height": "530px"}, function(){$("dl.shema div.h-img").animate({"opacity":"1"})})});      
 }); 
 }

 var closeShema = function(){
  $("dl.shema div.h-img").animate({"opacity":"0"}, function(){$("dl.shema").animate({"height": "0px"}, function(){$("div.hr").animate({"height": "250px"})})});
  $("a#shema-link").removeClass("opened");
 }

 var openHomeShema = function(){
 $("a#shema-link").addClass("opened");
 $("#home dl.shema").animate({"height": "530px"}, function(){
   $("dl.shema div.h-img").animate({"opacity":"1"})
   });
 }

 var closeHomeShema = function(){
   $("dl.shema div.h-img").animate({"opacity":"0"}, function(){
       $("dl.shema").animate({"height": "0px"}, function(){})
       });
   $("a#shema-link").removeClass("opened");
 }

var close = function(t, callback){
  if(t.length){
  t.next("dd").find("div.h-fixed-width").animate({"opacity":"0"}, 300, function(){
      $(this).parents("dd.p-active").removeClass("p-active").slideUp(1000, callback);
    });
  t.removeClass("p-active");
  $("u", t).html("развернуть"); 
  }
  else {
    if(callback){
      callback();
    }  
  }
};

var open = function(t){
  closeShema();
  t.next("dd").addClass("p-active").slideDown(1000, function(){
    $(this).find("div.h-fixed-width").animate({"opacity":"1"}, 300);
    });
  t.addClass("p-active");
  $("u", t).html("свернуть");        
};


  $(function(){

    $("ul.service li, ul.sub-nav div.h-fixed-width").bind("click", function(){
        document.location.href = $(this).find("a").attr("href");
      });
    $("a#shema-link").bind("click", 
      function(){
      if($(this).hasClass("opened")){
        if($("body#home").length > 0){
          closeHomeShema();
        }
        else{
          closeShema();
        }
      }
      else
      {
        if($("body#home").length > 0){
          openHomeShema();
        }
        else{
          openShema();        
        }
      }
      return false;
      });
    $("dl.shema dt.p-active u").click(closeShema);
    $("body#home dl.shema dt.p-active u").click(closeHomeShema);
    $("dl.portfolio dd:not('.p-active')").css({"display":"none"});
    $("dl.portfolio dd:not('.p-active') div.h-fixed-width, dl.shema div.h-img").css({"opacity":"0"});

    $("ul.service li").bind("mouseenter mouseleave", function(){$(this).toggleClass("hover")});
    $("ul.sub-nav div.h-fixed-width").bind("mouseenter mouseleave", function(){$(this).parents("li").toggleClass("hover")});
    $("ul.news-gallery li").bind("mouseenter mouseleave", function(){
        $("ul.news-gallery li.active").removeClass("active");
        $(this).addClass("active")
        }
        );
    $("div.news-archive h3").bind("click", function(){$(this).parent("div.news-archive").toggleClass("news-archive-click")});

    $("span.contact-link a").bind("click", function(){
          $("div.viz-open").addClass("viz-open-show");
          return false;
        });
    $("body#home").bind("click", function(){
          $("div.viz-open").removeClass("viz-open-show");
        });
    
    $("dl.portfolio dt").bind("click", function(e){


      if($(this).is(".p-active"))
      {
        close($(this));
      }
      else
      {
        close($("dl.portfolio dt.p-active"));
        open($(this));
      }

      }).bind("mouseenter mouseleave", function(){
          $(this).toggleClass("hover");
        });
  }); 
})(jQuery);


