/* ------- Main Banner --------- */
function apriMainBanner() {
	document.getElementById("mainBanner").style.display = "block";
	for (f = 0; f < document.forms.length; f++) {
		var elements = document.forms[f].elements;
		for (e = 0; e < elements.length; e++) {
			if (elements[e].type == "select-one") {
				if(elements[e].id!="where") {
					elements[e].style.visibility = 'hidden';
				}
			}
		}
	}
}


function chiudiMainBanner() {
	document.getElementById("mainBanner").style.display = "none";
	for (f = 0; f < document.forms.length; f++) {
		var elements = document.forms[f].elements;
		for (e = 0; e < elements.length; e++) {
			if (elements[e].type == "select-one") {
				elements[e].style.visibility = 'visible';
			}
		}
	}
}

function apriPopupCartoline() {
	var winX;
	var winY;
	winX = (window.screen.width/2) - (420 + 10);
	winY = (window.screen.height/2) - (320 + 20);
	window.open('http://www.o-one.net/conad/ecards','','width=840,height=640,left='+winX+',top='+winY+',screenX='+winX+',screenY='+winY+',toolbar=0,resizable=0,location=0,status=0,directories=0,menubar=0,scrollbars=0');
}

function chiudiFocusCartoline() {
	document.getElementById("mainBanner").style.display = "none";
	for (f = 0; f < document.forms.length; f++) {
		var elements = document.forms[f].elements;
		for (e = 0; e < elements.length; e++) {
			if (elements[e].type == "select-one") {
				elements[e].style.visibility = 'visible';
			}
		}
	}
}

window.addEvent('domready', function() {
	init();
	window.fireEvent('siteready');
});

window.addEvent('unload', function() {} );

function init()
{
	// initialize map engine
	s = new siteEngine();
	s.init();
	s.initBlock( document );
	
	document.addEvent('updateblock', function(obj) {
		s.initBlock( obj );
	} );
}

