// stickmanlabs crossfade.js v0.2

// Copyright (c) 2007 Kevin Miller (http://stickmanlabs.com)
// 
// Crossfade is freely distributable under the terms of an MIT-style license.
// For details, see the stickmanlabs web site: http://stickmanlabs.com

var Crossfade = Class.create();

Crossfade.prototype = {
	initialize : function(elm, options){
		this.container = $(elm);
		this.counter = 1;
		this.direction = 'forward';
		this.next = null;
		this.options = Object.extend({
			url : '',
			currentslide : 0,
			slidecount : 5,
			interval : 5,
			preloadinterval : 1,
			toggleoverlay : [],
			crossfade : true,
			disableeffects : false,
			tabs : false,
			player : false,
			duration : 1.0,
			fps : 50
		}, options || {})
		if (this.options.player) this.makeplayer();
		if (this.options.tabs) this.setuptabs();
		this.firstslide();
		this.preload();
		this.preloadcycle = new PeriodicalExecuter(this.preload.bind(this), this.options.preloadinterval);
		this.slidecycle = new PeriodicalExecuter(this.slide.bind(this), this.options.interval); 
	},
	preload : function(){
		var container = this.container.id+'-'+this.counter;
		var opt = {
    		method: 'get',
			onCreate: function(){
			},
			onSuccess: function(t){
				$(container).innerHTML = t.responseText;
			},
			on404: function(t){
				alert('Error 404: location "' + t.statusText + '" was not found.');
			},
			onFailure: function(t){
				alert('Error ' + t.status + ' -- ' + t.statusText);
			}
		}       
		// Amendment for testing .html
		new Ajax.Request(this.options.url+this.counter+'.html', opt);
		this.counter++;
		if (this.counter == this.options.slidecount) this.preloadcycle.stop();
	},
	firstslide : function (){
		$(this.container.id+'-'+this.options.currentslide).style.display = 'none';	
		var current = this.container.id+'-'+this.options.currentslide;
		var opt = this.options;
		if (!this.options.disableeffects){
			new Effect.Appear(this.container.id+'-'+this.options.currentslide, 
				{duration: this.options.duration, fps: this.options.fps, beforeStart : function() {
					if (opt.tabs){	
						$(current+'-tab').className = 'fade-box-tab-active';
					}
					for (var x = 0; x < opt.toggleoverlay.length; x++){
						if (opt.toggleoverlay[x]){
							$(current+'-fade-box-overlay-'+x).style.display = 'none';
						}
					}
				}, afterFinish : function(){
					for (var x = 0; x < opt.toggleoverlay.length; x++){
						if (opt.toggleoverlay[x]){
							$(current+'-fade-box-overlay-'+x).style.display = 'block';
						}
					}
				}}			
			);			
		} else {
			$(this.container.id+'-'+this.options.currentslide).style.display = 'block';
			if (opt.tabs){
				$(current+'-tab').className = 'fade-box-tab-active';
			}
		}
	},
	slide : function(){
		var initialslide = this.options.currentslide;
		var opt = this.options;
		if (this.direction == 'forward' || this.direction == 'play') this.options.currentslide++;
		else if (this.direction == 'reverse') this.options.currentslide--;
		if (this.options.currentslide >= this.options.slidecount) this.options.currentslide = 0;
		if (this.options.currentslide < 0) this.options.currentslide = this.options.slidecount-1;
		if (this.next != null) this.options.currentslide = this.next;	
		var initial = this.container.id+'-'+initialslide;
		var current = this.container.id+'-'+this.options.currentslide;
		if (!this.options.disableeffects){
			new Effect.Parallel([ 	new Effect.Fade(initial, {sync:true}),
				new Effect.Appear(current, {sync:true})	],
				{duration: opt.duration, fps: opt.fps, beforeStart : function(){
					if (opt.tabs){
						$(initial+'-tab').className = 'fade-box-tab';		
						$(current+'-tab').className = 'fade-box-tab-active';
					}
					for (var x = 0; x < opt.toggleoverlay.length; x++){
						if (opt.toggleoverlay[x]){
							$(initial+'-fade-box-overlay-'+x).style.display = 'none';
							$(current+'-fade-box-overlay-'+x).style.display = 'none';
						}
					}
				}, afterFinish : function(){
					for (var x = 0; x < opt.toggleoverlay.length; x++){
						if (opt.toggleoverlay[x]){
							$(initial+'-fade-box-overlay-'+x).style.display = 'block';
							$(current+'-fade-box-overlay-'+x).style.display = 'block';
						}
					}
				}}
			);			
		} else {
			$(initial).style.display = 'none';
			$(current).style.display = 'block';
			if (opt.tabs){
				$(initial+'-tab').className = 'fade-box-tab';		
				$(current+'-tab').className = 'fade-box-tab-active';
			}
		}
		this.pauseslide();
	},
	toggleplay : function(){
		if (this.direction == 'pause' || this.direction == 'reverse' || this.direction == 'forward' || this.direction == 'skipto'){
			$(this.container.id+'-pause').style.display='none'; 
			$(this.container.id+'-play').style.display='block';
		} else if (this.direction == 'play'){ 
			$(this.container.id+'-play').style.display='none'; 
			$(this.container.id+'-pause').style.display='block';
		}
	},
	player : function(cmd, skipto){
		this.direction = cmd;
		this.toggleplay();
		for (var x = 0; x < this.options.toggleoverlay.length; x++){
			for (var z = 0; z < this.options.slidecount; z++){
				if ($(this.container.id+'-'+z+'-fade-box-overlay-'+x)) $(this.container.id+'-'+z+'-fade-box-overlay-'+x).style.display = 'block';
			}
		}
		if (cmd == 'pause'){
			this.slidecycle.stop();
			this.next = null;
		}else if(cmd == 'play'){
			this.next = null;
			this.slide();
			this.slidecycle = new PeriodicalExecuter(this.slide.bind(this), this.options.interval); 
		}else if (cmd == 'reverse' || cmd == 'forward'){
			this.slidecycle.stop();
			this.next = null;
			this.slide();
		}else if (cmd == 'skipto'){
			this.slidecycle.stop();
			this.next = skipto;
			this.slide();
		}
	},
	makeplayer : function(){
		$(this.container.id+'-fade-box-controls').style.display = 'block';
		$(this.container.id+'-play').style.display = 'none';
	},
	setuptabs : function(){
		for (var x = 0; x < this.options.slidecount; x++){
			$(this.container.id+'-'+x+'-tab').removeAttributeNS("", "href");
		}
	},
	pauseslide : function(){
		var mill = 300*this.options.duration;
		var date = new Date();
		var curDate = null;
		do { curDate = new Date(); }
		while(curDate-date < mill);
	} 
};
