/*
	Clearwired
	Britt Mileshosky
	5/16/2008
	
	Example:
	--------------------------------------------------------------------------------------------------------------------------------------------
	
		<script>
			var minitabs = new MiniTab("tabs","container");
		</script>
	
		<ol id="tabs">
			<li><a>Link 1</a></li>
			<li><a>Link 2</a></li>
		</ol>
	
		<div id="container">
			<div>Content 1: Associated with link 1</div>
			<div>Content 2: Associated with link 2</div>
		</div>
	
	--------------------------------------------------------------------------------------------------------------------------------------------
  	
*/


var MiniTabs = Class.create({

	/*
		Constructor
		Parameters:
			link_container: 		The container that has a list of links in order of content markup
			content_container: 		The container that has a group of divs as first level children in the same content order as the navigation list
			
			Example:
			--------------------------------------------------------------------------------------------------------------------------------------------
			
			<script>
				var minitabs = new MiniTab("tabs","container");
			</script>
			
			<ol id="tabs">
				<li><a>Link 1</a></li>
				<li><a>Link 2</a></li>
			</ol>
			
			<div id="container">
				<div>Content 1: Associated with link 1</div>
				<div>Content 2: Associated with link 2</div>
			</div>
			
			--------------------------------------------------------------------------------------------------------------------------------------------
	*/

	initialize: function( link_container , content_container ){
		// Initialize variables
		this.current_link = "";
		this.current_content_pane = "";
		this.content_panes = $A();
		this.navlinks = $A();
		
		// Element containers
		this.link_container = $(link_container);
		this.content_container = $(content_container);
		
		//The 'if' is a hack by Glenn Laguna, 10/29/2008, because if either
                //container is null an exception is thrown
		// Retrieve necessary element
		if (this.content_container) {
		  this.content_panes = this.content_container.select('div.minitab_content');
		  this.current_content_pane = this.content_panes.first();
		  this.content_panes.without( this.current_content_pane ).invoke('hide');
		}

		if (this.link_container) {
		  this.navlinks = this.link_container.select('a');
		  this.current_link = this.navlinks.first();
		  this.current_link.up().addClassName('current');
		}
		
		
		
		// get a scope to this instance
		var thiss = this;
		
		// obsevere click event for each link
		this.navlinks.each( function( link ) {
			link.observe( 'click' , function( event ){
				event.stop();
				
				var clicked = event.element();
					clicked.blur();
					clicked.up().addClassName('current');
					thiss.current_link.up().removeClassName('current');
					thiss.current_link = clicked;
					
				thiss.hidePane( thiss.current_content_pane );
				var pane_to_show = thiss.content_panes[thiss.navlinks.indexOf( clicked )]
				thiss.current_content_pane = thiss.showPane( pane_to_show );
			});
		})
	},
	
	showPane: function( pane ){
		pane.show();
		return pane;
	},
	
	hidePane: function( pane ){
		pane.hide();
	},
	
	collectLinks: function(){
		
	}
});
