function GetContent(path, div){
  div = div || '#maincontent';
  $(div).fadeTo(500, .01,
              function() {
                $(div).load(path, null,
                          function() {
                            $(div).fadeTo(1600,1);
                          });
              });
}

function FadeLoadOut(div){
  div = div || '#maincontent';
  $(div).fadeTo(1,.01);
}

function FadeLoadIn(div,speed){
  div = div || '#maincontent';
  speed = speed || 600;
  $(div).fadeTo(speed,1);
}

function getPics() {
    var flickr_url = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=482f4f1233cf39a81a6be2cd2d018118&photoset_id=72157620600513673&format=json&jsoncallback=?";
    $.getJSON(flickr_url, function(json){loadPic(json);});
}

function loadPic(json) {
    var pics = json.photoset.photo;
    var index = Math.floor(Math.random((new Date()).getSeconds()) * pics.length);
    var pic = pics[index];
    var src = "http://farm" + pic.farm + ".static.flickr.com/" + pic.server +
        "/" + pic.id + "_" + pic.secret + ".jpg";
    $("#mainphoto").attr('src',src);
    $("#mainphoto").attr('alt',pic.title);
    $("#mainphoto").attr('title',pic.title);
}

function getBlog() {
  FadeLoadOut("#blog-tim", 1);
  var blog_url = "http://hoolihan.net/blog-tim/?feed=json";
//   $.getJSON(blog_url, function(json){loadBlog(json);});
  $.getJSON(blog_url, loadBlog);
}

function loadBlog(json) {
  content = "<a href=\"http://hoolihan.net/blog-tim/\"><h3 class=\"widget-title\">Blog Posts</h3></a>";
  content += "<ul>";
  for(var post_index in json)
    {
      if(post_index > 3) break;
      var post = json[post_index];
      var li_html = "<li><a href=\"" + post.permalink + "\" " +
        "onclick=\"window.open('" + post.permalink + "');return false;\">" +
        post.title + "</a></li>";
      content += li_html;
    }
  content += "</ul>";
  $("#blog-tim").append(content);
  FadeLoadIn("#blog-tim", 2500);
}

function getTweets() {
  FadeLoadOut("#twitter", 1);
  var twitter_url = "http://api.twitter.com/1/statuses/user_timeline.json?" + 
    "user_id=9378332&count=4&include_rts=true&callback=?";
  $.getJSON(twitter_url, function(json){loadTweets(json);});
}

function loadTweets(data) {
  var json = eval(data);
  var user = json[0].user.screen_name;
  var image_url = json[0].user.profile_image_url;
  content = "<a href=\"http://twitter.com/" + user + 
    "\"><h3 class=\"widget-title\">Tweets</h3></a>";
  content += "<img src=\"" + image_url + "\" alt=\"" + user + "\">";
  content += "<ul>";
  for(var tweet_index in json) {
    if(tweet_index > 3) break;
    var tweet = json[tweet_index];
    var link = "http://twitter.com/" + tweet.user.screen_name + "/status/" + tweet.id_str;
    var li_html = "<li><a href=\"" + link + "\" " +
      "onclick=\"window.open('" + link + "');return false;\">" + 
      tweet.text + "</a></li>";
    content += li_html;
  }
  content += "<ul>";
  $("#twitter").append(content);
  FadeLoadIn("#twitter", 2500);
}

function disable(){return false;}

$(document).ready(function () {
            $.ajaxSetup({cache:false});
		    getPics();
            getBlog();
            getTweets();
		  });

