var newsCount = 0;
var timerID = null;
var fadeFlag = 0;

var Class = {
  create: function() {
    return function() { 
      this.initialize.apply(this, arguments);
    }
  }
}

Function.prototype.bind = function(object) {
  var __method = this;
  return function() {
    __method.apply(object, arguments);
  }
}

if ( window.Trans == undefined )
   Trans = {};
   
Trans.FadeTo = Class.create();
Trans.FadeTo.prototype = {

   initialize: function( element, opacity, duration, steps, options) {
      this.element  = do_elem(element);
      this.opacity  = opacity;
      this.duration = duration;
      this.steps    = steps;
      this.options  = arguments[4] || {};
      this.fadeTo();
   },

   fadeTo: function() {
      if (this.isFinished()) {
         if(this.options.complete) this.options.complete(this);
         return;
      }

      if (this.timer)
         clearTimeout(this.timer);

      var stepDuration = Math.round(this.duration/this.steps) ;
      var currentOpacity = this.getElementOpacity();
      var delta = this.steps > 0 ? (this.opacity - currentOpacity)/this.steps : 0;

      this.changeOpacityBy(delta);
      this.duration -= stepDuration;
      this.steps--;

      this.timer = setTimeout(this.fadeTo.bind(this), stepDuration);
   },

   changeOpacityBy: function(v) {
      var currentOpacity = this.getElementOpacity();
      var newOpacity = Math.max(0, Math.min(currentOpacity+v, 1));
      this.element.ricoOpacity = newOpacity;

      this.element.style.filter = "alpha(opacity:"+Math.round(newOpacity*100)+")";
      this.element.style.opacity = newOpacity; /*//*/;
   },

   isFinished: function() {
      return this.steps <= 0;
   },

   getElementOpacity: function() {
      if ( this.element.ricoOpacity == undefined ) {
         var opacity;
         if ( this.element.currentStyle ) {
            opacity = this.element.currentStyle.opacity;
         }
         else if ( document.defaultView.getComputedStyle != undefined ) {
            var computedStyle = document.defaultView.getComputedStyle;
            opacity = computedStyle(this.element, null).getPropertyValue('opacity');
         }

         this.element.ricoOpacity = opacity != undefined ? opacity : 1.0;
      }

      return parseFloat(this.element.ricoOpacity);
   }
}

function do_elem() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
      return element;

    elements.push(element);
  }

  return elements;
}

function build_item(url, date, title) {
	var newsItem = '<span class="smallerheader">' + date + '</span> &nbsp;<a href="' + url + '" target="_blank" class="link_small">' + title + '</a>';
	return newsItem;
}

function startTrans() {
	startTop   = do_elem('news_items').offsetTop;
	startLeft  = do_elem('news_items').offsetLeft;
	new Trans.FadeTo( 'news_items', 0, 500, 10);
}

function setStatus(s,n) {
	do_elem('statusContainer').style.visibility = "visible";
	do_elem('statusMsg').innerHTML = s;
	setTimeout( "do_elem('statusContainer').style.visibility = 'hidden';", n );
}

function resetTrans() {
	startTop   = do_elem('news_items').offsetTop;
	startLeft  = do_elem('news_items').offsetLeft;
	new Trans.FadeTo( 'news_items', 1, 500, 10);
}

function setNews() {
	document.getElementById('news_items').innerHTML = newsArray[newsCount];
	newsCount++;
	if(fadeFlag == 1) {
		resetTrans();
	}
}

function do_next_news() {
	do_all_items();
	var len = newsArray.length;
	if(newsCount >= len) {
		newsCount = 0;
	}

	if(fadeFlag == 1) {
		startTrans();
		setTimeout('setNews()', 500);
	}
	else {
		setNews();
	}

	fadeFlag = 1;

	timerID = setTimeout('do_next_news()', 4000);
}
