var activeProjects = false;
var activeProjectsProject;
var activeProjectsProjectTemp;

function showProjects() {
  // ak niesu aktivne, zaktivnim
  if (activeProjects) { return; }
  activeProjects = true;
  deactivateProjectInfo();
  $('#project-info').addClass('active');
  
  if (activePresentation) { hidePresentation(); }
  if (activePortfolio) { hidePortfolio(); }
    
  var activeCategory = $('a#projects').parent().find('li.active').children('a').attr('id').charAt(1);
  var currentCategory = -1;  
  var ul = '';  
  for (var i in fullProjects) {
  
    // nova gategoria
    var newCategory = fullProjects[i].year;        
    if (newCategory != currentCategory) {
      currentCategory = newCategory;
      if (i > 0) { ul += '</ul>' }
      ul += '<ul class="works' + ((activeCategory == currentCategory) ? ' active' : '') +'" id="categ' + currentCategory +'">';
    }
    
    ul += fullProjects[i].getProjectsHTML();
    
  }  
  ul += '</ul>';  
  $('div#works').hide().html(ul).animate({height: 'show', opacity: 'show'});
  $('#project' + activeProjectsProject).addClass('active');
  _scrollTo(projectsCategoriesStart[activeCategory]);
  
  // obrazky
  projectsImages();  
}

function hideProjects() {  
  if (!activeProjects) { return; }
  activeProjects = false;
}

function projectsImages() {
  // obrazky
  var str = fullProjects[projArr[activeProjectsProject]].getGalleryHTML();
  $('div#image').stop().hide().html(str).css({opacity: 0, display: 'block'});
  var maxWidth = imgWidths[activeProjectsProject];  
  $('#over-text-bg').css({width: maxWidth + 'px'});  
  $('#over-text').css({width: '213px'});
  $('div#over-text-bg').css({opacity: 0.78});
  $('#over-text').find('.in1').removeClass('in1').addClass('in3');
  $('#over-text').find('.in2').removeClass('in2').addClass('in4');
  $('div#image').animate({opacity: 1}, 500);       
  $('div#images-switch').html('');    
}

function switchProjectsCategory(cat, activate, scroll) {
  showProjects();
  
  // zaktivnenie kategorie hore
  $('a#projects').parent().find('li.active').removeClass('active');
  $('a#p' + cat).parent().addClass('active');
   
  // zaktivnenie kategorie vpravo
  $('ul.works.active').removeClass('active');
  $('ul#categ' + cat).addClass('active');  

  // scrollovanie
  if (scroll) {
    _scrollTo(projectsCategoriesStart[cat]);
  }
     
  // zaktivnenie prveho projektu
  if (activate) {
    _switchProjectsProject(projectsCategoriesStart[cat]);    
  }   
}

function _scrollTo(projId) {
  var y = $('#project' + projId).position().top;
  if (-y == $('#works').position().top) { return; }
  $('#works').animate({top: -(y) + 'px'}, 1000);
}

function _switchProjectsProject(id) {  
  $('ul.works').find('li.active').removeClass('active');
  $('li#project' + id).addClass('active');    
  activeProjectsProject = id;  
  projectsImages(); 
}


function switchProjectsProject(id) {  
  if (activeProjectsProjectTemp != id) {
    activeProjectsProjectTemp = id;
    switchProjectsCategory(fullProjects[projArr[id]].year, false, true);
  }  
  if (activeProjectsProject == id) { return; }  
  _switchProjectsProject(id);   
}

function hoverProjectsProject(id) {
  if (activeProjectsProject == id) { return; }
  activeProjectsProjectTemp = activeProjectsProject;
  
  switchProjectsCategory(fullProjects[projArr[id]].year, false, false);
  _switchProjectsProject(id);   
}

function unhoverProjectsProject() { 
  if (activeProjectsProject == activeProjectsProjectTemp) { return; }
  activeProjectsProject = activeProjectsProjectTemp;
    
  switchProjectsCategory(fullProjects[projArr[activeProjectsProject]].year, false, false);
  _switchProjectsProject(activeProjectsProject);    
}

