
var portfolio = {
							
	inicialize: function(options){
		
		this.options = Object.extend({
			resizeDuration: 400,
			resizeTransition: Fx.Transitions.sineInOut,
			container: document.body,
			descriptions: true,
			opacity: 0.7,
			url: ''
		}, options || {});
		
		this.first_showing = 1;
		this.last_showing = 3;
		
		this.next = 1 + 3;
		
		this.number_objects = $A($$('.project_link')).length;
		
		
		$A($$('.project_link')).each(function(el) {
			
			el.onclick = this.click.pass(el, this);			
			
		}, this);
		
		if ( $('projects_cont') ) {		
		
			this.setupArrows();
		}
		
		var Tips1 = new Tips($$('.project'));
	
	},
	
	click: function(element) {
		
		this.url = element.href;
		
		scroller.toElement('cont_section');
		
		this.changeProfile();
		
		return false;
	},
	
	changeProfile: function() {
		
		// Amaga el profile actual
		var reposition = new Fx.Styles('portfolio_profile', {
			duration: 1000,
			wait:false,
			transition: Fx.Transitions.Cubic.easeInOut
		});
		reposition.start({
			'height': 0
		}).addEvent('onComplete',function() {
			this.loadContents();				
		}.bind(this));
		
		// carrega els continguts per loadContents()
		
	},
	
	
	scrollRight: function() {
		
		var slideshow_scroll = new Fx.Scroll("projects_wrapper",{					 
			wait: false,
			duration: 600, 
			transition: Fx.Transitions.Cubic.easeInOut
		});
		
		slideshow_scroll.toElement($("project-" + this.next));
		
		this.first_showing = this.first_showing + 3;
		this.last_showing = this.last_showing + 3;
		
		this.setupArrows();
				
		return false;
		
	},
	
	
	scrollLeft: function() {
		
		var slideshow_scroll = new Fx.Scroll("projects_wrapper",{					 
			wait: false,
			duration: 600, 
			transition: Fx.Transitions.Cubic.easeInOut
		});
		
		slideshow_scroll.toElement($("project-" + this.back));
		
		this.first_showing = this.first_showing - 3;
		this.last_showing = this.last_showing - 3;
		
		this.setupArrows();
				
		return false;
		
	},
	
	
	setupArrows: function() {
		
		$('arrow_left').removeEvents('click');
		$('arrow_right').removeEvents('click');
		
		
		if ( (this.first_showing + 3) > this.number_objects )
			this.next = this.first_showing;
		else
			this.next = this.first_showing + 3;
		
		if ( (this.first_showing - 3) < 1 )
			this.back = 1;
		else
			this.back = this.first_showing - 3;
		
		
		// Estic al principi
		if ( this.first_showing == 1 ) {
			
			//alert('al principi');
			
			$('arrow_right').addEvent('click', function() {
				this.scrollRight();
			}.bind(this));
			
			$('arrow_left').addClass('disabled_arrow_left');
			
			if ( $('arrow_right').hasClass('disabled_arrow_right') )
				$('arrow_right').removeClass('disabled_arrow_right');
		}
		
		
		// No estic ni al començament ni al final
		if ( this.first_showing > 1 && this.last_showing < this.number_objects ) {
			
			//alert('al mig');
			
			$('arrow_left').addEvent('click', function() {
				this.scrollLeft();
			}.bind(this));
			
			$('arrow_right').addEvent('click', function() {
				this.scrollRight();
			}.bind(this));
			
			if ( $('arrow_left').hasClass('disabled_arrow_left') )
				$('arrow_left').removeClass('disabled_arrow_left');
			
			if ( $('arrow_right').hasClass('disabled_arrow_right') )
				$('arrow_right').removeClass('disabled_arrow_right');
				
		}
		
		// Estic al final
		if ( this.last_showing >= this.number_objects ) {
			
			//alert('al final');
			
			$('arrow_left').addEvent('click', function() {
				this.scrollLeft();
			}.bind(this));
			
			if ( $('arrow_left').hasClass('disabled_arrow_left') )
				$('arrow_left').removeClass('disabled_arrow_left');
						
			$('arrow_right').addClass('disabled_arrow_right');
		}
		
		return false;
		
	},
	
	
	loadContents: function () {
		
		// Insert the ajax folder to call an ajax controller
		url = this.url.replace("/works/", "/works/work/");
		
		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).delay(500);
		 
		var ajaxOptions = {
			method: 'get',
			update: $('profile_content'),
			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();*/
		
		//alert('soc jo');
		this.images = [];
		$A($ES('img','profile_content')).each(function(el) {
			this.images.push(el.getProperty('src'));	
		}, this);
		
		new Asset.images(this.images, {
			onComplete: function(){
				//alert('hola');
				//$('section_cont').className = '';
				
				var reposition = new Fx.Styles('portfolio_profile', {
					duration: 1000,
					wait:false,
					transition: Fx.Transitions.Cubic.easeInOut
				});
								
				var new_height = $('profile_content').getStyle('height').toInt() + 48;
		
				reposition.start({
					'height': new_height
				}).addEvent('onComplete',function() {
					
				}.bind(this));
					
				Lightbox.init({descriptions: '.lightboxDesc'});
				
/*				var apareix = new Fx.Style('gallery', 'opacity',{duration:500});
				apareix.start(0,1);
				
				projectCard.inicialize();
*/				
			}.bind(this)
		});
		
	}
	
	
	
};

