$(document).ready(function() {

  // klikanie - 1.uroven
  $('ul#navigation').children('li').children('a').click(function() {    
  
    // treba vyrolovat submenu
    if ($(this).parent().children().is('ul') && !$(this).parent().hasClass('active')) {
      $(this).parent().children('ul').animate({width: 'show', opacity: 'show'}, 500);            
    }    

    // zasunutue submenu    
    if ($('ul#navigation').children('li.active').children().is('ul') && !$(this).parent().hasClass('active')) {         
      $('ul#navigation').children('li.active').children('ul').animate({ width: 'hide', opacity: 'hide'}, 500);                                  
    } 
       
    // zmena focusu
    $('ul#navigation').children('li.active').removeClass('active');       
    $(this).parent().addClass('active');
    
  });
  
  // klikanie - 2.uroven 
  ////////////////////// TOHOTO SA ZBAVIM KED BUDE PLNA FUNKCNOST, BUDE TO ZBYTOCNE
  $('ul#navigation').children('li:has(ul)').find('li').children('a').click(function() {
    
    // zmena focusu
    $(this).parent().parent().children('li.active').removeClass('active');       
    $(this).parent().addClass('active');
    
  });
   
  // specificke akcie
  $('#project-info').css({cursor: 'pointer'}).click(showProjectInfo).hover(function() {
    $(this).addClass('hovered');
  }, function() {    
    $(this).removeClass('hovered'); 
  });
  $('a#portfolio').click(function() { showPortfolio(); });
  $('a#p1').click(function() { switchPortfolioCategory(1, true); });
  $('a#p2').click(function() { switchPortfolioCategory(2, true); });
  $('a#p3').click(function() { switchPortfolioCategory(3, true); });
  $('a#p4').click(function() { switchPortfolioCategory(4, true); });
  $('a#projects').click(function() { showProjects(); });
  $('a#p5').click(function() { switchProjectsCategory(5, true, true); });
  $('a#p6').click(function() { switchProjectsCategory(6, true, true); });
  $('a#p7').click(function() { switchProjectsCategory(7, true, true); });
  $('a#p8').click(function() { switchProjectsCategory(8, true, true); });
  $('a#profil').click(function() {
  
    if (activeProjects) { hideProjects(); }
    if (activePortfolio) { hidePortfolio(); }
    if (activePresentation) { hidePresentation(); }
    $('div#works').animate({opacity: 'hide', height: 'hide'}, 500);
    if (document.getElementById('over-text') == null) { 
      showProfil(); 
    } else {    
      $('div#over-text').animate({opacity: 'hide'}, 500, showProfil);
    }    
    
  });
  $('a#contact').click(function() {
    
    if (activeProjects) { hideProjects(); }
    if (activePortfolio) { hidePortfolio(); }
    if (activePresentation) { hidePresentation(); }
    $('div#works').animate({opacity: 'hide', height: 'hide'}, 500);
    if (document.getElementById('over-text') == null) { 
      showContact(); 
    } else {    
      $('div#over-text').animate({opacity: 'hide'}, 500, showContact);
    }
    
  });
  
});

function showProfil() {  
  deactivateProjectInfo();
  var src = $('div#image').find('img').not(':hidden').attr('src');
  var img = (src == null) ? '' : '<img src="' + src + '" height="320" alt="Photo" />';
  $('div#image').html(img + '<div id="over-text" style="width: 704px; display: none"><div id="over-text-bg" style="width: 704px"></div><div class="in1"><div class="in2"></div></div></div>');
  $('div#over-text-bg').css({opacity: 1});
  $('#images-switch').html('');
  $('.in2').load('texts/profil.html', function() {
    $('#over-text').css({display: 'block', opacity: 0}).animate({opacity: 1}, 500);
  });
  $('.in1').attr('id', 'profil');          
}

function showContact() {  
  deactivateProjectInfo();  
  var src = $('div#image').find('img').not(':hidden').attr('src');
  var img = (src == null) ? '' : '<img src="' + src + '" height="320" alt="Photo" />';
  $('div#image').html(img +'<div id="over-text" style="display: none"><div id="over-text-bg"></div><div class="in1"><div class="in2"></div></div></div>');
  $('div#over-text-bg').css({opacity: 0.78});
  $('#images-switch').html('');
  $('.in2').load('texts/contact.html', function() {
    $('.in1').append('<a id="aaa1" href="http://www.spaceforms.eu/"></a><a id="aaa2" href="mailto:office@spaceforms.eu"></a><a id="aaa3" href="mailto:funczik@spaceforms.eu"></a><a id="aaa4" href="mailto:kubicsekova@spaceforms.eu"></a><a id="aaa5" href="mailto:funczikr@spaceforms.eu"></a>');
    $('#over-text').css({display: 'block', opacity: 0}).animate({opacity: 1}, 500);
  });      
  $('.in1').attr('id', 'contact');  
}

function showProjectInfo() {    
  $('#project-info').addClass('active').unbind('click').click(hideProjectInfo);
  activeProjectInfo = true;
  if (activePresentation) {        
    switchPresentationProject(activePresProject);
  }  
  if (activePortfolio) {
    $('#over-text').fadeIn(500);    
  }
}

function quickHideProjectInfo() {
  $('#project-info').removeClass('active').unbind('click').click(showProjectInfo);
  activeProjectInfo = false;
  if (activePortfolio) {
    $('#over-text').hide();    
  }
}

function quickShowProjectInfo() {    
  $('#project-info').addClass('active').unbind('click').click(hideProjectInfo);
  activeProjectInfo = true;
  if (activePortfolio) {
    $('#over-text').show();    
  }
}

function hideProjectInfo() {
  $('#project-info').removeClass('active').unbind('click').click(showProjectInfo);
  activeProjectInfo = false;
  if (activePortfolio) {
    $('#over-text').fadeOut(500);    
  }
}

function deactivateProjectInfo() {
  $('#project-info').unbind('click').unbind('mouseenter mouseleave').css({cursor: 'default'}).removeClass('active').removeClass('hovered');
}

function activateProjectInfo() {
  $('#project-info').css({cursor: 'pointer'}).hover(function() {
    $(this).addClass('hovered');
  }, function() {    
    $(this).removeClass('hovered'); 
  });
  if (activeProjectInfo) {
    $('#project-info').addClass('active').click(hideProjectInfo);
  } else { 
    $('#project-info').removeClass('active').click(showProjectInfo);
  }
}
