/*************************************/
/* (C) 2008 Bluedesk E-Business B.V. */
/*																	 */
/* Author: R.Muller									 */
/*************************************/

/* mootools 1.2 module */ 

var bdFxTabs = {

	_DEBUG: false, 
  _cookieCurrentTab: null,

	initialize: function(){
		//abort if no tabs on page
		//alert($(document.body).getElement('.tab_container'));
		if ( $(document.body).getElement('.tab_container')==null ) return;
		
		this._cookieCurrentTab = new Hash.Cookie('currentTab', {path: '/'});
		
		if (this._DEBUG)
        {
            var oDebug = new Element('div', {
			    'styles': {
					    'position': 'absolute',
					    'border': '1px solid red',
					    'left': '1px',
					    'top': '300px',
					    'z-index': '300',
					    'min-width': '20px',
					    'min-height': '20px'
			    },
			    'events': {
					    'click': function(){
							    //aaa
					    },
					    'mousedown': function(){
							    //aaa
					    }
			    },
			    'id': 'mnuDebugInfo'
			    });
			    
	
			    oDebug.inject($('body'));
			    
			}
    
			//set display to none of tabElement
			$$('.tab_container').each(function(e, i) {
				//add fx
				if (!e.Fx) {
            e.Fx = new Fx.Slide(e, {mode: 'horizontal'});	
            e.Fx.hide();
				}
				//set display none!
				e.setStyle('display', 'none');
				
				
			});
    
			// Searching the tab icons items	    
			$$('.tab_container').each(function(e, i) {
				var icon = e.getFirst('div.icon');
				var tabElement = e.getElement('div.tabElement');

				if (icon) {
				  icon.injectTop($('tab_icons'));
				  icon.addEvents({
					  'click': bdFxTabs.icon_click
  			    
				  });
				
				
				  icon.setAttribute('id', 'tab'+i);
		    
				  e.setAttribute('id', 'tab'+i+'_elements');
				
				  //add close div
				  var oBtnClose = new Element('div', {
			      'class': 'Close'
			      }).injectTop(tabElement);
			    oBtnClose.addEvents({
					  'click': bdFxTabs.close_click
			    });
  				
  				
				  if (!icon.Fx) {
              icon.Fx = new Fx.Slide(icon, {mode: 'horizontal'});	
              icon.Fx.show();
              
				  }

				} 
				
			});
		
			var sCurrentTab = this._cookieCurrentTab.get('tab');
			
			if (sCurrentTab!=null && sCurrentTab!='none'){
				var oShowElements = $(sCurrentTab+'_elements'); //.getElements('.tabElement');
				//hide icon tab
				if ($(sCurrentTab)){
					$(sCurrentTab).Fx.slideOut();
					//show tab content
					oShowElements.setStyle('display', 'block');
					oShowElements.setStyle('visibility', 'visible');
					oShowElements.Fx.show();
				}
			}else if (sCurrentTab == null){
			
				$('tab_icons').getElement('.icon').fireEvent('click', null, 1000);
				

			}
		
			
  },
 
	// Show elements
	icon_click: function()
	{
		var bOpened = false;
		var oIcon = this;
		var oParentContainer = $(this.id+'_elements');//this.getParent('.tab_container');
		var oShowElements = oParentContainer.getElements('div.tabElement');
		
		oIcon.Fx.slideOut();
		$('tab_icons').getElements('.icon').each(function (e,i){
			if (e!=oIcon){
				e.Fx.slideIn();
			}
		});
		
		//alert(oParentContainer);
		
		//close other slide tab when 'open' and then when completed open current tab
		$$('.tab_container').each(function(e, i) {
			if (oParentContainer != e){ //.getParent('.tab_container')
				if (e.Fx.open){
					bOpened = true;
					e.Fx.slideOut().chain(function(){
								
								e.setStyle('display', 'none');
								
								
								oParentContainer.setStyle('display', 'block');
								oParentContainer.setStyle('visibility', 'visible');
								oParentContainer.Fx.slideIn();
								bdFxTabs._cookieCurrentTab.set('tab', oIcon.id);
								
					});
				}
				
			}
		});
		
		//there was no close of an other open tab, open the current tab now
		if (!bOpened){
			oParentContainer.setStyle('display', 'block');
			oParentContainer.setStyle('visibility', 'visible');
			oParentContainer.Fx.slideIn();
			bdFxTabs._cookieCurrentTab.set('tab', oIcon.id);
		}
	},
	
	close_click: function()
	{
		var oParentContainer = this.getParent('.tab_container');
		var oHideElements = oParentContainer.getElements('div.tabElement');
		bdFxTabs._cookieCurrentTab.set('tab', 'none');
		
		//slide out tab
		oParentContainer.Fx.slideOut().chain(function(){
				oParentContainer.setStyle('display', 'none');
		});
		
		//restore tab icons
		$('tab_icons').getElements('.icon').each(function (e,i){
				e.Fx.slideIn();
		});
	}
	
};

	
window.addEvent('domready', function(){
    
    bdFxTabs.initialize();
    
});