var zi_max = 0;
var zi_selected = 0;
var loading = false;
function swap_lang(lang) {
  var document_url = ' '+ document.location;  
    if (document_url.indexOf("#") > -1) {
      new_url = '/' + (document_url.substr(document_url.indexOf("#") + 2))
      if (lang == 'gb')
        new_url = new_url.replace('portfoliopl', 'portfolio');
      else {
        new_url = new_url.replace('portfoliopl', 'portfolio');
        new_url = new_url.replace('portfolio', 'portfoliopl');
      }      
      document.location = new_url;
      return false;
    }  
    return true;
}

$(document).ready(function(){
  
  var document_url = ' '+ document.location;  
  if (document_url.indexOf("#") > -1) {
    new_url = '/' + (document_url.substr(document_url.indexOf("#") + 2))    
    document.location = new_url;
  }

  $('#flaga_polska').click(function(){ return swap_lang('pl'); });
  $('#flaga_brytyjska').click(function(){ return swap_lang('gb'); });
  
  var imgs = $('.portfolio img.pic');
  
  var zi = 0;
  $.each(imgs, function(key, obj){
    obj.old_zi = zi;
    $(obj).parent().parent().css({'z-index':obj.old_zi}); 
    if ($(obj).parent().parent().hasClass('active')) {
      zi_selected = zi;      
    }
    
    zi ++;
  });
  zi_max = zi + 1;
  
  //select 1
  $('.portfolio li.active').css({'z-index':zi_max});  
  $('.portfolio li.active img.pic').animate({width:'120px',height:'112px',marginTop:'-15px',marginLeft:'-35px'},100);  
  $('img.copy').clone().appendTo($('.portfolio li.active a')).removeClass('copy').addClass('background').show();
  
  $(imgs).hover(
    function(){
      if ($('.portfolio li.active').length) {
        var image = $('.portfolio li.active img.pic');        
        $(image).parent().parent().css({'z-index':zi_selected});
        $(image).parent().find('img.background').remove();
        $(image).parent().find('.shadow').hide();        
        $(image).parent().find('.shadow').hide();
        $(image).animate({width:'80px',height:'75px',marginTop:'0px',marginLeft:'0px'},100);
        $('.portfolio li.active').removeClass('active');
      }
      
      $(this).css({'z-index':1})    
      $(this).animate({width:'120px',height:'112px',marginTop:'-15px',marginLeft:'-35px'},200);
      $('img.copy').clone().appendTo($(this).parent()).removeClass('copy').addClass('background').stop().fadeIn(speed);
      $(this).parent().find('.shadow').hide();
      $(this).parent().parent().css({'z-index':zi_max});
    },
    function(){
     $(this).parent().parent().css({'z-index':this.old_zi});
     $(this).parent().find('img.background').remove();
     $(this).parent().find('.shadow').show();
     $(this).animate({width:'80px',height:'75px',marginTop:'0px',marginLeft:'0px'},100);
    
    }
  );
  
  $('ul.portfolio li a').click(function(){
    if (loading) {
      return false;
    }
    
    //stop loading other content
    loading = true;
    
    url = $(this).attr('href') + '/ajax';
    
    
    //hide objects    
    var to_hide = new Array('p_nazwa', 'p_rok', 'p_opis_gb', 'p_opis_pl', 't_nazwa_pl', 't_nazwa_gb', 'p_img1 img', 'p_img2 img', 'p_img3 img', 'p_adres', 'projekt');
    $.each(to_hide, function(key, id){
      if (id == 'projekt')
        $('#' + id).fadeOut(speed + 300, function() {
           $.get(url, null, print_content); 
        });
      else
        $('#' + id).fadeOut(speed);
    })
    
    
      
    var current_url = '' + document.location;
    
    if (current_url.indexOf('#') != -1) {
      current_url = current_url.substring(0, current_url.indexOf('#'));
    }
    
    document.location = current_url + '#' + $(this).attr('href'); 
    return false;    
  });
});

speed = 100;

function print_content(data, link) {
  result = (link == 'true')?data:parseJson(data);
    
  $.each(result, function(index, value){    
    if (value != 'empty') {           
      result[index] = 'empty';   
      if (!$('#' + index).length && index != 'p_main') {
        index = 'default';
      }
      
      switch (index) {
        case 'p_nazwa':
        case 'p_rok':
        case 'p_opis_gb':
        case 'p_opis_pl':
        case 't_nazwa_pl':
        case 't_nazwa_gb':          
            $('#' + index).html(value);
            $('#' + index).fadeIn(speed, function(){            
              print_content(result, 'true');
            }); 
            break;
        case 'p_img1':    
        case 'p_img2':
        case 'p_img3':          
            img = new Image();
            
            new_src = '/portfolio/'+ value +'_maly.jpg';
            img.index = index;
            img.value = value;
            
            $(img)         
            .load(function () {      
               new_src = '/portfolio/'+ this.value +'_maly.jpg';
               $('#' + this.index +' img').attr('src', new_src);
               $('#' + this.index +' img').fadeIn(speed, function(){
                 print_content(result, 'true');
               });
               $('#' + this.index).attr('href', '/portfolio/'+ this.value +'.jpg');                
            })
            .attr('src', new_src); 
            if (index == 'p_img3') {
              //allow loading next slide
              loading = false;
            }            
            break;          
        case 'p_main':          
              $('#projekt').css({'background-image': "url('/portfolio/" + value + ".png')"});
              $('#projekt').slideDown(speed + 300, function(){ 
                print_content(result, 'true');
              });             
            break;
        case 'p_adres':          
            if (value) {
              $('#' + index).attr('href', 'http://' + value);
              $('#' + index).fadeIn(speed, function(){ 
                print_content(result, 'true');                
              });             
            }
            else {
              print_content(result, 'true');
            }
            break;                              
        default :
            print_content(result, 'true');
            break;    
      }    
      return false;         
    }
  });  
}

parseJson = function (data) {
  if ((data.substring(0, 1) != '{') && (data.substring(0, 1) != '[')) {
    return { status: 0, data: data.length ? data : false };
  }
  return eval('(' + data + ');');
};
