// Panic Blog Functions
// (C) 2009 Panic Inc.
//
// Code: Neven+Cabel

function init() {
	if (navigator.appVersion.indexOf('iPhone OS ') > 0 || navigator.appVersion.indexOf('Android ') > 0) {
		document.body.className = 'mobile';
	}
}

//
// Paper Rotation Functions, For Cuteness
//

var maxRotationAngle = 1.5;
var disableRotation = true;

function rotatePages(theID)
{
	if (navigator.appVersion.indexOf('iPhone OS ') < 0 && disableRotation !== true) { // Don't rotate?

		post = document.getElementById(theID);
		
		if (typeof post.parentNode.className == 'string'            // we can read the parent's classname
			&& post.parentNode.className.indexOf('noRotation') < 1  // - and it's not set to noRotation
			&& ! post.style.webkitTransform 						// - and not already rotated
			&& ! post.style.mozTransform)							// - and not rotated in Firefox
			{
				rotateString = 'rotate(' + ((Math.random() * maxRotationAngle * 2) - maxRotationAngle) + 'deg)';
				post.style.webkitTransform = rotateString;
				// post.style.MozTransform = rotateString;			// Wow, that text looks terrible!
			}
		
	}
}

//
// Twitter Related Functions
//
// Adapted from http://twitter.com/javascripts/blogger.js
//

function twitterCallback2(twitters) {
	if (twitters.length > 0)
	{
		// There are non-reply tweets to show in our stream of last 50 tweets
		var statusHTML = [];
		for (var i=0; i<twitters.length; i++){
			var username = twitters[i].user.screen_name;
			var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
				return '<a href="'+url+'">'+url+'</a>';
			}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
				return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
			});
			statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
		}
		document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
	}
	else
	{
		// There are NOT any non-reply tweets to show in our stream of last 50 tweets
		// Show a placeholder instead
		document.getElementById('twitter_update_list').innerHTML = "We're so busy replying to folks on Twitter, we haven't said anything unique lately! Hopefully this should shame us into Tweeting.";
	}
	
	document.getElementById('twitter_update_list').style.opacity = 1;
}

// Called after the .json file has finished loading.

function filterCallback(twitter_json)
{
	var result = [];
	for (var index in twitter_json)
	{
		if(twitter_json[index].in_reply_to_user_id == null)
		{
			result[result.length] = twitter_json[index];
//			result[result.length-1].text = twitter_json[index].created_at + twitter_json[index].text; // date
		}
		if(result.length == 1)
			break;
	}
	twitterCallback2(result);
}

function relative_time(time_value) {
	var values = time_value.split(" ");
	time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	var parsed_date = Date.parse(time_value);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	delta = delta + (relative_to.getTimezoneOffset() * 60);

	if (delta < 60) {
		return 'less than a minute ago';
	} else if(delta < 120) {
		return 'about a minute ago';
	} else if(delta < (60*60)) {
		return (parseInt(delta / 60)).toString() + ' minutes ago';
	} else if(delta < (120*60)) {
		return 'about an hour ago';
	} else if(delta < (24*60*60)) {
		return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
	} else if(delta < (48*60*60)) {
		return '1 day ago';
	} else {
		return (parseInt(delta / 86400)).toString() + ' days ago';
	}
}

// 
// SimplePoster 1.0
//
// Extremely simple function to enable poster frame behavior.
//
// Tags to put in your calling element:
// "title" = the movie's URL (without extension)
// "fallback" = some text for incompatible browsers
// "m4v" = source to m4v video
// "mov" = source to mov video
// "ogv" = source to ogv video
//

function simplePoster(elem) {
	
	var vid = document.createElement("video"),
		backup = document.createElement("p"),
		src;

	vid.setAttribute("width", elem.getAttribute('width'));
	vid.setAttribute("height", elem.getAttribute('height'));
	vid.setAttribute("controls", "true");
	vid.setAttribute("autoplay", "true");
	
	if (elem.getAttribute("mov")) {
		src = document.createElement("source");
		src.setAttribute("src", elem.getAttribute("mov"));
		src.setAttribute("type", "video/quicktime");
		vid.appendChild(src);	
	}

	if (elem.getAttribute("m4v")) {
		src = document.createElement("source");
		src.setAttribute("src", elem.getAttribute("m4v"));
		src.setAttribute("type", "video/mp4");
		vid.appendChild(src);
	}
	
	if (elem.getAttribute("ogv")) {
		var src = document.createElement("source");
		src.setAttribute("src", elem.getAttribute("ogv"));
		src.setAttribute("type", "video/ogg");
		vid.appendChild(src);
	}
	
	backup.setAttribute("class", "simplePosterFallback");
	backup.appendChild(document.createTextNode(elem.getAttribute('fallback')));
	
	vid.appendChild(backup);
	elem.parentNode.replaceChild(vid, elem);

}