function siteEngine()
{
	this.init = function()
	{
		this.body = document.getElement('body');
		this.initCharSizes();
	},
	
	this.initBlock = function( obj )
	{
		var relabels = obj.getElements('.relabel');
		for (var i=0; i<relabels.length; i++)
		{
			relabels[i].importProps();
			this.addRelabel.delay( 500, this, relabels[i] );
		}
		
		var openmenu = obj.getElements('.openmenu');
		for (var i=0; i<openmenu.length; i++)
		{
			openmenu[i].importProps();
			this.addOpenMenu( openmenu[i] );
		}
		
		var loadmenu = obj.getElements('.loadmenu');
		for (var i=0; i<loadmenu.length; i++)
		{
			loadmenu[i].importProps();
			this.addLoadMenu( loadmenu[i] );
		}
		
		var slides = obj.getElements('.slide');
		for (var i=0; i<slides.length; i++)
		{
			slides[i].importProps();
			this.addSlide( slides[i] );
		}
		
		var accordions = obj.getElements('.accordion');
		for (var i=0; i<accordions.length; i++)
		{
			accordions[i].importProps();
			this.addAccordion( accordions[i] );
		}
		
		var loads = obj.getElements('.load');
		for (var i=0; i<loads.length; i++)
		{
			loads[i].importProps();
			this.addLoad( loads[i] );
		}
		
		var forms = obj.getElements('.form');
		for (var i=0; i<forms.length; i++)
		{
			forms[i].importProps();
			this.addForm( forms[i] );
		}
		
		var popups = obj.getElements('.popup');
		for (var i=0; i<popups.length; i++)
		{
			popups[i].importProps();
			this.putPopup( popups[i] );
		}

		var closepopups = obj.getElements('.close-popup');
		for (var i=0; i<closepopups.length; i++)
		{
			closepopups[i].importProps();
			closepopups[i].addEvent('click', this.closePopup.bind(this) );
		}
				
		var changes = obj.getElements('.change');
		for (var i=0; i<changes.length; i++)
		{
			changes[i].importProps();
			this.addChange( changes[i] );
		}

		var editables = obj.getElements('.editable');
		for (var i=0; i<editables.length; i++)
		{
			editables[i].importProps();
			this.addEditable( editables[i] );
		}
				
		var selects = obj.getElements('.select-full');
		for (var i=0; i<selects.length; i++)
		{
			selects[i].importProps();
			this.addFullSelect( selects[i] );
		}
	},
	
	this.initCharSizes = function( )
	{
		var small_s 	= $('small_size');
		var medium_s 	= $('medium_size');
		var big_s 		= $('big_size');
		if (small_s)
		small_s.addEvent('click', function() {
			$('content').set('class','');
			$('content').addClass('content-small');
		} );
		if (medium_s)
		medium_s.addEvent('click', function() {
			$('content').set('class','');
			$('content').addClass('content-medium');
		} );
		if (big_s)
		big_s.addEvent('click', function() {
			$('content').set('class','');
			$('content').addClass('content-big');
		} );
	},
	
	this.addOpenMenu = function( obj )
	{
		obj.a = obj.getElement('a'); // first link
		obj.submenu = obj.getElement('ul'); // the sub-menu
		obj.fx = new Fx.Slide(obj.submenu, {
			'link': 'cancel',
			'duration': 1000,
			'transition': Fx.Transitions.Quad.easeInOut
		});
		obj.status = true;
		if (!obj.a.hasClass('subselected'))
		{
			obj.status = false;
			obj.fx.hide();
		}
		if (!obj.a.hasClass('goto'))
		obj.a.addEvent('click', function(e, obj) {
			new Event(e).stop();
			this.openMenu( obj );
		}.bindWithEvent(this,obj) );
	},
	
	this.openMenu = function( obj )
	{
		obj.submenu.setStyle('display', 'block');
		obj.fx.slideIn();
		obj.status = !obj.status;
		// close others
		var toclose = obj.getParent().getElements('.openmenu');
		for (var i=0; i<toclose.length; i++)
		{
			if (toclose[i] != obj)
			{
				toclose[i].fx.slideOut();
				toclose[i].status = false;
				toclose[i].a.removeClass('subselected');
			}
		}
		if (obj.status)	obj.a.addClass('subselected');
		else			obj.a.removeClass('subselected');
	},
	
	this.addLoadMenu = function( obj )
	{
		obj.a = obj.getElement('a'); // first link
		obj.submenu = obj.getElement('ul'); // the sub-menu
		obj.a.addEvent('click', function(e, obj) {
			if (!obj.submenu)
			{
				new Event(e).stop();
				obj.xhr = new Request.HTML().get( obj.getURL() );
				obj.xhr.addEvent('onComplete', function(obj) {
					obj.addClass('openmenu');
					new Element('ul').setStyle('display','none').inject(obj).set('html', obj.xhr.response.text);
					this.addOpenMenu( obj );
					this.openMenu( obj );
				}.pass(obj, this) );
			}
		}.bindWithEvent(this,obj) );
	},
	
	this.addRelabel = function( obj )
	{
		obj.set('value', obj.label);
		obj.set('autocomplete', 'false');
		
                if (obj.get('type') == 'password')
                {
                    obj.defaultType = 'password';
                    obj.shadow = new Element('input', { 'type': 'text' } ).set('value', obj.label).inject( obj, 'after' );
                    obj.shadow.addEvent('focus', function() {
                        obj.setStyle('display', 'block');
                        obj.shadow.setStyle('display', 'none');
                        obj.focus();
                    }.pass(obj, this) );
                    obj.setStyle('display', 'none');
                    //obj.set('type', 'text');
                }

		obj.addEvent('focus', function() {
			if (obj.get('value') == obj.label)
                        {
                            obj.set('value', '');
                        }
		}.pass(obj, this) );
		
		obj.addEvent('blur', function() {
			if (obj.get('value') == '')
                        {
                            if (obj.shadow)
                            {
                                obj.setStyle('display', 'none');
                                obj.shadow.setStyle('display', 'block');
                            }
                            else
                                obj.set('value', obj.label);
                        }
		}.pass(obj, this) );
	},
	
	this.addSlide = function( obj )
	{
		obj.setStyle('position', 'relative');
		obj.block = obj.getElement('.slide-block');
                obj.close = obj.getElement('.slide-toggler');
		obj.fx = new Fx.Slide( obj.block,{
                    link: 'chain'} );
		if (obj.block.hasClass('slide-block-close'))
                    obj.fx.hide();
                if (!obj.close)
                    obj.close = new Element('a', {'class':'slide-close' + (obj.block.hasClass('slide-block-close') ? '' : ' slide-open') }).set('html', '<span>Chiudi</span>').inject(obj, 'top');
		obj.close.addEvent('click', function(obj) {
			obj.fx.toggle();
			obj.close.toggleClass('slide-open');
		}.pass(obj, this) );
                obj.close.addEvent('click', function(e) {
                        new Event(e).stop();
                } );
	},
	
	this.addAccordion = function( obj )
	{
		var togglers = obj.getElements('.acc-toggler');
		var blocks = obj.getElements('.acc-block');
		obj.fx = new Accordion(togglers, blocks, {
			opacity: true
		} );
		togglers.each( function(el, i) {
			if (el.hasClass('acc-open'))
			{
				togglers.removeClass('acc-selected');
				el.addClass('acc-selected');
				obj.fx.display( i );
			}
		} );
		obj.fx.addEvents( {
			'onActive': function(toggler,block) {
				toggler.addClass('acc-selected');
			},
			'onBackground': function(toggler,block) {
				toggler.removeClass('acc-selected');
			}
		} )
	},
	
	this.addLoad = function( obj )
	{
		var action = 'click';
		obj.addEvent(action, function(obj) {
			obj.xhr = new Request.HTML().get( obj.getURL() );
			obj.xhr.addEvent('onComplete', function(obj) {
				this.putAjax( obj );
			}.pass(obj, this) );
		}.pass(obj, this) );
		obj.addEvent(action, function(event) {
			new Event(event).stop();
		} );
	},
	
	this.putAjax = function( obj )
	{
		var target = $(obj.targetEl);
		if (!target.defaultHTML)
			target.defaultHTML = target.get('html');
		target.set('html', obj.xhr.response.text);
		target.close = target.getElement('.close');
		if (target.close)
		{
			target.close.addEvent('click', function(target) {
				target.set('html', target.defaultHTML);
			}.pass(target, this) );
		}
		document.fireEvent('updateblock', target);
	},
	
	this.addForm = function( obj )
	{
		var action = 'submit';
		obj.addEvent(action, function(obj) {
			obj.xhr = new Request.HTML({url: obj.getURL()}).post( obj );
			obj.xhr.addEvent('onComplete', function(obj) {
				this.putAjax( obj );
			}.pass(obj, this) );
		}.pass(obj, this) );
		obj.addEvent(action, function(event) {
			new Event(event).stop();
		} );
	},
	
	this.addChange = function( obj )
	{
		obj.addEvent('change', function(obj) {
			var form = obj.getParent('form');
			form.fireEvent('submit', obj);
		}.pass(obj, this) );
	},
	
	this.addEditable = function( obj )
	{
            if(obj != null){
		obj.toggler = obj.getElement('.edit');
		obj.label = obj.getElement('em');
		obj.text = obj.label.get('html');
		obj.width = obj.label.getSize().x;
              if(obj.toggler != null){
                  obj.toggler.addEvent('click', function() {
                          obj.label.empty();
                          obj.input = new Element('input', {'name': obj.inputName}).setStyles({'width': obj.width}).set('value', obj.text ).inject( obj.label );
                  }.pass(obj, this) );
              }
            }
	},
	
	this.putPopup = function( obj )
	{
		obj.addEvent('click', function(obj) {
			obj.xhr = new Request.HTML().get( obj.getURL() );
			obj.xhr.addEvent('onComplete', function(obj) {
				this.wait(1);
				this.parsePopup( obj );
			}.pass(obj, this) );
		}.pass(obj, this) );
		obj.addEvent('click', function(e) { new Event(e).stop(); } );
	},

	this.parsePopup = function( obj )
	{
            var text = obj.xhr.response.text;
            if(text.indexOf('[submit]') > -1) 
            {
                $(obj.form).submit();
            } 
            else 
            {
		this.popup = new Element('div', {'class': 'popup-window'}).setStyles({'position':'absolute', 'overflow': 'hidden'}).inject(document.body);
		this.popup.content = new Element('div', {'id': obj.popupid}).set('html', text).inject(this.popup);
		if ( (Browser.Engine.name == 'trident') && (Browser.Engine.version < 5) )
			this.popup.iframe = new Element('iframe')
			.setStyles({'display': 'block', 'position':'absolute', 'top':0, 'left':0, 'z-index':-1, 'width':3000, 'height': 3000, 'filter': 'mask()'})
			.inject(this.popup);
		this.popup.fx = new Fx.Tween( this.popup, 'opacity').set(0);
		this.popup.pos = this.popup.getCoordinates();
		var win = document.getSize();
		var scroll = document.getScroll();
		if (win.x == 0)
		{
			win = document.getScrollSize();
			win.x -= 20;
		}
		if (obj.x == 'center')
			this.popup.pos.left = ( win.x - this.popup.pos.width ) / 2 + scroll.x;
		else
			this.popup.pos.left = parseInt(obj.x);
		if (obj.y == 'center')
			this.popup.pos.top = ( win.y - this.popup.pos.height ) / 2 + scroll.y;
		else
			this.popup.pos.top = parseInt(obj.y);
		if (this.popup.pos.left < 0)	this.popup.pos.left = 0;
		if (this.popup.pos.top < 0)	this.popup.pos.top = 0;
		this.popup.setStyle('left', this.popup.pos.left);
		this.popup.setStyle('top', this.popup.pos.top);
		this.popup.close = new Element('a', {'href':'javascript:;', 'class': 'popup-close'}).set('html', 'X').inject(this.popup, 'top');
		this.popup.close.addEvent('click', this.closePopup.bind(this) );
		this.popup.fx.start(0,1);
		document.fireEvent('updateblock', this.popup);
            }
	},
	
	this.closePopup = function()
	{
		this.popup.fx.start(1,0).chain( function() { this.popup.remove(); }.bind(this) );
		this.wait(0);
	},

	this.wait = function( action )
	{
		if (!this.waitDiv)
		{
			this.waitDiv = new Element('div').inject(document.body);
			this.waitDiv.setStyles({'position': 'absolute'});
			this.waitDiv.fx = new Fx.Tween(this.waitDiv, 'opacity').set(0);
		}
		
		if (action == 0)
		{
			this.waitDiv.fx.start(.7,0);
			this.waitLayerOpen = 0;
		}
		else
		{
			var size = document.getScrollSize();
			if (size.x == 0)
				size = document.getSize();
			this.waitDiv.setStyles({
				'left': 0, 'top': 0,
				'width': size.x - 50, 'height': size.y
			});
			this.waitDiv.fx.start(0,.7);
			this.waitLayerOpen = 1;
		}
	},
	
	this.addFullSelect = function( obj )
	{
		var options = obj.getElements('option');
		obj.setStyle('display', 'none');
		obj.full = new Element('div', {'class': 'full-window'}).setStyles({'display':'none','position': 'absolute'}).inject(document.body);
		obj.full.fx = new Fx.Scroll( obj.full, {'duration':0} );
		obj.replace = new Element('input', {'type':'text', 'readonly': 'readonly', 'class': 'zones full-select'}).inject( obj, 'before' );
		obj.replace.addEvent('click', this.openFullSelect.pass(obj, this) );
		//obj.replace.addEvent('blur', this.closeFullSelect.pass(obj, this) );
		for (var i=0; i<options.length; i++)
		{
			var fo = new Element('a', {'href':'javascript:;', 'class': 'full-option'}).set('html', options[i].get('text')).inject(obj.full);
			fo.value = options[i].get('value');
			fo.addEvent('click', this.selectFullSelect.pass([obj,fo], this) );
			if (options[i].selected)
			{
				this.selectFullSelect( obj, fo );
			}
		}
	},
	
	this.openFullSelect = function( obj )
	{
		var size = obj.replace.getCoordinates();
		obj.full.setStyles( {
			'display': 'block',
			'width': size.width,
			'left': size.left,
			'top': size.top
		} );
		obj.full.fx.toElement( obj.selected );
	},
	
	this.closeFullSelect = function( obj )
	{
		this.closeFullSelectNow.delay( 200, this, obj );
	},
	
	this.closeFullSelectNow = function( obj )
	{
		obj.full.setStyles( {
			'display': 'none'
		} );
	},
	
	this.selectFullSelect = function( obj, fo )
	{
		obj.replace.set('value', fo.get('html') );
		obj.set('value', fo.value );
		obj.selected = fo;
		this.closeFullSelect( obj );
	}
}

