var fadeShow = function(args) 
{
	return( new fadeShow.obj.initialize(args) );
}

fadeShow.obj = 
{
	initialize: function()
	{
		// Do error checking here
	    if(Object.isUndefined(arguments[0])) {
			return(null); 
		}
		initobj = arguments[0];
		var self = this;
		
		this.containerDiv = initobj.containerDiv;
		
		// Set speed
		this.speed = 12000;
		if( initobj.speed == "fast"){ this.speed = this.speed/2; }
		else if( initobj.speed == "slow") { this.speed = this.speed * 2; }
		
		// Get our divs
		this.fadeDivs = $(this.containerDiv).childElements();
		if( this.fadeDivs.length < 2 ) { alert('nope');return(null); } // you can't fucking scroll 1 man
		this.numScreens = this.fadeDivs.length;
		
		// Set the dimensions of the container
		$(this.containerDiv).style.width = this.fadeDivs[0].getWidth() + "px";
		$(this.containerDiv).style.height = this.fadeDivs[0].getHeight() + "px";
		
		// Check for nav
		this.nav = null;
		
		if(initobj.nav)
		{
			if(initobj.nav.id && initobj.nav.activeClass && initobj.nav.inactiveClass)
			{
				if( $(initobj.nav.id))
				{
					this.nav = initobj.nav;
					this.nav.children = $(initobj.nav.id).firstDescendant().childElements();
					if( this.nav.children.length < this.numPages ) { alert('invalid nav'); this.nav = null; }
					else { this.nav.children[0].className = this.nav.activeClass; }
				}
			}
		}
		
		// Set defaults for class
		this.activeScreen = 0;
		this.currentScreen = 0;
		this.nextScreen = 1;
		this.isMoving = 0;
		this.setupSlides();
		
		return(this);
	},
	
	setupSlides: function()
	{
		// Reset all slides and hide them
		this.fadeDivs.each(function(f) {
			f.style.position = "absolute";
			f.style.zIndex = "0";
			f.hide();
		});
		
		// Show active screen and put it on top
		this.fadeDivs[this.activeScreen].style.zIndex = "100";
		this.fadeDivs[this.activeScreen].show();
		
		// Set next screen right below and show it
		this.fadeDivs[this.nextScreen].style.zIndex = "90";
		this.fadeDivs[this.nextScreen].show();
	},
	
	nextSlide: function()
	{
		if( ! this.approveAction() ) return;
		this.currentScreen = this.activeScreen;
		this.nextScreen = this.activeScreen + 1;
		if( this.nextScreen >= this.numScreens) { this.nextScreen = 0; }
		this.setupSlides();
		this.activeScreen++;
		if( this.activeScreen >= this.numScreens) { this.activeScreen = 0; }
		this.fadeScreen(this.fadeDivs[this.currentScreen]);
	},
	
	prevSlide: function()
	{
		if( ! this.approveAction() ) return;
		this.currentScreen = this.activeScreen;
		this.nextScreen = this.activeScreen -1;
		if( this.nextScreen < 0 ) { this.nextScreen = this.numScreens - 1; }
		this.setupSlides();
		this.activeScreen--;
		if( this.activeScreen < 0 ) { this.activeScreen = this.numScreens - 1; }
		this.fadeScreen(this.fadeDivs[this.currentScreen]);
	},
	
	goToSlide: function(num)
	{
		if( ! this.approveAction() ) return;
		if(this.fadeDivs[num])
		{
			this.currentScreen = this.activeScreen;
			this.nextScreen = num;
			this.setupSlides();
			this.activeScreen = num;
			this.fadeScreen(this.fadeDivs[this.currentScreen]);
		}
	},
	
	startFadeShow: function()
	{
		var self = this;
		var theFunk = function() { self.nextSlide(); }
		this.fadeInterval = setInterval(theFunk,this.speed);
	},
	
	approveAction: function()
	{
		if(this.isMoving == 1) { return(false); }
		return(true);
	},
	
	fadeScreen: function(s)
	{
		clearTimeout(this.fadeInterval);
		var self = this;
		this.isMoving = 1;
		if(self.nav)
		{
			var count = 0;
			self.nav.children.each( function(e) {
				e.className = "inactive";
				if(count == self.activeScreen) {
					e.className = "active";
				}
				count++;
			});
		}
			
		Effect.Fade(s, {duration: 1, afterFinish: function() {
			self.isMoving = 0;
			self.startFadeShow();
		}});
	}
}
fadeShow.obj.initialize.prototype = fadeShow.obj;
