	window.addEvent("domready", function() {
		
		initPage();

	});

	function inicializaEstadoItems()
	{
		$$(".wrapper, #background, #franja, #idiomas").setStyle("display", "none")
		$$(".wrapper, #background, #franja").setStyle("visibility", "visible");
		
		if($("descargas") != null) {
			//$$(".producto #descargas, .noticias #descargas").fade("hide");
			$("descargas").setStyle("visibility", "visible");
			$("descargas").set("slide", {
				mode:		'horizontal',
				transition: Fx.Transitions.Pow.easeOut
			});
						
			$("descargas").slide("hide");
		}
	}
	
	function creaScrolls()
	{
		var scroll = new Fx.Scroll('scroller', {
			wait: false,
			duration: 2000,
			offset: {'x': 0, 'y': 0},
			transition: Fx.Transitions.Quad.easeInOut
		});
		
		
		if($("contentNoticia"))
		{
			var scrollBis = new Fx.Scroll('contentNoticia', {
				wait: false,
				duration: 2000,
				offset: {'x': 0, 'y': 0},
				transition: Fx.Transitions.Quad.easeInOut
			});
			
			$('upBis').addEvent('click', function(event) {
				event.preventDefault();
				scrollBis.start(0, $('scroller').getScroll().y - 320);
			});
			
			$('downBis').addEvent('click', function(event) {
				event.preventDefault();
				scrollBis.start(0, $('scroller').getScroll().y + 320);
			});
		}
		
		$('up').addEvent('click', function(event) {
			event.preventDefault();
			scroll.start(0, $('scroller').getScroll().y - 320);
		});
		
		$('down').addEvent('click', function(event) {
			event.preventDefault();
			scroll.start(0, $('scroller').getScroll().y + 320);
		});
	}
	
	function ajaxificaLinks()
	{
			$$("#nav a[href!='#'], #listaProductos a, #footerLinks a").each(function(a) {
			
			var options = {
				
				tagToload:	{
					'.wrapperContent':'#wrapper',
					'.fondo':'#fondo'
				},
				
				onSuccess: function(request, response) {
					result 		= jQuery(jQuery(response.replace("<body", "<div"))[0]);
					
					if(Browser.Engine.gecko)
					{
						title 		= result.children("title")[0].text;
						document.title = title;
					}
					className	= result.children("div")[0].className;
					
					document.body.className = className;
				},
				onComplete: function() {
					initPage();
				},
				onStart: function() {
					apagaItems();
				}
			};
			
			jQuery(a).ajaxify(options);
		
			
		});
	}
	
	function creaSlideShow()
	{
		
		if($("listaImagenes"))
			jQuery('#listaImagenes').innerfade({
				speed: 500,
				timeout: 5000,
				type: 'sequence'
		});
		
	}
	
	function enciendeItems()
	{
		var fader1 = function() { jQuery("#background").fadeIn() }
		var fader2 = function() { jQuery("#franja").fadeIn() }
		var fader3 = function() { jQuery(".wrapper, #idiomas, #slider").fadeIn() }
		
		var fader = function() { 
			fader1();
			fader2.delay(500, this);
			fader3.delay(1000, this);
		}
		
		var imagesList = [];
		
		$$("img").each(function(img) {
			imagesList.push(img.get("src"));
		});
		
		//console.debug(imagesList);
		
		var myImages = new Asset.images(imagesList, {
			onComplete: function() {
				fader.delay(1000, this)
			}
		});
	}
	
	function apagaItems()
	{
		var imageURL = $$("#background img")[0].src;
		var apagador = function() { jQuery(".wrapper, #background, #franja, #idiomas").fadeOut(); };
		var myImage = new Asset.images(imageURL, {
			onComplete: function() {
				$("placeholder").set("html", "");
				var newImage = Element("img", {src: imageURL});
				newImage.inject($("placeholder"));
				apagador.delay(500, this);
			}
		});
		
	}
	
	function initNoticias()
	{
		/*
		$$("ul.noticias a").addEvent("click", function(event) {
			
			var setContent = function() {
				$("contentNoticia").set("html", this.getParent().getElements(".noticia").get("html"));
			}
			
			setContent.delay(500,this);
			
			var exampleFx = new Fx.Morph('noticia');
			exampleFx.start({
				'width':[0,200],
				'height':[0,450]
			});
			
			exampleFx.delay(1000,this);
			
		});*/
		
		$$("ul.noticias a").addEvent("click", function(event) {
			event.preventDefault();
			
			var sliderOpen = function() {
				$("noticia").slide("in");
			}
			
			//var exampleFx = new Fx.Morph('noticia');
			
			var setContent = function() {
				$("contentNoticia").set("html", this.getParent().getElements(".noticia").get("html"));
			}
			
			$("noticia").slide("out");
			setContent.delay(500,this);
			sliderOpen.delay(1000,this);
			
			/*exampleFx.start({
				'width':[0,200],
				'height':[0,450]
			});*/
			
			$$("ul.noticias a").setStyle("color", "#333333");
			this.setStyle("color", "#8ABCC1");
			
			$("fotoNoticia").fade("out");
			
			var cambiador = function() {
			
				$("fotoNoticia").set("html", "");
				if(this.getParent().getElements("img").length)
				{
					this.getParent().getElements("img")[0].clone().inject($("fotoNoticia"));
				}
			}
			
			cambiador.delay(1000, this);
			
			var mostrador = function() { $("fotoNoticia").fade("in"); };
			
			mostrador.delay(2000, this);
			
		});
		
		if($("noticia") != null) {
			$("noticia").setStyle("visibility", "visible");
			$("noticia").set("slide", {
				mode:		'horizontal'
			});
			
			$("noticia").slide("hide");
		}
	}
	
	function initProductos()
	{
		$$("#listaProductos a").addEvent("mouseover", function(event){
			$$("#listaProductos a").setStyle("color", "#333");
			this.setStyle("color", this.rel);
		});
		
		$$("#cerrar").addEvent("click", function(event){
			
			$("descargas").slide("out");
			
		});
		
		$$("#pdfs").addEvent('click', function(event) {
			event.preventDefault();
			$("descargas").slide();
		});
	}
	
	function initPage()
	{
		inicializaEstadoItems();
		creaScrolls();
		ajaxificaLinks();
		initProductos();
		initNoticias();
		creaSlideShow();
		enciendeItems();
		/*
		enciendeItems();
		enciendeItems();
		enciendeItems();
		enciendeItems();
		enciendeItems();
		enciendeItems();
		enciendeItems();
		enciendeItems();
		enciendeItems();
		*/
	}
