addLoadEvent(init);


var request;


function init(){
progInd = new ProgressIndicator(document.getElementById('progressGraphic'), "/images/prog");
getRSS('nprjazz');
rssForm();
}

function createRequest(url) {
   
	request = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			request = new XMLHttpRequest();
        } catch(e) {
			request = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	request = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		request = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		request = false;
        	}
		}
    }
	if(request) {
	    request.onreadystatechange = updatePage;
		request.open("GET", url, true);
		request.send("");
		return url;
	}
}

function getRSS(str) {
var url="/getrss3.php"
if(str == "Choose RSS Feed...") {
return false;
} else {
url=url+"?q="+str
url = url + "&dummy=" + new Date().getTime();
createRequest(url);
}
 }
     
function updatePage() {
progInd.start();
if(request.readyState == 4){  
if (request.status == 200) {
progInd.stop();
clearAll(document.getElementById('feedtitle'));
clearAll(document.getElementById('feed'));
var theText = request.responseText;
var xmlDoc = request.responseXML;

var title = xmlDoc.getElementsByTagName('title')[0];

var link = xmlDoc.getElementsByTagName('link')[0];

if(title.childNodes.length >0)
{
var thisFeedtxt = xmlDoc.getElementsByTagName('title')[0].firstChild.nodeValue;
} else {
 var thisFeedtxt = "No RSS Title!";
}

if(link.childNodes.length >0){
var thisFeedlink = xmlDoc.getElementsByTagName('link')[0].firstChild.nodeValue;
} else {
var thisFeedlink = null;
}


var newFeed = document.getElementById('feed');
var feedDivTitle = document.getElementById('feedtitle');
var feedH = document.createElement('h1');
var feedA = document.createElement('a'); 
feedA.setAttribute('href', thisFeedlink);
var feedHed = document.createTextNode(thisFeedtxt);
feedA.appendChild(feedHed);
feedH.appendChild(feedA);
feedDivTitle.appendChild(feedH);
var allItems = xmlDoc.getElementsByTagName('item');

for (var i=0; i<allItems.length; i++){

var title = allItems[i].getElementsByTagName('title')[0];
if (title.childNodes.length >0) {
Leadtxt = title.firstChild.nodeValue;
}

var link = allItems[i].getElementsByTagName('link')[0];
if (link.childNodes.length >0) {
var Linktxt = link.firstChild.nodeValue;
}

var description = allItems[i].getElementsByTagName('description')[0];
if (description.childNodes.length >0)  {
var Desctxt = description.firstChild.nodeValue;
if(Desctxt.indexOf('<br') != -1){
var Clipstring = Desctxt.indexOf('<br');
Desctxt = Desctxt.substring(0, Clipstring);
} 
} else {
var Desctxt = "";
}


if (theText.indexOf('<pubDate>') != -1) {
var Pubtxt = allItems[i].getElementsByTagName('pubDate')[0].firstChild.nodeValue;
}
if (allItems[i].getElementsByTagName('creator')[0])
{
author = true;
var Authtxt = allItems[i].getElementsByTagName('creator')[0].firstChild.nodeValue;
} else {
if (allItems[i].getElementsByTagName('dc:creator')[0])
{
author = true;
var Authtxt = allItems[i].getElementsByTagName('dc:creator')[0].firstChild.nodeValue;
} else {
author = false;
}
}


//create elements
var oneHed = document.createElement('h3');
var oneDesc = document.createElement('p');
var oneLink = document.createElement('a');
var onePub = document.createElement('span');
if(author){
var oneAuth = document.createElement('span');
var theAuth = document.createTextNode(Authtxt);
oneAuth.className = "by";
}
//add link URL attribute
oneLink.setAttribute('href', Linktxt); 
//create document text nodes
var thisHed = document.createTextNode(Leadtxt);
var theCopy = document.createTextNode(Desctxt); 
var thePub = document.createTextNode(Pubtxt); 
onePub.className = "date";
//append elements to text nodes
oneLink.appendChild(thisHed);
oneHed.appendChild(oneLink);


oneDesc.innerHTML = Desctxt;


onePub.appendChild(thePub);
if(author) {
oneAuth.appendChild(theAuth);
}
//actually add to feed div here
newFeed.appendChild(oneHed);
if(author){
newFeed.appendChild(oneAuth);
}
newFeed.appendChild(onePub);
newFeed.appendChild(oneDesc);
}
}
  }
}

