var PanelBox = Class.create();

PanelBox.prototype = {
  initialize: function () {
		return;
	},
	
	// public attributes: on_show, on_hide, timeout_id
	// private attributes: panel_list, tab_list
	
	// Initializers
  find_panels: function ( panel_spec ) {
		this.panel_list = $$( panel_spec );
		// alert( "Found " + this.panel_list.length + " panels." );
	},
  find_tabs: function ( tab_spec ) {
		this.tab_list = $$( tab_spec );
		// alert( "Found " + this.tab_list.length + " tabs." );
	},
	
	// Core
	pick_panel: function ( panel_id ) {
		if ( this.timeout_id ) {
			clearTimeout( this.timeout_id );
		}
		
		// alert( "Picking from " + this.panel_list.length + " panels." );
		// if ( this.tab_list ) {
		//   alert( "Picking from " + this.tab_list.length + " tabs." );
		// }

		var target_index;
		var offset = ( panel_id + "" ).match( /^([+]|[-]|)(\d+)$/ );
		if ( offset ) {
			// alert('Select offset ' + offset[1] + " / " + offset[2] );
			if ( offset[1].length ) {
				var selected_index = -1;
		    var found_it = this.panel_list.find( function ( element ) {
					selected_index ++;
					return element.hasClassName( 'active' );
				} );
				var current_index = found_it ? selected_index : 0;
				// alert('Select current ' + current_index + " + " + offset[2] );
				target_index = ( current_index / 1 ) + ( offset[2] / 1 );
			} else {
				target_index = offset[2];
			}
			// alert('Select target ' + target_index + " of " + this.panel_list.length );
			target_index = target_index % this.panel_list.length;
			// alert('Select for "' + offset[0] + '": index ' + target_index + " of " + this.panel_list.length );
			panel_id = this.panel_list[ target_index ].id;
		}

		var auto_this = this;
		var index = -1;
    this.panel_list.each( function ( element ) {
			// alert('Compare id ' + element.id + ' == ' + panel_id );
			index ++;
			if ( element.id == panel_id ) {
				if ( ! element.hasClassName( 'active' ) ) {
					// alert('Activating panel ' + element.id );
					if ( auto_this.tab_list ) {
						// alert('Activating tab ' + index + ": " + auto_this.tab_list[ index ] );
						auto_this.tab_list[ index ].addClassName('active');
					} else {
						// alert("No tabs in " + auto_this.tab_list );
					}
					if ( auto_this.show_panel ) {
						/// alert('Calling show_panel ' + index );
						auto_this.show_panel( element );
					} else {
						// alert("No show_panel " + index );
					}
					element.addClassName('active');
				}
			} else {
				if ( element.hasClassName( 'active' ) ) {
					// alert('Deactivating panel ' + element.id );
					if ( auto_this.tab_list ) {
						// alert('Deactivating tab ' + index + ": " + auto_this.tab_list[ index ] );
						auto_this.tab_list[ index ].removeClassName('active');
					} else {
						// alert("No tabs in " + auto_this.tab_list );
					}
					if ( auto_this.hide_panel ) {
						// alert('Calling hide_panel ' + index );
						auto_this.hide_panel( element );
					} else {
						// alert("No hide_panel " + index );
					}
					element.removeClassName('active');
				}
			}
		} ); 
	},
	
	// Auto Advance behavior
	auto_advance: function ( seconds_delay, increment, max_steps ) {
		this.step_count = this.step_count ? this.step_count : 0;
		this.step_count ++;

		if ( max_steps ) {
			this.max_steps = max_steps;
		}

		if ( seconds_delay ) {
			this.auto_advance_delay = seconds_delay;
		} else {
			this.auto_advance_delay = this.auto_advance_delay || 3;
		}
		if ( increment ) {
			this.auto_advance_incr = increment;
		} else {
			this.auto_advance_incr = ( this.auto_advance_incr && this.auto_advance_incr.length ) ? this.auto_advance_incr : "+1";
		}

		if ( ! this.max_steps || ( this.step_count < this.max_steps ) ) {
			// alert( "auto_advance: " + seconds_delay );
			//alert( this.max_steps + " " + this.step_count );
			var auto_this = this;
			this.timeout_id = setTimeout( 
				function () { auto_this.auto_advance_step() }, 
				this.auto_advance_delay * 1000 
			);
		}
	},
	auto_advance_step: function () {
		// alert( "auto_advance_step: " + this.auto_advance_incr );
		this.pick_panel( this.auto_advance_incr );
		this.auto_advance();
	}
};
