// JavaScript Document

var scroller = new Fx.Scroll(window,{
	duration: 1000, 
	transition: Fx.Transitions.Cubic.easeInOut/*,
	onComplete: function () {
		alert('acabà');	
	}*/
});



var navigation = {
							
	inicialize: function(options){
		
		this.options = Object.extend({
			resizeDuration: 400,
			resizeTransition: Fx.Transitions.sineInOut,
			container: document.body,
			descriptions: true,
			opacity: 0.7,
			url: ''
		}, options || {});
		
		
		$('go_content').addEvent('click', function(event) {
			event = new Event(event).stop();
			if ( !$('quick_navigation') ) 
				this.scrollToContent();
			else
				scroller.toElement('quick_navigation');
				
		}.bind(this));
		
		$('to-top').addEvent('click', function(event) {
			event = new Event(event).stop();
			
			scroller.toElement('top_options');
				
		});
		
		if ( $('gallery') ) {			
			$('gallery').effect('opacity', { duration: 1 }).start(0,1);
		}
		
		this.anchors = [];
		
		//	Recull tots els objectes amb l'etiqueta determinada
		$A($$('.link_projecte')).each(function(el) {
			
			//	### Codi del lightbox
			el.onclick = this.click.pass(el, this);			
			
		}, this);
			  
		this.fx = {
			//hide: 		$('gallery').effect('opacity', { duration:500, wait: false }).hide(),
			//appear: 	$('gallery').effect('opacity', { duration: 500 })
		};
		
		/*alert(this.fx);
		alert(this.fx.appear);*/
		
		this.ajaxRequest = Class.empty;

	},
	
	scrollToContent: function() {
		
		scroller.toElement('cont_section');
		
	},
	
	scrollToContentDelaying: function() {
		
		scroller.toElement('cont_section').delay(1500);
		
	},
	
	
	//	element: enllaç clicat per activar el programa
	//			és important que passi l'element de cara al futur
	//			quan més d'un link sigui clicable, perquè
	//			serà en la funció click on això serà processat
	click: function (element) {
		
		//	
		var anchor = 'link1';
		
		//	Guarda a la instància la url
		this.url = element.href;
		
		//if($(element)) scroller.toElement($(element)).addEvent('onComplete', this.loadContents(el));
		
		
		var scrollToTop = new Fx.Scroll(window,{
			duration: 1000, 
			transition: Fx.Transitions.Cubic.easeInOut,
			onComplete: function () {
				this.loadContents();
			}.bind(this)
		});
		
		if($(anchor)) scrollToTop.toElement($(anchor));
		
		$('section_cont').className = 'loading';
		
		return this.scroll();
		//return false;
		
	},
	
	
	scroll: function () {
		
		$('section_cont').className = 'loading';
		
	 	$('gallery').effect('opacity', { duration: 1 }).hide();
				
			//var apareix = new Fx.Style('gallery', 'opacity',{duration:500});
			//apareix.start(1,0);
		
		return false;
	},
	
	
	loadContents: function () {
		
		// check to see if there are specified dimensions
		// if not, fall back to default values
		//var aDim = this.rel.match(/[0-9]+/g);
		//this.options.contentsWidth = (aDim && (aDim[0] > 0)) ? aDim[0] : this.options.defContentsWidth;
		//this.options.contentsHeight = (aDim && (aDim[1] > 0)) ? aDim[1] : this.options.defContentsHeight;
						
		//this.bottom.setStyles({opacity: '0', height: '0px', display: 'none'});
		//this.center.className = 'mb_loading';
		//	Forma bona, però que no funciona
		//this.fx.hide;
		
		// Insert the ajax folder to call an ajax controller
		url = this.url.replace(".ws/", ".ws/ajax/");
		
		this.url = url;

		//*
		//* The simple way for an Ajax request, use onRequest/onComplete/onFailure
		//* to do add your own Ajax depended code.
		var showContents = this.showContents.bind(this);
		 
		var ajaxOptions = {
			method: 'get',
			update: $('section_cont'),
			onComplete: showContents
			};
			
		this.ajaxRequest = new Ajax(this.url, ajaxOptions).request();
			
		return false;
	},
	
	
	showContents: function () {
		
		// Mode sense precarrega d'imatges
		/*$('section_cont').className = '';
				
		var apareix = new Fx.Style('gallery', 'opacity',{duration:500});
		apareix.start(0,1);
		
		projectCard.inicialize();*/
		
		
		this.images = [];
		$A($ES('img','gallery')).each(function(el) {
			this.images.push(el.getProperty('src'));	
		}, this);
		
		new Asset.images(this.images, {
			onComplete: function(){
		
				$('section_cont').className = '';
				
				var apareix = new Fx.Style('gallery', 'opacity',{duration:500});
				apareix.start(0,1);
				
				projectCard.inicialize();
				
			}.bind(this)
		});
		
	}
		
};
