/**
 * @author - Sean Kellogg <skellogg@articulatedman.com
 * @copyright - 2008 Articulated Man, Inc
 * @package - tabBox
 * @version - 1.2
 * @dependencies - prototype.js 1.6+, scriptaculous.js 1.8+
 *
 * @functionality - creates a centered box in the page with the rest of the page
 *                  fadded out and fills with either flash or HTML content
 *
 * @usage
 * - Load the dependency libraries and then this library
 * - create as many FrameBox objects as you want passing a unique ID, the flash
 *   object OR html string, and any options you want to override.
 *   # example = new FrameBox('example','/assets/flash.swf',{color:'blue'});
 *
 * - set example.open() to any trigger you want to cause the box to open
 * - set example.close() to any trigger you want to cause the box to close
 *
 * @notes
 * - by default the box will stay stationary with the page when you scroll, but
 *   if you want it to scroll with the page, set stationary to 1
**/

TabBox = Class.create();
Object.extend(TabBox.prototype,{

	initialize: function(id,options){

		this.options = {
			frequency:	5,
			play:		true
		};

		this.id = id;
		this.options = Object.extend(this.options,options);
		this.play = this.options.play;

		this.displays = $(this.id).select('.link');
		this.current_index = 0;
		
		this.displays.each(function(s){
			s.hide();
			if (this.displays[0].id == s.id) this.current_id = this.parse_id(s.id);
		},this);

		$('link_' + this.current_id).show();
		if(this.play) this.start();

	},

	// public methods

	parse_id: function(full_id){
		var id = full_id.split('_');
		return id[1];
	},

	auto: function(id){

		var next_id = id;
		var current_id = this.current_id;
		this.current_id = id;

		Effect.Fade('link_' + current_id);
		Effect.Appear('link_' + next_id);

		$('control_' + current_id).toggleClassName('selected');
		$('control_' + next_id).toggleClassName('selected');

	},

	manual: function(id){

		var current_id = this.current_id;
		var next_id = id;
		this.current_id = id;

		$('link_' + current_id).hide();
		$('link_' + next_id).show();

		$('control_' + current_id).toggleClassName('selected');
		$('control_' + next_id).toggleClassName('selected');

	},

	go: function(id){

		id = (this.next_id) ? this.next_id : id;
		this.next_id = 0;
		if (id == this.current_id) return ;

		if( $('link_' + id).style.display ){
			this.manual(id);
		} else {
			this.next_id = id;
			setTimeout(this.go.bind(this),100);
		}

	},

	next: function(){

		this.current_index = (this.displays.length - 1 == this.current_index) ? 0 : this.current_index + 1;
		this.auto( this.parse_id(this.displays[this.current_index].id) );

	},

	stop: function(){
		this.play = false;
		clearTimeout(this.timer);
	},


	start: function(){
		this.play = true;
		this.periodicallyUpdate();
	},
	

	periodicallyUpdate: function(){
		if (this.timer != null) {
			clearTimeout(this.timer);
			this.next();
		}
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency*1000);
	}


});