$(document).ready(function(){
  if (document.location.hash.substr(0,3) == '#!/') {
    var launch_project = document.location.hash.substr(3);
    var $anchor = $("#project-"+launch_project);
    var json_string = $anchor.attr("data-json");
    $.facebox({ ajax: '/box.php?json='+json_string });
  }
  var social_txt = $("#social_links ul li.act").children('a').attr("title");
  $("#social_txt").html(social_txt);
  
  $("#social_links ul li").hover(function(){
    $("#social_links ul li").removeClass("act");
    $(this).addClass("act");
    var text = $(this).children('a').attr("title");
    $("#social_txt").html(text);
  });
  
  // super simpele tweets pakker, doet wattie moet doen.
  function getTweets() {
    var url = "http://api.twitter.com/1/statuses/user_timeline/johnny_does.json?callback=?";
    $.ajax({
      type:'GET',
      url: url,
      data:{},
      dataType:'json',
      success:function(tweet){
        for(var i=0;i<3;i++) {
          //var txt = tweet[i].text.replace(/(http:\/\/[^ ]+)/g,'<a href="$1">$1</a>');
          var txt = linkify(tweet[i].text);
          c = i+1;
          $("#tweets li:nth-child("+c+")").html(txt);
        }        
      }
    });
	}
	getTweets();
	
  $('#slideshow').cycle({
		fx: 'fade',
		timeout:  5000,
		speed: 1100
		
  });
  
  $(".item").mouseover(function(){
    $(this).children('h3').children('a').addClass('hover');
    $(this).children('img').css({ opacity: 0.8 });
  });
  $(".item").mouseout(function(){
    $(this).children('h3').children('a').removeClass('hover');
    $(this).children('img').css({ opacity: 1 });    
  });
  $(".item").click(function(){
    var $anchor = $(this).children('h3').children('a');
    document.location.hash = '!/'+$anchor.attr("data-url");
    var json_string = $anchor.attr("data-json");
    $.facebox({ ajax: '/box.php?json='+json_string });
    return false;
  });
  function imgHandle(step){
    var $cimg = $("#current_img");
    var total_images = $("#images").children('img').length;
    var current_img = parseFloat($cimg.val());
    var next_img = 1;
    if (step == 'prev') {
      if (current_img == 1) {
        next_img = total_images;
      } else {
        next_img = current_img - 1;
      }
    } else {
      if (current_img == total_images) {
        next_img = 1;
      } else {
        next_img = current_img + 1;
      }
    }
    $("#images img:nth-child("+current_img+")").hide();
    $("#images img:nth-child("+next_img+")").show();
    $cimg.val(next_img);
    fbPosition();
  }
  function fbPosition() {
    var windowHeight = $(window).height();
    var windowWidth = $(window).width();
    var fbHeight = $('#facebox').height();
    var fbWidth = $('#facebox').width();
  
    if(fbHeight < windowHeight) {
        $('#facebox').css('top', (Math.floor((windowHeight - fbHeight) / 2) + $(window).scrollTop()) );
    }
    if(fbWidth < windowWidth) {
        $('#facebox').css('left', (Math.floor((windowWidth - fbWidth) / 2) + $(window).scrollLeft()) );
    }
  }
  $("#left").live('click', function(){
    imgHandle('prev');
  });
  $("#right").live('click', function(){
    imgHandle('next');
  });
  $(document).bind('afterReveal.facebox', function() {
      fbPosition();
  });
  $(document).bind('afterClose.facebox', function() {

    document.location.hash = 'default';
    // prevent jump back to top
    
  });

});
