google.load("feeds", "1");

$(function() {
  if ($("#quotes").length) {
    $("#quotes .box").scrollable({items: "ul", circular: true})
      .navigator({navi: "#quotes .selectors"})
      .autoscroll({interval: 12000})
          .data('scrollable').seekTo(Math.floor(Math.random() * $('#quotes li:not(.cloned)').size()), 0);
  }
});

function getMonth(monthNo) {
  var months = {
    en: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
    et: ["jan", "veb", "mär", "apr", "mai", "jun", "jul", "aug", "sept", "okt", "nov", "dets"]
  };

  var tld = window.location.host.match(/codeborne\.(\w+)/);
  if (tld) {
    tld = tld[1];
  }
  return months[tld == "com" ? "en" : "et"][monthNo];
}

$(function() {
	$.getJSON("http://twitter.com/statuses/user_timeline.json"
			+ "?screen_name=codeborne&count=5&callback=?", function(tweets) {
		var b = $("#twitter ol");
		$.each(tweets, function(i, tweet) {
			// IE dies when there's no timezone abbreviation before +0000.
			var date = new Date(tweet.created_at.replace(/ (UTC ?)?\+/, " UTC+"));
		
			var h = $("<li><div class=date></div><div class=title><a></a></div></li>");
			h.find(".date").html(date.getDate() + ".&nbsp;" + getMonth(date.getMonth()));
			h.find(".title a")
				.attr("href", "http://twitter.com/" + tweet.user.screen_name + "/status/"
				// NOTE: IE replaces <a> text with URL when .href attr is set after it.
				+ tweet.id_str).text(tweet.text);
				
			b.append(h);
		});
	})
});

$(function() {
  var feed = new google.feeds.Feed("http://blog.codeborne.com/feeds/posts/default");
	feed.setNumEntries(8);
	
	feed.load(function(result) {
		if (result.error) return;
      
		var b = $("#blog ol");
		$.each(result.feed.entries, function(i, post) {
			var date = new Date(post.publishedDate);
			
			var h = $("<li><div class=date></div><div class=title><a></a></div></li>");
			h.find(".date").html(date.getDate() + ".&nbsp;" + getMonth(date.getMonth()));
			h.find(".title a").text(post.title).attr("href", post.link);
			
			b.append(h);
		});
	});
});

