var a = {
	init:function(){
		// init jc functions
		jc.init({
			slideShow_interval:4500,
			slideShow_indexPosition:'bottom right'
		});
		
		// fix last li child compatability
		$('ul li:last-child').addClass('last');
		
		a.twitter();
	},
	twitter:function(){
		$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?callback=?&screen_name=CCC_Ottawa', function(data) {
			var status = data[0];
			if (typeof status.text === 'undefined') {
				$('#twitter').html('No tweets available.');
			} else {
				$('#twitter-tweet').html(a.findUrls(status.text));
				$('#twitter-meta').html('<a href="http://twitter.com/CCC_Ottawa" target="_blank">@CCC_Ottawa</a> '+prettyDate(status.created_at)+' (<a href="http://twitter.com/intent/retweet?tweet_id='+status.id_str+'" target="_blank">retweet</a>)</div>');
			}
		});
	},
	findUrls:function(str){
		var re = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g;
		return str.replace(re, '<a href="$1">$1</a>');
	},
	emailSignUp:function(){
		var email = $.trim($('#newsletter-email').val());
		
		if (email === '' || !jc.util.string.isEmail(email)) { alert('Invalid email address.'); return; }
		
		$('#newsletter-form').css({'display':'none'});
		$('#newsletter-status').css({'display':'block'}).html('Processing...');
		
		$.post('/jpanel/_/php/ajax.mailChimp.php',{email:email},function(j){
			if (j == '1') {
				$('#newsletter-status').html('Thanks for signing up!');
			} else {
				$('#newsletter-status').html('Oops! There was a problem. Try again.');
				setTimeout(function(){
					$('#newsletter-form').css({'display':'block'});
					$('#newsletter-status').css({'display':'none'});
				},3000);
			}
		});
	}
};

/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);
			
	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;
			
	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

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