// Written by A.J. Michels 2008
// The American Academy of Neurology

function AAN_Newsstand(instance,id,articles,useFade){
	this.instance = instance;
	this.newsstand_id = id;
	this.article_list = articles;
	if(useFade==null){
		this.useFade = false;
	} else {
		this.useFade = useFade;
	}
	this.article_array = this.article_list.split(",");
	this.current_article = this.article_array[0];
	this.next_article = this.article_array[0];
	this.previous_article = this.article_array[0];
	this.rotateInterval = 10000;
	this.rotateTimeout;
	this.firstRotation = true;
	this.FadeTime = 1000;
	this.FadeTimeout;
	this.run = function(){
		this.hideAllArticles();
		this.startRotation(this.article_array[0]);
	}
	this.startRotation = function(article_id){
		this.showArticle(article_id);
		this.rotateTimeout = setTimeout(this.instance + ".startRotation(" + this.next_article + ")",this.rotateInterval);
	}
	this.pauseRotation = function(){
		clearTimeout(this.rotateTimeout);
	}
	this.userEnter = function(){
		this.pauseRotation()
	}
	this.userExit = function(){
		this.rotateTimeout = setTimeout(this.instance + ".startRotation(" + this.next_article + ")",this.rotateInterval);
	}
	this.showArticle = function(article_id){
		this.previous_article = this.current_article;
		this.current_article = article_id;
		this.next_article = this.calculateNextArticle(article_id);
		this.activateTab(article_id);
		if(this.useFade){
			this.hideAllArticles(article_id);
			clearTimeout(this.FadeTimeout);
			if(this.firstRotation){
				this.fade(this.createArticleElement(this.current_article),0,100,this.FadeTime);
				this.firstRotation = false;
			} else {
				this.fade(this.createArticleElement(this.previous_article),100,0,this.FadeTime);
			}
		} else {
			this.hideAllArticles();
			document.getElementById(this.createArticleElement(article_id)).style.display = 'block';
		}
	}
	this.hideArticle = function(article_id){
		document.getElementById(this.createArticleElement(article_id)).style.display = 'none';
	}
	this.hideAllArticles = function(exception){
		var array = this.article_array;
		for(var i=0; i<array.length; i++){
			if(exception != null && array[i] != exception){
				this.hideArticle(array[i]);
			} else {
				this.hideArticle(array[i]);
			}
		}
	}
	this.activateTab = function(article_id){
		this.deActivateAllTabs();
		this.addClass(this.createTabElement(article_id),'active');
	}
	this.deActivateTab = function(article_id){
		this.delClass(this.createTabElement(article_id),'active');
	}
	this.deActivateAllTabs = function(){
		var array = this.article_array;
		for(var i=0; i<array.length; i++){
			this.deActivateTab(array[i]);
		}
	}
	this.createArticleElement = function(article_id){
		var string = 'n' + this.newsstand_id + 'a' + article_id;
		return string;
	}
	this.createTabElement = function(article_id){
		var string = 'n' + this.newsstand_id + 't' + article_id;
		return string;
	}
	this.addClass = function(element,classname){
		var src_class = document.getElementById(element).className;
		var class_array = src_class.split(" ");
		for(var i=0; i<class_array.length; i++){
			if(class_array[i]==classname){return;}
		}
		document.getElementById(element).className = src_class + ' ' + classname;
	}
	this.delClass = function(element,classname){
		var src_class = document.getElementById(element).className;
		var class_array = src_class.split(" ");
		var new_class = '';
		var fail = true;
		for(var i=0; i<class_array.length; i++){
			if(class_array[i]==classname){fail=false;class_array[i]='';}
		}
		if(fail){return;}
		for(var i=0; i<class_array.length; i++){
			if(i!=0){
				new_class = new_class + ' ' + class_array[i];
			} else {
				new_class = new_class + class_array[i];
			}
		}
		document.getElementById(element).className = new_class;
	}
	this.calculateNextArticle = function(article_id){
		var array_loc = 0;
		for(var i=0; i<this.article_array.length; i++){
			if(this.article_array[i]==article_id){
				array_loc = i;
			}
		}
		if(array_loc == this.article_array.length - 1){
			return this.article_array[0];
		} else {
			return this.article_array[array_loc + 1];
		}
	}
	this.fade = function (id, opacStart, opacEnd, millisec) {
		//speed for each frame
		var speed = Math.round(millisec / 100);
		var timer = 0;
		//determine the direction for the blending, if start and end are the same nothing happens
		if(opacStart > opacEnd) { //fade out
			this.changeOpac(100,id);
			for(i = opacStart; i >= opacEnd; i--){
				this.FadeTimeout = setTimeout(this.instance + ".changeOpac(" + i + ",'" + id + "')",(timer * speed));
				timer++;
			}
			this.FadeTimeout = setTimeout(this.instance + ".fade('" + this.createArticleElement(this.current_article) + "',0,100," + this.FadeTime + ")",(timer * speed));
		} else if(opacStart < opacEnd) { //fade in
			this.changeOpac(0,id);
			for(i = opacStart; i <= opacEnd; i++){
				this.FadeTimeout = setTimeout(this.instance + ".changeOpac(" + i + ",'" + id + "')",(timer * speed));
				timer++;
			}
		}
	} 
	this.changeOpac = function (opacity, id) {
		var object = document.getElementById(id).style;
		if(opacity<=1){
			object.display = 'none';
		} else if(opacity>1) {
			object.display = 'block';
		}
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.zoom = 1;
		object.filter = "alpha(opacity=" + opacity + ")";
	}
	this.run();
}