TweefindWidget = {
	lastrank : 0,
	alreadyprocessed : '',
	readytorefresh : 1,
	apikey : '4dd201147123549987c2a6e681b791c9',
	init : function (){
		var poststring = 'q='+escape(tweefindtag)+'&mode=json&lang='+tweelang+'&apikey='+TweefindWidget.apikey;
		var communication = YAHOO.util.Connect.asyncRequest('POST', '../api/search.php', initCallBack, poststring);
		TweefindWidget.startPoll();
	},
	packBadge : function (jsonentry){
		imgurl   = jsonentry['profile_image_url'];
		rank     = (jsonentry['rank']/100)*8;
		fromuser = jsonentry['from_user'];
		msgid    = jsonentry['id'];
		msgtext  = jsonentry['text'];
		pattern = /(http:\/\/)([a-zA-Z0-9\.\_\/<>-]+)/g;
		msgtext = msgtext.replace(pattern,"<a class='userid' target='_blank' href=\"http://$2\">$1$2</a>");
		messagetime = jsonentry['created_at'];
		now = new Date().getTime();
		difftime = now - messagetime;
		if (difftime < 60) messagetime = "less than 1 minute ago";
		else {
				thedate = new Date(messagetime).toString();
				gmtoffset = thedate.indexOf("GMT");
				messagetime = thedate.substr(0,gmtoffset);
		}
		cardid = msgid+"_"+jsonentry['rank'];
		badge = "<div class='card' id='"+cardid+"'>"+
						"<div class='imgplace'>"+
						" <img width='46' height='46' src='"+imgurl+"' /><br />"+
						" <div class='rank' style='margin-top:4px'><img src='../images/rankg"+Math.round(rank)+".gif' /></div>"+
						"</div>"+
						"<div class='msgplace'>"+
							"<a target='_blank' href='http://twitter.com/"+fromuser+"' class='userid'>"+fromuser+"</a>: "+msgtext+
							"<div class='msgdata'>"+messagetime+" &nbsp;<a target='_blank' href='https://twitter.com/home?status=@"+fromuser+"&amp;in_reply_to_status_id="+msgid+"&amp;in_reply_to="+fromuser+"'><img border=0 src='../images/reply.gif' /></a>"+
							" <a target='_blank' href='http://www.tweefight.com/?opp="+fromuser+"'><img title='fight against "+fromuser+" on Tweefight.com' alt=''fight against "+fromuser+" on Tweefight.com' src='../images/tweefight.gif' border=0 /></a>"+
						"</div>"+
						"</div><div class='sep'></div>"+
					 "</div>";
		return badge;
	},
	refreshContent : function (){
		//if (TweefindWidget.readytorefresh == 1){
			var dummy = new Date().getTime();
			//var poststring = 'q='+escape(tweefindtag)+'&mode=json&rpp=30&lang='+tweelang+'&apikey='+TweefindWidget.apikey+'&dummy='+dummy;
			var poststring = 'q='+escape(tweefindtag)+'&mode=json&lang='+tweelang+'&apikey='+TweefindWidget.apikey+'&dummy='+dummy;
			//var communication = YAHOO.util.Connect.asyncRequest('POST', '../api/search.php', refreshCallBack, poststring);
			var communication = YAHOO.util.Connect.asyncRequest('POST', '../api/search.php', initCallBack, poststring);
			//TweefindWidget.readytorefresh == 0;
		//}
	},
	stopPoll : function (){
		clearInterval(tweeinterval);
		document.getElementById('pausebutton').style.display = 'none';
		document.getElementById('playbutton').style.display = 'block';
	},
	startPoll : function (){
		tweeinterval = setInterval("TweefindWidget.refreshContent()",20000);
		document.getElementById('pausebutton').style.display = 'block';
		document.getElementById('playbutton').style.display = 'none';
	}
	
}

initCallBack = {
	success : function(o) {
				resp = o.responseText;	
				jsonCall = YAHOO.lang.JSON.parse(resp);
				res = jsonCall['results'];
				htmlres = '';
				for (i=0;i < tweeslots;i++){
					TweefindWidget.lastrank = res[i]['rank'];
					//TweefindWidget.rankarray[i] = res[i]['rank'];
					TweefindWidget.alreadyprocessed += res[i]['id']+"_-_";
					cardid = res[i]['id']+"_"+res[i]['rank'];
					htmlres += "<div class='card' id='"+cardid+"'>"+TweefindWidget.packBadge(res[i])+"</div>";
					
				}
				document.getElementById('widgetwrapper').innerHTML = htmlres;
		},
		fails : function (o) {
			
		}
}
/*
refreshCallBack = {
	success : function(o) {
				resp = o.responseText;	
				jsonCall = YAHOO.lang.JSON.parse(resp);
				res = jsonCall['results'];
				htmlres = '';
				newrank = res[0]['rank'];
				cardid = res[0]['id']+"_"+res[0]['rank'];
				wrapper = document.getElementById('widgetwrapper');
				if (TweefindWidget.alreadyprocessed.indexOf(res[0]['id'])!=-1) return;
				if (newrank >= TweefindWidget.lastrank){
					lastchild = wrapper.lastChild;
					wrapper.removeChild(lastchild);
					insbef = -1;
					nodebefore = null;
					for (i=0;i<wrapper.childNodes.length;i++){
						theid = wrapper.childNodes[i].getAttribute('id');
						splitted = theid.split("_");
						if (newrank > splitted[1]) {
							insbef = i;
							nodebefore = wrapper.childNodes[i];
							break;
						}
					}
					thediv = document.createElement("div");
					thediv.setAttribute('id',cardid);
					thediv.setAttribute('class','card');
					thediv.innerHTML = TweefindWidget.packBadge(res[0]);
					if (insbef == -1) wrapper.appendChild(thediv);
					else wrapper.insertBefore(thediv,nodebefore);
					TweefindWidget.alreadyprocessed += res[0]['id']+"_-_";
				}
				TweefindWidget.readytorefresh = 1;
		},
		fails : function (o) {
			
		}
}*/