var feature = (function(){
	var 	currentfeature = null, 
		container = null,
		total_features = 0,
		tillscroll = 10,
		id = 1,
		toscroll = null,
		currentoffset = 0,
		parent = null,
		scroller = null;
		
	return{
		init: function(){
			if(!document.getElementById(container+	'-links')) return false;
			var elems = document.getElementById(container+	'-links').getElementsByTagName('li');
			for(var i=0; i < elems.length; i++){
				OP.observe(elems[i],'click',feature.selectFeature);
			}
			total_features = i;
			
			if(elems.length > 1){
				feature.startFeature();
			}
		},
		
		startFeature: function(){
			setInterval(feature.processFeature,1000);
		},
		
		processFeature: function(){
			if(tillscroll == 0){
				id = parseInt(id)+1 >= total_features ? 1 : parseInt(id)+1;
				feature.setAndScroll(id);
				tillscroll = 10;
			}
			else{
				tillscroll--;
			}
		},
		
		selectFeature: function(){
			var oldid = id;
			var tmp = this.id == undefined ? event.srcElement : this;
			id = tmp.id.split('-')[1];
			tillscroll = 15;
			if(id == oldid) return false;
			feature.setAndScroll(id);
			return false;
		},
		
		setAndScroll: function(id){
			currentfeature = document.getElementById('feature-'+id);
			parent = currentfeature.offsetParent
			toscroll = currentfeature.offsetTop;
			feature.startScroller();
		},
		
		startScroller: function(){
			scroller = setInterval(feature.scroll, 15);
			this.updateOpacity(0.75);
		},
		
		clearScroller: function(){
			var tmpfeatures = parent.getElementsByTagName('li');
			var e;
			var toappend = [];
			for(var i=0; i < tmpfeatures.length; i++){
				e = tmpfeatures[i];
				if(e.offsetTop < parent.scrollTop){
					toappend.push(e);
				}
			}
			for(var elem2 in toappend){
				parent.removeChild(toappend[elem2]);
				parent.appendChild(toappend[elem2]);
			}
			parent.scrollTop = 0;
			clearInterval(scroller);
			this.updateOpacity(1.0);
		},
		
		scroll: function(){
			if(parent.scrollTop != currentfeature.offsetTop){
				var diff = Math.abs(parent.scrollTop - currentfeature.offsetTop);
				if(diff == 0) feature.clearScroller();
				parent.scrollTop += diff > 10 ? 10 : diff;
			}
			else{
				feature.clearScroller();
			}
		},
		
		updateOpacity: function(value){
			typeof parent.style.opacity == "string" ? parent.style.opacity = value : parent.style.filter = 'alpha(opacity=' + value*100 + ')';
		},
		
		setContainer: function(){
			container = arguments[0];
		}
	}
})();

feature.setContainer('features');
OP.addLoadProcess(feature.init);