// JavaScript Document

document.observe("dom:loaded", function()
{
	VDBlock.init();
	
	if(location.hash && (Block = VDBlock.Register.get(location.hash.substr(1))) ) 
	{	
		Block.open();
	}
	else
	{
		VDBlock.Register.get("villa-delphia").open();
	}
});

document.observe("HashEvent:change", function()
{
	Block = VDBlock.Register.get(location.hash.substr(1));
	if(Block) Block.open();
});



VDBlock = Class.create({

	id : null,
	text : null,
	title : null,
	block : null,
	
	isOpen : false,
	
	initialize : function(element)
	{
		this.block = element;
		this.text = this.block.down(".text");
		this.title = this.block.down("h1");
		this.id = this.block.getAttribute("id").substr(6);
		
		// registreren
		VDBlock.Register.set(this.id, this);
		
		// element dichtklappen
		this.text.hide();
		
		// observen
		this.block.observe("click", this.open.bind(this));
	},
	
	open : function()
	{
		if(this.isOpen)
		{
			return;
		}
		if(VDBlock.isBusy) 
		{
			// merk op dat de queue gewoon overschreven wordt!
			VDBlock.queue = this;
			return;
		}
		
		VDBlock.isBusy = true;
		VDBlock.Register.values().invoke("close");
		this.isOpen = true;
		
		HashEvent.setSilent(this.id);
		document.title = "Herenhuis Villa Delphia - " + this.title.innerHTML;
		
		if(pageTracker && pageTracker._trackPageview) 
		{
			pageTracker._trackPageview("/" + this.id);
		}
		
		this.block.setStyle({cursor : "inherit"});
		
		this.text.blindDown({afterFinish : this.afterOpen.bind(this)});
	},
	
	afterOpen : function()
	{
		VDBlock.isBusy = false;
		if(VDBlock.queue)
		{
			VDBlock.queue.open();
			VDBlock.queue = null;
		}
	},
		
	
	close : function()
	{
		if(!this.isOpen) return;
		
		this.block.setStyle({cursor : "pointer"});
		
		this.text.blindUp({queue : {scope:"vd"}});
		this.isOpen = false;
	}
});

VDBlock.Register = $H();
VDBlock.isBusy = false;
VDBlock.queue = null;
VDBlock.init = function()
{
	$$(".block").each(function(block){new VDBlock(block);});
}