var timer;
 
$(document).ready(function() {
	$.timer(0, function (timer) {
		getTweet($("#container_1").html());
		timer.reset(5000);
	});
});
 
function getTweet(before_result)
{
	var results = new Array();
    var count = 1;
    
	$.post("../getTweet.php"
		, { query: "転職" }
		,  function(xml){
			$('entry', xml).each(function(i){
				 results[count] = $(this).find("title").text();
				 count++;
			});
			if ( before_result != results[1] ) {
				for ( i = 1; i <= 3; i++ ) {
					$("#container_" + i).hide();
					$("#container_" + i).html(results[i]);
					$("#container_" + i).show("slow");
				}
			}
		}
	);		
}

jQuery.timer = function (interval, callback)
 {
 /**
  *
  * timer() provides a cleaner way to handle intervals  
  *
  *	@usage
  * $.timer(interval, callback);
  *
  *
  * @example
  * $.timer(1000, function (timer) {
  * 	alert("hello");
  * 	timer.stop();
  * });
  * @desc Show an alert box after 1 second and stop
  * 
  * @example
  * var second = false;
  *	$.timer(1000, function (timer) {
  *		if (!second) {
  *			alert('First time!');
  *			second = true;
  *			timer.reset(3000);
  *		}
  *		else {
  *			alert('Second time');
  *			timer.stop();
  *		}
  *	});
  * @desc Show an alert box after 1 second and show another after 3 seconds
  *
  * 
  */

	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };
