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

$(document).ready(function() {


	// Live Content auto-refreshing

	if ( $(".LiveContent").length > 0 ) {
	
		LC_title = $(".LiveContent").attr('rel');
	
		// // console.log( LC_title );
	
		LC_split = LC_title.split(",")

		LC_numvars = LC_split.length;
		
		
		// set url segments to default.
		cHash = "_";
		cLast = "_";
		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]);
		
			
				switch ( cName ) {
		
					case "hashtag": { cHash = cVal; break; }
					case "last": { cLast = cVal; break; }
					case "limit": { cLimit = cVal; break; }				
		
				}
				
				
				
				if (cLast == "_") {
					cLast = $(".LiveContent .tweet:first").attr('rel');
				}
				
				// console.log( "cLast: " + cLast );
		
			});	
		
		}
		
		
		// Set variable defaults	
		
		if (cHash.length <= 0) { cHash = "_"; /* console.log( "set hash to default" ); */ }
		if (cLast.length <= 0) { cLast = "_"; /* console.log( "setting last to default" ); */ }
		if (cLimit.length <= 0) { cLimit = "20"; /* console.log( "results are default" ); */ }
		

		lcURL = "/media/twitter_live_new_tweets/" + cHash + "/" + cLast + "/" + cLimit + "/";

	
		
		setTimeout("autoRefreshLC(lcURL)", refreshRate);
	
	}






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

			
				
      what_feed = "/media/twitter_live_new_tweets/" + $(this).attr("rel") + "/_/25/";
            
      $(".LiveContentWrap .LiveContent").load(what_feed, function(){
				cLast = $(".LiveContent .tweet:first").attr('rel');
				
				lcURL = "/media/twitter_live_new_tweets/" + cHash + "/" + cLast + "/" + cLimit + "/";
				
				LC_title = "hashtag:" + cHash + ", limit:" + cLimit
				
				$(".LiveContent").attr("rel", LC_title);
      });
      
      $(".mxnowNavLinks .navlink").removeClass("on");
      
      $(".mxnowNavLinks .navlink[rel='" + $(this).attr("rel") + "']").addClass("on");
      
      $(".mxnow-current-hashtag").html( $(this).attr("rel") );
      
      
      $(".mxnowMoreInfo a").attr("href", "http://www.mountainx.com/media/twitter/xpressville/" + $(this).attr("rel") + "/200")
      
      return false;
      
  });


	$("#TwitterFormatToggle").css("cursor", "pointer");

	$("#TwitterFormatToggle").click(function () {
		$(".tweet .tf").fadeToggle("slow");
	});
		
});	// End Document ready




// ------------------------
// Fade toggle function
jQuery.fn.fadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle'}, speed, easing, callback);
}; 



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

autoRefreshLC = function(feed_url) {
	
	$.ajax({
	
		url: feed_url,
		dataType: "html",
		cache: false,
		success: function(html) {
			num_new_tweets = 0;
			new_tweets = html;
			
			
			if ( html != "0" ) {
						
				num_new_tweets = $(html).find(".status").length;
				
				 // console.log( "New Tweets: " + num_new_tweets );
			} else {
				// console.log("same old content. :(");
			}
			
				
				
						
			
			if ( num_new_tweets > 0 ) {
			
				here_down = ( (cLimit - 1) - num_new_tweets );
			
				if (num_new_tweets >= cLimit ) {
					$(".LiveContent .tweet").fadeOut("slow", function () {
						$(this).remove();
					});
											
					$(new_tweets).filter(".tweet").each( function() {
						$(".LiveContent").prepend( "<div class='tweet new on'>" + $(new_tweets).html() + "</div>" );
						
						$(".LiveContent .tweet.new").slideDown("medium")
						.removeClass("new");
					});
				} else {
					$(".LiveContent .tweet").eq(here_down).nextAll().fadeOut("slow", function () {
						$(this).remove();
					});				
					
					$(new_tweets).filter(".tweet").each( function() {
						$(".LiveContent").prepend( "<div class='tweet new on' rel='" + $(new_tweets).filter(".tweet").attr("rel") + "'>" + $(new_tweets).html() + "</div>" );
						
						$(".LiveContent .tweet.new").slideDown("medium")
						.removeClass("new");
					});
				}
				
				
				cLast = $(".LiveContent .tweet:first").attr('rel');
				
				
				
				// Build new status (rel) string
				lcURL = "/media/twitter_live_new_tweets/" + cHash + "/" + cLast + "/" + cLimit + "/";
	
				// console.log ("setting new rel string to " + lcURL + ".")
	
	
	
			} else {
			
				// console.log( "NOT adding tweets" );
				
			}

			
			
			
			// Re-call auto refresh function
			setTimeout("autoRefreshLC(lcURL)", refreshRate);
		}
	
	});
}