/* Portfolio */
function changeImg(icon, icon_active) {
    $(icon).hide();
    $(icon_active).show();
}

var now_show = 'icons';
var after_line = false;

$(function(){  
    
    $("#portfolio_tolist").click(function(){
        $('#list_icons').hide();
        $('#list_text').show();

        $("#menu_icons").hide(); 
        $("#menu_list").show(); 
        updateYear();
        //установим в типе отображения как мы сейчас открыли
        setCookie ('portfolio_type', 'tolist', '/', '', '', '');
        return false;
    });
    
    $("#portfolio_toicons").click(function(){
        $('#list_icons').show();
        $('#list_text').hide();
        
        $("#menu_icons").show(); 
        $("#menu_list").hide();
        updateYear();
        //установим в типе отображения как мы сейчас открыли
        setCookie ('portfolio_type', 'toicons', '/', '', '', ''); 
        
        return false;
    });
    
    $("li.blue > a.blue-dash").click(function(){
        $('li.blue').show();
        $('li.red').hide();
        
        $('h3.withoutlink').hide();
        $('h3.withlink').show();

        type = $(this).attr('title');
        $('a.blue-dash[title="'+type+'"]').parent().hide();
        $('li.red[title="'+type+'"]').show();
        
        div = $(this).parent().parent();
        div.children('h3.withoutlink').hide();
        div.children('h3.withlink').show();
        
        filterList(type);
        
        return false;
    })
     
    $("h3.withlink > a.blue-dash").click(function(){
        $('li.blue').show();
        $('li.red').hide();
        
        $('h3.withoutlink').hide();
        $('h3.withlink').show();

        $(this).parent().hide().prev().show();
        
        filterList('all');
        return false;
    })
    
    /*прелоад
    $('img[@id*="_active"]').each( function() { 
      $.preloadImages.add([$(this).css('backgroundImage').replace(/url\(/g, '').replace(/\)/g, '')]);
    });
    $.preloadImages.start();*/
    
    $("#list_icons #show_me_last_please").click(function(){
      if (after_line == false){
        $("#list_icons #after_line").show();
        $("#list_text #after_line").show();
        after_line = true;
        $.cookie('after_line', '1', { 'path': '/' });
      } else {
        $("#list_icons #after_line").hide();
        $("#list_text #after_line").hide();
        after_line = false;
        $.cookie('after_line', '0', { 'path': '/' });
      }
        
      updateYear();
    })

    $("#list_text #show_me_last_please").click(function(){
      if (after_line == false){
        $("#list_icons #after_line").show();
        $("#list_text #after_line").show();
        after_line = true;
        $.cookie('after_line_true', '1', { 'path': '/' });
      } else {
        $("#list_icons #after_line").hide();
        $("#list_text #after_line").hide();
        after_line = false;
        $.cookie('after_line_true', '0', { 'path': '/' });
      }
        
      updateYear();
    })

    var url = window.location.href;
    var hash_pos = url.indexOf('#');     
    if (parseInt(hash_pos) > 0) {
      var current_type = url.substr(parseInt(hash_pos) + 1);
      if (''!==current_type) {
        $("a[title^='"+current_type+"']").click();
      } 
    }
    
    if ($.cookie('after_line') == '1'){
      $("#list_icons #show_me_last_please").click();
    }    
    
    if ($.cookie('portfolio_filter') != 'all' && $.cookie('portfolio_filter') != null){
      $("a[title^='"+$.cookie('portfolio_filter')+"']").click();
    }
});

function filterList(type) {
if (type == "all") {
    $('#list_icons div').show();
    $('#list_text li').show();
  } else {
    $('#list_icons div[id^="prf_item"]').hide();
    $('#list_icons div[title*="'+type+'"]').show();
    
    $('#list_text li').hide();
    $('#list_text li[title*="'+type+'"]').show();
  }
  
  updateYear();
  
  //установим в фильтре как что мы сейчас открыли
  setCookie ('portfolio_filter', type, '/', '', '', '');
}

function moveToImage(right, id) {
  
  $('img[id^="process_imgs_"]').each( function() { $(this).removeClass('act'); });   
  $('p[id^="process_desc_"]').each( function() { $(this).hide(); }); 
  
  $('#process_imgs_' + id).addClass('act');   
  $('#process_desc_' + id).show();   
  $('#demoOne').animate({ right: right }, "slow");  
  //$('#demoOne').css({right: right + 'px' }); 
}

function updateYear(){
  $("#list_icons div.portfolio-icons").show().attr('rel','').each(function(){
    divs = $(this).find('div[id^="prf_item"]:visible');
    if (divs.html() == null) $(this).hide().attr('rel','h');
  });

  $("#list_text div.portfolio-icons").show().attr('rel','').each(function(){
    divs = $(this).find('li[id^="prf_item"]:visible');
    if (divs.html() == null) $(this).hide().attr('rel','h');
  });
  
  updateLastYear();
}

function updateLastYear(){
  if(after_line){
    divs = $("#list_icons #show_me_last_please").parent().parent().nextAll('.portfolio-icons:visible');
    if (divs.html()!==null) $("#list_icons #show_me_last_please").parent().parent().show(); else $("#list_icons #show_me_last_please").parent().parent().hide();
    $("#list_icons #show_me_last_please").parent().parent().nextAll('.portfolio-icons').hide();
  }
    if($("#list_icons #after_line").attr('rel') == 'h') $("#list_icons #show_me_last_please").parent().parent().hide(); else $("#list_icons #show_me_last_please").parent().parent().show();
    
  if(after_line){
    divs = $("#list_text #show_me_last_please").parent().parent().nextAll('.portfolio-icons:visible');
    if (divs.html()!==null) $("#list_text #show_me_last_please").parent().parent().show(); else $("#list_text #show_me_last_please").parent().parent().hide();
    $("#list_text #show_me_last_please").parent().parent().nextAll('.portfolio-icons').hide();
    
  }
  if($("#list_text #after_line").attr('rel') == 'h') $("#list_text #show_me_last_please").parent().parent().hide(); else  $("#list_text #show_me_last_please").parent().parent().show();
  
  if (after_line == true){
    $("#list_icons #after_line").show();
    $("#list_text #after_line").show();
  } else {
    $("#list_icons #after_line").hide();
    $("#list_text #after_line").hide();
  }
}
