
var httpRequestObj
var myXML = null
var numTwits = null

function getXML(s) {
	
	var tweetCon = s.findName("tweetContainer")
	
	// clear any tweets that are in the container before proceeding.
	tweetCon.children.clear();

	var url =  "proxy.php"

	if (window.ActiveXObject) {
		httpRequestObj = new ActiveXObject("Microsoft.XMLHTTP")
		//httpRequestObj = new ActiveXObject("Msxml2.XMLHTTP")
	}
	
	else {
		httpRequestObj = new XMLHttpRequest()
	}

	httpRequestObj.open("GET", url, true)
	httpRequestObj.onreadystatechange= function () {
		if (httpRequestObj.readyState == 4) {
			myXML=httpRequestObj.responseXML
			//alert(httpRequestObj.readyState)
			if(s && myXML != null) {
				populateSilverlightCanvas(s)
			}
		}
	}

	httpRequestObj.setRequestHeader('Accept','message/x-formresult')
	httpRequestObj.send(null)
	return false
}

function populateSilverlightCanvas(s) {
	
	var tweetCon = s.findName("tweetContainer")
	
	//weak Firefox, strip out white space
	if (navigator.appName != "Microsoft Internet Explorer") stripWhiteSpace(myXML)
		
	var tweets = myXML.getElementsByTagName('status')
	
	var canvasPosition = 10;
	
	numTwits = tweets.length;
	
	if (numTwits == 0) {
		alert("There was a problem contacting the Twitter Servers.")
		return
	}
	
	
	for (i=0; i<numTwits; i++) {		
		
		var tweet_val = tweets[i].childNodes[2].firstChild.nodeValue

		tweet_val = unescape(tweet_val)
		
		var tweetter_val = tweets[i].childNodes[5].childNodes[1].firstChild.nodeValue
		
		var img_url = tweets[i].childNodes[5].childNodes[5].firstChild.nodeValue
		
		if (!img_url.toLowerCase().match(".jpg") && !img_url.toLowerCase().match(".png") && img_url.toLowerCase().substr(0,7) == "http://") {
			img_url = "no_gifs_in_silverlight.png";
		}
		
		var	tweet_tags_str =  '<Canvas Name="tweetHolder_' + i + '" Canvas.Top="' + canvasPosition + '" Canvas.Left="0">'
			tweet_tags_str += '		<TextBlock Name="Twitter_' + i + '" Foreground="#0088BC" TextWrapping="Wrap" FontSize="12" FontFamily="Arial" FontWeight="bold" Canvas.Top="15" Canvas.Left="60" Width="290">' + tweetter_val + ' Says:</TextBlock>'
			tweet_tags_str += '		<TextBlock Name="Tweet_' + i + '" Foreground="#0088BC" TextWrapping="Wrap" FontSize="9" FontFamily="Arial" Canvas.Top="32" Canvas.Left="60" Width="290">' + tweet_val + '</TextBlock>'
			tweet_tags_str += '		<Rectangle Width="52" Height="52" Fill="#FF777777" Canvas.Left="0" Canvas.Top="10"/>'
			tweet_tags_str += '		<Image Name="Avatar_' + i + '" Source="' + img_url + '" Width="48" Height="48" Canvas.Top="12" Canvas.Left="2"/>'
			tweet_tags_str += '		<Path Width="350" Height="1" Fill="#0088BC" Stretch="Fill" Stroke="#0088BC" Canvas.Left="0" Canvas.Top="0" Data="M59,147 L315.50049,147" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeStartLineCap="Round" StrokeThickness="2" StrokeDashArray="1"/>'
			tweet_tags_str += '</Canvas>'
			
		var tweetTags=plugin.content.createFromXaml(tweet_tags_str)

		tweetCon.children.add(tweetTags)
		
		var createdClip = s.findName('Tweet_'+i);
		canvasPosition += createdClip.actualHeight + 60;
		
  	}
	
	//alert(canvasPosition);
	
	configScroller(canvasPosition);
	
}

// STRIP WHITE SPACE FOR FIREFOX

function is_ws(nod) {
	return !(/[^\t\n\r ]/.test(nod.data))
}

function findWhiteSpace(node, nodeNo) {

	for (i=0; i<node.childNodes.length; i++) {
		if (node.childNodes[i].nodeType == 3 && is_ws(node.childNodes[i])) {
			nodesToDelete[nodesToDelete.length] = node.childNodes[i]
		}
		if (node.childNodes[i].hasChildNodes()) {
			findWhiteSpace(node.childNodes[i], i)
		}
	}
	node = node.parentNode
	i = nodeNo
}

function stripWhiteSpace(node) {

	nodesToDelete = Array()
	findWhiteSpace(node, 0)

	for(i=nodesToDelete.length-1;i>=0;i--) {
		nodeRef = nodesToDelete[i]
		nodeRef.parentNode.removeChild(nodeRef)
	}
}