var refreshRate = 30000;
var cHash = "";
var lcURL = "";

$(document).ready(function() {


	DEBUG = false;


	// Live Content auto-refreshing

	if ( $("#mxnowWrap .liveContent").length > 0 ) {
	
		LC_title = $("#mxnowWrap .liveContent").attr('title');
	
		$.log( LC_title );
	
		LC_split = LC_title.split(",")

		LC_numvars = LC_split.length;
		
		
		// set url segments to default.
		cFeed = "_";
		cHash = "_";
		cLimit = "20";
		
		
		// If we have configuration variables
		
		// parse them out.
		
		if (LC_numvars > 0) {
		
		
			$.each( LC_split, function(i, n){
		
				cName = $.trim(n.split(":")[0]);
				cVal = $.trim(n.split(":")[1]);
		
				$.log(cName + "|" + cVal);
		
				switch ( cName ) {
		
					case "feed": { cFeed = cVal; $.log( "set: " + cName ); break; }
					case "hashtag": { cHash = cVal; $.log( "set: " + cName ); break; }
					case "limit": { cLimit = cVal; $.log( "set: " + cName ); break; }				
		
				}
		
			});	
		
		}
		
		
		// Set variable defaults	
		
		//if (cFeed.length <= 0) { cFeed = "_"; $.log( "setting feed to default" ); }
		//if (cHash.length <= 0) { cHash = "_"; $.log( "set hash to default" ); }
		//if (cLimit.length <= 0) { cLimit = "20"; $.log( "results are default" ); }
		
		
		if ($("#mxnowWrap .liveContent").hasClass("xpressville")) {
			lcURL = "/media/twitter_xpressville_ajax/" + cHash + "/" + cLimit + "/";
		} else {
			lcURL = "/media/twitter_ajax/" + cFeed + "/" + cHash + "/" + cLimit;
		}
		
		$.log( lcURL );
		
		
		setTimeout("autoRefreshLC(lcURL)", refreshRate);
		setTimeout("autoRefreshHatch('/media/twitter_xpressville_ajax/hatchavl/15/')", refreshRate);
	}






	// Live content hashtag filtering
	// navigation controls	
	$(".mxnowNavLinks .navlink").click(function () { 
	
			$("#mxnowWrap .liveContentWrap .liveContent .tweet .status, #mxnowWrap .liveContentWrap .liveContent .tweet .date").fadeTo("fast", "0.35");
	
			lcURL = lcURL.replace(eval("/"+cHash+"/i"), $(this).attr("rel"));
			
			cHash = $(this).attr("rel");

			//setTimeout("autoRefreshLC(lcURL)", refreshRate);
			
			$.log(lcURL);
			
      what_feed = "/media/twitter_xpressville_embed/" + $(this).attr("rel") + "/25/";
      
      $("#mxnowWrap .liveContentWrap .liveContent").load(what_feed);
      
      $(".mxnowNavLinks .navlink").removeClass("on");
      
      $(".mxnowNavLinks .navlink[rel='" + $(this).attr("rel") + "']").addClass("on");
      
      return false;
      
  });



		
});	// End Document ready





// ------------------------
// Auto refreshing function

