// Analytics
var _gaq = [['_setAccount', 'UA-16532459-1'], ['_trackPageview']];
(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = 'http://www.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

$(document).ready(function() {
  // Logo
  $('a.home img').mouseenter(function() {
      this.src = this.src.replace('.png', '_hover.png');
  }).mouseleave(function() {
      this.src = this.src.replace('_hover', '');
  });
  
  // Twitter
  var t = $('#twitter p'), a = $('#twitter a.next');
  t.each(function() {
    $(this).html($(this).html().replace(/(https?:\/\/[^\s]+)/gi, '<a href="$1" target="_blank">$1</a>'));
  });
  a.click(function() {
    if($(this).hasClass('active'))
      t.not('.first').slideUp();
    else
      t.slideDown();
    a.toggleClass('active');
    return false;
  });
  
  // Projecten
  var p = $('#projects div.project'), i = 0, c = p.length;
  function next() {
    p.eq(i).fadeOut();
    i = (i + 1) % c;
    p.eq(i).fadeIn();
  }
  window.setInterval(next, 5000);
  
  // Afbeeldingen
  $('ol.images').each(function() {
    var l = $(this), f = l.find('li'), i = 0, c = f.length, nav = $('<div />').addClass('imagesnavigation');
    function show(j) {
      nav.find('a').eq(i+1).removeClass('active');
      nav.find('a').eq(j+1).addClass('active');
      f.eq(i).fadeOut();
      f.eq(j).fadeIn();
      i = j;
    }
    f.click(function() { show((i + 1) % c); });
    
    l.after(nav);
    for(var n = 0; n < c; n++) {
      var img = f.eq(n).children(), title = img.attr('alt');
      img.attr('title', title);
      nav.append(n ? ' / ' : '').append($('<a />').text(n + 1).attr('href', '#').addClass(n ? '' : 'active').attr('title', title)
                                .click(function() { show(parseInt($(this).text()) - 1); return false; }));
    }
                                
    nav.prepend(' ').prepend($('<a />').text('<').attr('href', '#').click(function() { show(i - (i > 0)); return false; }));
    nav.append(' ').append($('<a />').text('>').attr('href', '#').click(function() { show(i + (i < (c - 1))); return false; }));
  });
});
