/*************************************************************
	Script		: CMSX
	Version		: 3.4
	Authors		: Fabio Di Lupo
	Desc		: Raccolta script AJAX per Sistema Gestione Contenuti
	Licence		: Open Source MIT Licence
**************************************************************
	Script		: MultiBox
	Version		: 1.3.1
	Authors		: Samuel Birch
	Desc		: Supports jpg, gif, png, flash, flv, mov, wmv, mp3, html, iframe
	Licence		: Open Source MIT Licence
	Modified    : Versione adattata per CMSX 3.1 da Fabio Di Lupo
**************************************************************/
var MultiBox = new Class({
	getOptions: function(){
		return {
			initialWidth: 250,
			initialHeight: 250,
			container: document.body,
			useOverlay: false,
			contentColor: '#FFF',
			showNumbers: true,
			showControls: true,
			//showThumbnails: false,
			//autoPlay: false,
			waitDuration: 2000,
			descClassName: false,
			descMinWidth: 400,
			descMaxWidth: 600,
			movieWidth: 400,
			movieHeight: 300,
			offset: {x:0, y:0},
			fixedTop: false,
			path: '/obj/image/media/',
			onOpen: Class.empty,
			onClose: Class.empty,
			openFromLink: true,
			relativeToWindow: true
		};
	},
	initialize: function(className, options){
		this.setOptions(this.getOptions(), options);
		this.openClosePos = {};
		this.timer = 0;
		this.contentToLoad = {};
		this.index = 0;
		this.opened = false;
		this.contentObj = {};
		this.containerDefaults = {};
		if(this.options.useOverlay){
			this.overlay = new Overlay({container: this.options.container, onClick:this.close.bind(this)});
		}
		this.content = $$('.'+className);
		if(this.options.descClassName){
			this.descriptions = $$('.'+this.options.descClassName);
			this.descriptions.each(function(el){
				el.setStyle('display', 'none');
			});
		}
		this.container = new Element('div').addClass('MultiBoxContainer').injectInside(this.options.container);
		this.iframe = new Element('iframe').setProperties({
			'id': 'multiBoxIframe',
			'name': 'mulitBoxIframe',
			'src': 'javascript:void(0);',
			'frameborder': 1,
			'scrolling': 'no'
		}).setStyles({
			'position': 'absolute',
			'top': -20,
			'left': -20,
			'width': '115%',
			'height': '115%',
			'filter': 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)',
			'opacity': 0
		}).injectInside(this.container);
		this.box = new Element('div').addClass('MultiBoxContent').injectInside(this.container);
		this.closeButton = new Element('div').addClass('MultiBoxClose').injectInside(this.container).addEvent('click', this.close.bind(this));
		this.controlsContainer = new Element('div').addClass('MultiBoxControlsContainer').injectInside(this.container);
		this.controls = new Element('div').addClass('MultiBoxControls').injectInside(this.controlsContainer);
		this.previousButton = new Element('div').addClass('MultiBoxPrevious').injectInside(this.controls).addEvent('click', this.previous.bind(this));
		this.nextButton = new Element('div').addClass('MultiBoxNext').injectInside(this.controls).addEvent('click', this.next.bind(this));
		this.title = new Element('div').addClass('MultiBoxTitle').injectInside(this.controls);
		this.number = new Element('div').addClass('MultiBoxNumber').injectInside(this.controls);
		this.description = new Element('div').addClass('MultiBoxDescription').injectInside(this.controls);
		if(this.content.length == 1){
			this.title.setStyles({
				'margin-left': 0
			});
			this.description.setStyles({
				'margin-left': 0
			});
			this.previousButton.setStyle('display', 'none');
			this.nextButton.setStyle('display', 'none');
			this.number.setStyle('display', 'none');
		}
		new Element('div').setStyle('clear', 'both').injectInside(this.controls);
		this.content.each(function(el,i){
			el.index = i;
			el.addEvent('click', function(e){
				new Event(e).stop();
				this.open(el);
			}.bind(this));
			if(el.href.indexOf('#') > -1){
				el.content = $(el.href.substr(el.href.indexOf('#')+1));
				if(el.content){el.content.setStyle('display','none');}
			}
		}, this);
		this.containerEffects = new Fx.Styles(this.container, {duration: 400, transition: Fx.Transitions.sineInOut});
		this.controlEffects = new Fx.Styles(this.controlsContainer, {duration: 300, transition: Fx.Transitions.sineInOut});
		this.reset();
	},
	setContentType: function(link){
		var str = link.href.substr(link.href.lastIndexOf('.')+1).toLowerCase();
		var contentOptions = {};
		if($chk(link.rel)){
			var optArr = link.rel.split(',');
			optArr.each(function(el){
				var ta = el.split(':');
				contentOptions[ta[0]] = ta[1];
			});
		}
		if(contentOptions.type != undefined){
			str = contentOptions.type;
		}
		this.contentObj = {};
		this.contentObj.url = link.href;
		this.contentObj.xH = 0;
		if(contentOptions.width){
			this.contentObj.width = contentOptions.width;
		}else{
			this.contentObj.width = this.options.movieWidth;
		}
		if(contentOptions.height){
			this.contentObj.height = contentOptions.height;	
		}else{
			this.contentObj.height = this.options.movieHeight;
		}
		if(contentOptions.panel){
			this.panelPosition = contentOptions.panel;
		}else{
			this.panelPosition = this.options.panel;
		}
		switch(str){
			case 'jpg':
			case 'gif':
			case 'png':
				this.type = 'image';
				break;
			case 'swf':
				this.type = 'flash';
				break;
			case 'flv':
				this.type = 'flashVideo';
				this.contentObj.xH = 70;
				break;
			case 'mov':
				this.type = 'quicktime';
				break;
			case 'wmv':
				this.type = 'windowsMedia';
				break;
			case 'rv':
			case 'rm':
			case 'rmvb':
				this.type = 'real';
				break;
			case 'mp3':
				this.type = 'flashMp3';
				this.contentObj.width = 320;
				this.contentObj.height = 70;
				break;
			case 'element':
				this.type = 'htmlelement';
				this.elementContent = link.content;
				this.elementContent.setStyles({
					display: 'block',
					opacity: 0
				})
	
				if(this.elementContent.getStyle('width') != 'auto'){
					this.contentObj.width = this.elementContent.getStyle('width');
				}
				
				this.contentObj.height = this.elementContent.getSize().size.y;
				this.elementContent.setStyles({
					display: 'none',
					opacity: 1
				})
				break;
			default:
				this.type = 'iframe';
				if(contentOptions.ajax){
					this.type = 'ajax';
				}
				break;
		}
	},
	reset: function(){
		this.container.setStyles({
			'opacity': 0,
			'display': 'none'
		});
		this.controlsContainer.setStyles({
			'height': 0
		});
		this.removeContent();
		this.previousButton.removeClass('MultiBoxButtonDisabled');
		this.nextButton.removeClass('MultiBoxButtonDisabled');
		this.opened = false;
	},
	getOpenClosePos: function(el){
		if (this.options.openFromLink) {
			if (el.getFirst()) {
				var w = el.getFirst().getCoordinates().width - (this.container.getStyle('border').toInt() * 2);
				if (w < 0) {
					w = 0
				}
				var h = el.getFirst().getCoordinates().height - (this.container.getStyle('border').toInt() * 2);
				if (h < 0) {
					h = 0
				}
				this.openClosePos = {
					width: w,
					height: h,
					top: el.getFirst().getCoordinates().top,
					left: el.getFirst().getCoordinates().left
				};
			}
			else {
				var w = el.getCoordinates().width - (this.container.getStyle('border').toInt() * 2);
				if (w < 0) {
					w = 0
				}
				var h = el.getCoordinates().height - (this.container.getStyle('border').toInt() * 2);
				if (h < 0) {
					h = 0
				}
				this.openClosePos = {
					width: w,
					height: h,
					top: el.getCoordinates().top,
					left: el.getCoordinates().left
				};
			}
		}else{
			if(this.options.fixedTop){
				var top = this.options.fixedTop;
			}else{
				var top = ((window.getHeight()/2)-(this.options.initialHeight/2)-this.container.getStyle('border').toInt())+this.options.offset.y;
			}
			this.openClosePos = {
				width: this.options.initialWidth,
				height: this.options.initialHeight,
				top: top,
				left: ((window.getWidth()/2)-(this.options.initialWidth/2)-this.container.getStyle('border').toInt())+this.options.offset.x
			};
		}
		return this.openClosePos;
	},
	open: function(el){
		this.options.onOpen();
		this.index = this.content.indexOf(el);
		this.openId = el.getProperty('id');
		if(!this.opened){
			this.opened = true;
			if(this.options.useOverlay){
				this.overlay.show();
			}
			
			this.container.setStyles(this.getOpenClosePos(el));
			this.container.setStyles({
				opacity: 0,
				display: 'block'
			});
			if(this.options.fixedTop){
				var top = this.options.fixedTop;
			}else{
				var top = ((window.getHeight()/2)-(this.options.initialHeight/2)-this.container.getStyle('border').toInt())+this.options.offset.y;
			}
			this.containerEffects.start({
				width: this.options.initialWidth,
				height: this.options.initialHeight,
				top: top,
				left: ((window.getWidth()/2)-(this.options.initialWidth/2)-this.container.getStyle('border').toInt())+this.options.offset.x,
				opacity: [0, 1]
			});
			this.load(this.index);
		}else{
			if (this.options.showControls) {
				this.hideControls();
			}
			this.getOpenClosePos(this.content[this.index]);
			this.timer = this.hideContent.bind(this).delay(500);
			this.timer = this.load.pass(this.index, this).delay(1100);
		}
	},
	getContent: function(index){
		this.setContentType(this.content[index]);
		var desc = {};
		if(this.options.descClassName){
		this.descriptions.each(function(el,i){
			if(el.hasClass(this.openId)){
				desc = el.clone();
			}
		},this);
		}
		//var title = this.content[index].title;
		this.contentToLoad = {
			title: this.content[index].title || '&nbsp;',
			//desc: $(this.options.descClassName+this.content[index].id).clone(),
			desc: desc,
			number: index+1
		};
	},
	close: function(){
		if(this.options.useOverlay){
			this.overlay.hide();
		}
		if (this.options.showControls) {
			this.hideControls();
		}
		this.hideContent();
		this.containerEffects.stop();
		this.zoomOut.bind(this).delay(500);
		this.options.onClose();
	},
	zoomOut: function(){
		this.containerEffects.start({
			width: this.openClosePos.width,
			height: this.openClosePos.height,
			top: this.openClosePos.top,
			left: this.openClosePos.left,
			opacity: 0
		});
		this.reset.bind(this).delay(500);
	},
	load: function(index){
		this.box.addClass('MultiBoxLoading');
		this.getContent(index);
		if(this.type == 'image'){
			var xH = this.contentObj.xH;
			this.contentObj = new Asset.image(this.content[index].href, {onload: this.resize.bind(this)});
			this.contentObj.xH = xH;
			/*this.contentObj = new Image();
			this.contentObj.onload = this.resize.bind(this);
			this.contentObj.src = this.content[index].href;*/
		}else{
			this.resize();
		}
	},
	resize: function(){
		if (this.options.fixedTop) {
			var top = this.options.fixedTop;
		}
		else {
			var top = ((window.getHeight() / 2) - ((Number(this.contentObj.height) + this.contentObj.xH) / 2) - this.container.getStyle('border').toInt() + window.getScrollTop()) + this.options.offset.y;
		}
		var left = ((window.getWidth() / 2) - (this.contentObj.width / 2) - this.container.getStyle('border').toInt()) + this.options.offset.x;
		if (top < 0) {
			top = 0
		}
		if (left < 0) {
			left = 0
		}
		this.containerEffects.stop();
		this.containerEffects.start({
			width: this.contentObj.width,
			height: Number(this.contentObj.height) + this.contentObj.xH,
			top: top,
			left: left,
			opacity: 1
		});
		this.timer = this.showContent.bind(this).delay(500);
	},
	showContent: function(){
		this.box.removeClass('MultiBoxLoading');
		this.removeContent();
		this.contentContainer = new Element('div').setProperties({id: 'MultiBoxContentContainer'}).setStyles({opacity: 0, width: this.contentObj.width+'px', height: (Number(this.contentObj.height)+this.contentObj.xH)+'px'}).injectInside(this.box);
		if(this.type == 'image'){
			this.contentObj.injectInside(this.contentContainer);
		}else if(this.type == 'iframe'){
			new Element('iframe').setProperties({
				id: 'iFrame'+new Date().getTime(), 
				width: this.contentObj.width,
				height: this.contentObj.height,
				src: this.contentObj.url,
				frameborder: 0,
				scrolling: 'auto'
			}).injectInside(this.contentContainer);
			
		}else if(this.type == 'htmlelement'){
			this.elementContent.clone().setStyle('display','block').injectInside(this.contentContainer);
		}else if(this.type == 'ajax'){
			new Ajax(this.contentObj.url, {
				method: 'get',
				update: 'MultiBoxContentContainer',
				evalScripts: true,
				autoCancel: true
			}).request();
			
		}else{
			var obj = this.createEmbedObject().injectInside(this.contentContainer);
			if(this.str != ''){
				$('MultiBoxMediaObject').innerHTML = this.str;
			}
		}
		this.contentEffects = new Fx.Styles(this.contentContainer, {duration: 500, transition: Fx.Transitions.linear});
		this.contentEffects.start({
			opacity: 1
		});
		this.title.setHTML(this.contentToLoad.title);
		this.number.setHTML(this.contentToLoad.number+' of '+this.content.length);
		if (this.options.descClassName) {
			if (this.description.getFirst()) {
				this.description.getFirst().remove();
			}
			this.contentToLoad.desc.injectInside(this.description).setStyles({
				display: 'block',color:'#4bf' //PATCH CMSX
			});
		}
		//this.removeContent.bind(this).delay(500);
		if (this.options.showControls) {
			this.timer = this.showControls.bind(this).delay(800);
		}
	},
	
	hideContent: function(){
		this.box.addClass('MultiBoxLoading');
		this.contentEffects.start({
			opacity: 0
		});
		this.removeContent.bind(this).delay(500);
	},
	
	removeContent: function(){
		if($('MultiBoxMediaObject')){
			$('MultiBoxMediaObject').empty();
			$('MultiBoxMediaObject').remove();
		}
		if($('MultiBoxContentContainer')){
			//$('MultiBoxContentContainer').empty();
			$('MultiBoxContentContainer').remove();	
		}
	},
	showControls: function(){
		this.clicked = false;
		
		if(this.container.getStyle('height') != 'auto'){
			this.containerDefaults.height = this.container.getStyle('height')
			this.containerDefaults.backgroundColor = this.options.contentColor;
		}
		this.container.setStyles({
			//'backgroundColor': this.controls.getStyle('backgroundColor'),
			'height': 'auto'
		});
		if(this.contentToLoad.number == 1){
			this.previousButton.addClass('MultiBoxPreviousDisabled');
		}else{
			this.previousButton.removeClass('MultiBoxPreviousDisabled');
		}
		if(this.contentToLoad.number == this.content.length){
			this.nextButton.addClass('MultiBoxNextDisabled');
		}else{
			this.nextButton.removeClass('MultiBoxNextDisabled');
		}
		this.controlEffects.start({'height': this.controls.getStyle('height')});

	},
	hideControls: function(num){
		this.controlEffects.start({'height': 0}).chain(function(){
			this.container.setStyles(this.containerDefaults);
		}.bind(this));
	},
	showThumbnails: function(){
	},
	next: function(){
		if(this.index < this.content.length-1){
			this.index++;
			this.openId = this.content[this.index].getProperty('id');
			if (this.options.showControls) {
				this.hideControls();
			}
			this.getOpenClosePos(this.content[this.index]);
			//this.getContent(this.index);
			this.timer = this.hideContent.bind(this).delay(500);
			this.timer = this.load.pass(this.index, this).delay(1100);
		}
	},
	previous: function(){
		if(this.index > 0){
			this.index--;
			this.openId = this.content[this.index].getProperty('id');
			if (this.options.showControls) {
				this.hideControls();
			}
			this.getOpenClosePos(this.content[this.index]);
			//this.getContent(this.index);
			this.timer = this.hideContent.bind(this).delay(500);
			this.timer = this.load.pass(this.index, this).delay(1000);
		}
	},
	createEmbedObject: function(){
		if(this.type == 'flash'){
			var url = this.contentObj.url;
			var obj = new Element('div').setProperties({id: 'MultiBoxMediaObject'});
			this.str = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" '
			this.str += 'width="'+this.contentObj.width+'" ';
			this.str += 'height="'+this.contentObj.height+'" ';
			this.str += 'title="MultiBoxMedia">';
  			this.str += '<param name="movie" value="'+url+'" />'
  			this.str += '<param name="quality" value="high" />';
  			this.str += '<param name="bgcolor" value="#000000" />' //PATCH CMSX anche bgcolor su embed
  			this.str += '<embed src="'+url+'" ';
  			this.str += 'quality="high" bgcolor="#000000" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" ';
  			this.str += 'width="'+this.contentObj.width+'" ';
  			this.str += 'height="'+this.contentObj.height+'"></embed>';
			this.str += '</object>';
		}
		if(this.type == 'flashVideo'){
			//var url = this.contentObj.url.substring(0, this.contentObj.url.lastIndexOf('.'));
			var url = this.contentObj.url;
			var obj = new Element('div').setProperties({id: 'MultiBoxMediaObject'});
			this.str = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" '
			this.str += 'width="'+this.contentObj.width+'" ';
			this.str += 'height="'+(Number(this.contentObj.height)+this.contentObj.xH)+'" ';
			this.str += 'title="MultiBoxMedia">';
  			this.str += '<param name="movie" value="'+this.options.path+'flvplayer.swf" />'
  			this.str += '<param name="quality" value="high" />';
  			this.str += '<param name="salign" value="TL" />';
  			this.str += '<param name="scale" value="noScale" />';
  			this.str += '<param name="FlashVars" value="path='+url+'" />';
  			this.str += '<embed src="'+this.options.path+'flvplayer.swf" ';
  			this.str += 'quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" ';
  			this.str += 'width="'+this.contentObj.width+'" ';
  			this.str += 'height="'+(Number(this.contentObj.height)+this.contentObj.xH)+'"';
  			this.str += 'salign="TL" ';
  			this.str += 'scale="noScale" ';
  			this.str += 'FlashVars="path='+url+'"';
  			this.str += '></embed>';
			this.str += '</object>';
		}
		if(this.type == 'flashMp3'){
			var url = this.contentObj.url;
			var obj = new Element('div').setProperties({id: 'MultiBoxMediaObject'});
			this.str = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" '
			this.str += 'width="'+this.contentObj.width+'" ';
			this.str += 'height="'+this.contentObj.height+'" ';
			this.str += 'title="MultiBoxMedia">';
  			this.str += '<param name="movie" value="'+this.options.path+'mp3player.swf" />'
  			this.str += '<param name="quality" value="high" />';
  			this.str += '<param name="salign" value="TL" />';
  			this.str += '<param name="scale" value="noScale" />';
  			this.str += '<param name="FlashVars" value="path='+url+'" />';
  			this.str += '<embed src="'+this.options.path+'mp3player.swf" ';
  			this.str += 'quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" ';
  			this.str += 'width="'+this.contentObj.width+'" ';
  			this.str += 'height="'+this.contentObj.height+'"';
  			this.str += 'salign="TL" ';
  			this.str += 'scale="noScale" ';
  			this.str += 'FlashVars="path='+url+'"';
  			this.str += '></embed>';
			this.str += '</object>';
		}
		if(this.type == 'quicktime'){
			var obj = new Element('div').setProperties({id: 'MultiBoxMediaObject'});
			this.str = '<object  type="video/quicktime" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"';
			this.str += ' width="'+this.contentObj.width+'" height="'+this.contentObj.height+'">';
			this.str += '<param name="src" value="'+this.contentObj.url+'" />';
			this.str += '<param name="autoplay" value="true" />';
			this.str += '<param name="controller" value="true" />';
			this.str += '<param name="enablejavascript" value="true" />';
			this.str += '<embed src="'+this.contentObj.url+'" autoplay="true" pluginspage="http://www.apple.com/quicktime/download/" width="'+this.contentObj.width+'" height="'+this.contentObj.height+'"></embed>';
			this.str += '<object/>';
		}
		if(this.type == 'windowsMedia'){
			var obj = new Element('div').setProperties({id: 'MultiBoxMediaObject'});
			this.str = '<object  type="application/x-oleobject" classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"';
			this.str += ' width="'+this.contentObj.width+'" height="'+this.contentObj.height+'">';
			this.str += '<param name="filename" value="'+this.contentObj.url+'" />';
			this.str += '<param name="Showcontrols" value="true" />';
			this.str += '<param name="autoStart" value="true" />';
			this.str += '<embed type="application/x-mplayer2" src="'+this.contentObj.url+'" Showcontrols="true" autoStart="true" width="'+this.contentObj.width+'" height="'+this.contentObj.height+'"></embed>';
			this.str += '<object/>';
		}
		if(this.type == 'real'){
			var obj = new Element('div').setProperties({id: 'MultiBoxMediaObject'});
			this.str = '<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"';
			this.str += ' width="'+this.contentObj.width+'" height="'+this.contentObj.height+'">';
			this.str += '<param name="src" value="'+this.contentObj.url+'" />';
			this.str += '<param name="controls" value="ImageWindow" />';
			this.str += '<param name="autostart" value="true" />';
			this.str += '<embed src="'+this.contentObj.url+'" controls="ImageWindow" autostart="true" width="'+this.contentObj.width+'" height="'+this.contentObj.height+'"></embed>';
			this.str += '<object/>';
		}
		return obj;
	}
});
MultiBox.implement(new Options);
MultiBox.implement(new Events);
/**************************************************************
	Script		: Overlay
	Version		: 1.2
	Authors		: Samuel birch
	Desc		: Covers the window with a semi-transparent layer.
	Licence		: Open Source MIT Licence
**************************************************************/
var Overlay = new Class({
	
	getOptions: function(){
		return {
			colour: '#000',
			opacity: 0.7,
			zIndex: 1,
			container: document.body,
			onClick: Class.empty
		};
	},

	initialize: function(options){
		this.setOptions(this.getOptions(), options);
		
		this.options.container = $(this.options.container);
		
		this.container = new Element('div').setProperty('id', 'OverlayContainer').setStyles({
			position: 'absolute',
			left: '0px',
			top: '0px',
			width: '100%',
			zIndex: this.options.zIndex
		}).injectInside(this.options.container);
		
		this.iframe = new Element('iframe').setProperties({
			'id': 'OverlayIframe',
			'name': 'OverlayIframe',
			'src': 'javascript:void(0);',
			'frameborder': 1,
			'scrolling': 'no'
		}).setStyles({
			'position': 'absolute',
			'top': 0,
			'left': 0,
			'width': '100%',
			'height': '100%',
			'filter': 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)',
			'opacity': 0,
			'zIndex': 1
		}).injectInside(this.container);
		
		this.overlay = new Element('div').setProperty('id', 'Overlay').setStyles({
			position: 'absolute',
			left: '0px',
			top: '0px',
			width: '100%',
			height: '100%',
			zIndex: 2,
			backgroundColor: this.options.colour
		}).injectInside(this.container);
		
		this.container.addEvent('click', function(){
			this.options.onClick();
		}.bind(this));
		
		this.fade = new Fx.Style(this.container, 'opacity').set(0);
		this.position();
		
		window.addEvent('resize', this.position.bind(this));
	},
	
	position: function(){ 
		if(this.options.container == document.body){ 
			var h = window.getScrollHeight()+'px'; 
			this.container.setStyles({top: '0px', height: h}); 
		}else{ 
			var myCoords = this.options.container.getCoordinates(); 
			this.container.setStyles({
				top: myCoords.top+'px', 
				height: myCoords.height+'px', 
				left: myCoords.left+'px', 
				width: myCoords.width+'px'
			}); 
		} 
	},
	
	show: function(){
		this.fade.start(0,this.options.opacity);
	},
	
	hide: function(){
		this.fade.start(this.options.opacity,0);
	}
	
});
Overlay.implement(new Options);

