
// Functions for index.php
$(document).ready(function() {
  $('.simpledialog').simpleDialog();
  $('#featured-pages a').click(function(e) {
    e.preventDefault();
    var url = $(this).attr('href') + ++actual_page
    $.get(url, function(data) {
      $('#featured-div').append(data);
      $('#featured-div').find('ul:last').slideDown('slow').show(function() {
        if (actual_page >= max_pages) $('#featured-pages').slideUp('slow').hide()
      })
    });
    return false;
  })

  $('#nav a').click(function(event) {
    var origin =  $(event.currentTarget)
    var uri = origin.attr('href').substr(1)
    goto_uri(uri, origin)
  })
})
$(window).load(function() {
  try { 
    var uri = window.location.href.split('#')[1]
    goto_uri(uri, $('#nav a[href*=#'+uri+']')) 
  } catch(e) { }
  
})

function goto_uri(uri, origin) {
  if (uri == 'apresentacao') goto_link('.header-wrap', origin, 0)
  if (uri == 'portfolio') goto_link('.portfolio-wrap', origin, 70)
  if (uri == 'o-que-esta-rolando') goto_link('.news-wrap', origin, 45)
  if (uri == 'contato') goto_link('.contact-wrap', origin, 70)
}

function goto_link(element, origin, gap) {
  origin.parent().parent().find('li').removeClass('current')
  origin.parent().addClass("current")
  var elementClicked = $(element);
  var destination = $(elementClicked).offset().top;
  $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-gap}, 500 );
  return false;
}
