var RuzafaLoader = Class.create(
{
	menu: false,

	initialize: function()
	{
		this.menu = false;
	},
	
	setupMenu: function(menu)
	{
		this.menu = menu;
		
		this.runLoader(this.menu.getUnloaded());
		this.salaLoader();
	},
	
	findLink: function(link)
	{
		url = link.href.replace("http://","");
		url = "/"+(url.split("/").slice(1).join("/"));
		url = url.replace("//","/");
		url	= $(document.body).down(".header .loader_config input[name='"+url+"']");
		
		return (url) ? url.value : false;
	},
	
	runLoader: function(parts)
	{
		var po = this;
		
		parts.each(function(link){
			view = po.findLink(link);
			
			if(view){
				var options = {
					onSuccess: function(t){
						po.loadContent(t,link);
					}
				};	
			
				var request = new Ajax.Request("/loader/"+view,options);
			}
		});
	},
	
	loadContent: function(t,link)
	{
		var text = t.responseText;
		
		if(text && text.length){
			var title = link.up("h3.title"); 
			//	Remove the spinning loader icon
			var spinner = title.down(".spinner");
			if(spinner) spinner.remove();
			//	Get the node to update
			var node = title.next(".layer");
			//	insert the content
			node.update(text);
			//	Then once the content is loaded, call the menu to bind the handlers
			this.menu.setupMouseHandler([link]);
			//	Mark the link as "loaded"			
			link.removeClassName("unloaded").addClassName("loaded");
			
			//	Attach a scroll bar to the content, then store it so you can retrieve it afterwards
			node.retrieve("scrollbar").attach(node.down(".content"));
			
			//	Search and perform some custom actions depending on the block loaded
			if(link.href.indexOf("quienes-somos") >= 0)	this.quienesSomos();
			if(link.href.indexOf("obras") >= 0)			this.obras();
			if(link.href.indexOf("contacto") >= 0)		this.contactForm();
		}
	},
	
	salaLoader: function()
	{
		if($(document.body).down(".page_sala")) return;
		
		var po = this;
		
		var options = {
			onSuccess: function(t){
				var text = t.responseText;
				
				if(text && text.length){
					$(document.body)
						.down(".sala_unloaded")
						.update(text)
						.removeClassName("sala_unloaded");
					
					var slideshow = RuzafaSlideshow.create(".sala .slideshow");
				}
			}
		};
		var request = new Ajax.Request("/loader/Sala",options);
	},
	
	quienesSomos: function()
	{
		var slideshow = RuzafaSlideshow.create(".quienes_somos .slideshow");
		
		//this.menu.setSlideshow("quienes_somos",slideshow);
	},
	
	obras: function()
	{
		var slideshow = RuzafaSlideshow.create(".menu .obras .slideshow");
		
		//this.menu.setSlideshow("obras",slideshow);
	},
	
	contactForm: function()
	{
		setupContactForm();
	}
});
