/**
 * renteTool.js
 *
 * add renteTool table functionality
 */

var renteTool = Class.create({
	
	/*
	 * CONSTRUCTOR
	 */
	initialize: function ( productWrapClassName , tabWrapClassName , pageWrapClassName , elements , activeClassName , wrapperClassName , transition )
	{
		// first transfer all delivered variables to the object
		this.productWrapClassName	= productWrapClassName;
		this.tabWrapClassName		= tabWrapClassName;
		this.pageWrapClassName		= pageWrapClassName;
		this.elements	 			= elements;
		
		this.activeClassName		= activeClassName;
		this.wrapperClassName		= wrapperClassName;
		this.transition				= transition;
		
		//internal variables
		this.elementCount 			= this.elements.length;

		this.queueId				= String( Math.random() );
		
		this.tabWraps				= new Array();
		this.tabs					= new Array();
		this.tabPages				= new Array();
		
		this.productWraps			= $$( '.' + this.productWrapClassName );
		this.productWrapCount			= this.productWraps.length;
		for( i = 0 ; i < this.productWrapCount ; i++ ) {
			this.tabWraps[i] = this.productWraps[i].down( '.' + this.pageWrapClassName );
			this.tabs[i] = new Array();
			this.tabPages[i] = new Array();
			for( j = 0 ; j < this.elementCount ; j++ ) {
				this.tabs[i][j] = this.productWraps[i].down( '.' + this.tabWrapClassName + ' .' + this.elements[j].lnk );
				this.tabPages[i][j] = this.productWraps[i].down( '.' + this.pageWrapClassName + ' .' + this.elements[j].pge );
			}
		}
		this.wrappers			= $$( '.' + this.wrapperClassName );
		this.wrapperCount		= this.wrappers.length;
		
		if( this.elementCount ) {
			this.initTabs();
		}
	},


	/*
	 * INITIALIZE RENTETOOL TABLENAV
	 */
	initTabs: function() {
		for( i = 0 ; i < this.productWrapCount ; i++ ) {
			for( var j = 0 ; j < this.elementCount ; j++ ) {
				Event.observe( this.tabs[i][j] , 'click' , this.tabClick.bindAsEventListener( this , this.tabs[i][j] , i ) );
			}
		}
	},

	/*
	 * TABCLICK HANDLER
	 */
	tabClick: function ( e , tab , row ) {
		// first stop the event
		Event.stop ( e );
		
		var page = null;
		// search the page for this tab
		var lnkCount = this.tabs[row].length;
		for( i = 0 ; i < lnkCount ; i++ ){
			if( this.tabs[row][i] == tab ) {
				page = this.tabPages[row][i];
				break;
			}
		}
		
		if( page != undefined ) {
			// then display the tab
			var tabDisplayed = this.displayTab ( tab , row );

			// shows or hides the page
			this.displayPage ( page , this.transition , tabDisplayed , row );
			
			// and finally fix the row height
			this.fixRowHeight ( page.up('div') , tab.up( 'td' ) );
		}
	},


	/*
	 * DISPLAY THE TAB AND OPTIONALLY SCROLL TO IT
	 */
	displayTab: function ( showTab , row ) {
		//first check if the tab exists
		showTab = $( showTab );

		if( showTab == null ) {
			return false;
		}
		
		var openTab = !( showTab.hasClassName( this.activeClassName ) );
		
		// first set all tabs in that row inactive
		var lnkCount = this.tabs[row].length;
		for( i = 0 ; i < lnkCount ; i++ ){
			this.tabs[row][i].removeClassName( this.activeClassName );
		}
		
		// then set the selected tab active
		if( openTab ) {
			showTab.addClassName( this.activeClassName );
		}
		
		return openTab;
	},
	
	
	/*
	 * DISPLAY THE PAGE
	 */
	displayPage: function ( showPage , transition , tabDisplayed , row ) {
		// hide all tabpages (and possible others)
		var pgeCount = this.tabPages[row].length;
		for( i = 0 ; i < pgeCount ; i++ ){
                  if ( this.tabPages[row][i]  != undefined )
                    this.tabPages[row][i].hide();
                }
		
		//then show the page, or hide the wrapper
		if( tabDisplayed ) {
			// show the active wrapper
			this.tabWraps[row].show();

			//first check if the page exists
			showPage = $( showPage );
	
			if( showPage == null ) {
				return false;
			}
			
			var pageVisible = showPage.visible();
			
			// show the active tabpage
			if( pageVisible ) {
				showPage.hide();
			} else {
				showPage.show();
			}
		} else {
			this.tabWraps[row].hide();
		}
		
		return true;
	},
	 
	/*
	 * FIX ROW HEIGHT
	 */
	fixRowHeight: function ( page , wrapper ) {
		wrapper = $( wrapper );
		wrapper.setStyle( 'height:' + ( page.getHeight() + 28 ) + 'px; #height:' + ( page.getHeight() + 24 ) + 'px;');
	}
});


/*
 *	Rentetool Filter
 */
var renteFilter = Class.create({
	
	/*
	 * CONSTRUCTOR
	 */
	initialize: function ( selectId , urlPrefix )
	{
		var filterBox = $( selectId );
		Event.observe( filterBox , 'change', function(change) {
			window.location = urlPrefix + filterBox.value;
		});
	}
});


