(function($){
	$(document).bind('ready', function() {
		$("input#email, input#name", "body#homepage").clearonfocus();
		$("#rotator_inside").find('')
		$("ul#image_nav").find('li a').click(function(){
			var ahref = $(this).attr("href");
			ahref = ahref.replace("#","");
			$("ul#image_nav").find('li.selected').removeClass('selected');
			$(this).parent('li').addClass('selected');
			$sibling = $(this).parents('#image_nav').siblings("#rotator");
			$sibling.find('a').fadeOut(80);
			$sibling.find('a#image-'+ahref).fadeIn();
			return false;
		});
		$("body#homepage").find("#events").find('li').each(function(){
			var eventname = $(this).find('a').text();
			if(eventname.length > 40){ $(this).find('a').text(eventname.substring(0,37)+"..."); }
			var eventdate = eventname.substring(eventname.indexOf(" ")+1,eventname.indexOf("]"));
			$(this).find('span').text(eventdate);
		});
		$("#resources_docs").find(".main_content > div").each(function(){
			var href=$(this).find('a').attr("href");
			var result = href.lastIndexOf(".");
			var doctype = href.substring(result+1,href.length);
			if(doctype == "doc" || doctype == "docx"){ $(this).addClass("doc"); }
			else if(doctype == "xls"){ $(this).addClass("xls"); }
			else if(doctype == "pdf"){ $(this).addClass("pdf"); }
			else if(doctype == "ppt"){ $(this).addClass("ppt"); }
		});
		$("#resources_media").find(".main_content > div").each(function(){
			var href=$(this).find('a').attr("href");
			var result = href.lastIndexOf(".");
			var doctype = href.substring(result+1,href.length);
			if(doctype == "mp3" || doctype == "wav" || doctype == "m4r"){ $(this).addClass("audio"); }
			else if(doctype == "mov"){ $(this).addClass("video"); }
			else if(doctype == "avi"){ $(this).addClass("video"); }
			else if(doctype == "mp4"){ $(this).addClass("video"); }
			else if(doctype == "flv"){ $(this).addClass("video"); }
			else if(doctype == "wma"){ $(this).addClass("video"); }
		});
		
		if( $("body#homepage")[0] ){
			/*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();
			window.twitter = [];
			$totalAPICalls = 2;
			$totalToDisplay = 1;//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 = " ",
			username1="AudioBible",
			username2="theseedcompany",
			username3="edweaver",
			username4="t4global",
			username5="moverstreet",
			username6="AveryWillis",
			username7="rbrek",
			username8="jerrynwiles46",
			username9="MissionExchange",
			username10="hksechiang";
			//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=4&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=4&callback=?";
			//multithreading the 3 calls at the same time
			fetch_tweets(window.url[1]);
			fetch_tweets(window.url[2]);
		}
	});

	/*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 > 0 )
				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) both of the 2 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
				var firsttime = 0;
				$.each(big_array, function() {
					//hide the spinning pirate ship wheel! - since it's done loading 
					if(firsttime < 1){
						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>"});
						$("p#main_tweet").html(new_text);
						$("#twitter_profile img").attr("src",this[1]).width("66px").height("66p")
						$("p#username").html("From <span><a href='http://twitter.com/"+this[3]+"'>"+this[3]+"</a></span> "+relative_time(this[4]));
						//console.log(new_text);
						/*
						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>';*/
						window.twitter['last_id'] = this[0];
					}
					firsttime = 1;
				});
			}
		});
	};
	//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"}}
					}
				}
			}
		}
	};


	/* Copyright (c) 2008 Brandon Aaron (http://brandonaaron.net)
	 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
	 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
	 *
	 * $LastChangedDate$
	 * $Rev$
	 *
	 * Version 0.2
	 *
	 * Usage: 
	 *   $("input[name=q]")
	 *      .val("Search")    // set an initial value if it doesn't already exist
	 *      .clearonfocus();  // prepare the element for clearing on focus
	 */
	jQuery.fn.clearonfocus = function() {
		return this
			.bind('focus', function() {
				if ( !this.defaultValue ) this.defaultValue = this.value;
				if ( this.defaultValue && this.defaultValue != this.value ) return;
				this.value = '';
			})
			.bind('blur', function() {
				if ( this.value.match(/^\s*$/) )
					this.value = this.defaultValue;
			});
	};
})( jQuery.noConflict() );
