function init () {
	hash = window.location.hash.replace('#','');
	if (hash.length > 1) {
		load_page(hash);
		$('li.page-' + hash).parent().children().each(function(){$(this).removeClass('current');});
		$('li.page-' + hash).addClass('current');
	}
	init_secondary('');
}
function init_secondary (prefix) {
	$(prefix + '.ajax').each(function(){
		var page = $(this).attr('href').replace(/\//g, '');
		$(this).attr('href', '#' + page);
		$(this).click(function(){
			load_page(page);
			$(this).parent().parent().children().each(function(){$(this).removeClass('current');});
			$(this).parent().addClass('current');
		});
	});
	setTimeout('hide_messages()', 7000);
	$('div.message').click(hide_messages);
}
function load_page (page) {
	$('#tabs .throbber').slideDown('fast');
	$.ajax({
		url: '/ajax/',
		type: 'post',
		data: 'mode=page&page=' + page,
		success: function(response){
			$('#container-inner').html(response);
			setTimeout("$('#tabs .throbber').slideUp('fast')", 500);	
			init_secondary('#container ');
		}
	});
	return false;
}
function submit (form) {
	query = '';
	$('#tabs .throbber').slideDown('fast');
	$(form).children('input').each(function(){
		query += '&' + $(this).attr('name') + '=' + $(this).val();
	});
	$.ajax({
		url: '/ajax/',
		type: 'post',
		data: 'mode=action' + query,
		success: function(response){
			$('#container-inner').html(response);
			setTimeout("$('#tabs .throbber').slideUp('fast')", 500);
			init_secondary('#container ');
		}
	});
	return true;
}
function hide_messages () {
	$('div.message').each(function(){
		$(this).fadeOut('slow');
	});
}
function shake (id) {
	$('#' + id).effect('shake', {times: 4}, 50);
}
function check (checked, element) {
	$("#" + element + " input[type='checkbox']").attr('checked', checked);
}
function add_friend (id, button) {
	new Request({
		url: '/ajax/add-friend',
		onSuccess: function (response) {
			if (response == 'added') {
				alert("You're now friends with this user!");
				button.innerHTML = 'Remove Friend';
				button.removeClass('add');
				button.addClass('remove');
				button.onclick = function() {
					remove_friend(id, button);
				}
			} else if (response == 'already friends') {
				alert("You're already friends with this user.");
			} else if (response == 'not added') {
				alert('An unknown error occured. Please try again.');	
			}
		}
	}).send('id=' + id);
	return false;
}
function remove_friend (id, button) {
	request = new Request({
		url: '/ajax/remove-friend',
		onSuccess: function (response) {
			if (response == 'removed') {
				alert("You're no longer friends with this user.");
				button.innerHTML = 'Add Friend';
				button.removeClass('remove');
				button.addClass('add');
				button.onclick = function () {
					add_friend(id, button);
				}
			} else if (response == 'not friends') {
				alert("You're not friends with this user.");
			} else if (response == 'not removed') {
				alert('An unknown error occured. Please try again.');	
			}
		}
	}).send('id=' + id);
	return false;
}
function remove_friend_mini (id, li) {
	request = new Request({
		url: '/ajax/remove-friend',
		onSuccess: function (response) {
			if (response == 'removed') {
				fx = new Fx.Slide(li, {opacity: true}).slideOut();
			} else if (response == 'not friends') {
				alert("You're not friends with this user.");
			} else if (response == 'not removed') {
				alert('An unknown error occured. Please try again.');	
			}
		}
	}).send('id=' + id);
	return false;
}
function delete_activity (id, li) {
	request = new Request({
		url: '/ajax/delete-activity',
		onSuccess: function (response) {
			if (response == 'deleted') {
				fx = new Fx.Slide(li, {opacity: true}).slideOut();
			} else if (response == 'not deleted') {
				alert('An unknown error occured. Please try again.');	
			}
		}
	}).send('id=' + id);
	return false;
}
function user_tweets (json) {
	if (json.length > 0) {
		json.each(function(tweet, i) {
			status = tweet.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(F){return'<a href="' + F + '" target="_blank">' + F + '</a>'}).replace(/\B@([_a-z0-9]+)/ig,function(F){return F.charAt(0) + '<a href="http://twitter.com/' + F.substring(1) + '">' + F.substring(1) + '</a>'});
			new Element('li', {'html': status + '&nbsp;<a href="http://twitter.com/' + tweet.user.screen_name + '/statuses/' + tweet.id + '" target="_blank" class="date">' + relative_time(tweet.created_at) + '</a>'}).inject('recent-tweets');
		});
	} else {
		no_tweets = new Element('li', {
			'html': 'No recent Tweets could be loaded.'
		}).inject('recent-tweets');
	}		
}
function relative_time (C) {
	var B = C.split(' ');
	C = B[1] + ' ' + B[2] + ', ' + B[5] + ' ' + B[3];
	var A = Date.parse(C);
	var D = (arguments.length > 1) ? arguments[1] : new Date();
	var E = parseInt((D.getTime() - A) / 1000);
	E = E + (D.getTimezoneOffset() * 60);
	if (E < 60) {
		return 'just now';
	} else {
		if (E < 120) {
			return 'a minute ago';
		} else {
			if (E < (60 * 60)) {
				return (parseInt(E / 60)).toString() + ' minutes ago';
			} else {
				if (E < (120 * 60)) {
					return 'an hour ago';
				} else {
					if (E < (24 * 60 * 60)) {
						return (parseInt(E / 3600)).toString() + ' hours ago';
					} else {
						if (E < (48 * 60 * 60)) {
							return '1 day ago';
						} else {
							return (parseInt(E / 86400)).toString() + ' days ago';
						}
					}
				}
			}
		}
	}
}
$(document).ready(init);