"use strict";
var HAMMER = {
	init: function () {
		this.Twitter.init();
		this.TeamSpeak.init();
		this.PIS.init();
	},

	Twitter: {
		element: 'twitter_update_list',
		feed: 'http://twitter.com/statuses/user_timeline/hammerofgoel.json',
		
		init: function () {
			if (! document.getElementById(this.element)) {
				return false;
			}
			document.getElementById(this.element).innerHTML = '<li>Loading...</li>';
			$.jTwitter('hammerofgoel', 1, this.callback);
			return true;
		},
		
		callback: function (twitters) {
			var statusHTML, element, relativeTime, i, username, status;
			
			statusHTML = [];
			element = HAMMER.Twitter.element;
			relativeTime = function (time_value) {
				var values, parsed_date, relative_to, delta;
				
				values = time_value.split(" ");
				time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
				parsed_date = Date.parse(time_value);
				relative_to = (arguments.length > 1) ? arguments[1] : new Date();
				delta = parseInt((relative_to.getTime() - parsed_date) / 1000, 10);
				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, 10)).toString() + ' minutes ago';
				} else if (delta < (120 * 60)) {
					return 'about an hour ago';
				} else if (delta < (24 * 60 * 60)) {
					return 'about ' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
				} else if (delta < (48 * 60 * 60)) {
					return '1 day ago';
				} else {
					return (parseInt(delta / 86400, 10)).toString() + ' days ago';
				}
					
			};
			
			for (i = 0; i < twitters.length; i += 1) {
				username = twitters[i].user.screen_name;
				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 href="http://twitter.com/' + username + '/statuses/' + twitters[i].id + '">' + relativeTime(twitters[i].created_at) + '</a></li>');
			}
			document.getElementById(element).innerHTML = statusHTML.join('');
		}
	},
	
	TeamSpeak: {
		element: 'ts_update_list',
		init: function () {
			return true;
		}
	},
	
	PIS: {
		originalImage: 0,
		
		init: function() {
			if (! $('.pis_gallery')) {
				return false;
			}
			$('img.pis_gallery').hover(HAMMER.PIS.rollover, HAMMER.PIS.rollout);
			return true;
		},
		
		rollover: function(event) {
			originalImage = $(this).attr("src");
			var colorVersion = originalImage.split("_bw")[0];
			$(this).attr({src: colorVersion + ".png"});			
		},
		
		rollout: function(event) {
			$(this).attr({src: originalImage});
		}
	},
};

$(document).ready(function () {
	HAMMER.init();
});