function rssForm() {
var rssForm = document.getElementById('rssform');
if(rssForm != null)
{
return;
} else {
//for nyt
var nyt = Array(); 

var nytEnv = Array();
nytEnv['name'] = 'NYT > Environment';
nytEnv['url'] = 'nytenviro';
nyt[0] = nytEnv;
var nytTech = Array();
nytTech['name'] = 'NYT > Technology';
nytTech['url'] = 'nyttech';
nyt[1] = nytTech;
var nytTv = Array();
nytTv['name'] = 'NYT > Television';
nytTv['url'] = 'nyttv';
nyt[2] = nytTv;
var nytThr = Array();
nytThr['name'] = 'NYT > Theater';
nytThr['url'] = 'nyttheater';
nyt[3] = nytThr;
var nytMv = Array();
nytMv['name'] = 'NYT > Movies';
nytMv['url'] = 'nytmv';
nyt[4] = nytMv;
var nytMu = Array();
nyt[5] = nytMu;
nytMu['name'] = 'NYT > Music';
nytMu['url'] = 'nytmus';

var npr = Array(); 

var nprArt = Array();
nprArt['name'] = 'NPR: Arts & Culture';
nprArt['url'] = 'nprarts';
npr[0] = nprArt;
var nprJazz = Array();
nprJazz['name'] = 'NPR: Jazz & Blues';
nprJazz['url'] = 'nprjazz';
npr[1] = nprJazz;
var nprPea = Array();
nprPea['name'] =  'NPR: People & Places';
nprPea['url'] = 'nprpeople';
npr[2] = nprPea;
var nprWesat = Array();
nprWesat['name'] = 'NPR: W E Saturday';
nprWesat['url'] = 'nprwesat';
npr[3] = nprWesat;       
var nprWesun = Array();
nprWesun['name'] = 'NPR: W E Sunday';
nprWesun['url'] = 'nprwesun';
npr[4] = nprWesun; 

var mr = Array();

var mrMov = Array();
mrMov['name'] = 'Rotten Tomatoes: Movies';
mrMov['url'] = 'rttomato';
mr[0] = mrMov;

var theForm = document.createElement('form'); 
theForm.setAttribute('id','rssform');
theForm.setAttribute('action', 'altajrss.js'); // path to script should be correct here!
var theSelection = document.createElement('select'); 
theSelection.setAttribute('name','selectrss');
theForm.appendChild(theSelection);
var topOpt = document.createElement('option'); 
var topTxt = document.createTextNode('Choose RSS Feed...');
topOpt.appendChild(topTxt);
theSelection.appendChild(topOpt);
var nytGrp = document.createElement('optgroup'); 
nytGrp.setAttribute('label','New York Times');
var nprGrp = document.createElement('optgroup');
nprGrp.setAttribute('label','National Public Radio');

var mrGrp = document.createElement('optgroup'); 
mrGrp.setAttribute('label','More...');

theSelection.appendChild(nytGrp);
theSelection.appendChild(nprGrp);
theSelection.appendChild(mrGrp);

for (var index=0; index< nyt.length; index++) {
var nytOpt = document.createElement('option');
nytOpt.setAttribute('value', nyt[index].url);
var nytTxt = document.createTextNode(nyt[index].name);
nytOpt.appendChild(nytTxt);
nytGrp.appendChild(nytOpt);
}


for (var j=0; j< npr.length; j++) {
var nprOpt = document.createElement('option');
nprOpt.setAttribute('value', npr[j].url);
var nprTxt = document.createTextNode(npr[j].name);
nprOpt.appendChild(nprTxt);
nprGrp.appendChild(nprOpt);
}


for (var k=0; k< mr.length; k++) {
var mrOpt = document.createElement('option');
mrOpt.setAttribute('value', mr[k].url);
var mrTxt = document.createTextNode(mr[k].name);
mrOpt.appendChild(mrTxt);
mrGrp.appendChild(mrOpt);
}

var brk = document.createElement('br');
theForm.appendChild(brk);
var ctrlBut = document.createElement('input');
ctrlBut.setAttribute('type', 'button');
ctrlBut.setAttribute('value', 'Go!');
ctrlBut.className = 'go';
ctrlBut.onclick = function(){
getRSS(theSelection.value);
}
theForm.appendChild(ctrlBut);
}
var newRss = document.getElementById('rss');
var curTitleDiv = document.getElementById('feedtitle');
newRss.insertBefore(theForm, curTitleDiv);
}


function clearAll(el) {
	while(el.firstChild) el.removeChild(el.firstChild);
}

/******** Progress Indicator borrowed from wikipedia widget*********/

function ProgressIndicator(element, imageBaseURL) {
    this.count = 0;
    this.timer = null;
    this.element = element;
    this.element.style.display = "none";
    this.imageBaseURL = imageBaseURL;
}

ProgressIndicator.prototype = {
    start : function () {
        this.element.style.display = "block"; 
        var thisHed = document.getElementById('feedtitle');
        clearAll(thisHed);
        thisHed.appendChild(this.element);
        if (this.timer) clearInterval(this.timer);
        this.tick();
        var localThis = this;
        this.timer = setInterval (function() { localThis.tick() }, 40);
    },

    stop : function () {
        clearInterval(this.timer);
        this.element.style.display = "none";
        
    },

    tick : function () {
        var imageURL = this.imageBaseURL + (this.count + 1) + ".png";
        this.element.src = imageURL;
        this.count = (this.count + 1) % 12;
    }
}



// This function not currently called -- 
// Function: findChild(element, nodeName, namespace)
// Scans the children of a given DOM element for a node matching nodeName, optionally in a given namespace.
//
// element: The DOM element to search.
// nodeName: The node name to search for.
// namespace: Optional namespace the node name must be in.
//
// Returns the child node if found, otherwise null.
//
function findChild(element, nodeName, namespace)
{
    var child;

    for (child = element.firstChild; child != null; child = child.nextSibling) {
        if (child.localName == nodeName) {
            if (namespace == null || child.namespaceURI == namespace)
                return child;
        }
    }

    return null;
}




