var newsReader = {
	feed: null,
	newsNavigation: null,
	newsContent: null,
	currentNode: 0,
	items: null
};
newsReader.parseDate = function(sDate){
    //QaD FIX
    try{
    var retval = "";
    var a1 = sDate.split("T");
    var a2 = a1[1].split("+");
    retval = a1[0] + " " + a2[0];
    return retval;
    }catch(e){
        return sDate;
    }
};
var gFeed = null;
newsReader.init = function(){
	this.feed = gFeed;
	this.newsNavigation = document.getElementById('newsNavigation');
	this.newsContent = document.getElementById('newsContent');
	newsReader.clear();	
	newsReader.fetch(this.feed);
}

newsReader.clear = function(){
    // clear navi items
	while(this.newsNavigation.hasChildNodes())
	    this.newsNavigation.removeChild(this.newsNavigation.firstChild);
	
	while(this.newsContent.hasChildNodes())
	    this.newsContent.removeChild(this.newsContent.firstChild);
}

newsReader.populateResults = function(oParser){	
	this.items = oParser.items;	
	this.currentNode = 0;
	this.showNode(this.currentNode);
}

newsReader.showNode = function(index){

    this.currentNode = index;
    this.clear();
    var ofr = document.createDocumentFragment();
 	this.currentNode = index;
    this.clear();
    var ofr = document.createDocumentFragment();
	// previous item
	var liPrev = document.createElement("li");
	if(index == 0){
	    liPrev.appendChild(document.createTextNode("« Prev"));
	}
	else{
	    var aPrev = document.createElement("a");
	    aPrev.appendChild(document.createTextNode("« Prev"));
	    aPrev.href = "javascript:newsReader.readPrevNode();";
	    liPrev.appendChild(aPrev);
	}
	liPrev.id = "prev";
	ofr.appendChild(liPrev);
	//if(this.items.length > 15)
	  //  this.items = this.items.slice(0,15);
	    var oLi = document.createElement("li");
	       var oA = document.createElement("span");
	        oA.appendChild(document.createTextNode((index+1) + " / " + this.items.length));
	        oLi.appendChild(oA);
			oLi.id = "pages";
	    ofr.appendChild(oLi);

	
	/*
	if(this.items.length > 15)
	    this.items = this.items.slice(0,15);
	for(var i = 0; i < this.items.length; i++){
	    var oLi = document.createElement("li");
	    if(index != i){
	        var oA = document.createElement("a");
	        oA.href = "javascript:newsReader.showNode("+i+");";
	        oA.appendChild(document.createTextNode("item"));
	        oLi.appendChild(oA);
	    }
	    else{
	        oLi.appendChild(document.createTextNode("item"));
	    }
	    ofr.appendChild(oLi);
	}
	*/
	// next button
	var liNext = document.createElement("li");
	if(index == this.items.length - 1){
	    liNext.appendChild(document.createTextNode("Next »"));
	}
	else{
	    var aNext = document.createElement("a");
	    aNext.appendChild(document.createTextNode("Next »"));
	    aNext.href = "javascript:newsReader.readNextNode();";
	    liNext.appendChild(aNext);
	}
	liNext.id = "next";
	ofr.appendChild(liNext);
	this.newsNavigation.appendChild(ofr);
	// news content
	// .title
	var oH3 = document.createElement("h3");
	var aTitle = document.createElement("a");
	/*for(var i = 0; i < this.items.length; i++){
		alert("INDEX " + i + ": " + this.items[i].title.value);
	}*/
	
	aTitle.appendChild(document.createTextNode(this.items[index].title.value));
	aTitle.href = this.items[index].link.value;
	aTitle.target = "_blank";
	oH3.appendChild(aTitle);
	oH3.appendChild(document.createTextNode(" "));
	var oSmall = document.createElement("small");
	oSmall.appendChild(document.createTextNode(this.parseDate(this.items[index].date.value)));
	oH3.appendChild(oSmall);
	this.newsContent.appendChild(oH3);
	//.date
	var oP = document.createElement("p");
	if(this.items[index].content != null && this.items[index].content.value != null)
	    oP.innerHTML = this.items[index].content.value;
	else if(this.items[index].summary != null && this.items[index].summary.value != null)
	    oP.innerHTML = this.items[index].summary.value;
	else if(this.items[index].description.value != null)
	    oP.innerHTML = this.items[index].description.value;
	else
	    oP.innerHTML = "";
	this.newsContent.appendChild(oP);
}


newsReader.fetch = function(sUrl){
    var oTli = document.createElement("li");
	oTli.appendChild(document.createTextNode("Loading..."));
	oTli.id = "newsLoading";
	this.newsNavigation.appendChild(oTli);
	function parserCallback(oParser){
		newsReader.populateResults(oParser);
	}
	xparser.getFeed("/aspx/HTTPFetch.aspx?url=" + encodeURIComponent(sUrl + "?foo=" + newsReader.getMiscID()), parserCallback, this) ;	
}

newsReader.getMiscID = function(){
	return Math.floor(Math.random()*10000000);
}

newsReader.readNextNode = function(){
	if(this.currentNode != this.items.lenght -1){
	    
	    this.showNode(this.currentNode + 1);
	}
}

newsReader.readPrevNode = function(){
	if(this.currentNode != 0)
		 this.showNode(this.currentNode-1);
}
