/*!
 * http://www.reynoldsftw.com
 *
 * Created by Steve Reynolds
 */

var timer;   
  
$(document).ready(function() {   
  
	if ($("#twitter_feed").length > 0) {
		$("#twitter_feed > *").hide();
		getSearch(); 
	}
	
	if ($("#tweets").length > 0) {
		getTweets(); 
	}
  
  	$('.newWindow').live("click", function() {
		 window.open(this.href, 'twitterWindow', 'width=680, height=800, resizable = 1, status=0, toolbar=0, location=0, scrollbars=1');
		 return false;
	});
  
});
  
function getSearch(fadeItIn)   
{

		var title = new Array();
		var name = new Array();
		var uri = new Array();
		var image = new Array();
		var id = new Array();
		
		//clearTimeout(timer);   
		var results = "";
		var theQuery = "alliansen";
		
		$.post("wp-content/themes/default/js/getSearch.php", {
			query: theQuery
		}, function(xml){
			$('entry', xml).each(function(i){
			
				title[i] = $(this).find("title").text();
				name[i] = $(this).find("name").text();
				uri[i] = $(this).find("uri").text();
				image[i] = $(this).find("link[rel='image']").attr("href");
				id[i] = $(this).find("id").text();
				
			});
			
			title[0] = hyperlinks(title[0]);
			title[0] = twitter_users(title[0]);
			
			var tit = title[0].toString();
			
			
			results = "<a href=\"twitterfeed\" class=\"newWindow\" style=\"font-weight:bold;\">Senast från Twitter</a>: <a href='" + uri[0] + "' rel='nofollow'>" + name[0] + "</a>" + " " + tit + "</a></a><a href=\"twitterfeed\" class=\"newWindow\"> [Läs alla]</a>";
			
			$("#twitter_feed").html(results);
			if (fadeItIn != 1) {
				$('#twitter_feed').hide().fadeIn(1000);
			}
			
		});
		
		
		timer = setTimeout('getSearch(1)', 30000);
}

function getTweets()   
{
	var title = new Array();  
	var name = new Array();  
	var uri = new Array();  
	var image = new Array();  
	var id = new Array(); 

	var results = "";   
	var theQuery = "alliansen";   
  
	$.post("../wp-content/themes/default/js/getSearch.php", {query: theQuery},  function(xml){   
		$('entry',xml).each(function(i){   
			
			title[i] = $(this).find("title").text();  
			title[i] = hyperlinks(title[i]);
			title[i] = twitter_users(title[i]);
			name[i] = $(this).find("name").text();  
			uri[i] = $(this).find("uri").text();  
			image[i] = $(this).find("link[rel='image']").attr("href");  
			id[i] = $(this).find("id").text(); 
			
			results += "<div class='tweet'><img src='"+image[i]+"' height='48' width='48' class='fl_left' /><p class='fl_left' style='margin-left: 10px; width: 460px;'><a href='" + uri[i] + "' rel='nofollow'>" + name[i] + "</a><br />" + "" + title[i] + "</p><br class='fl_clear' /><div style='clear:both;height:0'></div></div>"; 
		});   
		
		$("#tweets").html(results);
	
	});     
}

function hyperlinks(text) {
	text = text.replace(/\s(http:\/\/[a-z][a-zA-Z0-9\/\*\-\?\.\&\%\$]*)/ig," <a href=\"$1\" class=\"twitter-link\">$1</a>");
	text = text.replace(/\s([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/ig," <a href=\"$1\" class=\"twitter-link\">$1</a>$2");
	// match www.something.domain/path/file.extension?some=variable&another=asf%
	text = text.replace(/\s(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/ig," <a href=\"http://$1\" class=\"twitter-link\">$1</a>$2");
	return text;
}

function twitter_users(text) {
    text = text.replace(/([\.|\,|\:|\�|\�|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/ig, "$1<a href=\"http://twitter.com/$2\" class=\"twitter-user\">@$2</a>$3 ");
    return text;
}