$(document).ready(function() {  
  $('#featured .nav li:first-child').addClass('active');
  $('#offer dt:first-child').addClass('active');
  $('#portfolio-article #project-list li:first').addClass('active');
  
  var intervalId = setTimeout("changeFeatured()", 10000);
  
  $('a[href^=#]').click(function(event) {
    event.preventDefault();
    
    var element = $($(this).attr('href'));
    
    if (element.is('li')) {
      element.find('a').click();
    } else if (element.is('dt')) {
      element.click();
      element = element.parent().prev();
    }
    
    $('html,body').animate({scrollTop: element.offset().top},'fast');
  });
  
  $('#offer dt').each(function() {
    var margin = $('#offer dl').position().top - $(this).position().top;
    $(this).next().css('margin-top', margin);
  });
  
  $('#offer dt').click(function() {
    var clicked = this;
    $(clicked).next().css('display', 'none');
    
    /*$('#offer .active').removeClass('active').next().fadeOut();
    
    $(clicked).addClass('active').next().fadeIn();*/
    
    $('#offer .active').next().animate({
      height: 'hide'
    }, 'fast', function() {
      $('#offer .active').removeClass('active');
      
      $(clicked).addClass('active');
      
      $(clicked).next().animate({
        height: 'show'
      }, 'fast');
    });
  });
  
  /*$('h2 .nav li, .header li').not('inactive').find('a').unbind().click(function(event) {
    event.preventDefault();
    $('html,body').animate({scrollTop: $($(this).attr('href')).offset().top},'fast');
  });*/
  
  $('#featured .nav a').unbind().click(function(event) {
    event.preventDefault();
    clearTimeout(intervalId);
    
    $('#featured .active').removeClass('active');
    $(this).parent().addClass('active');
    
    $('#featured .section li:visible').fadeOut('fast');
    $($(this).attr('href')).fadeIn('fast');
    
  });
  
  $('#project-list').jcarousel({
    scroll: 5,
    initCallback: carouselCallback
  });
  updatePortfolio();
});

function carouselCallback(carousel) {
  $('#portfolio-article #project-list li > a').unbind().click(function(event) {
    event.preventDefault();
    
    $('#portfolio-article #project-list .active').removeClass('active');
    $(this).parent().addClass('active');
    
    updatePortfolio();
    
    var position = $('#project-list .active').prevAll().length - 2;
    if (position < 1) {
      position = 1;
    }
    carousel.scroll(position);
  });
  
  $('#project-one-left').unbind().not('.inactive').click(function(event) {
    event.preventDefault;
    
    $('#portfolio-article #project-list .active').prev().find('a').click();
  });
  
  $('#project-one-right').unbind().not('.inactive').click(function(event) {
    event.preventDefault;
    
    $('#portfolio-article #project-list .active').next().find('a').click();
  });
}

function changeFeatured()
{
  var next = $('#featured .active').next();
  //alert(next.length);
  if ( ! next.length) {
    next = $('#featured .nav li:first-child');
    //alert('istnieje');
  }
  //alert(next);
  next.find('a').click();
  intervalId = setTimeout("changeFeatured()", 10000);
}

function updatePortfolio()
{
  $('#project-preview').append('<div style="position:absolute;top:0px;left:0px;display:none">' + $('#portfolio-article #project-list .active .content').html() + '</div>');
  
  $('#project-preview div:first-child').fadeOut('fast', function() {
    if ($('#project-preview > div').length > 1) {
      $(this).remove();
    }
  });
  
  $('#project-preview div:last-child').fadeIn('fast');
  
  if ($('#project-list .active').prev().length) {
    $('#project-one-left').removeClass('inactive');
  } else {
    $('#project-one-left').addClass('inactive');
  }
  
  if ($('#project-list .active').next().length) {
    $('#project-one-right').removeClass('inactive');
  } else {
    $('#project-one-right').addClass('inactive');
  }
  
  var position = $('#project-list .active').prevAll().length - 2;
  //console.log($('#project-list').jcarousel());
  
}

function checkPortfolioArrows()
{
  var activeProject = $('#portfolio-article .projects > li.active');
  
  if (activeProject.prev().not('.prev').length) {
    $('#portfolio-article .projects > li.prev').removeClass('inactive');
  } else {
    $('#portfolio-article .projects > li.prev').addClass('inactive');
  }
  
  if (activeProject.next().not('.next').length) {
    $('#portfolio-article .projects > li.next').removeClass('inactive');
  } else {
    $('#portfolio-article .projects > li.next').addClass('inactive');
  }
  
  var activePhoto = $('#portfolio-article .projects li.active li.active');

  if (activePhoto.prev().not('.prev').length) {
    $('#portfolio-article .projects li.active li.prev').removeClass('inactive');
  } else {
    $('#portfolio-article .projects li.active li.prev').addClass('inactive');
  }
  
  if (activePhoto.next().not('.next').length) {
    $('#portfolio-article .projects li.active li.next').removeClass('inactive');
  } else {
    $('#portfolio-article .projects li.active li.next').addClass('inactive');
  }
}
