/**
 * Ajax RSS Feed Reader 1.3
 *
 * Created by Bantai (Marvin Tjon)
 * $Date: 26-10-2007
 * 
 * Last update: 13-03-2011
 */


function AjaxFeedReader(url, showItems, targetDiv, display)
{
   AjaxFeedReader.id = ++AjaxFeedReader.id || 1;
   this.id = AjaxFeedReader.id;
   this.firstItem = 0;
   this.itemsCount = 0;
   this.showItems = showItems;
   this.url = url;
   this.port = location.port || 80;
   this.basehref = location.protocol+"//"+location.hostname;
   this.items = null;
   this.targetDiv = document.getElementById(targetDiv);
   this.header = document.getElementById(display['header']);
   this.headerText = this.header.innerHTML;
   this.display = display;
   this.data = null;
   this.dropdown = null;
   this.list = null;
   var self = this;

   $.ajax({
     type: "POST"
//   ,cache: false
//   ,ifModified: true
     ,url: self.basehref+self.url
     ,beforeSend: function(){self.setHeader("loading...");}
     ,success: function(data){self.init(data);self.setHeader(self.headerText);}
     ,dataType: "xml"
      });
}

AjaxFeedReader.prototype.setHeader = function(string)
{
   if (this.header)
      this.header.innerHTML = string;
}

AjaxFeedReader.prototype.init = function(data)
{
   if (this.data == null)
     this.data = data;

   if (this.items == null)
   {
      try
      {
        this.items = this.data.getElementsByTagName("item");
      }
      catch (err)
      {
        alert('Unknown or no data received.');
        return false;
      }
   }

   var self = this;
   $(this.targetDiv).fadeOut("normal", function() {self.buildOutput()});
}

AjaxFeedReader.prototype.buildOutput = function()
{
   this.targetDiv.innerHTML = "<!-- -->";
   this.itemsCount = this.items.length;

   for (i=this.firstItem; (i<this.firstItem+this.showItems && i<this.itemsCount && this.items[i]!=null); i++)
   {
      this.parseRSS(this.items[i]);
   }

   if(this.display['nav'])
      this.addNav();

   if(this.display['dropdown_bottom'] || this.display['dropdown_top'])
        this.makeDropdown(); //only called once

   $(this.targetDiv).fadeIn("normal");
   this.updateDropdown();
}


AjaxFeedReader.prototype.addNav = function()
{
   var nav = document.createElement("DIV");
   nav.className = "feedReaderNav";
   var hr = document.createElement("HR");
   hr.style.marginBottom = "1em";
   nav.appendChild(hr);
   var navNext = document.createElement("B");
   var navPrev = document.createElement("B");
   navNext.id = "navNext_"+this.id;
   navPrev.id = "navPrev_"+this.id;
   var self = this;
   $(navNext).click(function(){self.nextPage()});
   $(navPrev).click(function(){self.prevPage()});
   navNext.innerHTML = "Older";
   navPrev.innerHTML = "Newer";

    //first page
   if (this.firstItem  == 0)
      navPrev.style.visibility = "hidden"; //hide newer
   else
      navPrev.style.visibility = "visible"; //show newer

   //last page
   if (this.firstItem + this.showItems >= this.itemsCount)
   {
      navPrev.style.visibility = "visible"; //show older
      navNext.innerHTML = "Rewind"
   }
   else
   {
      navNext.style.visibility = "visible"; //show older
   }

   nav.appendChild(navPrev);
   nav.appendChild(navNext);
   this.targetDiv.appendChild(nav);
}

AjaxFeedReader.prototype.parseRSS = function(item)
{
     display = this.display;
     var type = (this.targetDiv.tagName == "UL" || this.targetDiv.tagName == "OL") ? "LI" : "DIV";
     var elm = document.createElement(type);
     elm.className = "feedReaderItem";

     var link = $(item).find("link").text();


    if ( display["title"] )
    {
      var title = $(item).find("title").text();
      var titleHeader = document.createElement(display["title"]);
      var titleLink = document.createElement("A");
      titleHeader.appendChild(titleLink);
      titleHeader.className = "feedReaderTitle";
      titleLink.innerHTML = title;//title.substr(0, 39) + ((title.length > 39)? "..." : "");
      titleLink.title     = title;
      titleLink.href      = link;
      elm.appendChild(titleHeader);
    }

    if ( display["pubDate"] )
    {
      var pubDate = $(item).find("pubDate").text();
      var dt = new Date(pubDate);
      var pubDateBox = document.createElement("SMALL");
      pubDateBox.className = "feedReaderPubDate";
      pubDateBox.innerHTML =  dt.toLocaleString();
      elm.appendChild(pubDateBox);
    }

    if ( display["desc"] )
    {
      var desc = $(item).find("description").text();
      //var strippedDesc = desc.replace(/(<([^>]+)>)/ig,"");
      var descText = document.createElement("DIV");
      descText.className = "feedReaderDesc";
      //descText.innerHTML =  desc.substr(0, 85) + ((desc.length > 47)? "..." : "");
      descText.innerHTML =  desc;
      elm.appendChild(descText);
    }

    if ( display["more"] )
    {
      var readMore = document.createElement("P");
      readMore.className = "feedReaderMore";
      var moreLink = document.createElement("A");
      readMore.appendChild(moreLink);
      moreLink.innerHTML = "More...";
      moreLink.href = link;
      elm.appendChild(readMore);
    }

    this.targetDiv.appendChild(elm);
}