/******* IMPLEMENTATION ***********/
Element.implement({
	importProps: function()
	{
		this.saveProps( this, this );
	},
	
	copyProps: function( obj )
	{
		this.saveProps( obj, this );
	},
	
	saveProps: function( source, target )
	{
		var trigger = "logics@";
		if (source.className.indexOf(trigger) !== -1)
		{
			// import rel settings
			var logics = source.className.substring( source.className.indexOf(trigger) + trigger.length );
			var parts = logics.split('|');
			for (var j=0; j<parts.length; j++)
			{
				kv = parts[j].split(':');
				eval("target."+kv[0]+" = '"+kv[1].replace("�","&#39;")+"'");
			}
		}
	},
	
	getURL: function( )
	{
		if (this.urlproto == '[form]')
		{
			var form = this.getParent('form');
			return this.confirmproto + '?' + form.toQueryString();
		}
		else
		{
		var uri = (this.tagName == 'FORM' ? this.getProperty('action') : this.getProperty('href') );
		if (this.urlproto)
			var url = this.urlproto.replace( '[url]', uri + ((this.urlproto.length > 5) ? '&' : '') );
		else
			var url = uri;
		url = url + (url.indexOf('?') !== -1 ? '&' : '?') + 'nocache=' + Math.floor(Math.random()*100);
		return url;
	        }
	}
} );

