/**
 * ydtab class
 * @description tested in IE_,IE7,FF_,FF3,Safari _,Safari 4(beta),Google Chrome & Opera 9.6__
 * @subpackage prototype
 * @author basil suter
 * @date 13-03-2008
 * @version v2.0a
 */

// ydtab class creation
var ydtab = Class.create({
	
	// construct
	initialize: function(tab_count,tab_wrap,nav_wrap,class_activ,class_inactiv){
		// ver gen
		this.tab_count = tab_count; // set tab count from external
		this.tab_wrap = tab_wrap;
		this.nav_wrap = nav_wrap;
		this.class_activ = class_activ;
		this.class_inactiv = class_inactiv;
		
    // fix
		this.this_tab = 0;
		this.elmn_tab_wrap = $(this.tab_wrap).childElements();
		
		// activ in now
		this.obsrv(); // set click observ options
		this.drp(this.this_tab); // exec drop-in tabber with index 0
	},
	
	// observ all elments in nav_wrap onclick this.drp
	obsrv: function(){
		nav_wrap_arr = $$('#'+this.nav_wrap+' li')
		nav_wrap_arr.each(function (tog_this, index) {
		  tog_this.observe('click', function () {
        tabber.drp(index);
		  }.bind(this));
		});
	},
	
	// drop-in function for changes
	drp: function(this_tab){
		// set the THIS CHANGE PARAM "this_tab"
		this.this_tab = this_tab; // this tab will be activated	
		this.elmn_tab_wrap.each(function(div){
			$(div).setStyle({
				display: 'none'
			});
		});
		
		// start now the activation and linktriggerer
		this.activate();
		this.linktrigger();
	},
	
	// activate this selected tab
	activate: function(){
		$(this.elmn_tab_wrap)[this.this_tab].setStyle({
			display: 'block'
		});
	},
	
	// set style classes for navigation wrap
	linktrigger: function(){
		for(i=0;i<this.tab_count; i++){
			this_elmn = $$('#'+this.nav_wrap+' li')[i];
			if(this.this_tab==i){
				$(this_elmn).className = this.class_activ
			}else{
				$(this_elmn).className = this.class_inactiv
			}
		}
	}
	
});

// comparisation between
function ydtab_compare(wrap,nav){
	count_1 = $(wrap).childElements().size();
	count_2 = $(nav).childElements().size();
	if(count_1!=count_2){
		alert("YDTAB: TAB_WRAP NOT EQUAL NAV_WRAP");
	}else{
		return count_1;
	}
}

// init class by loading
document.observe('dom:loaded',function(){
	/*
	 * ydtab config params
	 * @param tabs wrapper id
	 * @param navigation wrapper id
	 * @param navigation activ class
	 * @param navigation inactiv class
	 */
	ydtab_wrapper 		= 'ydtab_wrap';
	ydtab_navigator 	= 'ydtab_nav';
	ydtab_class_activ 	= 'ydtab_activ';
	ydtab_class_inactiv	= 'ydtab_inactiv';
	// if both elements are available go trough and start ydtabber
	if(document.getElementById(ydtab_wrapper) && document.getElementById(ydtab_navigator)){
		// start class and write in tabber
		tabber = new ydtab(ydtab_compare(ydtab_wrapper,ydtab_navigator),ydtab_wrapper,ydtab_navigator,ydtab_class_activ,ydtab_class_inactiv); // tabber is now reserved	
	}
});