﻿

var httpRequestObj
var myXML






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



function getNews(s) {

	//var url =  "http://rss.news.yahoo.com/rss/topstories"
	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(myXML)
			if(s) {
				populateSilverlightNews(s)
			}
		}
	}

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


//getNews()  //try to load first to pre-cache news


function populateSilverlightNews(s) {

	
	if(navigator.appName!="Microsoft Internet Explorer") {
		//weak Firefox, strip out white space
		stripWhiteSpace(myXML)
	}
		
	var stories=myXML.getElementsByTagName('item')
	
	rssCon=s.findName("rssContainer")
	
	for (i=0;i<10;i++) {		
		
		
		title_val=stories[i].childNodes[0].firstChild.nodeValue
		title_val=title_val.substring(0,title_val.length-6)	
		
		
		desc_val=stories[i].childNodes[4].firstChild.nodeValue
		start_desc=desc_val.indexOf("</a>")
		end_desc=desc_val.indexOf("</p>")
		
		re=/&/g
		
		title_val=title_val.replace(re," &amp;")
		
		
		


		if(start_desc==-1) {
			desc_val=stories[i].childNodes[4].firstChild.nodeValue	
		}
		else {
			desc_val=desc_val.substring(start_desc+4, end_desc)
			desc_val=desc_val.replace(re," &amp;")
		}
		
		
	
		if(stories[i].getElementsByTagName("media:content").length>0) {
		//if(1) {
			img_val=stories[i].getElementsByTagName("media:content")[0].attributes.getNamedItem("url").value.split("?")[0]
			//img_val="good"
			//alert(i+" - "+img_val)
		}
		else {
			img_val=""
		}
		//alert(img_val)
		
		//alert(stories[i].getElementsByTagName("media:content"))
		
		
		
		var	news_story_str =  '<Canvas Name="headlineHolder_'+i+'" Canvas.Top="'+(i*135+15)+'" Canvas.Left="15">'
			news_story_str += '		<Rectangle Fill="#111" Width="540" Height="30" />'
			news_story_str += '		<TextBlock Name="newsTitle_'+i+'" Foreground="#FFFFFF" FontSize="16" FontFamily="Georgia" FontWeight="Bold" Canvas.Top="6" Canvas.Left="8">'+title_val+'</TextBlock>'
			news_story_str += '		<TextBlock Name="newsStory_'+i+'" TextWrapping="Wrap" Foreground="#FFF" FontSize="10" FontFamily="Georgia" Canvas.Top="46" Canvas.Left="138" Width="400">'+desc_val+'</TextBlock>'
			//news_story_str += '		<TextBlock Name="newsStoryX_'+i+'" TextWrapping="Wrap" Foreground="#FFF" FontSize="10" FontFamily="Georgia" Canvas.Top="46" Canvas.Left="0" Width="400">'+img_val+'</TextBlock>'
			news_story_str += '		<Image Name="newsPic_'+i+'" Width="120" Height="80" Stretch="Fill" Canvas.Left="10" Canvas.Top="46" Source="'+img_val+'"/>'
			news_story_str += '</Canvas>'


	
			storyTags=plugin.content.createFromXaml(news_story_str)
	
			rssCon.children.add(storyTags)		
		
  	}
	
	
	/*
	
	title_val=myXML.childNodes[1].firstChild.childNodes[8].childNodes[0].firstChild.nodeValue		
	title_val=title_val.substring(0,title_val.length-6)	
	s.findName("newsTitle").Text=title_val
	
	
	desc_val=myXML.childNodes[1].firstChild.childNodes[8].childNodes[4].firstChild.nodeValue	
	start_desc=desc_val.indexOf("</a>")
	end_desc=desc_val.indexOf("</p>")
	desc_val=desc_val.substring(start_desc+4, end_desc)
	s.findName("newsStory").Text=desc_val
	
	img_val=myXML.childNodes[1].firstChild.childNodes[8].getElementsByTagName("media:content")[0].attributes.getNamedItem("url").value
	
	s.findName("newsPic1").Source=img_val
	
	

	desc_val=myXML.childNodes[1].firstChild.childNodes[9].childNodes[4].firstChild.nodeValue	
	start_desc=desc_val.indexOf("</a>")
	end_desc=desc_val.indexOf("</p>")
	desc_val=desc_val.substring(start_desc+4, end_desc)
	s.findName("newsStory2").Text=desc_val
	
	
	title_val=myXML.childNodes[1].firstChild.childNodes[9].childNodes[0].firstChild.nodeValue		
	title_val=title_val.substring(0,title_val.length-6)	
	s.findName("newsTitle2").Text=title_val	
	
	img_val=myXML.childNodes[1].firstChild.childNodes[9].getElementsByTagName("media:content")[0].attributes.getNamedItem("url").value
	s.findName("newsPic2").Source=img_val
	
	*/
}