/*var keywords_show,keywords_search,keywords_replace,active_key,tweets_per_fetch from twittycafe.php*/

var pe=null;
var mutex=0;
var timeline;
var timeline_cursor;
var update_count;

function highlight_keywords(text,keyword_list) {
	for(i=0;i<keyword_list.length;i++){
		keyword=keyword_list[i];
		pattern=new RegExp(keyword,'gi');
		text=text.replace(pattern,'<span class="highlighted">'+keyword+'</span>');
	}
	return text;
}

function update_timeline(key) {
	user=timeline[timeline_cursor].user;
	text=timeline[timeline_cursor].text;
	keyword_list=keywords_replace[key].split(" ");
	text=highlight_keywords(text,keyword_list);
	HTMLtoinsert='<div class="item item-'+key+'" style="display:none"><img src="/twittycafe/img/white-bird-'+key+'.gif" alt="" /><p>'+user+': '+text+'</p></div>';

//	HTMLtoinsert="<div class='item' style='display:none'><span class='marked"+key+"'>"+keywords_search[key]+"</span><br /><span class='user'>"+timeline[timeline_cursor].user+"</span>: "+timeline[timeline_cursor].text+"</div>";
//	HTMLtoinsert="<div class='item' style='display:none'><span class='user'>"+timeline[timeline_cursor].user+"</span> "+timeline[timeline_cursor].text.replace(keywords_search[key], "<br /><span class='marked"+key+"'>"+keywords_search[key]+"</span>")+"</div>";
//	HTMLtoinsert="<div class='item'><span class='user'>"+timeline[timeline_cursor].user+"</span> <span class='tweet'>"+timeline[timeline_cursor].text+"</span></div>";

	timeline_cursor++;
	if(timeline_cursor>=timeline.length) timeline_cursor=0;
	Element.insert('twittycafe_posts_inner', { bottom: HTMLtoinsert });
	last_elem_index=$$('.item').length-1;
	new Effect.BlindDown($$('.item')[last_elem_index], { duration: 0.6 });
	
	update_count++;
	if(update_count>5)
		$$('.item')[0].remove();

/*		new Effect.BlindUp($$('.item')[5], {
			duration: 0.6,
			afterFinish: function(){
				$$('.item')[5].remove();
			}
		});*/
}

function fetch_row(key) {
	new Ajax.Request('/twittycafe/twittycafe-updater.php', {
		method:'get',
		parameters: {q: keywords_search[key]},
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport){
			timeline=transport.responseText.evalJSON(true);
			timeline_cursor=Math.floor(Math.random()*timeline.length);
			update_timeline(key);
			pe=new PeriodicalExecuter(update_timeline.bind(this,key),5);
		},
		onComplete: function(transport){
			mutex=0;
			$('twittycafe_spinner').style.display='none';
		}
	});
}

function changekey(new_key) {
	if(active_key==new_key) return;
	if(mutex) return;

	mutex=1;
	if(pe){pe.stop();pe=null;}
	$('twittycafe_spinner').src='/twittycafe/img/ajax-loader-'+new_key+'.gif';
	$('twittycafe_spinner').style.display='';
	if(active_key!=-1){
		$('twittycafe_bird_'+active_key).removeClassName('in');
		$('twittycafe_bird_'+active_key).update(keywords_show[active_key]);
	}
	$('twittycafe_mainbox').style.backgroundImage='url(/twittycafe/img/box-bg-'+new_key+'.gif)';
	$('twittycafe_keyword').update(keywords_show[new_key]);

	$('twittycafe_bird_'+new_key).addClassName('in');
	$('twittycafe_bird_'+new_key).update('');
	
	$$('#twittycafe_box .item').each(
		function(item){
			item.remove();
		}
	);
	update_count=0;
	fetch_row(new_key);
	active_key=new_key;
}

function init(){
	tmp=new Array();
	for(i=0;i<keywords_search.length;i++){
		tmp[i]=new Image;tmp[i].src='/twittycafe/img/box-bg-'+i+'.gif';
		Event.observe($('twittycafe_bird_'+i), 'click', changekey.bind(this,i));
	}
	changekey(2);
}

Event.observe(window, 'load', init);
