// jTicker.js

// Copyright 2007, Jeremy R. Geerdes
// All rights reserved
// For more information or a project quote:
// http://jgeerdes.home.mchsi.com
// jgeerdes@mchsi.com

(function(){
function jFeed(a,b)
 {
 this.a=a;
 this.options = (typeof(b)=='object') ? b : {};
 if (!this.options.numEntries) this.options.numEntries = 999;
 }
jFeed.prototype.load = function(a)
 {
 var z = this;
 if (typeof(google)=='object'&&google.feeds&&this.a.match(/^http:\/\//)&&!this.a.match('http:\/\/'+window.location.host))
  {
  var b = new google.feeds.Feed(this.a);
  b.setNumEntries(this.options.numEntries);
  b.load(function(b){a(b.feed);});
  }
 else if (!this.a.match(/^http:\/\//)||this.a.match('http:\/\/'+window.location.host))
  {
  var b;
  if (this.xhr) b = this.xhr;
  else
   {
   b = 0;
   if (window.XMLHttpRequest) b=new XMLHttpRequest();
   else if (window.ActiveXObject)
    {
    try { b = new ActiveXObject('MSXML2.XMLHTTP'); }
    catch (e)
     {
     try { b = new ActiveXObject('Microsoft.XMLHTTP'); }
     catch (e) {}
     }
    }
   if (!b) {alert('Oops. Unable to initialize XMLHttpRequest.');return false;}
   this.xhr = b;
   }
  var y = function()
   {
   if (b.readyState==4&&b.status==200&&b.responseXML)
    {
    var c = b.responseXML;
    var d = {
     title : (c.getElementsByTagName('title').length>0) ? c.getElementsByTagName('title')[0].firstChild.nodeValue : z.a,
     link : (c.getElementsByTagName('link').length>0) ? c.getElementsByTagName('link')[0].firstChild.nodeValue : z.a,
     description : (c.getElementsByTagName('description').length>0) ? c.getElementsByTagName('description')[0].firstChild.nodeValue : ((c.getElementsByTagName('subtitle').length>0) ? c.getElementsByTagName('subtitle').firstChild.nodeValue : null),
     author : (c.getElementsByTagName('name').length>0) ? c.getElementsByTagName('name')[0].firstChild.nodeValue : null,
     entries : []
    };
    var e = (c.getElementsByTagName('entry').length>0) ? c.getElementsByTagName('entry') : ((c.getElementsByTagName('item').length>0) ? c.getElementsByTagName('item') : []);
    for (var x=0;x<z.options.numEntries&&x<e.length;x++)
     {
     var f=e[x];
     var g={
      title:(f.getElementsByTagName('title').length>0) ? f.getElementsByTagName('title')[0].firstChild.nodeValue : null,
      link:(f.getElementsByTagName('link').length>0) ? f.getElementsByTagName('link')[0].firstChild.nodeValue : null,
      content:(f.getElementsByTagName('content').length>0) ? f.getElementsByTagName('content')[0].firstChild.nodeValue : ((f.getElementsByTagName('summary').length>0) ? f.getElementsByTagName('summary')[0].firstChild.nodeValue : ((f.getElementsByTagName('description').length>0) ? f.getElementsByTagName('description')[0].firstChild.nodeValue : '')),
      publishedDate:(f.getElementsByTagName('published').length>0) ? f.getElementsByTagName('published')[0].firstChild.nodeValue : ((f.getElementsByTagName('pubdate').length>0) ? f.getElementsByTagName('pubDate')[0].firstChild.nodeValue : null),
      categories : null
     };
     g.contentSnippet = g.content.replace(/\<.*?\>/g).substring(0,120);
     d.entries.push(g);
     }
    a(d);
    }
   }
  b.onreadystatechange = y;
  b.open('get',z.a,true);
  if (!navigator.userAgent.indexOf('MSIE')) b.overrideMimeType('text/xml');
  b.send(null);
  }
 else
  {
  alert('Oops. The feed you\'re trying to load is not local, and you haven\'t loaded the Google AJAX Feeds API.');
  return false;
  }
 return true;
 };
jFeed.prototype.setNumEntries = function(a){this.options.numEntries=a;};
jFeed.prototype.setResultFormat = function(){return false;};
jFeed.prototype.includeHistoricalEntries = function(){return false;};
function jTicker(a,b,c)
 {
 this.a = (typeof(a)=='string') ? document.getElementById(a) : a;
 var a = this.a;
 a.style.overflow = 'hidden';
 this.options = (typeof(c)=='object') ? c : {};
 if (!this.options.transition) this.options.transition = 'random';
 if (!this.options.switchInterval) this.options.switchInterval = 10;
 if (!this.options.refreshInterval) this.options.refreshInterval = 15;
 if (this.options.controls==undefined) this.options.controls = 1;
 while (a.firstChild) a.removeChild(a.firstChild);
 this.b = document.createElement('div');
 var d = this.b;
 d.className = 'jTicker';
 d.style.position = 'relative';
 d.style.height = '100%';
 d.style.width = '100%';
 var z = this;
 //d.onmouseover = function(){z.pause();};
 //d.onmouseout = function(){z.play();};
 this.c = document.createElement('div');
 var e = this.c;
 e.className = 'jTickerTitle';
 e.style.position = 'relative';
 e.style.height = '100%';
 e.style.overflow = 'hidden';
 this.d = document.createElement('div');
 var f = this.d;
 f.className = 'jTickerBody';
 f.style.position = 'relative';
 f.style.height = '100%';
 f.style.overflow = 'hidden';
 this.e = document.createElement('div');
 var h = this.e;
 h.className = 'jTickerControls';
 h.style[(document.all?'style':'css')+'Float'] = 'right';
 h.style.height = '100%';
 var zz = this;
 var i = this.prevButton = document.createElement('div');
 i.className='jTickerButton prev';
 i.appendChild(document.createTextNode(' '));
 i.onclick=function(){zz.transition(zz.activeItem-1);};
 h.appendChild(i);
 var j = this.nextButton = document.createElement('div');
 j.className='jTickerButton next';
 j.appendChild(document.createTextNode(' '));
 j.onclick=function(){zz.transition(zz.activeItem+1);};
 h.appendChild(j);
 this.activeItem = null;
 this.f = [];
 for (var z=0;z<b.length;z++) this.addFeed(b[z]);
 this.g = [];
 d.appendChild(e);
 if (this.e) d.appendChild(this.e);
 d.appendChild(f);
 a.appendChild(d);
 }
jTicker.prototype.getContainer = function(){return this.b;};
jTicker.prototype.getTitle = function(){return this.c;};
jTicker.prototype.getBody = function(){return this.d;};
jTicker.prototype.getControl = function(){return this.e;};
jTicker.prototype.getFeeds = function(){return this.f;};
jTicker.prototype.getItems = function(){return this.g;};
jTicker.prototype.addFeed = function(a)
 {
 this.f.push(a);
 var z = this;
 a.load(function(a){z.populate(a);});
 };
jTicker.prototype.populate = function(a)
 {
 var t = this;
 var b = document.createElement('div');
 b.className = 'jTickerTitle';
 b.innerHTML = '<a class="jTickerTitle" href="'+a.link+'" title="'+a.title+'"/>'+a.title+'</a>'
 b.style.position = 'absolute';
 b.style.visibility = 'hidden';
 this.getTitle().appendChild(b);
 for (var z=0;z<a.entries.length;z++)
  {
  var y = a.entries[z];
  var c = document.createElement('div');
  c.className = 'jTickerItem';
  c.innerHTML = '<a class="jTickerItem" href="'+y.link+'" title="'+y.contentSnippet.replace(/"/g, '\\"')+'">'+y.title+'</a>';
  c.style.position = 'absolute';
  c.style.visibility = 'hidden';
  this.getBody().appendChild(c);
  this.getItems().push({
   title:b,
   html:c,
   transTimer:null,
   show:function(e)
    {
    if (this.transTimer) clearTimeout(this.transTimer);
    var f = [this.title,this.html];
    var g = 0;
    for (var z=0;z<f.length;z++)
     {
     var y = f[z];
     var x = (y==this.html) ? t.getBody() : t.getTitle();
     if (e.match(/^fade/))
      {
      y.style.left = '0px';
      y.style.top = '0px';
      if (jtCurrStyle(y,'visibility')=='hidden')
       {
       y.style.opacity = 0;
       if (navigator.userAgent.indexOf('MSIE')!=-1) y.style.filter = 'alpha(opacity=0)';
       y.style.visibility = 'visible';
       }
      var co = parseFloat(jtCurrStyle(y,'opacity'))+.1;
      if (co>1) co=1;
      if (co<0) co=0;
      y.style.opacity = co;
      if (navigator.userAgent.indexOf('MSIE')!=-1) y.style.filter = 'alpha(opacity='+(co*100)+')';
      if (co<1) g=1;
      }
     else if (e.match(/^vert_ticker/))
      {
      var h = (e.match(/_down$/)) ? -1 : 1;
      var w = (x.offsetHeight>y.offsetHeight) ? x.offsetHeight : y.offsetHeight;
      if (jtCurrStyle(y,'visibility')=='hidden')
       {
       y.style.opacity = 1;
       if (navigator.userAgent.indexOf('MSIE')!=-1) y.style.filter = 'alpha(opacity=100)';
       y.style.top = (w*h)+'px';
       y.style.visibility = 'visible';
       }
      y.style.left = '0px';
      var i = Math.round(w/10);
      if (i<1) i=1;
      i*=h;
      var j = parseInt(jtCurrStyle(y,'top'));
      j = ((isNaN(j)) ? 0 : j)-i
      if ((h==1&&j<0)||(h==-1&&j>0)) j = 0;
      if (j!=0) g=1;
      y.style.top = j+'px';
      }
     else if (e.match(/^hor_ticker/))
      {
      var h = (e.match(/_right$/)) ? -1 : 1;
      var w = (x.offsetWidth>y.offsetWidth) ? x.offsetWidth : y.offsetWidth;
      if (jtCurrStyle(y,'visibility')=='hidden')
       {
       y.style.opacity = 1;
       if (navigator.userAgent.indexOf('MSIE')!=-1) y.style.filter = 'alpha(opacity=100)';
       y.style.left = (w*h)+'px';
       y.style.visibility = 'visible';
       }
      y.style.top = '0px';
      var i = Math.round(w/10);
      if (i<1) i=1;
      i*=h;
      var j = parseInt(jtCurrStyle(y,'left'));
      j = ((isNaN(j)) ? 0 : j)-i
      if ((h==-1&&j>0)||(h==1&&j<0)) j = 0;
      if (j!=0) g=1;
      y.style.left = j+'px';
      }
     }
    if (g)
     {
     var z=this;
     z.transTimer=setTimeout(function(){z.show(e);},50);
     }
    else
     {
     for (var z=0;z<f.length;z++)
      {
      var y = f[z];
      y.style.left = '0px';
      y.style.top = '0px';
      y.style.opacity = 1;
      if (navigator.userAgent.indexOf('MSIE')!=-1) y.style.filter = 'alpha(opacity=100)';
      y.style.visibility='visible';
      }
     }
    },
   hide:function(e)
    {
    if (this.transTimer) clearTimeout(this.transTimer);
    var f = [this.html];
    if (t.getItems()[t.activeItem] && t.getItems()[t.activeItem].title != this.title) f.push(this.title);
    var g = 0;
    for (var z=0;z<f.length;z++)
     {
     var y = f[z];
     var x = (y==this.html) ? t.getBody() : t.getTitle();
     if (e.match(/^fade/))
      {
      y.style.left = '0px';
      y.style.top = '0px';
      var co = parseFloat(jtCurrStyle(y,'opacity'))-.1;
      if (co>1) co = 1;
      if (co<0) co = 0;
      y.style.opacity = co;
      if (navigator.userAgent.indexOf('MSIE')!=-1) y.style.filter = 'alpha(opacity='+(co*100)+')';
      if (co>0) g=1;
      }
     else if (e.match(/^vert_ticker/))
      {
      var h = (e.match(/_down$/)) ? -1 : 1;
      var w = (y.offsetHeight>x.offsetHeight) ? y.offsetHeight : x.offsetHeight;
      var i = Math.round(w/10);
      if (i<1) i=1;
      i*=h;
      var j = parseInt(jtCurrStyle(y,'top'));
      j = ((isNaN(j)) ? 0 : j)-i;
      y.style.top = j+'px';
      y.style.left = '0px';
      if (j>w) j=w;
      if (j<-w) j=-w;
      if ((h==1&&j>-w)||(h==-1&&j<w)) g=1;
      }
     else if (e.match(/^hor_ticker/))
      {
      var h = (e.match(/_right$/)) ? -1 : 1;
      var w = (y.offsetWidth>x.offsetWidth) ? y.offsetWidth : x.offsetWidth;
      var i = Math.round(w/10);
      if (i<1) i=1;
      i*=h;
      var j = parseInt(jtCurrStyle(y,'left'));
      j = ((isNaN(j)) ? 0 : j)-i;
      if (j>w) j=w;
      if (j<-w) j=-w;
      y.style.left = j+'px';
      y.style.top = '0px';
      if ((h==1&&j>-w)||(h==-1&&j<w)) g=1;
      }
     }
    if (g)
     {
     var z=this;
     z.transTimer=setTimeout(function(){z.hide(e);},50);
     }
    else
     {
     for (var z=0;z<f.length;z++)
      {
      var y = f[z];
      y.style.visibility = 'hidden';
      }
     }
    }
   });
  }
 if (!this.refreshInt)
  {
  var z = this;
  this.refreshInt = setInterval(function(){z.refresh();},this.options.refreshInterval*60000);
  }
 this.pause();
 this.play();
 }
jTicker.prototype.reset = function()
 {
 this.pause();
 while (this.getTitle().firstChild) this.getTitle().removeChild(this.getTitle().firstChild);
 while (this.getBody().firstChild) this.getBody().removeChild(this.getBody().firstChild);
 this.g=[];
 }
jTicker.prototype.transition = function(a)
 {
 var b = this.getItems();
 this.activeItem = (typeof(a)=='number') ? a : ((typeof(this.activeItem)=='number') ? this.activeItem+1 : 0);
 while (this.activeItem>=b.length) this.activeItem -= b.length;
 while (this.activeItem<0) this.activeItem += b.length;
 var d = this.options.transition;
 if (d=='random')
  {
  var e = ['','fade','hor_ticker','hor_ticker_right','vert_ticker','vert_ticker_down'];
  d = e[Math.round(Math.random()*5.4999)];
  }
 for (var z=0;z<b.length;z++)
  {
  var c = b[z];
  if (z==this.activeItem) c.show(d);
  else c.hide(d);
  }
 this.p = 1;
 this.pause();
 this.play();
 }
jTicker.prototype.isPaused = function(){return ((typeof(this.transInt)=='undefined') ? 1 : 0);};
jTicker.prototype.refresh = function()
 {
 var z = this;
 z.pause();
 z.reset();
 for (var y=0;y<this.getFeeds().length;y++) this.getFeeds()[y].load(function(a){z.populate(a);});
 }
jTicker.prototype.pause = function()
 {
 if (!this.isPaused())
  {
  clearTimeout(this.transInt);
  this.transInt=undefined;
  }
 };
jTicker.prototype.play = function()
 {
 if (this.isPaused())
  {
  var z=this;
  if (!z.p) this.transition();
  z.transInt=setTimeout(function(){z.transition();},this.options.switchInterval*1000);
  }
 };
function jtCurrStyle(a,b)
 {
 var c;
 if (a.currentStyle) c = a.currentStyle[b];
 else
  {
  var d = function(a,b){return '-'+b.toLowerCase();};
  var e = b.replace(/([A-Z])/g, d);
  try {c = document.defaultView.getComputedStyle(a,null).getPropertyValue(e);}
  catch(f)
   {
   try {c = a.style[b];}
   catch(f){}
   }
  }
 return c;
 }
function ua(a,b,c){var d='on';if(!c){c=window;}if(typeof(a)=='string'){a=[a];}for(var f in a){var e=a[f];if(c.addEventListener){c.addEventListener(e,b,0);}else if(c.attachEvent){c.attachEvent(d+e,b);}else{c[d+e]=b;}}}
function ub(a,c,b){if(!b){b=window;}if(b.removeEventListener){b.removeEventListener(a,c,0);}else if(b.detachEvent){b.detachEvent('on'+a,c);}else{b['on'+a]=null;}}
if(!window.jeremy){window.jeremy={addEvent:ua,removeEvent:ub};}
window.jeremy.jTicker={jTicker:jTicker,jFeed:jFeed};
})();
