// JavaScript Document

var twWindows = 0;
		
function loadTweeter(tweeterName)
{
	$(".twWindow").fadeOut("fast");

	twWindows++;
	z = document.getElementById(tweeterName);

	posX = findPos(z)[0];
	posY = findPos(z)[1];

	a = document.createElement("div");
	a.setAttribute("class", "twWindow");
	a.setAttribute("id", "w" + twWindows);

	a.innerHTML = twWindowTopHTML(false) + "<br /><br /><br /><br /><br /><center><img src='/images/ajax-loader.gif' alt='loading' /></center>" + twWindowBottomHTML(false);

	a.style.left = (posX + 24 - 180) + "px";
	a.style.top = (posY +24 - 120) + "px";
	
	$("body").append(a);
	$("#w" + twWindows).fadeIn("slow");

	$.get("/jsonproxy.cfm?q=" + tweeterName, function(data){
	
		// Parse the JSON.
		eval("var jsonData = " + data + ";");

		// Generate the HTML for the current status.
		if(jsonData["status"]["text"] != "") {
			var showTweet = true;
			var tweetBody = jsonData["status"]["text"];

			// Replace URLs with links.
			tweetBody = tweetBody.replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim, '<a href="$&" target="_blank">$&</a>').replace(/([^\/])(www[\S]+(\b|$))/gim, '$1<a href="http://$2" target="_blank">$2</a>');

			// Replace out @Replys with links.
			var tweetMatch = /(@[a-zA-Z0-9_]*)/g;			
			tweetBody = tweetBody.replace(tweetMatch, "<a href='http://twitter.com/$1' target='new'><strong>$1</strong></a>");
			tweetBody = tweetBody.replace(/a href\=\'http\:\/\/twitter\.com\/\@/g, "a href='http://twitter.com/");
			
			var statusText = "<div class='statTweet-mid'>" + tweetBody + "</div><div class='statTweet-low'></div>";
		} else {
			var showTweet = false;
			var statusText = "";
		}
		
		// Generate HTML for the ID and Fullname.
		var tweeterName = "<strong>ID: <a href='http://twitter.com/" + jsonData["screen_name"] + "' target='_blank'>@" + jsonData["screen_name"] + "</a></strong>";
		var fullName = "<br /><strong>Name:</strong> <i>" + jsonData["name"] + "</i>"

		// Generate HTML for the Bio.
		if(jsonData["description"] != "")
			var bioText = "<div class='clearboth'></div><strong>Bio:</strong><br /><div class='tweeterDesc'>" + jsonData["description"] + "</div>";
		else
			var bioText = "";
		
		// Set the Window's HTML to the generated HTML.
		a.innerHTML = twWindowTopHTML(showTweet) + statusText + "<div class='twWindow-actualcontent'><img class='tweeter' src='" + jsonData["profile_image_url"] + "' align='left' width='48' height='48' />" + tweeterName + fullName + bioText + "</div>" + twWindowBottomHTML(true);
		
	});	
}

function closeTweeter() {
	$(".twWindow").fadeOut("fast");	
}

function twWindowTopHTML(tweetStatus) {
	statimg = "";
	if(tweetStatus)
		statimg = "<img src='/images/tweetStatus_01.png' alt='' />"
	
	return "<div class='twWindow-top'>" + statimg + "</div><div class='twWindow-mid'><div class='twWindow-content'>";
}

function twWindowBottomHTML(close) {
	if(close)
		return "</div></div><div class='twWindow-low'><div class='twWindowClose'><a href='javascript:closeTweeter();'><img src='/images/closeWindow.png' alt='Close Window' /></a></div></div>";	
	else
		return "</div></div><div class='twWindow-low'></div>";
}


function findPos(obj) {
	var curleft = curtop = 0;
	
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	
	return [curleft,curtop];
}

function loadSearch() { 
	window.location = getTermUrl(document.searchForm.q.value);
}

function getTermUrl(term)
{
	term = term.split(' ').join('+')
	
	if(term.match(/^[a-zA-Z0-9_ ]/)) {
		return '/search/' + escape(term);
	} else {	
		return '/?q=' + escape(term);
	}
}

