//
//  RSS Ticker - JS feed reader and displayer
//
//  Clio Soleil (clio@PrettyGetter.TV)
//  PrettyGetter Productions (http://PrettyGetter.TV)
//  

// Relative URL syntax:
var lastrssbridgeurl = "/_cmslogic/lastrss/bridge.php";
//var lastrssbridgeurl = "http://"+window.location.hostname+"/lastrss/bridge.php";

//-------------------------------------------------------------------
function createAjaxObj()
//-------------------------------------------------------------------
{
  var httprequest = false;
  if (window.XMLHttpRequest)
  { 
    // if Mozilla, Safari etc
    httprequest = new XMLHttpRequest()
    if (httprequest.overrideMimeType)
      httprequest.overrideMimeType("text/xml")
  }
  else if (window.ActiveXObject)
  { 
    // if IE
    try { httprequest = new ActiveXObject("Msxml2.XMLHTTP"); } 
    catch (e) 
    { 
      try { httprequest = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e){}
    }
  }
  return httprequest;
}

//-------------------------------------------------------------------
// Main RSS Ticker Object function
function rssticker_ajax(RSS_id, cachetime, divId, divClass, delay, logicswitch)
//-------------------------------------------------------------------
{
  this.RSS_id = RSS_id;              //Array key indicating which RSS feed to display
  this.cachetime = cachetime;        //Time to cache feed, in minutes. 0=no cache.
  this.tickerid = divId;             //ID of ticker div to display information
  this.delay = delay;                //Delay between msg change, in miliseconds.
  this.logicswitch = (typeof logicswitch != "undefined") ? logicswitch : "";
  this.mouseoverBol = 0;             //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
  this.pointer = 0;
  this.opacitysetting = 0.2;         //Opacity value when reset. Internal use.
  
  //Arrays to hold each component of an RSS item
  this.title = [], this.link = [], this.description = [], this.pubdate = []; 
  this.ajaxobj = createAjaxObj();
  document.write("<div id='"+divId+"' class='"+divClass+"'>Initializing ticker...</div>");
  
  //detect if moz-opacity is defined in external CSS for specified class
  if (window.getComputedStyle) 
    this.mozopacityisdefined = (window.getComputedStyle(document.getElementById(this.tickerid), "").getPropertyValue("-moz-opacity") == 1) ? 0 : 1;
  this.getAjaxcontent();
}

//-------------------------------------------------------------------
// getAjaxcontent() - Makes asynchronous GET request to "bridge.php" with the supplied parameters
rssticker_ajax.prototype.getAjaxcontent = function()
//-------------------------------------------------------------------
{
  if (this.ajaxobj)
  {
    var instanceOfTicker = this;
    var parameters = "id=" + encodeURIComponent(this.RSS_id) + "&cachetime=" + this.cachetime + "&bustcache=" + new Date().getTime();
    this.ajaxobj.onreadystatechange = function() { instanceOfTicker.initialize(); }
    this.ajaxobj.open("GET", lastrssbridgeurl + "?" + parameters, true);
    this.ajaxobj.send(null);
  }
}

// -------------------------------------------------------------------
// initialize() - Initialize ticker method: gets contents of RSS content and parse it using JavaScript DOM methods 
rssticker_ajax.prototype.initialize = function()
// -------------------------------------------------------------------
{ 
  //if request of file completed
  if (this.ajaxobj.readyState == 4)
  { 
    //if request was successful
    if (this.ajaxobj.status == 200)
    { 
      var xmldata = this.ajaxobj.responseXML;

      //if no <item> elements found in returned content
      if (xmldata.getElementsByTagName("item").length == 0)
      { 
        document.getElementById(this.tickerid).innerHTML = "<b>Error</b> fetching remote RSS feed!<br />" + this.ajaxobj.responseText;
        return;
      }
      
      var instanceOfTicker = this;
      this.feeditems = xmldata.getElementsByTagName("item");

      //Cycle through RSS XML object and store each peice of an item inside a corresponding array
      for (var i = 0; i < this.feeditems.length; i++)
      {
        this.title[i] = this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue;
        this.link[i] = this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue;

        // CLIO EDIT 10/14/2008 - Sometimes there is no description; suppress script error in that case
        if (this.feeditems[i].getElementsByTagName("description")[0].firstChild)
          this.description[i] = this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue;
        this.pubdate[i] = this.feeditems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue;
      }
      
      document.getElementById(this.tickerid).onmouseover = function() { instanceOfTicker.mouseoverBol = 1; }
      document.getElementById(this.tickerid).onmouseout = function() { instanceOfTicker.mouseoverBol = 0; }
      this.rotatemsg();
    }
  }
}

// -------------------------------------------------------------------
// rotatemsg() - Rotate through RSS messages and displays them
rssticker_ajax.prototype.rotatemsg = function()
// -------------------------------------------------------------------
{
  var instanceOfTicker = this;
  
  //if mouse is currently over ticker, do nothing (pause it)
  if (this.mouseoverBol == 1) 
    setTimeout(function(){instanceOfTicker.rotatemsg()}, 100)
  //else, construct item, show and rotate it!
  else
  { 
    var target = null;
    if (this.RSS_id != "local")
  	  target = "target='_blank' ";

    var tickerDiv = document.getElementById(this.tickerid);
    var linktitle = "<div class='rsstitle'><a href='" + this.link[this.pointer] + "' " + target + ">" + this.title[this.pointer] + "</a></div>";
    var description = "<div class='rssdescription'>" + this.description[this.pointer] + "</div>";
    var feeddate = "<div class='rssdate'>" + this.pubdate[this.pointer] + "</div>";
    if (this.logicswitch.indexOf("description") == -1) description = "";
    if (this.logicswitch.indexOf("date") == -1) feeddate = "";
    
    //STRING FOR FEED CONTENTS 
    var tickercontent = linktitle + feeddate + description;

    //FADE EFFECT- RESET OPACITY
    this.fadetransition("reset");
    tickerDiv.innerHTML = tickercontent;
    
    //FADE EFFECT- PLAY IT
    this.fadetimer1 = setInterval(function() { instanceOfTicker.fadetransition("up", "fadetimer1"); }, 100) 
    this.pointer = (this.pointer < this.feeditems.length - 1) ? this.pointer + 1 : 0;
    
    //update container every second
    setTimeout(function() { instanceOfTicker.rotatemsg(); }, this.delay); 
  }
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
rssticker_ajax.prototype.fadetransition = function(fadetype, timerid)
// -------------------------------------------------------------------
{
  var tickerDiv = document.getElementById(this.tickerid);
  if (fadetype == "reset")
    this.opacitysetting = 0.2;
  if (tickerDiv.filters && tickerDiv.filters[0])
  {
    //IE6+
    if (typeof tickerDiv.filters[0].opacity == "number") 
      tickerDiv.filters[0].opacity = this.opacitysetting * 100;
    //IE 5.5-
    else 
      tickerDiv.style.filter = "alpha(opacity=" + this.opacitysetting*100 + ")";
  }
  else if (typeof tickerDiv.style.MozOpacity != "undefined" && this.mozopacityisdefined)
    tickerDiv.style.MozOpacity = this.opacitysetting;

  if (fadetype == "up")
    this.opacitysetting += 0.2;
  if (fadetype == "up" && this.opacitysetting >= 1)
    clearInterval(this[timerid]);
}