function sendAjaxRequest(url, eventhandler) {
  if (window.ActiveXObject) {
	var xhr = new ActiveXObject("Microsoft.XMLHTTP")
  } else if (window.XMLHttpRequest) {
	var xhr = new XMLHttpRequest();
  }
  
  if (url.match("[?]")) {
		var split = new Array();
		split = url.split("?");
		url = split[0];
		var postdata  = split[1];
	} else {
		var postdata = '';
	}
  
  xhr.onreadystatechange = function() {
	 if(xhr.readyState == 4)
		if(xhr.status == 200) {
		   eventhandler(xhr);
		} else {
		   //document.body.innerHTML = xhr.responseText
		}
  }
  
  xhr.open("POST", url, true )
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
  xhr.send(postdata)
}

function requestLastFm() {
	var lastfm = $('#lastfmul');
	var lastfmload = $('#lastfmload');
	
	sendAjaxRequest("/ajax/lastfm.php", function(ajaxRequest) {
		if (ajaxRequest.responseText) {
					
			var ans = eval('(' + ajaxRequest.responseText + ')');

			if (ans.succes) {
				$(lastfmload).remove();
				
				for (var i = 0; i < ans.plays.length; i++) {
					var li = document.createElement("LI");
						
						var a = document.createElement("A");
						a.title = ans.plays[i].title;
						a.href = ans.plays[i].url["0"];
						a.target = "TOP";
						
						a.innerHTML += ans.plays[i].disp;
						
						var img = document.createElement("IMG");
						img.src = ans.plays[i].albumart["0"];
						img.alt = ans.plays[i].title;
						img.title = ans.plays[i].title;
						img.style.width = "34px";
						img.style.height = "34px";
						
						$(a).prepend(img);
						
					$(li).append(a);
					
						
					
					$(lastfm).append(li);
				}
				
				//lastfm.innerHTML += ans.html;
			}
		}
	});	
}

function requestTwitter() {
	var twitter = $('#twitterul');
	var twitterload = $('#twitterload');
	
	sendAjaxRequest("/ajax/twitterfeed2.php", function(ajaxRequest) {
		if (ajaxRequest.responseText) {
					
			var ans = eval('(' + ajaxRequest.responseText + ')');
			
			if(ans['@attributes'].update == true) {
				updateTwitter();
			}
			
			if (ans.item) {
				$(twitterload).remove();
				
				for (var i = 0; i < 6; i++) {
					var li = document.createElement("LI");
						
						var div = document.createElement("DIV");
						$(div).addClass("tweet");
					
						var message = hyperlinks(ans.item[i].description);
						message = twitter_users(message);
						
						$(div).html(message);
						
							var profilePic = document.createElement("A");
							$(profilePic).attr("href", "http://www.twitter.com/" + ans.item[i].user);
							
								var img = document.createElement("IMG");
								$(img).attr("src", ans.item[i].avatar);
								$(img).css({
									width: "34px",
									height: "34px"
								});
							
							$(profilePic).append(img);

						$(div).prepend(profilePic);
					
						var br = document.createElement("BR");
					
						$(div).append(br);
						
							var a = document.createElement("A");
							$(a).attr("href", ans.item[i].url);
							$(a).addClass("twitter-link");
							$(a).text("# posted " + ans.item[i].date);
					
						$(div).append(a);
						
					$(li).append(div);
					
					twitter.append(li);
				}
			}
		}
	});	
}

function updateTwitter() {
	sendAjaxRequest("/ajax/twitterfeed.php", function(ajaxRequest) {
		if (ajaxRequest.responseText) {
			//console.log("twitter has been updated with the new xml");
		}
	});
}

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;
}

window.onload = function () {
	dp.SyntaxHighlighter.ClipboardSwf = "http://www.gayadesign.com/wp-content/themes/gdblocked/SyntaxHighlighter/Scripts/clipboard.swf";
	dp.SyntaxHighlighter.HighlightAll("code");
}
