        jQuery(function($) {
          function portfolio_menu_view_click(that)
          {
            var hash = !empty($(that).attr("content"))? $(that).attr("content"): $(that).attr("hash");
            $("#player").remove();
            $("<div id='grey_cover'></div>").height($("html").height()).width($("html").width()).prependTo("body");
            $("#redirect_link").attr("href", hash).click();
          }
          
          function portfolio_menu_click(that)
          {
            if ($(that).hasClass("selected"))
              return false;
            if ($(that).hasClass("expanded"))
            {
              var level_cl = getPortfolioNextLevel($(that).attr("class"));
              var level_cur = getPortfolioLevel($(that).attr("class"));
              if (level_cl.length && level_cur.length)
              {
                var $child = $(that).parent("li").next("li");
                while ($child.length && !$("div." + level_cur, $child).length)
                {
                  $("div." + level_cl + ":not(.opened)", $child).parent("li").addClass("inactive");
                  $("div." + level_cl + ".expanded:not(.view_link):not(.opened)", $child).click();
                  $child = $child.next("li");
                }
              }
              $(that).removeClass("expanded");
              $(that).addClass("collapsed");
              return true;
            }
            
            if ($(that).hasClass("collapsed"))
            {
              var level_cl = getPortfolioNextLevel($(that).attr("class"));
              var level_cur = getPortfolioLevel($(that).attr("class"));
              if (level_cl.length && level_cur.length)
              {
                var $child = $(that).parent("li").next("li");
                while ($child.length && !$("div." + level_cur, $child).length)
                {
                  $("div." + level_cl + ":not(.opened)", $child).parent("li").removeClass("inactive");
                  $child = $child.next("li");
                }
              }
              $(that).removeClass("collapsed");
              $(that).addClass("expanded");
              return true;
            }
            
            if (!empty($(that).attr("content")))
            {
              var level_cl = getPortfolioNextLevel($(that).attr("class"));
              var level_cur = getPortfolioLevel($(that).attr("class"));
              var $holder = $(that).parents("ul");
              var $active_item = $(that).parent("li").next("li").children("div");
              var link_level = false;
              var active_hash;
              var active_passed_flg = $active_item.hasClass(level_cl)? 0: -1;
              if ($(".link_level." + level_cl).length)
                link_level = true;
              if (active_passed_flg > -1)
              {
                active_hash = !empty($active_item.attr("hash"))? $active_item.attr("hash"): $active_item.attr("content");
                
                active_hash = active_hash.replace("http://", "");
                active_hash = active_hash.replace(window.location.host, "");
                
                $active_item = $active_item.parent("li");
                var $active_item_last_subitem = $active_item.next("li");
                var $tmp = $active_item;
                while ($active_item_last_subitem.length && !$("div." + level_cur, $active_item_last_subitem).length)
                {
                  $tmp = $active_item_last_subitem;
                  $active_item_last_subitem = $active_item_last_subitem.next("li")
                }
                $active_item_last_subitem = $tmp;
              }
              else
                $active_item = $(that).parent("li");
              $(that).attr("hash", $(that).attr("content")).removeAttr("content").addClass("expanded");
              $.get($(that).attr("hash"), function(content){
                $("#content_injected").append($(content).addClass("to_prepare")).change();
                if ($("h2", $(content)).length)
                {
                  portfolio_menu_view_click(that);
                  return false;
                }
                
                
                
                var items;
                if ($(that).attr("hash").indexOf("awards") < 0)
                  items = $("li", $(content));
                else
                  items = $("div", $(content));
                                
                var items_l = items.length;
                var $item;
                var hash, tmp_hash;
                for (i=0;i<items_l;i++)
                {
                  tmp_hash = $("a", items[i]).attr("href");
                  tmp_hash = tmp_hash.replace("http://", "");
                  tmp_hash = tmp_hash.replace(window.location.host, "");                    
                  $("a", items[i]).attr("href", tmp_hash);
                  
                  if (active_passed_flg > -1 && tmp_hash == active_hash)
                  {
                    active_passed_flg = 1;
                    continue;
                  }
                  
                  hash = $("a", items[i]).attr("href");
                  
                  if (hash.indexOf("awards") > -1)
                    hash += "projects/"
                  
                  
                  $item = $('<li><div class="' + level_cl + '" content="' + hash + '">' + $("a", items[i]).html() + '<em></em></div></li>');
                  if (!link_level)
                    $("div", $item).click(function(){portfolio_menu_click(this)});
                  else
                    $("div", $item).removeClass("collapsed").addClass("expanded").addClass("view_link").click(function(){portfolio_menu_view_click(this);});
                    
                  if (active_passed_flg == 0)
                    $item.insertBefore($active_item);
                  else if (active_passed_flg == 1)
                    $active_item_last_subitem = $item.insertAfter($active_item_last_subitem);
                    else
                      $active_item = $item.insertAfter($active_item);
                }
              }); 
              
              if (level_cl.length)
                $("div." + level_cl + ".expanded:not(.view_link)", $child).click();
            }
              
            if (!empty($(that).attr("project")))
            {
              var $selected = $("div.selected", $(that).parents("ul"));
              $selected.removeClass("selected").parent("li").removeClass("selected");
              $(".view img[project=" + $(that).attr("project") + "]").parent().click();
              $(that).addClass("selected");
            }
          }          
          function project_click(that)
          {
            $(that).parents("li").click();
            $(".view span").each(function(){
              $("a", this).clone().insertBefore(this).click(function(){
                project_click(this);
              });
              $(this).remove();
            });            
            
            $(".left ul .selected").removeClass("selected");
            $(".left ul li div[project=" + $("img", that).attr("project") + "]").addClass("selected").each(function(){
              $(this).parent("li").addClass("selected");
              if ($(this).hasClass("disabled"))
              {
                var $prev = $(this).parent("li").addClass("disabled").prev("li");
                while (!$("div", $prev).hasClass("link_level"))
                  $prev = $prev.prev("li");
                $("div", $prev.addClass("selected")).addClass("selected");
              }
            });
            $("li div.project.disabled").parent("li").addClass("disabled");
            
            $(".project_current_title").html($(that).attr("title"));
            if ($(".project_current_description").length && !empty($(that).attr("description")))
              $(".project_current_description").html($(that).attr("description"));
            $(".right .project_description").remove();
            if ($(".project_description", $(that)).length)
            {
              $(".right:not(.list_view)", $(that).parents(".content_holder")).append('<p class="project_description">' + $(".project_description", $(that)).html() + '</p>');
            }
            $(".right .project_brand").remove();
            if ($(".project_brand", $(that)).length)
            {
              $(".right:not(.list_view) .project_client", $(that).parents(".content_holder")).after('<strong class="project_brand">бренд: ' + $(".project_brand", $(that)).html() + '</strong>');              
            }
            $("#project_current_player").remove();
                  
            $("img.project_current").hide();
            if ($("img", that).attr("project").indexOf(".flv") > -1)
            {
              $("img.project_current").hide().parents(".main").css("min-width", 480);
              $('<div id="project_current_player" class="flv_player">Для просмотра ролика необходим <a href="http://get.adobe.com/flashplayer/" target="_blank">Adobe Flash Player версии 8 и выше</a></div>').insertBefore("img.project_current");
              var so = new SWFObject("http://www.lbl.ru/img/player/player4www.swf", "player", "480px", "400px", "8", "#454348");
              so.addVariable("path", escape($("img", that).attr("project")));
              so.write("project_current_player");
            }
            else
            {
              if ($("img", that).attr("project").indexOf(".mp3") > -1)
              {
                $("img.project_current").hide().parents(".main").css("min-width", 480);
                $('<div id="project_current_player">Для прослушивания ролика необходим <a href="http://get.adobe.com/flashplayer/" target="_blank">Adobe Flash Player версии 8 и выше</a></div>').insertBefore("img.project_current");
                var so = new SWFObject("/img/player/player.mp3.f.swf", "player", "436px", "255px", "8", "#454348");
                so.addParam("allowScriptAccess", "always");
                so.addVariable("path", escape($("img", that).attr("project")));
                so.write("project_current_player");
              }
              else
              {
                $("img.project_current").load(function(){
                  var $holder = $(this).parent(".center");
                  $(this).parents(".main").css("min-width", $(this).width() + $holder.siblings(".left").width() + $holder.siblings(".right.list_view").width() + 20);                  
                  $(".img_loader", $(this).parent()).remove();
                  $(this).show();
                });
                $("img.project_current").width($("img", that).attr("project_width") + "px"); 
                $("img.project_current").height($("img", that).attr("project_height") + "px"); 
                $("img.project_current").after("<div class='img_loader'></div>");
                $("img.project_current").attr("src", $("img", that).attr("project"));
                $("img.project_current").attr("alt", $("img", that).attr("alt"));
              }
              
            }            
            
            $(that).wrap("<span></span>");
            $("img", that).clone().insertBefore(that);
            
            if ($(".view span").parent("li").prev().length)
            {
              $(".list_view strong.bkw").addClass("hidden");
              $(".list_view a.bkw").removeClass("hidden");
            }
            else
            {
              $(".list_view strong.bkw").removeClass("hidden");
              $(".list_view a.bkw").addClass("hidden");
            }

            if ($(".view span").parent("li").next().length)
            {
              $(".list_view strong.frw").addClass("hidden");
              $(".list_view a.frw").removeClass("hidden");
            }
            else
            {
              $(".list_view strong.frw").removeClass("hidden");
              $(".list_view a.frw").addClass("hidden");
            }
          } 

          function getPortfolioNextLevel(cls)
          {
            if (empty(cls))
              return false;
            if ((level_num_p = cls.indexOf("lvl")) > -1)
              return "lvl" + String(Number(cls.substr(level_num_p + 3).split(" ")[0]) + 1);
            return false;
          }

          function getPortfolioLevel(cls)
          {
            if (empty(cls))
              return false;
            if ((level_num_p = cls.indexOf("lvl")) > -1)
              return "lvl" + String(Number(cls.substr(level_num_p + 3).split(" ")[0]));
            return false;
          }
          
          
          $(".page .box.content .content_holder.main").bind("before_content_view", function(){
            $("html").addClass("grey_page_layout");
            $(".page .box.content .content_holder.main .left li div:not(.view_link)").click(function(){portfolio_menu_click(this)});               
            $(".page .box.content .content_holder.main .left li div.view_link").click(function(){portfolio_menu_view_click(this)});
            $(".project_current, #project_current").load(function(){
              $("body").height($(".page").height());
            });
            
            $(".list_view a.bkw").click(function(){
              $($(".view span:eq(0)").parent("li").prev().children("a").get(0)).click();
              return false;
            });
            
            $(".list_view a.frw").click(function(){
              $($(".view span:eq(0)").parent("li").next().children("a").get(0)).click();
              return false;
            });
           
            $(".view a").click(function(){
              project_click(this);
              return false;
            });

            
            $(".list_view").bind("view", function(){
                $("#grey_cover").remove();
                $(".portfolio_section_navigation .force").removeClass("force").click();
                if ($(".view li:first a:visible").length)
                {
                  $(".view li:first a").click();
                }
                else
                {
                  $(".view li:first a").click();
                }
                
                var offset = -240;

                var $view_items = $(".view li");
                var view_items_l = $view_items.length;
                var view_items_i, view_items_width = 0;
                for (view_items_i=0;view_items_i<view_items_l;view_items_i++)
                    view_items_width += $($view_items[view_items_i]).width();
                if ($(".view a").length > 4 || view_items_width > 500)
                  $(".list_view").show();
                else
                  offset = - $(".view").width();
                
              	$('.view').serialScroll({
              		items:'li',
              		prev:'',
              		next:'',
              		offset: offset,
              		start:0,
              		duration:1200,
              		force:true,
              		stop:true,
              		lock:false,
              		cycle:false, //don't pull back once you reach the end
              		easing:'easeOutQuart', //use this easing equation for a funny effect
              		jump: true //click on the images to scroll to them
              	});    
            });
          });
          $(".page .box.content .content_holder.main").bind("before_content_close", function(){
            $("#player").remove();
            $("img.project_current").hide();
            $("body, .page").height("auto");
            $("body, .page").height("1");
            $("html").removeClass("grey_page_layout");
          });                      
        });