
news_rotator = function() {
    // construct
    var _self = this;
    this.stories = [];
    this.currentIndex = 0;
    this.inited = false;
    
    _self._init = function() {
        if (_self.inited) return ;
        _self.container = document.getElementById('news-rotator');
        var className = "story-container";
        // locate all the story containers 
        var allElements = this.container.getElementsByTagName('div');
  		for (var i = 0; (element = allElements[i]) != null; i++) {
		    var elementClass = element.className;

		    if (elementClass && elementClass.indexOf(className) != -1)
			    _self.stories.push(element);
		}

        _self.inited = true;
    };
    
    // timer handler
    _self._tick = function() {
        _self._init();
        var nextIndex = (currentIndex + 1) % this.stories.length;
        _self.blend(currentIndex, nextIndex);
        // hide previous banner 
        //_self.stories[currentIndex].setAttribute('style', 'visibility:hidden;display:none;');
        // show next banner
        //_self.stories[nextIndex].setAttribute('style', 'display:block');
        
        currentIndex = nextIndex;
    };
    
    
    _self._opacityValue = 0;
    _self._opacityDelta = 15;
    _self._blendInterval;
    _self._blendCurrentIndex = 0;
    _self._blendNextIndex = 0;
    
    _self.blend = function(currIndex, nextIndex) {
        // stop current blend timer 
        if (_self._blendInterval != null) {
            window.clearInterval(_self._blendInterval);
            _self._blendInterval = null;
        }
        // setup blending vars 
        _self._blendCurrentIndex = currIndex;
        _self._blendNextIndex = nextIndex;
        _self._opacityValue = 0
        // start blend timer
        _self._blendInterval = window.setInterval(
		    _self._blendTick, 10 
		);
    };
    
    _self._blendTick = function() {
        
        if (_self._opacityValue > 100 + _opacityDelta) {
            // stop blending timer
            window.clearInterval(_self._blendInterval);
            _self._blendInterval = null;
            _self._opacityValue = 0;
            // hide previous banner 
            _self.stories[_self._blendCurrentIndex].style.visibility='hidden';
            // show next banner
            _self.stories[_self._blendNextIndex].style.visibility='visible';

            return;
        }
        else {
            var valInOpacity = _self._opacityValue /100;
            var valOutOpacity = 1.0 - _self._opacityValue /100;
            // hide previous banner 
            _self.stories[_self._blendCurrentIndex].style.visibility='visible';
            // show next banner
            _self.stories[_self._blendNextIndex].style.visibility='visible';

            //_self.stories[_self._blendCurrentIndex].setAttribute('style', 'opacity:'+ valOutOpacity);
            //_self.stories[_self._blendNextIndex].setAttribute('style', 'opacity:'+ valInOpacity);
            _self.stories[_self._blendCurrentIndex].style.opacity=valOutOpacity;
            _self.stories[_self._blendNextIndex].style.opacity=valInOpacity;
            //.filters[0].opacity=value

        }
        _self._opacityValue += _opacityDelta;
    };
    
    // begin rotation
    _self._interval = window.setInterval(
		_self._tick,
		5000 /* 5 secs */
		);
    
}



window.load = news_rotator();