function swf(swf, w, h)
{
	document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"");
	document.write("codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\"");
	document.write("width=\""+w+"\" height=\""+h+"\">");
	document.write("<param name='movie' value='"+swf+"' />\n");
	document.write("<param name='quality' value='high' /><param name='wmode' value='transparent' />\n");
	document.write("<embed src=\""+swf+"\" quality=\"high\" wmode=\"transparent\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\""+w+"\" height=\""+h+"\"></embed>");
	document.write("</object>\n");
}



var Accordion = new Class({

	Extends: Fx.Elements,

	options: {/*
		onActive: $empty,
		onBackground: $empty,*/
		display: 0,
		show: false,
		height: true,
		width: false,
		opacity: true,
		fixedHeight: false,
		fixedWidth: false,
		wait: false,
		alwaysHide: false
	},

	initialize: function(){
		var params = Array.link(arguments, {'container': Element.type, 'options': Object.type, 'togglers': $defined, 'elements': $defined});
		arguments.callee.parent(params.elements, params.options);
		this.togglers = $$(params.togglers);
		this.container = $(params.container);
		this.previous = -1;
		if (this.options.alwaysHide) this.options.wait = true;
		if ($chk(this.options.show)){
			this.options.display = false;
			this.previous = this.options.show;
		}
		if (this.options.start){
			this.options.display = false;
			this.options.show = false;
		}
		this.effects = {};
		if (this.options.opacity) this.effects.opacity = 'fullOpacity';
		if (this.options.width) this.effects.width = this.options.fixedWidth ? 'fullWidth' : 'offsetWidth';
		if (this.options.height) this.effects.height = this.options.fixedHeight ? 'fullHeight' : 'scrollHeight';
		for (var i = 0, l = this.togglers.length; i < l; i++) this.addSection(this.togglers[i], this.elements[i]);
		this.elements.each(function(el, i){
			if (this.options.show === i){
				this.fireEvent('onActive', [this.togglers[i], el]);
			} else {
				for (var fx in this.effects) el.setStyle(fx, 1);
			}
		}, this);
		if ($chk(this.options.display)) this.display(this.options.display);
	},

	addSection: function(toggler, element, pos){
		toggler = $(toggler);
		element = $(element);
		var test = this.togglers.contains(toggler);
		var len = this.togglers.length;
		this.togglers.include(toggler);
		this.elements.include(element);
		if (len && (!test || pos)){
			pos = $pick(pos, len - 1);
			toggler.inject(this.togglers[pos], 'before');
			element.inject(toggler, 'after');
		} else if (this.container && !test){
			toggler.inject(this.container);
			element.inject(this.container);
		}
		var idx = this.togglers.indexOf(toggler);
		toggler.addEvent('click', this.display.bind(this, idx));
		if (this.options.height) element.setStyles({'padding-top': 0, 'border-top': 'none', 'padding-bottom': 0, 'border-bottom': 'none'});
		if (this.options.width) element.setStyles({'padding-left': 0, 'border-left': 'none', 'padding-right': 0, 'border-right': 'none'});
		element.fullOpacity = 1;
		if (this.options.fixedWidth) element.fullWidth = this.options.fixedWidth;
		if (this.options.fixedHeight) element.fullHeight = this.options.fixedHeight;
		element.setStyle('overflow', 'hidden');
		if (!test){
			for (var fx in this.effects) element.setStyle(fx, 1);
		}
		return this;
	},

	display: function(index){
		index = ($type(index) == 'element') ? this.elements.indexOf(index) : index;
		if ((this.timer && this.options.wait) || (index === this.previous && !this.options.alwaysHide)) return this;
		this.previous = index;
		var obj = {};
		this.elements.each(function(el, i){
			obj[i] = {};
			var hide = (i != index) || (this.options.alwaysHide && (el.offsetHeight > 0));
			this.fireEvent(hide ? 'onBackground' : 'onActive', [this.togglers[i], el]);
			for (var fx in this.effects) obj[i][fx] = hide ? 1 : el[this.effects[fx]];
		}, this);
		return this.start(obj);
	}

});