// JavaScript Document
<!--
var classHeritage = new Class({
	Implements: [Chain, Options, Events],
	options: {
		objectParent: undefined,
		objectMove: undefined,
		objectClickEvent: undefined,
		objectSlider: undefined,
		objectShowSpot: undefined
	},
	initialize: function(options){
		this.setOptions(options);
		this.status = 'close';
		this.idParent = this.options.objectParent.get('id');
		this.objScrollbar;
		
		this.sectionContent = this.options.objectMove.getElement('.heritage_content');
		this.sectionContent.hide();
		
		this.backbutton = $('btn_back');
		
		this.objFxSlider;
		this.objFxSlider = new Fx.Slide(this.options.objectSlider, {mode: 'horizontal',duration: 1000}).hide();
		
		this.boxShowSpot;
		this.options.objectShowSpot.hide();
		
		this.heightDescription = 0;
		$('heritageScrollarea').fade('out');

		$('heritageVlist').setStyle('height', this.sectionContent.getStyle('height').toInt() - 20);
		$('heritageScrollarea').setStyle('height', this.sectionContent.getStyle('height').toInt() - 20);
		$('heritageScrollBarContainer').setStyle('height', this.sectionContent.getStyle('height').toInt() - 60);

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

	start: function() {
		if(this.status == 'close')  {
			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) {
			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 doRequest = function(xChain){
			this.options.objectMove.setStyles({
				width: 745,
				height: 345
			});
			
			var myRequest = new Request.HTML({
				url: 'getContent.php', 
				method: 'post',
				onRequest: function() {
					//this.elLoading.show();
				}.bind(this),
				onComplete: function() {
					//this.elLoading.hide();
					xChain.callChain();
				}.bind(this),
				onFailure: function() {
					//setError('Caricamento dei dati fallito.');
				},
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					if(responseHTML) {
						$('heritageVlist').getElement('.description').set('html', responseHTML );
			
						if(this.heightDescription == 0) {
							var elDescription = $('heritageVlist').getElement('.description');
							var numItems = elDescription.getElements('a').length;
							var heightSingleItem = (elDescription.getElements('a'))[0].getStyle('height').toInt();
							var heightList = numItems * heightSingleItem;
							this.heightDescription = 280 + heightList; //$('heritageVlist').getElement('.description').getStyle('height').toInt();
							$('heritageVlist').getElement('.description').setStyle('height', this.heightDescription +'px');
						}
						
						if(this.heightDescription > this.sectionContent.getStyle('height').toInt() - 20 && !this.objScrollbar) {
							this.objScrollbar = new ScrollBar( $('heritageVlist'), $('heritageScrollBarContainer'), $('heritageScrollKnob'), {
								mode: 'vertical',
								steps: 1000 - (1000-(this.heightDescription / 2)),
								offset: -2
							});
							$('heritageScrollarea').fade('in');
						} 
		
						if(this.objScrollbar) {
							this.objScrollbar.goToTop();
						}
						
					}
				}.bind(this)
			}).send('cnt=heritage');
			
		}.bind(this, x);
		
		var showElements = function() {
			this.sectionContent.show();
			this.objFxSlider.slideIn();
			(
				function(){ 
					this.backbutton.show();
					this.options.objectShowSpot.show();
				}.bind(this)
			).delay(500);
			
		}.bind(this);

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

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

		var hideElements = function(xChain){
			this.options.objectMove.setStyles({
				width: this.options.objectParent.getStyle('width'),
				height: this.options.objectParent.getStyle('height')
			});
			
			this.options.objectShowSpot.hide();
			this.sectionContent.hide();
			this.backbutton.hide();
			
			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(hideElements);
		x.chain(returnToInitial);
		x.callChain();
	}
});

function loadHeritageVideo(cntName) {
	if(!$('heritageFlvPlayer')) {
		$('heritage_spot').empty();
		var elPlayer = new Element('div', {'id': 'heritageFlvPlayer'});
		$('heritage_spot').grab(elPlayer);
		if(!$('heritage_spot').hasClass('heritage_player')) $('heritage_spot').addClass('heritage_player');
	}
	//else 
		//$('heritageFlvPlayer').empty(); //.set('html', '');
	
	/*menuItems: [false, false, false, false, false, false], */
	var flashvars  = {
		config:"{ playList: [ {overlayId: 'play' }, " +
				"{ url: '../content/heritage/"+ cntName +".flv', allowResize: false } ], " + 
				"autoPlay:true,  " +
				"loop: false,  " +
				"showMenu:false,  " +
				"getControlBarBackgroundColor:0xDDDDDD, initialScale: 'scale', " +
				"autoRewind: true}"
	};

	// need this next line for local testing, it's optional if your swf is on the same domain as your html page
	var params = {
		allowScriptAccess: "always"
	};

	swfobject.embedSWF("swf/FPClassicOnlyPlayVolume.swf", "heritageFlvPlayer", "352", "288", "7", "Scripts/expressInstall", flashvars, params);
}
//-->
