
$(document).ready(function(){
		
	/*
	 * Adjust Column Heights
	 *
	 * Equalize column heights
	 */	
	function equalColumns(elems)
	{
		var h = 0;
		elems.css({height: 'auto'});
		
		elems.each(function(){
			if($(this).height() > h) h = $(this).height();
			return false;
		});
		
		elems.css({height: h});
		
		return false;
	}
	
	function updateColumns()
	{
		if($('#index').length == 0){
			equalColumns($('#sidebar, #content'));	
			
			$(window).resize(function(){
				equalColumns($('#sidebar, #content'));				
			});
		}else{
			var h_content = $('#index #content').height();
			var h_side = $('#index #sidebar').height();
			
			if(h_content < h_side){	
				$('#index #container').css({height: h_side + 80});
			}
		}
	}
	
	/*
	 * Ads Swapper
	 *
	$('#ads .swap').each(function(){
		
		var $this = $(this);
		
		
		function swapAd()
		{
			var $curr = $this.find('.active');
			var $next = $curr.next();
			
			if( $next.length == 0){
				$next = $this.find('a').first();	
			}
			
			$next.addClass('active').css({opacity: 0, zIndex: 10});
			$next.animate({opacity: 1}, {queue: false, duration: 300, complete: function(){ 
																						 $curr.css({opacity: 0, zIndex: 0}); }});	
			
		}
		
		setInterval(swapAd, 3000);
		
	});
	/**/
	/*
	 * Navigation bg fix
	 */
	$('.nav li a').each(function(){
		if($(this).height() > 20){	
			$(this).addClass('alt');
		}
	});
	
	/*
	 * Twitter Resource Functions
	 *
	 * http://twitter.com/javascripts/blogger.js
	 */
	
	function tweet_to_html(text){
		var status = 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>';
		});
		
		return '<span>' + status + '</span>';
	}
	
	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';
		}
	}
	
	/*
	 * Twitter Feed 
	 */
	
	twit_obj = {username: 'BHMBizAlliance', count: 5, interval: 50000, live: true, list: $('.twitter ul'), tweets: []};
	
	if($('#index').length == 1) twit_obj.count = 3;
	
	function initTweets()
	{	
		updateColumns();
	
		if ( $('.twitter ul li').length == 0 ) return false;
	
		$('.twitter ul li').remove();
		
		$.ajax({
		
			type		: 'GET',
			dataType	: 'jsonp',
			url			: 'http://twitter.com/statuses/user_timeline/' + twit_obj.username + '.json?count=' + twit_obj.count,
			success		: parseTweets			
		
		});
		
		if(twit_obj.live) setTimeout(getTweets, twit_obj.interval);
		
		updateColumns();
	}
	
	function getTweets()
	{		
		$.ajax({
				type 		: 'GET',
				dataType 	: 'jsonp',
				url 		: 'http://twitter.com/statuses/user_timeline/' + twit_obj.username + '.json?count=' + twit_obj.count,
				success		: function(json){
					parseTweets(json);
					updateTweets();
				}
		});
		
		setTimeout(getTweets, twit_obj.interval);		
	}
	
	function parseTweets(json)
	{
		json.reverse();
		
		for(i in json){
			var entry = json[i];
			var tweet = twit_obj.tweets[entry.id];
			
			if(tweet == undefined){
				tweet = {
					id 			: entry.id,
					username 	: entry.user.screen_name,
					text 		: entry.text,
					html 		: tweet_to_html(entry.text),
					created 	: entry.created_at,
					ele 		: $('<li></li>')
				}			
			
				tweet.ele.html( tweet.html + ' <a style="font-size:85%" href="http://twitter.com/' + tweet.username + '/statuses/' + tweet.id + '" class="date">'+relative_time(tweet.created)+'</a>' );
				
				tweet.ele.css({opacity: 0});
				
				twit_obj.list.prepend(tweet.ele);
				
				tweet.ele.animate({opacity: 1});
				
				twit_obj.tweets[entry.id] = tweet;
				
				if(twit_obj.list.find('li').length > 5){
					twit_obj.list.find('li').last().animate({opacity: 0}, {duration: 300, complete: function(){ $(this).remove(); }});
				}
			}
		}	
		
		updateColumns();
	}
	
	function updateTweets()
	{
		for(i in twit_obj.tweets){
			var tweet = twit_obj.tweets[i];
			
			tweet.ele.html( tweet.html + ' <a style="font-size:85%" href="http://twitter.com/' + tweet.username + '/statuses/' + tweet.id + '" class="date">'+relative_time(tweet.created)+'</a>' );
		}
	}	
	
	initTweets();
	
		
});
