/*
Twitter news feed 1.0.0 - 4/11/2011 - http://www.modrobotics.com
BY Eric Lundby http://www.modrobotics.com

** jQuery 1.2.* or higher required

Thanks to juitter: http://juitter.com/

Use at your own risk. The developer takes no responsibility for loss of data, damages 
or other issues resulting from the use of this script.

*/

jQuery.noConflict();
(function(jQuery) {	
	
	var conf = {},
		// JUITTER DEFAULT CONFIGURATION ========================
		// YOU CAN CHANGE THE DYNAMIC VARS ON CALLING THE start method, see the system.js for more information about it.

		numMSG = 8; // set the number of messages to be show
		containerDiv="juitterContainer", // //Set a place holder DIV which will receive the list of tweets example <div id="juitterContainer"></div>
		loadMSG="Loading Cubelets tweets...", // Loading message, if you want to show an image, fill it with "image/gif" and go to the next variable to set which image you want to use on 
	
		// some global vars,
		msgNb=1,
		jURL = '/custom/php/juitter.php';
		var oldestTweet = "";
		var time,contDiv,loadMSG,gifName,numMSG,tweetCount;
		var tweetList = {};
		var running=false;

	jQuery.Juitter = {
		registerVar: function(opt){
			contDiv=opt.placeHolder?opt.placeHolder:containerDiv;
			loadMSG=opt.loadMSG?opt.loadMSG:loadMSG;
			gifName=opt.imgName?opt.imgName:imgName;
			numMSG=opt.total?opt.total:numMSG;
			openLink=opt.openExternalLinks?"target='_blank'":"";
		},
		start: function(opt) {
			if(jQuery("#"+contDiv)){	
				this.registerVar(opt);
				// show the load message
				this.loading();
				// query the twitter API and create the tweets list
				this.conectaTwitter();	
			}   
		},
		loading: function(){
			if(loadMSG=="image/gif"){
				jQuery("<img></img>")
					.attr('src', gifName)
					.appendTo("#"+contDiv); 
			} else jQuery("#"+contDiv).html(loadMSG);
		},
		delRegister: function(){
			// remove the oldest entry on the tweets list
			if(msgNb>=numMSG){
				jQuery(".twittLI").each(
					function(o,elemLI){
						if(o>=numMSG) jQuery(this).hide("slow");													  
					}
				);
			}	
		},
		addAdditional: function (numTweets){
			if(numTweets <= 0 ){ return; }
			var send_dict = {
				url: jURL,
				type: "GET",
				data: {"count": numTweets, "type": "fetch"},
				dataType: "json",
				error: function(XMLHttpRequest, textStatus, errorThrown){
					try{						
						console.debug(textStatus);
					}catch(err){}
				},
				success: function(json, textStatus){
					for (var i =-1; i<json.length-1; i++)//Last json entry is the timestamp of the oldest tweet
					{
						if (i == -1)
						{
							jQuery("<ul></ul>")
							.attr('id', 'twittList')
							.attr('class','twittList')
							.prependTo("#"+contDiv); 
						}
						else{
							var tHTML = json[i];
							jQuery("<li></li>") 
							.html(tHTML)  
							.attr('id', 'twittLI'+msgNb)
							.attr('class', 'twittLI')
							.appendTo(".twittList");
							jQuery('#twittLI'+msgNb).hide();
							jQuery('#twittLI'+msgNb).show("slow");
							msgNb++;							
							
						}									
					}										
				}
			};
		
			jQuery.ajax(send_dict);

		},
		conectaTwitter: function(e){
			tweetList = {};
			jQuery("#"+contDiv).html("");
			jQuery.Juitter.addAdditional(numMSG+1);// query the twitter api and create the tweets list			
		}
	};	
})(jQuery);

function deleteTweet(timestamp){
	var type = "remove";
	var send_dict = {
				url: jURL,
				type: "GET",
				data: {"timestamp": timestamp, "type": type},
				dataType: "json",
				error: function(XMLHttpRequest, textStatus, errorThrown){
					try{						
						console.debug(textStatus);
					}catch(err){}
				},
				success: function(json, textStatus){				
					window.location.href=window.location.href;					
				}
	};
		
	jQuery.ajax(send_dict);
}


