// JavaScript Document
var CQuicklinksSwitch = {  
	load: function(blokken,quicklinks) 
	{   
//	alert('hier');return;
//alert(quicklinks);
		this.showblok = 0;
		this.hideblok = 0;
		this.effect = false;
		this.effecttimer = false
		this.timer = 500;
		this.hoverTimer = true;
		this.hoverTime = 500;		
		this.defaultblok = 0;
		this.defaultblokstatus = false;
		this.links = $(quicklinks).select('.bg');
		this.blokken = $(blokken).select('.blok');		
		//this.obj = obj;
//		alert(this.items.length);
		this.pe = false;		
		if(this.links.length==0) return false;
		if(this.blokken.length==0) return false;	
		if(this.links.length < this.links.blokken) return false;		

		for(var i=0; i<this.blokken.length; i++)
		{
			if(!this.blokken[i].visible())
			{ 
				this.defaultblok = i;		
				this.defaultblokstatus = true;
			}
		}
		for(var i=0; i<this.blokken.length; i++)
		{		
			this.set(this.blokken[i]);			
		}
	},
	autoslide: function()
	{
		//clearTimeout(this.effecttimer); 
		if(this.pe)
			this.pe.stop();
		this.pe = new PeriodicalExecuter(this.autonext.bind(this),5);			
	},
	showeffect: function()
	{
//		this.links[this.showblok].setOpacity(1);
//		this.links[this.hideblok].setOpacity(0);	
		if(this.showblok!=this.hideblok)
		{
			//event.stop();

				
			for(var i=0; i<this.links.length; i++)
			{
				if(i!=this.showblok && i!=this.hideblok)
					this.links[i].hide();
			}	
			//alert(this.showblok + ' - ' +this.hideblok);				
//				this.links[this.showblok].show();
//				this.links[this.hideblok].hide();				
//				Effect.Fade(this.links[this.hideblok]);
//				Effect.Appear(this.links[this.showblok]);				
				this.links[this.showblok].setStyle({zIndex:1000, opacity:0});
				this.links[this.hideblok].setStyle({zIndex:1001, opacity:1});

			
			this.effect = new Effect.Parallel([
				Effect.Fade(this.links[this.hideblok], {sync: true,duration: 1.0}),
				Effect.Appear(this.links[this.showblok], {sync: true,duration: 1.0})			
				]);							
		}
	},
	autonext: function()
	{
		//clearTimeout(this.effecttimer); 
		clearTimeout(this.hoverTimer);				
		this.hideblok = 0;
		this.showblok = 1;
		for(var i=0; i<this.links.length; i++)
		{
			if($(this.links[i]).visible())
			{
				this.hideblok = i;
				this.showblok = i+1;
			}
		}
		if(this.showblok>=this.links.length) this.showblok=0;
//		alert(a);
		this.showeffect();
//		this.links[hide].hide();
	},	
	set: function(obj)
	{

		obj.observe('mouseover',this.show.bind(this));
		if(this.defaultblokstatus)
		{
			//alert(obj.className);
			obj.observe('mouseout',this.showdefault.bind(this));
							
		}
		else
		{
			this.autoslide();
			obj.observe('mouseout',this.autoslide.bind(this));			
		}
//		prev.observe('click',this.prev.bind(this));			
//		next.show();
//		prev.show();	
//			obj.pe = new PeriodicalExecuter(this.autonext,10);

//			this.autoslide();			
			//obj.writeAttribute('running',false);
	},
	show: function(event)
	{
		clearTimeout(this.hoverTimer);	
		var element = Event.element(event);	
		this.hoverTimer = setTimeout(function() {this.show2(element);}.bind(this), this.hoverTime);
	},
	show2: function(element)
	{
		//var element = Event.element(event);
//		alert(element.className);
		this.hideblok = 0;
		this.showblok = 1;
		for(var i=0; i<this.blokken.length; i++)
		{
			if(this.blokken[i] == element || element.descendantOf(this.blokken[i]))
				this.showblok = i;
		}
		for(var i=0; i<this.links.length; i++)
		{
			if($(this.links[i]).visible())
				this.hideblok = i;
		}		
		if(this.hideblok>this.links.length) this.hideblok=0;
		if(this.pe)
		{
			this.pe.stop();		
			//alert('stop');
		}
		this.showeffect();
//		this.effecttimer = setTimeout(this.showeffect.bind(this),this.timer);
//		this.links[hide].hide();			

	},
	showdefault: function(event)
	{
		clearTimeout(this.hoverTimer);
		this.hoverTimer = setTimeout(function() {this.showdefault2();}.bind(this), this.hoverTime);		
	},
	showdefault2: function()
	{
		this.hideblok = 0;
		this.showblok = this.defaultblok;
		for(var i=0; i<this.links.length; i++)
		{
			if($(this.links[i]).visible())
				this.hideblok = i;					

		}
		this.showeffect();		
//		this.effecttimer = setTimeout(this.showeffect.bind(this),this.timer);
//		this.links[hide].hide();
	}	
};
