var em = {
	cTweet : 0,
	sliding : undefined,
	init : function()
	{
		em.getTweets();
		em.eventHandlers();
		em.hideAuthors();
		
		$('.article-copy').columnize({columns:2,lastNeverTallest:true, buildOnce:true });
		
	},
	getTweets : function()
	{
		if ($.browser.msie && $.browser.version < 7)
		{
			$("#tweet_results .wrap").html("<h3>Sorry! Your Browser doesn't support this!</h3>");
		}

		
		
		$.ajax({
			  url: tweetURL,
			  cache: false,
			  success: function(html){
			  	$("#tweets-wrap #tweet_results .wrap .loading").hide();
			    $("#tweets-wrap #tweet_results .wrap").html(html);
			    
			    if(html === "over-capacity" || html === "")
				{
					$("#tweet_results .wrap").html("<h3>Sorry! Twitter isn’t working right now.</h3>");
				}else{
					var tweetBlocks = $("#tweet_results .chunk");
					if(tweetBlocks.length > 1)
					{
						em.slideShow();
						$("#tweet_results #tweet-controls").show();
					}
					$("#tweets-wrap #tweet_results .chunk:first").show();
				}
				
				
						    
			  }
			});
	},
	slideShow : function()
	{
		
		var tweetBlocks = $("#tweet_results .chunk");
		sliding = setInterval(function(){
			
			if(em.cTweet == tweetBlocks.length - 1){
				em.cTweet = -1;
			}
			
			
			em.cTweet = em.cTweet +1;
			$("#tweet_results .chunk").hide();
			$("#tweet_results .chunk").eq(em.cTweet).fadeIn("slow");	
			
		},10000);	
	},
	hideAuthors : function()
	{
		
		var authors = [
		"Adam Bracegirdle",
		"Allyson Hohman",
		"Andrea Donatucci",
		"Brendan Best",
		"David Fasullo",
		"David Armano",
		"Deanna Lambert",
		"Devlin Dunsmore",
		"Eden Shaffer",
		"Gabby Hon",
		"Jason Theodor",
		"Jessi Odenbach",
		"Kara Spudic",
		"Lauren Ysseldyke",
		"Len Kendall",
		"Mark Szabo",
		"Mo Goltz",
		"Nick Parish",
		"Nicole Armstrong",
		"Richard Deede",
		"Scott Weisbrod",
		"Sean Howard",
		"Steve Susi",
		"Stephanie Cunningham-Long",
		"Tyler Hellard",
		"William Bertolo"		
		];
		
		$(".author-list li a").each(function(){
			
			for(var i = 0; i <= $(".author-list li").length; i++)
			{
			
				if($(this).html() == authors[i])
				{
					$(this).hide();
				}
			}
				
		});	
	},
	sortTags : function(sortby)
	{
		
		var tmp=[];
		$(".tag-list a").each(function(){
		   tmp.push($(this));                                                                                                                                               });
			tmp = tmp.sort();
			for(i=0;i<tmp.length;i++){
		   	$("#div_id").append(tmp[i]);
		   	if(i<tmp.length-1) $("#div_id").append(", ");
		
			} 
					
	},
	eventHandlers : function()
	{
		$("#tweet_results #tweet-controls .prev").click(function(){
			clearInterval(sliding);
			var tweetBlocks = $("#tweet_results .chunk");
			if(em.cTweet != 0){
				
				$("#tweet_results .chunk").hide();
				$("#tweet_results .chunk").eq(em.cTweet - 1).fadeIn("slow");
				em.cTweet = em.cTweet - 1;
				
			}else{
				$("#tweet_results .chunk").hide();
				$("#tweet_results .chunk").eq(tweetBlocks.length - 1).fadeIn("slow");
				em.cTweet = tweetBlocks.length - 1;
			}
			em.slideShow();
			
		});
		
		$("#tweet_results #tweet-controls .next").click(function(){
			clearInterval(sliding);
			var tweetBlocks = $("#tweet_results .chunk");
			
			if(em.cTweet != tweetBlocks.length - 1){
				$("#tweet_results .chunk").hide();
				$("#tweet_results .chunk").eq(em.cTweet + 1).fadeIn("slow");
				em.cTweet = em.cTweet + 1;
					
			}else{
				$("#tweet_results .chunk").hide();
				$("#tweet_results .chunk").eq(0).fadeIn("slow");
				em.cTweet = -1;
			}
			em.slideShow();
				

		});
		
		$("#footer-tabs li").click(function(){
			if($(this).hasClass("selected") == false)
			{
				$("#footer-tabs li").removeClass("selected");
				$("#footer-pages li").removeClass("selected");
				
				var tClass = $(this).attr("class");
				$(this).addClass("selected");
				$("." + tClass, "#footer-pages").addClass("selected");
			}
			
		});
		
		//Show Comments
		/*$(".post-comments-wrap .show").click(function(){
			if($(this).html() != "No comments")
			{
				$(this).parent().find(".hide").show();
				$(this).parent().parent().find(".commentlist").show();
				$(this).hide();
			}
		});*/
		
		//Show Comments
		/*$(".post-comments-wrap .hide").click(function(){
			$(this).parent().find(".show").show();
			$(this).parent().parent().find(".commentlist").hide();
			$(this).hide();
			
		});*/
		
		//Toggle Form
		/*$(".post-comments-wrap .toggle_form").click(function(){
			$(this).parent().find("#respond").toggle();
			$(this).toggleClass("off");

		});*/
		
		$("#searchsubmit").click(function(){
			$("#searchform").submit();
		});
		
		$.fn.sort = function() {
		    return this.pushStack([].sort.apply(this, arguments), []);
		};
		
		
		function sortCountAsc(a, b)  
 		{  
 			var a = $(a).attr("data-count");
 			var b = $(b).attr("data-count");
 			if(a > b){
				return -1;
			}else if( a < b){
				return 1;
			}else{
				return 0;
			}
 		}
		
 		function sortOrderAsc(a, b)  
 		{  
 			var a = $(a).attr("data-order");
 			var b = $(b).attr("data-order");
 			if(a > b){
				return -1;
			}else if( a < b){
				return 1;
			}else{
				return 0;
			}
 		}
		
		
		
		$("#byAlpha").click(function() {
		        $('.tag-list a').sort().appendTo('.tag-list');
		});
		
		$("#byCount").click(function() {
		        $('.tag-list a').sort(sortCountAsc).appendTo('.tag-list');
		});
		
		$("#byOrder").click(function() {
		        $('.tag-list a').sort(sortOrderAsc).appendTo('.tag-list');
		});
		
	}
};
