/**
 * FOK.tabBlock - bouw een zelf-bladerend tabbladenblok
 * Code gebaseerd op YUI (developer.yahoo.com/yui).
 *
 * @author Peter Breuls
 * @copyright Fok Internet Sites 2007-2008
 */
if(typeof FOK=="undefined"){
	var FOK={};
	FOK.register = {}
}
FOK.register.tabBlocks = new Array();

FOK.tabBlock = function(baseName,titel,interval,subtitle){

	this.tabBlockID = FOK.register.tabBlocks.length;
	FOK.register.tabBlocks[this.tabBlockID] = this;
	var id = this.tabBlockID;

	this.tabView = new YAHOO.widget.TabView(baseName);
	this.tabView.addClass('yui-navset');
	this.tabView.subtitle = new Array();

	this.titel    = titel;
	this.interval = interval;
	this.baseName = baseName;
	this.subtitle = subtitle;

	this.userActivateTab  = function(e,currentTab){

		var tabs = this.tabView.get('tabs');
		for (var i = 0, len = tabs.length; i < len; i++) {
			var tab = tabs[i];
			tab.set('active', false);
			tab.set('contentVisible', false);
		}
		currentTab.set('active', true);
		currentTab.set('contentVisible',true);
		this.currentIndex = this.tabView.getTabIndex(currentTab);
		if(this.subtitle==1){
			var element = this.baseName+'_subtitle';
			document.getElementById(element).innerHTML=this.tabView.subtitle[this.currentIndex];
		}
		this.cycleRunning = 0;
	}

	this.resumeCycle = function(e,currentTab){
		this.tabView.set('activeTab',currentTab);
		this.currentIndex = -1;
		this.cycleRunning = 1;
	}

	this.stopCycle = function(e,currentTab){
		this.tabView.set('activeTab',currentTab);
		this.currentIndex = -1;
		if(this.cycleStopped == 1){
			this.cycleStopped = 0;
		}else{
			this.cycleStopped = 1;
		}
	}

	this.setSubtitle = function(e,i){
		var element = this.baseName+'_subtitle';
		try{
			document.getElementById(element).innerHTML=this.tabView.subtitle[this.tabView.getTabIndex(e.newValue)];
		}catch(e){}
	}

    this.addTab = function(titel,body,subtitle){

	    var tabs = this.tabView.get('tabs');
	   	var i = tabs.length;

	    this.tabView.addTab( new YAHOO.widget.Tab({
	        label: titel,
	        content: body
	    }));

	    this.tabView.subtitle[this.tabView.subtitle.length] = subtitle;

		this.tabView.getTab(i).addListener('mouseover', this.userActivateTab,tabs[i],this);
		this.tabView.getTab(i).addListener('mouseout', this.resumeCycle,tabs[i],this);
		this.tabView.getTab(i).addListener('click', this.stopCycle,tabs[i],this);
    }

    this.toString = function(){
		return "FOK.tabBlock '"+this.titel+"' #"+this.baseName+" "+this.interval+"sec.";
    }

	this.cycle = function(){

		if(this.cycleStopped == 1){
			setTimeout("FOK.register.tabBlocks["+this.tabBlockID+"].cycle();",(this.interval*1000));
			return;
		}

		if(this.cycleRunning == 0){
			setTimeout("FOK.register.tabBlocks["+this.tabBlockID+"].cycle();",(this.interval*1000));
			return;
		}

		var tabs = this.tabView.get('tabs');
		if(this.currentIndex<0){
			var next=0;
		}else if(typeof(this.currentIndex)!='undefined'){
			var next = this.currentIndex+1;
			if(next>=tabs.length){
				next=0;
			}
		}else{
			var next=0;
		}
		this.currentIndex = next;
		this.tabView.set('activeTab',this.tabView.getTab(next));
		setTimeout("FOK.register.tabBlocks["+this.tabBlockID+"].cycle();",(this.interval*1000));
	}
	if(subtitle==1){
		this.tabView.addListener('activeTabChange', this.setSubtitle,null,this);
	}

	var block = document.getElementById(baseName);

	if(block!=null){
		var elems = block.getElementsByTagName('ul');
		var ul = elems[0];
		var content = block.childNodes[1];
		content.onmouseover = function(){
			FOK.register.tabBlocks[id].cycleRunning = 0;
		}
		content.onmouseout = function(a,b){
			FOK.register.tabBlocks[id].cycleRunning = 1;
		}

		var header = document.createElement('span');
		header.setAttribute('name', baseName+'_header');
		header.setAttribute('id', baseName+'_header');
		header.innerHTML = titel;
		block.insertBefore(header,ul);

		if(subtitle==1){
			var subtitle = document.createElement('div');
			subtitle.setAttribute('name', baseName+'_subtitle');
			subtitle.setAttribute('id', baseName+'_subtitle');
			subtitle.className='subtitle';
			subtitle.innerHTML = this.tabView.subtitle[0];
			block.appendChild(subtitle);
		}
	}else{
		this.addTab = function(){}
		this.cycle = function(){}
		alert("Area '"+baseName+"' not defined.");
	}
}