/*
 * SimpleFader v1.0.0
 * http://www.faebusoft.ch/projects/simplefader
 * 
 * Copyright 2010, faebusoft.ch, Fabian von Allmen
 * Licensed under GPL Version 2 licenses.
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * 
 * Version: v1.0.0 (2010-10-27)
 * Author: faebusoft.ch, Fabian von Allmen
 */
var simplefader={
	num:-1,
	cur:-99,
	old:-1,
	step:1,
	intervall:6*1000, // 6 seconds by default
	fadeintervall: 700,
	holderid:'simplefader',
	controlid:'simplefadercontrols',
	screens:null,
	timer:null,
	running:true,
	stop:false,
	screenchooser:true,
	playresume:false,
	init:function() {
		this.screens = jQuery('#'+this.holderid).children('.screen');
		this.num = this.screens.length;
		
		if (this.num == 0) {
			return;
		}
		
		this.screens.each(function(i) {
			jQuery(simplefader.screens[i]).mouseover(function() {
				simplefader.running = false;
			});
			jQuery(simplefader.screens[i]).mouseout(function() {
				simplefader.running = true;
			});
			
			// Create link
			if (simplefader.screenchooser) {
				jQuery('#'+simplefader.controlid).append('<a href="#" id="simplefaderctrl'+i+'" onclick="simplefader.fadeTo('+i+');return false;" onfocus="this.blur();"></a>');
			}
		});
		
		jQuery('#'+simplefader.controlid).mouseover(function() {
			simplefader.running = false;
		});
		jQuery('#'+simplefader.controlid).mouseout(function() {
			simplefader.running = true;
		});
		
		if (this.playresume) {
			jQuery('#'+simplefader.controlid).append('<a href="#" onclick="simplefader.startStop();return false;" class="playresume" onfocus="this.blur();">&nbsp;</a>');
		}
		
		if (this.step < 0) {
			this.fadeTo(this.num - 1);
		} else {
			this.fadeTo(0);
		}
			
		// Undo Stop event
		if (this.stop) {
			this.startStop();
		}
	},
	fadeTo:function(pos) {
		window.clearTimeout(this.timer);
		this.old = this.cur;
		this.cur = pos;
		
		this.fade();
		this.timer=window.setTimeout('simplefader.walk();',this.intervall);
		
		if (!this.stop) {
			this.startStop();
		}
	},
	startStop:function() {
		this.stop = !this.stop;
		
		if (this.stop) {
			jQuery('#'+simplefader.controlid+' a.playresume').addClass('fadernavon');
		} else {
			jQuery('#'+simplefader.controlid+' a.playresume').removeClass('fadernavon');
		}
	},
	walk:function() {
		if (this.running && !this.stop) {
			// Stop Timer
			window.clearTimeout(this.timer);
			this.old = this.cur;
			
			if (this.cur == -99) {
				this.cur = 0;
			} else {
				this.cur += this.step;
			}
			
			if (this.cur >= this.num) {
				this.cur = 0;
			} else if (this.cur < 0) {
				this.cur = this.num - 1;
			}
			
			this.fade();
		}
		
		// Start Timer
		this.timer=window.setTimeout('simplefader.walk();',this.intervall);
	},
	fade:function() {
		if (this.old > -1) {
			jQuery('#simplefaderctrl'+simplefader.old).removeClass('fadernavon');
			jQuery(this.screens[this.old]).fadeOut(this.fadeintervall, function() {
				//jQuery('#simplefaderctrl'+simplefader.old).removeClass('fadernavon');
			});
		}
		if (this.cur > -1) {
			jQuery(this.screens[this.cur]).fadeIn(this.fadeintervall, function() {
				jQuery('#simplefaderctrl'+simplefader.cur).addClass('fadernavon');
			});
		}
	}
};
