// -------------------------------------------------------------------
// RSS Display Box (Ajax invocation)- Created Dec 2nd 2006
// Author: Dynamic Drive (http://www.dynamicdrive.com)
// -------------------------------------------------------------------

//Path to "main.php" *relative* to "demo.htm", or the HTML page on your server displaying the RSS Box:

var rssoutputscript="/js/rssbox/main.php"

//Switch to full URL to "main.php" on your server below, so script will work with any page on your server, not just "demo.htm"
//Do NOT change window.location.hostname, which is your site's root domain, dynamically determined
//Configure below line and uncomment it to activate it:

//rssoutputscript="http://"+window.location.hostname+"/mystuff/rssbox/main.php"


////////////No need to edit beyond here//////////////

function createAjaxObj(){
var httprequest=false
if (window.XMLHttpRequest){ // if Mozilla, IE7, Safari etc
httprequest=new XMLHttpRequest()
}
else if (window.ActiveXObject){ // if IE6 or below
try {
httprequest=new ActiveXObject("Msxml2.XMLHTTP");
} 
catch (e){
try{
httprequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
return httprequest
}

// -------------------------------------------------------------------
// PUBLIC: rssdisplaybox(RSS_id, cachetime, divId, divClass)
// Main RSS Display Box Object function.
// -------------------------------------------------------------------

function rssdisplaybox(RSS_id, divId, divClass){
this.loadingHTML='<img src="'+rssoutputscript.replace('main.php', '')+'/loading.gif" /> Loading content...' //Specify HTTML to show while feed is being fetched
this.RSS_id=RSS_id //Array key indicating which RSS feed to display
this.boxid=divId //CSS ID of DIV that will hold the RSS feed items
this.boxclass=divClass //CSS class of DIV
this.cachetime=30 //default cache time, in minutes
this.template=""
}

// -------------------------------------------------------------------
// PUBLIC: set_items_shown(items_fetched, items_shown_per_page)
// 1) Sets how many RSS items to fetch (0 indicates all) 2) Optionally, how many items to show "per page block".
// -------------------------------------------------------------------

rssdisplaybox.prototype.set_items_shown=function(items_fetched, items_shown_per_page){
this.utotalitems=parseInt(items_fetched) //user defined # of items to fetch (may differ from what's actually available
if (typeof items_shown_per_page!="undefined" && (items_shown_per_page<items_fetched || items_fetched==0))
this.itemsperpage=parseInt(items_shown_per_page) //define how many items to show per page (pagination links are created)
}

// -------------------------------------------------------------------
// PUBLIC: set_template(name)- Specifies the name of a template, used within if/else logic of function outputbody() in outputbody.php
// -------------------------------------------------------------------

rssdisplaybox.prototype.set_template=function(tname){
this.template=tname.toLowerCase()
}

// -------------------------------------------------------------------
// PUBLIC: set_cache_time(minutes)- Set cache time in minutes. INT.
// -------------------------------------------------------------------

rssdisplaybox.prototype.set_cache_time=function(minutes){
this.cachetime=parseInt(minutes)
}

// -------------------------------------------------------------------
// PUBLIC: set_paginate_location(loc)- Sets the location of the pagination links relative to RSS box. "top" or "bottom"
// -------------------------------------------------------------------

rssdisplaybox.prototype.set_paginate_location=function(loc){
this.paginateloc=loc
}


// -------------------------------------------------------------------
// PRIVATE: getAjaxcontent()
// -------------------------------------------------------------------

rssdisplaybox.prototype.start=function(){
var paginatehtml='<div id="'+this.boxid+'-paginatediv" class="rsspaginate"></div>'
var rssboxhtml='<div id="'+this.boxid+'" class="'+this.boxclass+'"></div>'
var rssboxhtmlcomplete=(typeof this.itemsperpage=="undefined")? rssboxhtml : (this.paginateloc=="top")? paginatehtml+rssboxhtml : rssboxhtml+paginatehtml
document.write(rssboxhtmlcomplete)
this.ajaxobj=createAjaxObj()
this.getAjaxcontent()
}


// -------------------------------------------------------------------
// PRIVATE: getAjaxcontent()- Makes asynchronous GET request to "content.php" with the supplied parameters
// -------------------------------------------------------------------

rssdisplaybox.prototype.getAjaxcontent=function(){
if (this.ajaxobj){
var instanceOfBox=this
var parameters="id="+encodeURIComponent(this.RSS_id)+"&cachetime="+this.cachetime+"&limit="+this.utotalitems+"&template="+this.template+"&bustcache="+new Date().getTime()
document.getElementById(this.boxid).innerHTML=this.loadingHTML
this.ajaxobj.onreadystatechange=function(){instanceOfBox.initialize()}
this.ajaxobj.open('GET', rssoutputscript+"?"+parameters, true)
this.ajaxobj.send(null)
}
}

// -------------------------------------------------------------------
// PRIVATE: initialize()- Initialize RSS Display Box method.
// -------------------------------------------------------------------

rssdisplaybox.prototype.initialize=function(){ 
if (this.ajaxobj.readyState == 4){ //if request of file completed
if (this.ajaxobj.status==200){ //if request was successful
var rsscontent=this.ajaxobj.responseText
if (typeof this.itemsperpage!="undefined"){ //If variable containing how many items to show per page is defined
document.getElementById(this.boxid).style.visibility="hidden" //Hide display box before creating the pages and rendering pagination links
document.getElementById(this.boxid).innerHTML=rsscontent
this.gatheritems() //Round up all the items in the display box dynamically for breaking up into "pages". Items stored as <div class="rsscontainer"> elements.
}
else //else, just show RSS items without further delay
document.getElementById(this.boxid).innerHTML=rsscontent
}
else //if an error has occured
document.getElementById(this.boxid).innerHTML=this.ajaxobj.responseText
}
}

// -------------------------------------------------------------------
// PRIVATE: gatheritems()- Gather all the RSS items within display box (<div class="rsscontainer">), store as array, and prepare to create pagination div
// -------------------------------------------------------------------

rssdisplaybox.prototype.gatheritems=function(){
var rssdivtags=document.getElementById(this.boxid).getElementsByTagName("div") //find all div tags within RSS display box
this.rssitems=[] //define array to hold items
for (var i=0; i<rssdivtags.length; i++){ //loop thru the div tags
if (rssdivtags[i].className=="rsscontainer")
this.rssitems[this.rssitems.length]=rssdivtags[i] //store each RSS item DIV inside array
}
this.pagecount=Math.ceil(this.rssitems.length/this.itemsperpage) //calculate number of pages needed to show the items
for (var i=0; i<this.rssitems.length; i++)
this.rssitems[i].style.display="none" //Hide all items to begin with as we figure out which should be shown per the page selected
document.getElementById(this.boxid).style.visibility="visible" //unhide main RSS box
this.currentpage=0
this.buildpagination()
this.showpage(this.currentpage)
}

// -------------------------------------------------------------------
// PRIVATE: showpage(pagenumber)- Shows a page based on parameter passed (0=page1, 1=page2 etc)
// -------------------------------------------------------------------

rssdisplaybox.prototype.showpage=function(pagenumber){
var totalitems=this.rssitems.length //total number of items
var showstartindex=pagenumber*this.itemsperpage //array index of item to start showing per pagenumber setting
var showendindex=showstartindex+this.itemsperpage-1 //array index of item to stop showing after per pagenumber setting
for (var i=0; i<totalitems; i++){
if (i>=showstartindex && i<=showendindex)
this.rssitems[i].style.display="block"
else
this.rssitems[i].style.display="none"
}
this.currentpage=pagenumber
}

// -------------------------------------------------------------------
// PRIVATE: navigate(keyword)- Navigation interface function that calls showpage() with a different parameter based on keyword passed
// -------------------------------------------------------------------

rssdisplaybox.prototype.navigate=function(keyword){
if (keyword=="previous")
this.currentpage=(this.currentpage>0)? this.currentpage-1 : (this.currentpage==0)? this.pagecount-1 : 0
else if (keyword=="next")
this.currentpage=(this.currentpage<this.pagecount-1)? this.currentpage+1 : 0
this.showpage(this.currentpage)
this.paginatediv.getElementsByTagName("select")[0].selectedIndex=this.currentpage
}

// -------------------------------------------------------------------
// PRIVATE: buildpagination()- Create pagination div for RSS box
// -------------------------------------------------------------------

rssdisplaybox.prototype.buildpagination=function(){
var instanceOfBox=this
var paginatediv=document.getElementById(this.boxid+"-paginatediv")
//HTML for pagination DIV. Customize as desired, though do not modify the "rel" attributes or remove the SELECT element:
var paginationcode='<form><a href="#" rel="previous">Prev</a> <select class="paginateselect"></select> <a href="#" rel="next">Next</a></form>'
paginatediv.innerHTML=paginationcode
var paginatedropdown=paginatediv.getElementsByTagName("select")[0]
for (var i=0; i<this.pagecount; i++)
paginatedropdown.options[i]=new Option("Page "+(i+1)+" of "+this.pagecount, i)
paginatedropdown.selectedIndex=this.currentpage
paginatedropdown.onchange=function(){
instanceOfBox.showpage(this.selectedIndex)
}
var paginatelinks=paginatediv.getElementsByTagName("a")
for (var i=0; i<paginatelinks.length; i++){
if (paginatelinks[i].getAttribute("rel")=="previous" || paginatelinks[i].getAttribute("rel")=="next")
paginatelinks[i].onclick=function(){
instanceOfBox.navigate(this.getAttribute("rel"))
return false
}
}
this.paginatediv=paginatediv
}

