$(document).ready(function(){

FB.ensureInit ( function () {
        FB.Connect.ifUserConnected(update_user_box, noupdate_user_box);
});

	$(".titrelisting").css({opacity: 0});

	$("#nav ul li a").hover(function(){$(this).next("span").fadeIn(500) }, function () {$(this).next("span").fadeOut(500);});

	$(".arrow-top-medium-white").animate({left: '+=300'}, 3000);
	
	$("a.extlink").click( function() {
        window.open( $(this).attr('href') );
        return false;
    });	
	
	$("#linkstar").hover(function () {
			$("#linkstar .titrelisting").stop().animate({opacity: 1}, '300');}, 
		function () {
			$("#linkstar .titrelisting").stop().animate({opacity: 0}, '300');}
	);	
	
	$("#linkcom").hover(function () {
			$("#linkcom .titrelisting").stop().animate({opacity: 1}, '300');}, 
		function () {
			$("#linkcom .titrelisting").stop().animate({opacity: 0}, '300');}
	);		
	
	$("#linkliked").hover(function () {
			$("#linkliked .titrelisting").stop().animate({opacity: 1}, '1000');}, 
		function () {
			$("#linkliked .titrelisting").stop().animate({opacity: 0}, '1000');}
	);
	
	$("#linkplus").hover(function () {
			$("#linkplus .titrelisting").stop().animate({opacity: 1}, '300');}, 
		function () {
			$("#linkplus .titrelisting").stop().animate({opacity: 0}, '300');}
	);		
	
	$('#wp-data').cycle({ 
		fx:    'fade', 
		timeout: 5000,
		speed: 500
	}).show();	

	/* Search */
	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}
	$("#srch_fld").autocomplete("/wp-content/themes/kilone/ajaxsearch.php", {
		width: 183,
		selectFirst: false
	});		
});

jQuery.fn.reverse=Array.prototype.reverse;
String.prototype.linkify=function(){
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g,function(m) {
		return m.link(m);
	});
	};
String.prototype.linkuser=function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/g,function(u) {
		var username=u.replace("@","")
		return u.link("http://twitter.com/"+username);
	});
	};

String.prototype.linktag=function() {
	return this.replace(/[#]+[A-Za-z0-9-_]+/,function(t) {
	var tag=t.replace("#","%23")
	return t.link("http://search.twitter.com/search?q="+tag);});};
	
	function fetch_tweets(elem)
{elem=$(elem);
input=elem.attr('title');
lang=elem.attr('lang');
if(input!=window.monitter['text-'+input])
{window.monitter['last_id'+input]=0;window.monitter['text-'+input]=input;window.monitter['count-'+input]=12;}

var url="http://search.twitter.com/search.json?q="+input+"&lang="+lang+"&rpp="+rrp+"&since_id="+window.monitter['last_id'+input]+"&callback=?";
$.getJSON(url,function(json)
{
$('div.echotweet:gt(4)',elem).each(function(){$(this).fadeOut('slow')});
$(json.results).reverse().each(function()
{
if($('#tw'+this.id,elem).length==0)
{
window.monitter['count-'+input]++;
var thedate=new Date(Date.parse(this.created_at));
var thedatestr=thedate.getHours()+':'+thedate.getMinutes();
var divstr='<div id="tw'+this.id+'" class="echotweet"><img style="float:left;margin:0 3px 0 0;" width="40" height="40" src="'+this.profile_image_url+'" ><b><a href="http://twitter.com/'+this.from_user+'" onclick="window.open(this.href); return false;">'+this.from_user+'</a></b> : '+this.text.linkify().linkuser().linktag()+' &nbsp;-&nbsp;<b>'+thedatestr+'</b></div>';
window.monitter['last_id'+input]=this.id;
elem.prepend(divstr);
$('#tw'+this.id,elem).hide();
$('#tw'+this.id+' img',elem).hide();
$('#tw'+this.id+' img',elem).fadeIn(4000);
$('#tw'+this.id,elem).fadeIn('slow');
}});
input=escape(input);
rrp=1;
setTimeout(function(){fetch_tweets(elem)},30000);});return(false);}

$(document).ready(function(){window.monitter={};
$('.monitter').each(function(e){rrp=6;fetch_tweets(this);
});

});		