// JavaScript Document

<!--

var classPeople = new Class({

	Implements: [Chain, Options, Events],

	options: {
		objectParent: undefined,
		objectMove: undefined,
		objectClickEvent: undefined,
		objectSlider: undefined,
		objectDetail: undefined
	},

	initialize: function(options){
		this.setOptions(options);
		
		this.status = 'close';
		this.statusDetail = 'close';
		this.idParent = this.options.objectParent.get('id');
		
		this.arPeoples = new Array();
		this.options.objectSlider.getElements('a').each(function(item, index){
			this.arPeoples[index] = item.get('id');
		}.bind(this));
		
		this.backbutton = $('btn_back');
		
		this.sectionContent = this.options.objectMove.getElement('.people_content');
		this.sectionContent.hide();
		
		this.elLoading = this.options.objectDetail.getElement('.loading');
		
		this.objectLnkSlide = new Fx.Slide(this.options.objectSlider, {mode: 'horizontal'}).hide();
		this.options.objectSlider.getElements('a').each(function(item, index){
			item.addEvent('click', function() {
				this.openContentDetail(item.get('id'));
			}.bind(this));
		}.bind(this));
		
		new Fx.Tween(this.options.objectDetail, {property: 'opacity'}).start(0);

		/*$('people_Vlist').setStyle('height', this.options.objectDetail.getStyle('height'));
		$('people_Scrollarea').setStyle('height', this.options.objectDetail.getStyle('height'));
		$('people_ScrollBarContainer').setStyle('height', this.options.objectDetail.getStyle('height').toInt() - 40);
		
		$('people_Scrollarea').fade('out');*/

		this.options.objectClickEvent.addEvent('click', function() {this.start()}.bind(this));
	},

	start: function() {
		if(this.status == 'open')  {
		}
		else {
			this.openlink();
		}
	},

	openlink: function(){
		this.status = 'open';
		
		$('lnk_home').removeEvents('click');
		$('lnk_home').addEvent('click', function() {this.closelink()}.bind(this));
		$('lnk_home').show();
		
		this.backbutton.removeEvents('click');
		this.backbutton.addEvent('click', function() {this.closelink()}.bind(this));
		
		$('box_button_pages').getElements('li[id^=page_]').each(function(item, index){
			if(item.get('id') != this.idParent) item.fade('out');
		}, this);
		
		var x = new Chain();
		var moveOblo = function(xChain) {
			var mover = new Fx.Move(this.options.objectMove, {
				relativeTo: $('wall_row_2'),
				position: 'upperLeft',
				offset: {x: 25, y: 40},
				duration: '1000', 
				transition: 'cubic:out',
				onComplete: function() {
					xChain.callChain();
				}
			}).start();
		}.bind(this, x);
		
		var showElements = function() {
			this.options.objectMove.setStyles({
				width: 745,
				height: 345
			});
			
			this.options.objectSlider.getParent().setStyle('opacity', 1)
			this.options.objectSlider.getParent().show();
			this.sectionContent.show();
			this.objectLnkSlide.slideIn();
			this.backbutton.show();
		}.bind(this);

		x.chain(moveOblo);
		x.chain(showElements);
		x.callChain();
	},

	closelink: function() {
		this.status = 'close';
		this.reset();
		
		var x = new Chain();
		var closeSlider = function(xChain){ 
			if($(this.options.objectSlider)) 
				new Fx.Slide(this.options.objectSlider, {
					mode: 'horizontal',
					onComplete: function() {
						xChain.callChain();
					}
				}).slideOut();
			else xChain.callChain();

		}.bind(this, x);

		var moveOblo = function(xChain){
			this.options.objectMove.setStyles({
				width: this.options.objectParent.getStyle('width'),
				height: this.options.objectParent.getStyle('height')
			});
			
			this.sectionContent.hide();
			this.backbutton.hide();
			
			var mover = new Fx.Move(this.options.objectMove, {
				relativeTo: this.options.objectParent,
				duration: '1000', 
				position: 'upperLeft',
				offset: {x: 0, y: 0},
				transition: 'cubic:out',
				onComplete: function() {
					xChain.callChain();
				}
			}).start();
		}.bind(this, x);
		
		var returnToInitial = function(){
			$('box_button_pages').getElements('li[id^=page_]').each(function(item, index){
				if(item.get('id') != this.idParent) item.fade('in');
			}, this);
			
			$('lnk_home').hide();
		}.bind(this);
		
		x.chain(closeSlider);
		x.chain(moveOblo);
		x.chain(returnToInitial);
		x.callChain();
	},
	
	openContentDetail: function(cnt) {
		var x = new Chain();
		this.backbutton.removeEvents('click');
		this.backbutton.addEvent('click', function() {this.closeContentDetail()}.bind(this));
		
		var hideSlider = function(xChain){ 
			/*new Fx.Tween(this.options.objectSlider.getParent(), {
				property: 'opacity', 
				duration: 500,
				onComplete: function() {
					xChain.callChain();
				}
			}).start(0);*/
			new Fx.Move(this.options.objectSlider, {
				relativeTo: this.options.objectSlider.getParent(),
				position: 'center',
				duration: '1000', 
				offset: {x: 0, y: 200},
				transition: 'cubic:out',
				onComplete: function() {
					xChain.callChain();
				}
			}).start();
		}.bind(this, x);
	
		var showDetail = function(xChain){
			//this.options.objectSlider.getParent().hide();
			//this.options.objectDetail.addClass(cnt +'_b');
			this.options.objectDetail.setStyle('background-image', 'url(images/people/'+ cnt +'_b.jpg)'); // no-repeat left 10px');
			this.options.objectDetail.show();
			this.statusDetail = 'open';
			
			new Fx.Tween(this.options.objectDetail, {
				property: 'opacity', 
				duration: 500,
				onComplete: function() {
					xChain.callChain();
				}
			}).start(1);
			
		}.bind(this, x);
	
		var doRequest = function(){
			var myRequest = new Request.HTML({
				url: 'getContent.php', 
				method: 'post',
				onRequest: function() {
					this.elLoading.show();
				}.bind(this),
				onComplete: function() {
					this.elLoading.hide();
				}.bind(this),
				onFailure: function() {
					//setError('Caricamento dei dati fallito.');
				},
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					if(responseHTML) {
						$('people_descr_detail').set('html', responseHTML );
						/*$('people_Vlist').getElement('.description').set('html', responseHTML );
						var hDetail = $('people_Vlist').getElement('.description').getStyle('height').toInt();
						
						if(hDetail > 300) {
							new ScrollBar($('people_Vlist'), $('people_ScrollBarContainer'), $('people_ScrollKnob'), {
								mode: 'vertical',
								steps: 1000 - (1000-(hDetail / 2)),
								offset: -2
							});
							$('people_Scrollarea').fade('in');
						}*/
					}
				}.bind(this)
			}).send('cnt='+cnt);
			
		}.bind(this);
	
		x.chain(hideSlider);
		x.chain(showDetail);
		x.chain(doRequest);
		x.callChain();
	},
	
	closeContentDetail: function() {
		var x = new Chain();
		this.backbutton.removeEvents('click');
		this.backbutton.addEvent('click', function() {this.closelink()}.bind(this));

		var hideDetail = function(xChain){
			new Fx.Tween(this.options.objectDetail, {
				property: 'opacity', 
				duration: 500,
				onComplete: function() {
					xChain.callChain();
				}
			}).start(0);
		}.bind(this, x);
	
		var openSlider = function(xChain){ 
			this.statusDetail = 'close';
			this.options.objectDetail.hide();
			$('people_descr_detail').set('html', '' );
			
			/*this.options.objectSlider.getParent().show();
			new Fx.Tween(this.options.objectSlider.getParent(), {
				property: 'opacity', 
				duration: 500
			}).start(1);*/
			
			new Fx.Move(this.options.objectSlider, {
				relativeTo: this.options.objectSlider.getParent(),
				position: 'center',
				duration: '1000', 
				offset: {x: 0, y: 30},
				transition: 'cubic:out'
			}).start();
		}.bind(this, x);
	
		x.chain(hideDetail);
		x.chain(openSlider);
		x.callChain();
	},

	reset: function() {
		this.options.objectDetail.hide();
		$('people_descr_detail').set('html', '' );
		/*$('people_Vlist').getElement('.description').set('html', '');
		$('people_Scrollarea').fade('out');*/
		this.statusDetail = 'close';
	}
});
//-->