/*
+-------------------------------------------------------------------+
|                 H T M L - C A L E N D A R   (v2.9)                |
|                                                                   |
| Copyright Gerd Tentler               www.gerd-tentler.de/tools    |
| Created: May 27, 2003                Last modified: Jan. 10, 2009 |
+-------------------------------------------------------------------+
| This program may be used and hosted free of charge by anyone for  |
| personal purpose as long as this copyright notice remains intact. |
|                                                                   |
| Obtain permission before selling the code for this program or     |
| hosting this software on a commercial website or redistributing   |
| this software over the Internet or in any other medium. In all    |
| cases copyright must remain intact.                               |
+-------------------------------------------------------------------+

EXAMPLE #1:  myCal = new CALENDAR();
document.write(myCal.create());

EXAMPLE #2:  myCal = new CALENDAR(2004, 12);
document.write(myCal.create());

EXAMPLE #3:  myCal = new CALENDAR();
myCal.year = 2004;
myCal.month = 12;
document.write(myCal.create());

Returns HTML code
==========================================================================================================
*/
var cal_ID = 0;

function CALENDAR(year, month) {
    //========================================================================================================
    // Configuration
    //========================================================================================================
    this.tFontFace = 'Tahoma, Verdana'; // title: font family (CSS-spec, e.g. "Arial, Helvetica")
    this.tFontSize = 12;                 // title: font size (pixels)
    this.tFontColor = '#FFFFFF';         // title: font color
    this.tBGColor = '#8E9CDB';           // title: background color

    this.hFontFace = 'Tahoma, Verdana'; // heading: font family (CSS-spec, e.g. "Arial, Helvetica")
    this.hFontSize = 11;                 // heading: font size (pixels)
    this.hFontColor = '#FFFFFF';         // heading: font color
    this.hBGColor = '#8E9CDB';           // heading: background color

    this.dFontFace = 'Tahoma, Verdana'; // days: font family (CSS-spec, e.g. "Arial, Helvetica")
    this.dFontSize = 11;                 // days: font size (pixels)
    this.dFontColor = '#000000';         // days: font color
    this.dBGColor = '#FFFFFF';           // days: background color

    this.wFontFace = 'Tahoma, Verdana'; // weeks: font family (CSS-spec, e.g. "Arial, Helvetica")
    this.wFontSize = 11;                 // weeks: font size (pixels)
    this.wFontColor = '#FFFFFF';         // weeks: font color
    this.wBGColor = '#dd0000';           // weeks: background color

    this.saFontColor = '#dd0000';        // Saturdays: font color
    this.saBGColor = '#dddddd';          // Saturdays: background color

    this.suFontColor = '#000000';        // Sundays: font color
    this.suBGColor = '#ffffcc';          // Sundays: background color

    this.tdBorderColor = '#FF0000';      // today: border color

    this.borderColor = '#8E9CDB';        // border color
    this.hilightColor = '#FFFF00';       // hilight color (works only in combination with link)

    this.link = '';                      // page to link to when day is clicked
    this.offset = 2;                     // week start: 0 - 6 (0 = Saturday, 1 = Sunday, 2 = Monday ...)
    this.weekNumbers = false;             // view week numbers: true = yes, false = no

    //--------------------------------------------------------------------------------------------------------
    // You should change these variables only if you want to translate them into your language:
    //--------------------------------------------------------------------------------------------------------
    // weekdays: must start with Saturday because January 1st of year 1 was a Saturday
    this.weekdays = ['Sa', 'Do', 'Lu', 'Ma', 'Me', 'Gi', 'Ve'];

    // months: must start with January
    this.months = ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio',
                 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'];
    // error messages
    this.error = ['Year must be 1 - 3999!', 'Month must be 1 - 12!'];

    //--------------------------------------------------------------------------------------------------------
    // Don't change from here:
    //--------------------------------------------------------------------------------------------------------
    this.size = 0;
    this.mDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    if (year == null && month == null) {
        var obj = new Date();
        year = obj.getYear();
        if (year < 1900) year += 1900;
        month = obj.getMonth() + 1;
    }
    else if (year != null && month == null) month = 1;
    this.year = year;
    this.month = month;
    this.specDays = {};

    //========================================================================================================
    // Functions
    //========================================================================================================
    this.set_styles = function() {
        cal_ID++;
        var html = '<style> .cssTitle' + cal_ID + ' { ';
        if (this.tFontFace) html += 'font-family: ' + this.tFontFace + '; ';
        if (this.tFontSize) html += 'font-size: ' + this.tFontSize + 'px; ';
        if (this.tFontColor) html += 'color: ' + this.tFontColor + '; ';
        if (this.tBGColor) html += 'background-color: ' + this.tBGColor + '; ';
        html += '} .cssHeading' + cal_ID + ' { ';
        if (this.hFontFace) html += 'font-family: ' + this.hFontFace + '; ';
        if (this.hFontSize) html += 'font-size: ' + this.hFontSize + 'px; ';
        if (this.hFontColor) html += 'color: ' + this.hFontColor + '; ';
        if (this.hBGColor) html += 'background-color: ' + this.hBGColor + '; ';
        html += '} .cssDays' + cal_ID + ' { ';
        if (this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
        if (this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
        if (this.dFontColor) html += 'color: ' + this.dFontColor + '; ';
        if (this.dBGColor) html += 'background-color: ' + this.dBGColor + '; ';
        html += '} .cssWeeks' + cal_ID + ' { ';
        if (this.wFontFace) html += 'font-family: ' + this.wFontFace + '; ';
        if (this.wFontSize) html += 'font-size: ' + this.wFontSize + 'px; ';
        if (this.wFontColor) html += 'color: ' + this.wFontColor + '; ';
        if (this.wBGColor) html += 'background-color: ' + this.wBGColor + '; ';
        html += '} .cssSaturdays' + cal_ID + ' { ';
        if (this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
        if (this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
        if (this.saFontColor) html += 'color: ' + this.saFontColor + '; ';
        if (this.saBGColor) html += 'background-color: ' + this.saBGColor + '; ';
        html += '} .cssSundays' + cal_ID + ' { ';
        if (this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
        if (this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
        if (this.suFontColor) html += 'color: ' + this.suFontColor + '; ';
        if (this.suBGColor) html += 'background-color: ' + this.suBGColor + '; ';
        html += '} .cssHilight' + cal_ID + ' { ';
        if (this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
        if (this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
        if (this.dFontColor) html += 'color: ' + this.dFontColor + '; ';
        if (this.hilightColor) html += 'background-color: ' + this.hilightColor + '; ';
        html += 'cursor: default; ';
        html += '} </style>';

        return html;
    }

    this.leap_year = function(year) {
        return (!(year % 4) && (year < 1582 || year % 100 || !(year % 400))) ? true : false;
    }

    this.get_weekday = function(year, days) {
        var a = days;
        if (year) a += (year - 1) * 365;
        for (var i = 1; i < year; i++) if (this.leap_year(i)) a++;
        if (year > 1582 || (year == 1582 && days >= 277)) a -= 10;
        if (a) a = (a - this.offset) % 7;
        else if (this.offset) a += 7 - this.offset;

        return a;
    }

    this.get_week = function(year, days) {
        var firstWDay = this.get_weekday(year, 0);
        return Math.floor((days + firstWDay) / 7) + (firstWDay <= 3);
    }

    this.table_cell = function(content, cls, date, style) {
        var size = Math.round(this.size * 1.5);
        var clsName = cls.toLowerCase();
        var html = '<td align=center width=' + size + ' class="' + cls + '"';

        if (content != '&nbsp;' && clsName.indexOf('day') != -1) {
            var link = this.link;

            if (this.specDays[content]) {
                if (this.specDays[content][0]) {
                    style += 'background-color:' + this.specDays[content][0] + ';';
                }
                if (this.specDays[content][1]) {
                    html += ' title="' + this.specDays[content][1] + '"';
                }
                if (this.specDays[content][2]) link = this.specDays[content][2];
            }
            if (link) {
                html += ' onMouseOver="this.className=\'cssHilight' + cal_ID + '\'"';
                html += ' onMouseOut="this.className=\'' + cls + '\'"';
                html += ' onClick="document.location.href=\'' + link + '?date=' + date + '\'"';
            }
        }
        if (style) html += ' style="' + style + '"';
        html += '>' + content + '</td>';

        return html;
    }

    this.table_head = function(content) {
        var html, ind, wDay, i;
        var cols = this.weekNumbers ? 8 : 7;

        html = '<tr><td colspan=' + cols + ' class="cssTitle' + cal_ID + '" align=center><b>' +
           content + '</b></td></tr><tr>';
        for (i = 0; i < this.weekdays.length; i++) {
            ind = (i + this.offset) % 7;
            wDay = this.weekdays[ind];
            html += this.table_cell(wDay, 'cssHeading' + cal_ID);
        }
        if (this.weekNumbers) html += this.table_cell('&nbsp;', 'cssHeading' + cal_ID);
        html += '</tr>';

        return html;
    }

    this.viewEvent = function(from, to, color, title, link) {
        if (from > to) return;
        if (from < 1 || from > 31) return;
        if (to < 1 || to > 31) return;

        while (from <= to) {
            this.specDays[from] = [color, title, link];
            from++;
        }
    }

    this.create = function() {
        var obj, html, curYear, curMonth, curDay, start, stop, title, daycount,
        inThisMonth, weekNr, wdays, days, ind, cls, style, content, date, i;

        this.size = (this.hFontSize > this.dFontSize) ? this.hFontSize : this.dFontSize;
        if (this.wFontSize > this.size) this.size = this.wFontSize;

        obj = new Date();
        curYear = obj.getYear();
        if (curYear < 1900) curYear += 1900;
        curMonth = obj.getMonth() + 1;
        curDay = obj.getDate();

        if (this.year < 1 || this.year > 3999) html = '<b>' + this.error[0] + '</b>';
        else if (this.month < 1 || this.month > 12) html = '<b>' + this.error[1] + '</b>';
        else {
            this.mDays[1] = this.leap_year(this.year) ? 29 : 28;
            for (i = days = 0; i < this.month - 1; i++) days += this.mDays[i];

            start = this.get_weekday(this.year, days);
            stop = this.mDays[this.month - 1];

            html = this.set_styles();
            html += '<table border=0 cellspacing=0 cellpadding=0><tr>';
            html += '<td' + (this.borderColor ? ' bgcolor=' + this.borderColor : '') + '>';
            html += '<table border=0 cellspacing=1 cellpadding=3>';
            title = this.months[this.month - 1] + ' ' + this.year;
            html += this.table_head(title);
            daycount = 1;

            if ((this.year == curYear) && (this.month == curMonth)) inThisMonth = true;
            else inThisMonth = false;

            if (this.weekNumbers) weekNr = this.get_week(this.year, days);

            while (daycount <= stop) {
                html += '<tr>';

                for (i = wdays = 0; i <= 6; i++) {
                    ind = (i + this.offset) % 7;
                    if (ind == 0) cls = 'cssSaturdays';
                    else if (ind == 1) cls = 'cssSundays';
                    else cls = 'cssDays';

                    style = '';
                    date = this.year + '-' + this.month + '-' + daycount;

                    if ((daycount == 1 && i < start) || daycount > stop) content = '&nbsp;';
                    else {
                        content = daycount;
                        if (inThisMonth && daycount == curDay) {
                            style = 'padding:0px;border:3px solid ' + this.tdBorderColor + ';';
                        }
                        else if (this.year == 1582 && this.month == 10 && daycount == 4) daycount = 14;
                        daycount++;
                        wdays++;
                    }
                    html += this.table_cell(content, cls + cal_ID, date, style);
                }

                if (this.weekNumbers) {
                    if (!weekNr) {
                        if (this.year == 1) content = '&nbsp;';
                        else if (this.year == 1583) content = 52;
                        else content = this.get_week(this.year - 1, 365);
                    }
                    else if (this.month == 12 && weekNr >= 52 && wdays < 4) content = 1;
                    else content = weekNr;

                    html += this.table_cell(content, 'cssWeeks' + cal_ID);
                    weekNr++;
                }
                html += '</tr>';
            }
            html += '</table></td></tr></table>';
        }
        return html;
    }
}

/* Inizio funzioni CMSX - max lunghezza */
function maxLunghezza(obj,lunghezza){
var mlength=obj.getAttribute? parseInt(lunghezza) : ""
if (obj.getAttribute && obj.value.length>mlength)
    {obj.value=obj.value.substring(0,mlength);
    alert("Raggiunta la massima lunghezza disponibile del testo in questo campo.")
    }
}
/* apre finestra popup centrata */
var win= null;
function Finestra(mypage,myname,w,h){
var winl = (screen.width-w)/2;
var wint = (screen.height-h)/2;
var settings ='height='+h+',';
settings +='width='+w+',';
settings +='top='+wint+',';
settings +='left='+winl+',';
settings +='scrollbars=no,resizable=no,toolbar=no';
win=window.open(mypage,myname,settings);
if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}
function MM_openBrWindow(theURL,winName,features) {
window.open(theURL,winName,features);
}

/* apre finestra popup centrata con scroll*/
var winmia= null;
function FinestraScroll(mypage,myname,w,h){
var winl = (screen.width-w)/2;
var wint = (screen.height-h)/2;
var settings ='height='+h+',';
settings +='width='+w+',';
settings +='top='+wint+',';
settings +='left='+winl+',';
settings +='scrollbars=yes,resizable=no,toolbar=no';
winmia=window.open(mypage,myname,settings);
if(parseInt(navigator.appVersion) >= 4){winmia.window.focus();}
}
function MM_openBrWindow(theURL,winName,features) {
window.open(theURL,winName,features);
}

/* Attivazioni su evento domready */
window.addEvent('domready', function() {
    /* Preparo animazione SIDE Menu */
    var list = $$('.colonnamenu ul li.muovi');
    list.each(function(element) {
        var fx = new Fx.Styles(element, { duration: 200, wait: false });
        element.addEvent('mouseenter', function() {
            fx.start({ 'margin-left':'5px' });
        });
        element.addEvent('mouseleave', function() {
            fx.start({ 'margin-left':'0px' });
        });
    });
    /* Inietto la classe tool in tutti gli elementi dei menu */
    $$('div.menuorizzontale ul li a > div').addClass('tool');
    $$('div#language-menu a > div').addClass('tool');
    $$('div.colonnamenu ul li a > div').addClass('tool');
    $$('div#validazioni a > div').addClass('tool');
    
    /* Inizializzo Tooltip */
    var Tips1 = new Tips($$('.tool'));
    var Tips2 = new Tips($$('.blank'));
    var Tips3 = new Tips($$('.od'));
    /* Setto Target */
    for (var ik=0; ik<document.links.length; ik++)
    {
        if (document.links[ik].className=="blank") { document.links[ik].target="_blank"; };
        if (document.links[ik].className=="anteprima") { document.links[ik].target="_blank"; };
    }
   // Inietto stili sperimentali CSS 3.0 supportati da Safari, Firefox, Chrome
    $$('fieldset').setStyle('-moz-border-radius', '5px');
    $$('fieldset').setStyle('-webkit-border-radius', '5px');
    $$('button').setStyle('-moz-border-radius', '5px');
    $$('button').setStyle('-webkit-border-radius', '5px');
    $$('label').setStyle('-moz-border-radius-topleft', '5px');
    $$('label').setStyle('-webkit-border-top-left-radius', '5px');
    $$('.privacy').setStyle('-moz-border-radius', '5px');
    $$('.privacy').setStyle('-webkit-border-radius', '5px');
    $$('.notizie').setStyle('-moz-border-radius', '5px');
    $$('.notizie').setStyle('-webkit-border-radius', '5px');
    /* Setto SmootScroll */
    new SmoothScroll({duration: 2500});
    /* Attivo multibox per mappa satellitare */
    var box = {};
    box = new MultiBox('mbmap', {descClassName: 'multiBoxDesc',useOverlay:true});
    var AlbumVideo=document.getElementById("AlbumVideo")
    if (AlbumVideo!=null) {
        AlbumVideoObj = new MultiBox('mb', {descClassName: 'multiBoxDesc',useOverlay:true});
    };
    var AlbumFoto=document.getElementById("listafoto")
    if (AlbumFoto!=null) {
        AlbumFotoObj = new MultiBox('mb', {descClassName: 'multiBoxDesc',useOverlay:true});
    };
    /* Setto Timer sessione */
    var ChiudiSessione=document.getElementById("ChiudiSessione")
    if (ChiudiSessione!=null) {
        var box = new Element('div', {'id':'FXTimer'}).injectInside('pagina');
        var box = new Element('p').setHTML('<b>Messaggio di Warning</b><br />La tua sessione scade tra meno di 5 minuti!').injectInside('FXTimer');
    };
});
    
/* Attivazioni su evento onload */
window.addEvent('load', function() {
    var pubblicita=document.getElementById("advertising")
    if (pubblicita!=null) {
        var pubblicitaobj = new Fx.Styles('advertising', {duration: 2000, transition: Fx.Transitions.Quart.easeInOut});
        pubblicitaobj.start({'opacity':[0, .99]});
    }; 
    /* Setto Timeout a 15 minuti = 900000 millesimi*/ 
    var ChiudiSessione=document.getElementById("ChiudiSessione")
    if (ChiudiSessione!=null) {         
        var TimerSessione = setTimeout(function(){
            document.getElementById('FXTimer').style.display = 'block';
            var muovi1 = new Fx.Styles('FXTimer', {duration: 3000, transition: Fx.Transitions.Quart.easeInOut });
            muovi1.start({'opacity':[0, .9]});
        }, 900000)
    };
});
