var a2f = {
	Version: '0.0.1.beta',
	selectedTab: new Array(),
	
	tabConfigList: new Array(),
	actionList: new Array(),
	
	onLoad: function () {
		for ( var i = 0; i < this.actionList.length; i++ )
		{
			eval( this.actionList[i] );
		}
	},
	
	actionMakeTabPanels: function () {
		for ( var i = 0; i < this.tabConfigList.length; i++ )
		{
			this.makeTabPanel( this.tabConfigList[i] );
		}
	},
	
	addTabConfig: function ( tabConfig ) {
		this.tabConfigList.push( tabConfig );
		this.appendCommand('makeTabPanel');
	},
	
	appendCommand: function ( commandName ) {
		var command_string = '';
		switch ( commandName )
		{
			case 'makeTabPanel':
				command_string = 'this.actionMakeTabPanels();';
				break;
		}
		
		var found = false;
		if ( command_string != '' )
		{
			if ( !this.inArray( command_string, this.actionList ) )
			{
				this.actionList.push( command_string );
			}
		}
	},
	
	changeTab: function ( panelId, tabId , labelId) {
		if ( this.selectedTab[panelId] )
		{
			if ( this.selectedTab[panelId].tabObj )
				this.selectedTab[panelId].tabObj.style.display = 'none';
			
			if ( this.selectedTab[panelId].labelObj )
				this.selectedTab[panelId].labelObj.className = 'unselected';
		}

		
		tabObj = $(tabId);
		if ( tabObj )
		{
			tabObj.style.display = 'block';
			$(labelId).className = 'selected';
			
			this.selectedTab[panelId] = { tabObj: tabObj, labelObj: $(labelId) };
		}
		
	},
	
	makeTabPanel: function ( tabConfig ) {

		container = $(tabConfig.tabContainerId);		
		if ( !container )
		{
			return;
		}
		
		if ( !tabConfig.tabElements)
		{
			return;
		}
		
		selectedId = tabConfig.selectedId;
		
		if ( tabConfig.tabElements )
		{
			// create div that contains all tab labels
			var tabLinks = document.createElement("div");
			tabLinks.className = 'a2f_TabLabels';
			
			var selTabId = '';
			var selLabelId = '';
			
			for ( var idx in tabConfig.tabElements )
			{
				tabId =  tabConfig.tabElements[idx].id ;
				tabLabelId =  tabConfig.tabElements[idx].label_id ;
				
				if ( tabId == selectedId )
				{
					selTabId = tabId;
					selLabelId = tabLabelId;
				}
			}
			
			for ( var el in tabConfig.tabElements )
			{
				tabId =  tabConfig.tabElements[el].id ;
				tabObj = $(tabId);
				if ( !tabObj )
				{
					continue;
				}
				
				container.appendChild(tabObj);
				$(tabObj).style.display = 'none';
			}
			
			//alert(selTabId + ' , ' + selLabelId);
			this.changeTab( tabConfig.tabContainerId, selTabId, selLabelId );
		}
	},
	
	inArray: function ( checkElement, arrayObj  ) {
		var found = false;
		for ( var i = 0; i < arrayObj.length; i++ )
		{
			if ( arrayObj[i] == checkElement )
			{
				found = true;
				break;
			}
		}
		return found;
	}
}

Event.observe( window, 'load', function (){
			a2f.onLoad()
			}
	);