autoRefreshLC = function(feed_url) {
	
	
	
	
	$.ajax({
	
		url: feed_url,
		dataType: "html",
		cache: false,
		success: function(html) {
			num_new_tweets = 0;
			found_match = false;
			new_tweets = html;
			
			
			if ( $(html).find(".status:first").html() != $("#mxnowWrap .liveContent .tweet:first .status").html() ) {
				
				$.log("NEW CONTENT!");

				$("#mxnowWrap .liveContent .tweet .status").each(function(i) {
				
					
					if ( $(html).find(".status").eq(i).html() != $("#mxnowWrap .liveContent .tweet:first .status").html() ) {
						if (!found_match) {
							num_new_tweets++;
						}
						
					} else {
						found_match = true;
					}
				
				});
				$.log( "New Tweets: " + num_new_tweets );
			} else {
				$.log("same old content. :(");
			}
			
				
				
				
			$.log( "cLimit: " + cLimit);
			
				$.log( "num_new_tweets: " + num_new_tweets);
			
			$.log( "((cLimit - 1) - num_new_tweets): " + ((cLimit - 1) - num_new_tweets));
			
			$.log( $(new_tweets).find(".tweet").html() )
			
			
			if ( num_new_tweets > 0 ) {
			
				here_down = ((cLimit - 1) - num_new_tweets);
				
				$.log( "kill from here on down: " + here_down );
						
				if (num_new_tweets >= cLimit ) {
					$("#mxnowWrap .liveContent .tweet").fadeOut("slow", function () {
						$(this).remove();
					});
						
					$(new_tweets).filter(".tweet").each( function() {
						$("#mxnowWrap .liveContent").prepend( "<div class='tweet new on'>" + $(this).html() + "</div>" );
						
						$("#mxnowWrap .liveContent .tweet.new").slideDown("medium")
						.removeClass("new");
					});
				} else {
					$("#mxnowWrap .liveContent .tweet").eq(here_down).nextAll().fadeOut("slow", function () {
						$(this).remove();
					});
						
					$(new_tweets).filter(".tweet").eq(num_new_tweets).prevAll().each( function() {
						$("#mxnowWrap .liveContent").prepend( "<div class='tweet new on'>" + $(this).html() + "</div>" );
						
						$("#mxnowWrap .liveContent .tweet.new").slideDown("medium")
						.removeClass("new");
					});
				}
				
	
			}
		
			num_new_tweets = 0;
			found_match = false;
			
			setTimeout("autoRefreshLC(lcURL)", refreshRate);
			setTimeout("autoRefreshHatch('/media/twitter_xpressville_ajax/hatchavl/15/')", refreshRate);
		}
	
	});
}









// ------------------------
// Auto refreshing function

autoRefreshHatch = function(feed_url) {
	
	$.ajax({
	
		url: feed_url,
		dataType: "html",
		cache: false,
		success: function(html) {
			num_new_tweets = 0;
			found_match = false;
			new_tweets = html;
			
			
			if ( $(html).find(".status:first").html() != $("#HatchTweets .liveContent .tweet:first .status").html() ) {
				
				$.log("NEW CONTENT!");

				$("#HatchTweets .liveContent .tweet .status").each(function(i) {
				
					
					if ( $(html).find(".status").eq(i).html() != $("#HatchTweets .liveContent .tweet:first .status").html() ) {
						if (!found_match) {
							num_new_tweets++;
						}
						
					} else {
						found_match = true;
					}
				
				});
				$.log( "New Tweets: " + num_new_tweets );
			} else {
				$.log("same old content. :(");
			}
			
				
				
				
			$.log( "cLimit: " + cLimit);
			
				$.log( "num_new_tweets: " + num_new_tweets);
			
			$.log( "((cLimit - 1) - num_new_tweets): " + ((cLimit - 1) - num_new_tweets));
			
			$.log( $(new_tweets).find(".tweet").html() )
			
			
			if ( num_new_tweets > 0 ) {
			
				here_down = ((cLimit - 1) - num_new_tweets);
				
				$.log( "kill from here on down: " + here_down );
						
				if (num_new_tweets >= cLimit ) {
					$("#HatchTweets .liveContent .tweet").fadeOut("slow", function () {
						$(this).remove();
					});
						
					$(new_tweets).filter(".tweet").each( function() {
						$("#HatchTweets .liveContent").prepend( "<div class='tweet new on'>" + $(this).html() + "</div>" );
						
						$("#HatchTweets .liveContent .tweet.new").slideDown("medium")
						.removeClass("new");
					});
				} else {
					$("#HatchTweets .liveContent .tweet").eq(here_down).nextAll().fadeOut("slow", function () {
						$(this).remove();
					});
						
					$(new_tweets).filter(".tweet").eq(num_new_tweets).prevAll().each( function() {
						$("#HatchTweets .liveContent").prepend( "<div class='tweet new on'>" + $(this).html() + "</div>" );
						
						$("#HatchTweets .liveContent .tweet.new").slideDown("medium")
						.removeClass("new");
					});
				}
				
	
			}
		
			num_new_tweets = 0;
			found_match = false;
		}
	
	});
}