function setActiveStyleSheet(title) 
{
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
   {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) 
     {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
   $.cookie('monitter_style',title);
}
// Twitter stream js:
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("@","wapzan")
    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.twitter['text-'+input])
		{
			window.twitter['last_id'+input] = 0;
			window.twitter['text-'+input] 	= input;
			window.twitter['count-'+input]  = 12;;
		}
		
		
		if(window.twitter['count-'+input] > 10)
		{
			elem.prepend('<div class="tweet"><img src="http://monitter.com/widget/favicon.gif" align="absmiddle" />real time twitter by: <a href="http://monitter.com" target="_blank">monitter.com</a></div>');
			window.twitter['count-'+input] = 0;
		}
		
		var url = "http://search.twitter.com/search.json?q="+input+"&lang="+lang+"&rpp="+rrp+"&since_id="+window.twitter['last_id'+input]+"&callback=?";
		//var url = "http://monitter.com/widget/test.json?&callback=?";
		console.log(url);
		$.getJSON(url, function(json)
		{			
			console.log('hello');
			$('div.tweet:gt('+window.twitter['limit']+')',elem).each(function(){$(this).fadeOut('slow')});
			
			$(json.results).reverse().each(function()
			{
				if($('#tw'+this.id,elem).length == 0)
				{
					window.twitter['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="tweet"><img width="48" height="48" src="'+this.profile_image_url+'" ><p class="text">'+this.text.linkify().linkuser().linktag()+'<br />&nbsp;<b><a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a></b> &nbsp;-&nbsp;<b>'+thedatestr+'</b></p></div>';
				
					window.twitter['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)},5000);
		});
	
	return(false);	
}
$(document).ready(function(){});

$(document).ready(function(){

	window.twitter = {};
	$('.monitter').each(function(e)	{
		
		rrp = 6;
		fetch_tweets(this);
	});

	$('#slidebox').slideDown(1000);
		
	if($.cookie('monitter_style'))
	{
		setActiveStyleSheet($.cookie('monitter_style'));
		
		if($.cookie('monitter_style') == 'Light')
		{
			$('#header a.themer').text(window.monitter['darktheme']);		
		}
		else if($.cookie('monitter_style') == 'Dark')
		{	
			$('#header a.themer').text(window.monitter['lighttheme']);
		}
	}

	$('a.themer').click(function()
	{
		if($('#header a.themer').text() == window.monitter['lighttheme'])
		{
			setActiveStyleSheet('Light');
			$('#header a.themer').text(window.monitter['darktheme']);
		}
		else
		{
			setActiveStyleSheet('Dark');
			$('#header a.themer').text(window.monitter['lighttheme']);
		}
	});


});