AjaxFeedReader.prototype.currentPage = function()
{
 return ((this.firstItem + this.showItems) / this.showItems) - 1;
}

AjaxFeedReader.prototype.prevPage = function()
{
   if(this.firstItem > 0)
   {
       this.firstItem = this.firstItem - this.showItems;
       this.init();
   }
}

AjaxFeedReader.prototype.nextPage = function()
{
   if(this.firstItem + this.showItems < this.itemsCount)
      this.firstItem = this.firstItem + this.showItems;
   else
      this.firstItem = 0; // rewind

   this.init();
}

AjaxFeedReader.prototype.showPage = function(page)
{
   if (page >= 0 && page <= this.itemsCount)
   {
      this.firstItem = (page) * this.showItems;
      this.init();
   }

  this.updateDropdown();
}

AjaxFeedReader.prototype.updateDropdown = function()
{
  if (this.dds == null)
   return false;
  var page = this.currentPage(this.firstItem);
  var self = this;
  $(self.dds).each(function(a){
        $(this).find("select").each(function(i){this.selectedIndex = page});
   });
}

AjaxFeedReader.prototype.makeDropdown = function()
{
   //construct
   this.dropdown = document.createElement("p");
   this.dropdown.className = "feedReaderDropdown";
   this.dropdown.innerHTML = "Show "

   list = document.createElement("select");
   var pages = Math.ceil(this.itemsCount / this.showItems);
   var options = new Array(pages);
   for (var i=1;i<=pages;i++)
   {
      var title = this.items[(i-1)*(this.showItems)].getElementsByTagName("title")[0].childNodes[0].nodeValue;
      options[i] = document.createElement("option");
      options[i].innerHTML = 'Page ' + i + ': ' + title;
      list.appendChild(options[i]);
   }

   list.selectedIndex = this.currentPage();

   var self = this;
   $(list).change(function(){
        self.showPage(this.selectedIndex);
      });

   this.dropdown.appendChild(list);

   this.dds = new Array();

   //one time creation
   if (this.display['dropdown_top'])
   {
      var dd1 = $(self.dropdown).clone(true);
      dd1.insertBefore(self.targetDiv);
      this.dds.push(dd1);

      this.display['dropdown_top'] = false;
   }
   if (this.display['dropdown_bottom'])
   {
      var dd2 = $(self.dropdown).clone(true);
      dd2.insertAfter(self.targetDiv);
      this.dds.push(dd2);

      this.display['dropdown_bottom'] = false;
   }
}

function renoiseNewsReader()
{
   var displayNews = new Array();
   displayNews['title'] = "h3";
   displayNews['pubDate'] = true;
   displayNews['desc'] = true;
   displayNews['more'] = false;
   displayNews['nav'] = false;
   displayNews['dropdown_top'] = true;
   displayNews['dropdown_bottom'] = false ;
   displayNews['header'] = "latestnews";

   var newsFeed = "/indepth/category/renoise-news/feed/";
   newsReader = new AjaxFeedReader(newsFeed, 1, "ajaxNews", displayNews);

   var displayForum = new Array();
   displayForum['title'] = "div";
   displayForum['pubDate'] = false;
   displayForum['desc'] = false;
   displayForum['more'] = false;
   displayForum['nav'] = false;
   displayForum['dropdown_top'] = false;
   displayForum['dropdown_bottom'] = false;
   displayForum['header'] = "latesttopics";


   var forumFeed = "/board/index.php?app=core&module=global&section=rss&type=forums&id=1";
   forumReader = new AjaxFeedReader(forumFeed, 10, "RSS_forum", displayForum);

   var displayArticles = new Array();
   displayArticles['title'] = "div";
   displayArticles['pubDate'] = false;
   displayArticles['desc'] = false;
   displayArticles['more'] = false;
   displayArticles['nav'] = false;
   displayArticles['dropdown_top'] = false;
   displayArticles['dropdown_bottom'] = false;
   displayArticles['header'] = "latestarticles";

   var indepthFeed = "/indepth/feed/";
//   indepthReader = new AjaxFeedReader(indepthFeed, 5, "RSS_indepth", displayArticles);
}      

