(function($){
	$(document).ready(function() {
		/*Twitter Feed*/
		//Setup the arrays and counts, api call number, and define some more stuff
		var url = [];
		$arr = new Array();
		$arr[1] = new Array();
		$arr[2] = new Array();
		$arr[3] = new Array();
		window.twitter = [];
		$totalAPICalls = 3;
		$totalToDisplay = 2;//total tweets to show
		$count = 0;
		$.fn.reverse = [].reverse;
		window.url = [];

		/*twitter variables*/
		//UNCOMMENT the next line to active the "#summit" and thencomment the one with next line, w/ the church.
		//var hashtag = "%23summit",
//		var hashtag = "church",
		var hashtag = "alphasummit",
		username1="chris_hodges",
		username2="joechampion",
		username3="rebogard",
		username4="tlane1002",
		username5="G8Men",
		username6="K_Blacq",
		username7="Luis_Patten",
		username8="Ronald_DeArmond",
		username9="mdavis",
		username10="courtsimas",
		mainfeed="thealphasummit";
		//here are hte 3 api calls necessary. had to split it up because twitter only allows 140 characters in their search calls.
//		window.url[1] = "http://search.twitter.com/search.json?q=+"+hashtag+"+from%3A"+username1+"+OR+from%3A"+username3+"+OR+from%3A"+username5+"+OR+from%3A"+username7+"+OR+from%3A"+username9+"&rpp=7&callback=?";
//		window.url[2] = "http://search.twitter.com/search.json?q=+"+hashtag+"+from%3A"+username2+"+OR+from%3A"+username4+"+OR+from%3A"+username6+"+OR+from%3A"+username8+"+OR+from%3A"+username10+"&rpp=7&callback=?";
//		window.url[3] = "http://search.twitter.com/search.json?q=+from%3A"+mainfeed+"&rpp=7&callback=?";
		window.url[1] = "http://search.twitter.com/search.json?q=+"+hashtag+"&rpp=7&callback=?";
		window.url[2] = "http://search.twitter.com/search.json?q=+"+hashtag+"&rpp=7&callback=?";
		window.url[3] = "http://search.twitter.com/search.json?q=+from%3A"+mainfeed+"&rpp=7&callback=?";
		//multithreading the 3 calls at the same time
		fetch_tweets(window.url[1]);
		fetch_tweets(window.url[2]);
		fetch_tweets(window.url[3]);

		/*Visual Element Rollover for the 'what is twitter'*/
		$('a#what_is_twitter_a').hover(
			function(){	if(!($('#what_is_twitter').is(":animated")))	$('#what_is_twitter').animate({"top":"-5px"},100);	$(this).height(34);	},
			function(){	$('#what_is_twitter').animate({"top":"-14px"},50);	$(this).height(25);	}
		);
	});

	/*Twitter feed functions*/
	function fetch_tweets(url){
		display = false;
		//once display gets set to true (on the last fetch), then it spits out the tweets, but not until then.
		$.getJSON(url, function(json) {
			if($count > 1 )
				display = true;
			if(display === false){
				$count++;
				$arr[$count] = $(json.results);
			}
			else{
				$count++;
				$arr[$count] = $(json.results);
				var arr2 = new Array();
				//here we concatonate (using a special js concat function for json objects) all 3 json's (which are stored in an array of arrays)
				for(i=1; i<=$totalAPICalls; i++){
					arr2 = concat_collection(arr2, $arr[i])
				}
				var big_array = new Array();
				//loop through all the json objects, and store them into an array
				$.each(arr2,function(){
					var tmp_array = [this.id,this.profile_image_url,this.text,this.from_user, this.created_at];
					big_array.push(tmp_array);
				});
				//we needed the objects in an array so we could sort by status id (from twitter) and then reverse them so latest goes to top!
				big_array.sort();
				big_array.reverse();
				//now loop through the array and pull out all the info we need to display
				$.each(big_array, function() {
					//hide the spinning pirate ship wheel! - since it's done loading
					$("#tweets").css("backgroundImage","none");
					if($('.tweet_list #tw'+this[0]).length == 0 && $('.tweet_list').find('li').length < $totalToDisplay){
						//hey, let's auto link twitter user names, and web links, shall we?
						var new_text =  this[2].replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g,function(F){return'<a href="'+F+'">'+F+"</a>"}).replace(/\B@([_a-z0-9]+)/ig,function(F){return F.charAt(0)+'<a href="http://www.twitter.com/'+F.substring(1)+'">'+F.substring(1)+"</a>"});

						var divstr = '<li id="tw'+this[0]+'" class="tweet">'+
						'<a href="http://twitter.com/'+this[3]+'" target="_blank"><img width="50" height=50" src="'+this[1]+'" ></a>'+
						'<p>'+ new_text +
						' -&nbsp;'+ relative_time(this[4])
						+'</p></li>';
						//this is here for storing the last id we pulled - just in case we want to make other api calls later
						//- hey we gotta think about scaling right?
						window.twitter['last_id'] = this[0];
						$('.tweet_list').append(divstr);
					}
				});
				//if the second api call, with those usernames, has more than half the tweets shown, lets put a link at the bottom.
				//however, sometimes the javascript runs too fast, and the $arr[1] is empty still at this check. completely. dunno why.
				//works in firefox, but safari and ie sometimes don't process it right.
				//so then the 'view more tweets' doesn't get added. therefor i threw in the second check, for the entire array length
				//you guys can edit at your own will.
				if($arr[1].length > 7 || big_array.length > 10)
//					$("ul.tweet_list").after("<div id='more_tweets'><a target='_blank' href='"+window.url[1].replace(".json","").replace("&callback=?","") +"'>View more</a> Tweets like this.</div>");
					$("ul.tweet_list").after("<div id='more_tweets'></div>");
			}
		});
	};
	//special json concat function. yeah fun stuff.
	function concat_collection(obj1, obj2) {
		var i;
		var arr  = new Array();
		var len1 = obj1.length;
		var len2 = obj2.length;
		for (i=0; i<len1; i++) {arr.push(obj1[i]);}
		for (i=0; i<len2; i++) {arr.push(obj2[i]);}
		return arr;
	}
	//function to show the difference b/w the time that twitter returns for the tweet, and now - aka, relative time.
	function relative_time(C){
		var B=C.split(" ");
		C=B[1]+" "+B[2]+", "+B[4]+" "+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"less than a minute ago"}
			else{
				if(E<120){return"about a minute ago"}
				else{
					if(E<(60*60)){return(parseInt(E/60)).toString()+" minutes ago"}
					else{if(E<(120*60)){return"about an hour ago"}
					else{if(E<(24*60*60)){return"about "+(parseInt(E/3600)).toString()+" hours ago"}
					else{if(E<(48*60*60)){return"1 day ago"}else{return(parseInt(E/86400)).toString()+" days ago"}}
					}
				}
			}
		}
	};
})( jQuery.noConflict() );