function checkXML(name, response)
{
	var result = response.responseXML.getElementsByTagName(name)[0].firstChild;
	if(result == null) return false;
	else return true;
}

function resultXML(name, response)
{
	var result = response.responseXML.getElementsByTagName(name)[0].firstChild.nodeValue;
	return result;
}

var Twitter = "http://api.twitter.com/1/statuses/user_timeline/Sweru.xml"

function getTwitter(){
	new Ajax.Request("/lib/module/proxy.php?url=" + Twitter + "&mimetype=text/xml",
	{
		method: "get",
		/* onLoading: function(response) {
			$("snsText").innerHTML = "Loading from Twitter...";
		}, */
		onSuccess: function(response) {
			try	{
				if(!checkXML("profile_image_url", response)) $("snsIcon").src = "./img/_blank.gif";
				else $("snsIcon").src = resultXML("profile_image_url", response);
	
				var snsText = resultXML("text", response).replace(/((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\wㄱ-ㅎㅏ-ㅣ가-힣\;\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)/g, "<a href=\"$1\">$1</a>");
				snsText = snsText.replace(/@(\S+)/g, "@<a href=\"http://twitter.com/$1\">$1</a>");
				
				$("snsText").innerHTML = snsText;
	
				var getDate = resultXML("created_at", response).substring(0, 19) + " GMT" + resultXML("created_at", response).substring(20, 30);
				var convertDate = new Date(getDate);
				var snsDate = new Date(convertDate.getTime() - convertDate.getTimezoneOffset());
	
				$("snsTag").innerHTML = snsDate.toString().substring(0, 16) + " - " + resultXML("source", response);
			} catch(e) {
				$("snsText").innerHTML = e; //"There was a problem retrieving the Twitter data.";
			}
		},
		onFailure: function(response) {
			$("snsText").innerHTML = e;
		}
	})
}


var Facebook = "https://graph.facebook.com/me/feed?access_token=AAACEdEose0cBAO3Ievel2L4mPZCbC3CZCZCCgOeLNY7ZBeuCKJ2DLRGVB0qa7ZBgnyKLuNTf3X5GeoLRes6Jzd6u7KXjZBOcEMr4H7A0Yt3gZDZD";
// "https://developers.facebook.com/tools/explorer"

function getFacebook(){
	new Ajax.Request("/lib/module/proxy.php?url=" + Facebook + "&mimetype=text/xml",
	{
		method: "get",
		/* onLoading: function(response) {
			$("snsText").innerHTML = "Loading from Twitter...";
		}, */
		onSuccess: function(response) {
			try	{
				var fbText = response.responseText.evalJSON();

				$("fbName").innerHTML = fbText.data[0].from.name;
				$("fbText").innerHTML = fbText.data[0].message;
			} catch(e) {
				$("fbText").innerHTML = e; //"There was a problem retrieving the Twitter data.";
			}
		},
		onFailure: function(response) {
			$("fbText").innerHTML = e;
		}
	})
}

// addEvent(getFacebook);
