// JavaScript Document

var scroller = new Fx.Scroll(window,{
	duration: 1000, 
	transition: Fx.Transitions.Cubic.easeInOut
});



var showProject = {
							
	inicialize: function(options){
		
		
		this.options = Object.extend({
			resizeDuration: 400,
			resizeTransition: Fx.Transitions.sineInOut,
			container: document.body,
			descriptions: true,
			opacity: 0.7,
			url: ''
		}, options || {});
		
		
		
		$('link1').addEvent('click', function(event) {
			event = new Event(event).stop();
			scroller.toElement('menu');
		});
		
		
		/*$('link2').addEvent('click', function(event) {
			event = new Event(event).stop();
			var morph = new Fx.Styles('section_cont', {duration:800, wait:false, transition: Fx.Transitions.Cubic.easeInOut });
			morph.start({
				'height': 470
			});
			//this.fx.enlarge.start({
				//'height': 470
			//});
		}.bind(this));*/
		
		
		if ( $('gallery') ) {			
			$('gallery').effect('opacity', { duration: 1 }).start(0,1);
			$('gallery').setStyles({
			   visibility: 'hidden',
			   opacity: 300
			});
		}
		
		this.anchors = [];
		
		this.setupLinks();
		
		this.setupNavBetweenSect();
			  
		this.fx = {
			//hide: 		$('gallery').effect('opacity', { duration:500, wait: false }).hide(),
			enlarge:	$('section_cont').effect({duration:800, wait:false, transition: Fx.Transitions.Cubic.easeInOut})
			//appear: 	$('gallery').effect('opacity', { duration: 500 })
		};
		
		this.ajaxRequest = Class.empty;

	},
	
	setupLinks: function() {
		
		//	Recull tots els objectes amb l'etiqueta determinada
		$A($$('.link_projecte')).each(function(el) {
			
			el.onclick = this.click.pass(el, this);			
			
		}, this);
		
		//	Recull tots els objectes amb l'etiqueta determinada
		$A($$('.link_casestudie')).each(function(el) {
			
			el.onclick = this.click.pass(el, this);			
			
		}, this);
		
		// Set function on main-section links
		$A($$('.section')).each(function(el) {
			
			el.onclick = this.click.pass(el, this);			
			
		}, this);
		
	},
	
	setupNavBetweenSect: function () {
		
		// Case studie to link		
		if ( $('to-project') ) {
		
			$('to-project').onclick = this.click.pass($('to-project'), this);
			
		}
		
	},
	
	
	//	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) {
		
		this.rel = element.rel;
		
		if ( $('flash_cont') )
			if ( this.rel != 'index' ) {$('flash_cont').style.display = 'none';}
		
		//$('content_cont').removeProperty('style');
		//	
		this.top = 'nav_sup';
		//	Guarda a la instància la url
		this.url = element.href;
		
		this.changeLanguageHref();
			
		var new_height = 480;
		
		//if($(element)) scroller.toElement($(element)).addEvent('onComplete', this.loadContents(el));
		//if ( $('section_cont').getStyle('height').toInt() != 470 ) {
		if ( element.rel == 'index' ) { new_height = 135; }
		
		var morph = new Fx.Styles('section_cont', {duration:800, wait:false, transition: Fx.Transitions.Cubic.easeInOut });
		morph.start({
			'height': new_height
		});
			//this.fx.enlarge.start({
				//'height': 470
			//});
		//}
		
		return this.scroll();
		
	},
	
	
	scroll: function () {
		
		var scrollToTop = new Fx.Scroll(window,{
			duration: 1000, 
			transition: Fx.Transitions.Cubic.easeInOut,
			onComplete: function () {
				this.loadContents();
			}.bind(this)
		});
		
		if($(this.top)) scrollToTop.toElement($(this.top));
		
		return this.hideContents();
		
	},
	
	hideContents: function () {
		
		// Empty the div, to prevent an ugly effect in casa would be a flash inside
		if( $('image_cont') ) $('image_cont').empty();
		
		$('section_cont').className = 'loading';
		
	 	$('gallery').effect('opacity', { duration: 1 }).hide();
		
		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
		// Also filters the language value
		url = this.url.replace(".ws/ca/", ".ws/ajax/");		
		url = url.replace(".ws/es/", ".ws/ajax/");		
		url = url.replace(".ws/en/", ".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);
		
		if ( this.images.length < 1 ) {
			$('section_cont').className = '';
		
			var apareix = new Fx.Style('gallery', 'opacity',{duration:500});
			apareix.start(0,1);
		}
		
		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();
				manifesto.inicialize();
				home.inicialize();
				this.setupLinks();
				Lightbox.init({descriptions: '.lightboxDesc', showControls: true});		
				this.setupNavBetweenSect();

/*
				if (this.rel == 'index') {
					var header = new FlashObject("http://www.grapa.ws/swf/fonsbo.swf", "header", "953", "535", "9", "", true);
					header.addParam("wmode", "transparent");
					header.addParam("quality", "high");
					header.write("flash_background");	
				}
*/
					
			}.bind(this)
		});
		
		
	},
	
	
	changeLanguageHref: function() {
		var languages = ['ca','es','en'];
		languages.each(function(el) {
								
			url = this.url.replace(".ws/ca/", ".ws/"+el+"/");
			url = url.replace(".ws/es/", ".ws/"+el+"/");
			url = url.replace(".ws/en/", ".ws/"+el+"/");
			$(el).href = url;
			
		}, this);
	}
		
};

