function applyArticleDescriptionHider(){
	if(new Xu('articoli').isNull()) return;
	
	var showFunc = function(e, source, type){
		var self = new Xu(source);
		
		self.
		getData('div').
		setStyle('display', 'block');
		
		self.
		setStyle('display', 'none').
		getData('otherLink').
		setStyle('display', 'block');
	}
	
	var hideFunc = function(e, source, type){
		var self = new Xu(source);
		self.
		getData('div').
		setStyle('display', 'none');
		
		self.setStyle('display', 'none').
		getData('otherLink').
		setStyle('display', 'block');
	}
	
	var divs = new Xu('articoli').getByClass('div', 'estensione');
	
	var div      = null;
	var hideLink = null;
	var showLink = null;
	var li       = null;
	for(var i = 0; i < divs.length; ++i){
		div = new Xu(divs[i]);
		
		div.
		setVar('className', 'estensione-nascosta').
		setStyle('display', 'none');
		
		hideLink =
		new Xu('a' ,true).
		setVar('href', 'javascript:;').
		setData('div', div).
		addEvent('onclick', hideFunc).
		setVar('className', 'hideLink').
		append( new Xu('text', true, 'nascondi dettagli') ).
		insertBeforeItem(div).
		setStyle('display', 'none');
		
		
		
		showLink =
		new Xu('a', true).
		setData('otherLink', hideLink).
		setData('div', div).
		setVar('href', 'javascript:;').
		addEvent('onclick', showFunc).
		setVar('className', 'showLink').
		append( new Xu('text', true, 'mostra dettagli') ).
		insertBeforeItem(div);
		
		hideLink.setData('otherLink', showLink);
		
		li = new Xu(div.parentItem());
		
		li.setVar('className', (li.getVar('className') == 'offerte-stock' ? 'offerte-stock-ridotto' : 'ridotto'));
	}
}

function applyTechnicalInfoHider(){
	if(new Xu('dati-tecnici').isNull()) return;
	
	var showFunc = function(e, source, type){
		var self = new Xu(source);
		self.
		getData('table').
		setStyle('display', 'block');
		
		self.
		setStyle('display', 'none').
		getData('otherLink').
		setStyle('display', 'block');
	}
	
	var hideFunc = function(e, source, type){
		var self = new Xu(source);
		self.
		getData('table').
		setStyle('display', 'none');
		
		self.
		setStyle('display', 'none').
		getData('otherLink').
		setStyle('display', 'block');
	}
	
	var table = new Xu( new Xu('dati-tecnici').getByTag('table', 0) ).setStyle('display','none');
	
	var label = new Xu('dati-tecnici').getByTag('span', 0).firstChild.nodeValue;
	new Xu( new Xu('dati-tecnici').getByTag('span', 0) ).removeAll();
	
	var hideLink =
	new Xu('a' ,true).
	setVar('href', 'javascript:;').
	setData('table', table).
	addEvent('onclick', hideFunc).
	setVar('className', 'hideLink').
	append( new Xu('text', true, label) ).
	appendTo( new Xu('dati-tecnici').getByTag('span', 0) ).
	setStyle('display', 'none');
	
	
	
	var showLink =
	new Xu('a', true).
	setData('otherLink', hideLink).
	setData('table', table).
	setVar('href', 'javascript:;').
	addEvent('onclick', showFunc).
	setVar('className', 'showLink').
	append( new Xu('text', true, 'MOSTRA ' + label) ).
	appendTo( new Xu('dati-tecnici').getByTag('span', 0) );
	
	hideLink.setData('otherLink', showLink);
}

function applySearchPanelHider(){
	if(new Xu('modulo').isNull() || new Xu('modulo').getVar('className') != 'da-nascondere') return;
	
	var showFunc = function(e, source, type){
		new Xu(source).
		setStyle('display', 'none').
		getData('fieldset').
		setStyle('display', 'block');
	}
	
	var hideFunc = function(e, source, type){
		var self = new Xu(source);
		self.
		getData('hideLink').
		setStyle('display', 'block');
		
		self.
		getData('fieldset').
		setStyle('display', 'none');
	}
	
	var fieldset = new Xu( new Xu('modulo').getByTag('fieldset', 0) ).setStyle('display','none');
	
	var hideLink =
	new Xu('a' ,true).
	setVar('href', 'javascript:;').
	setData('fieldset', fieldset).
	addEvent('onclick', showFunc).
	setVar('className', 'hideLink').
	append( new Xu('text', true, '» Modifica/raffina la tua ricerca') ).
	appendTo( new Xu('modulo') );
	
	var label = new Xu('modulo').getByTag('legend', 0).firstChild.nodeValue;
	new Xu(new Xu('modulo').getByTag('legend', 0)).removeAll();
	
	new Xu('a', true).
	setData('hideLink', hideLink).
	setData('fieldset', fieldset).
	setVar('href', 'javascript:;').
	addEvent('onclick', hideFunc).
	setVar('className', 'showLink').
	append( new Xu('text', true, label) ).
	appendTo( new Xu('modulo').getByTag('legend', 0) );
}

function applyShoppingCartFormConfirm(){
	if(new Xu('carrello').isNull()) return;
	
	var func = function(){
		return confirm('Sei sicuro di voler rimuovere questo articolo?');
	}
	
	var links = new Xu('carrello').getByClass('a', 'elimina');
	
	for(var i = 0; i < links.length; ++i) new Xu(links[i]).addEvent('onclick', func);
}

function applyPrintButton(){
	var func = function(){
		var agree = confirm('Sei sicuro di voler stampare questo articolo?');
		if(agree){
			if(!new Xu('dati-tecnici').isNull()){
				var links = new Xu('dati-tecnici').getByTag('a');
				var link  = null;
				
				for(var i = 0; i < links.length; ++i){
					link = new Xu(links[i]);
					
					if(link.getStyle('display') != 'none' && link.getVar('className') == 'showLink' || link.getVar('className') == 'hideLink'){
						if(link.getVar('className') == 'showLink') link.fireEvent('onclick');
					}
				}
			}
			window.print();
		}
	}
	
	var tmp = new Xu('bottone-stampa');
	if(!tmp.isNull()){
		tmp.
		setVar('href', 'javascript:;').
		addEvent('onclick', func);
	}
	
	tmp = new Xu('bottone-stampa2');
	if(!tmp.isNull()){
		tmp.
		setVar('href', 'javascript:;').
		addEvent('onclick', func);
	}
}

function applyShoppingCartExcludeEffect(){
	if(new Xu('riassunto-prodotti').isNull()) return;
	
	var func = function(e, source, type){
		new Xu(source).getData('row').setVar('className', (source.checked ? 'escluso' : ''));
	}
	
	var num   = 0;
	var input = null;
	var row   = null;
	while(true){
		input = new Xu('elimina_' + num);
		
		if(input.isNull()) break;
		
		row = new Xu('riga_' + num);
		
		input.
		setData('row', row).
		addEvent('onclick', func).
		fireEvent('onclick');
		
		++num;
		
		if(num == 100) break;
	}
}

function ScreenManager(){
}

ScreenManager.prototype = {
	getSize: function(){
		var width  = 0;
		var height = 0;
		
		if(window.innerWidth){
			width = window.innerWidth;
			height = window.innerHeight;
		}
		else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
			width  = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		}
		else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
			width  = document.body.clientWidth;
			height = document.body.clientHeight;
		}
		
		return {
			width:  width,
			height: height
		}
	},
	
	getScroll: function(){
		var scrollX = 0;
		var scrollY = 0;
		
		if(window.pageYOffset){
			scrollX = window.pageXOffset;
			scrollY = window.pageYOffset;
		}
		else if(document.body && (document.body.scrollLeft || document.body.scrollTop)){
			scrollX = document.body.scrollLeft;
			scrollY = document.body.scrollTop;
		}
		else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)){
			scrollX = document.documentElement.scrollLeft;
			scrollY = document.documentElement.scrollTop;
		}
		
		return {
			scrollX: scrollX,
			scrollY: scrollY
		}
	}
}


function ImagePopupManager(link, closeText, loadingText, displayText){
	link = new Xu(link);
	if(link.isNull()) return;
	
	this.link        = link;
	this.closeText   = (closeText ? closeText : 'Chiudi');
	this.loadingText = (loadingText ? loadingText : 'Loading...');
	this.displayText = (displayText ? displayText : null);
	this.imagePath   = null;
	this.description = null;
	this.image       = null;
	this.loaded      = false;
	this.display     = null;
	
	this.updateLink();
}

ImagePopupManager.prototype = {
	updateLink: function(){
		this.imagePath   = this.link.getVar('href');
		this.description = this.link.getVar('title');
		
		var func = function(e, source, type){
			new Xu(source).getData('main').loadImage();
		}
		
		this.link.
		setVar('href', 'javascript:;').
		setVar('target', '_self').
		setData('main', this).
		addEvent('onclick', func);
	},
	
	loadImage: function(){
		if(!new Xu('ImagePopupManagerDiv').isNull()) new Xu('ImagePopupManagerDiv').removeSelf();
		if(this.image == null){
			this.displayLoading();
			
			var selfObj = this;
			
			var func = function(){
				selfObj.imageLoaded();
			}
			
			this.image = new Image();
			this.image.onload = func;
			this.image.src = this.imagePath;
		}
		else if(this.loaded) this.displayImage();
	},
	
	imageLoaded: function(){
		this.loaded = true;
		this.displayImage();
	},
	
	displayLoading: function(){
		this.removeDisplay();
		
		this.display =
		new Xu('div', true).
		setVar('id', 'ImagePopupManagerDiv').
		setStyle('position', 'absolute').
		append(
			new Xu('h1', true).
			append( new Xu('text', true, this.loadingText) ).
			get()
		).
		appendTo( new Xu().getByTag('body', 0) );
		
		this.centerDisplay();
	},
	
	displayImage: function(){
		this.removeDisplay();
		
		var image =
		new Xu('img', true).
		setVar('src', this.imagePath);
		
		var func = function(e, source, type){
			new Xu(source).getData('main').removeDisplay();
		}
		
		var closeLink =
		new Xu('a', true).
		setVar('href', 'javascript:;').
		setData('main', this).
		addEvent('onclick', func).
		append( new Xu('text', true, this.closeText) );
		
		this.display =
		new Xu('div', true).
		setVar('id', 'ImagePopupManagerDiv').
		setStyle('position', 'absolute').
		append( closeLink ).
		append(image).
		appendTo( new Xu().getByTag('body', 0) );
		
		if(this.description){
			this.display.
			append(
				new Xu('h1', true).
				append( new Xu('text', true, this.description) ).
				get()
			);
		}
		
		this.centerDisplay();
		if(location.href.indexOf('#') != -1){
			var newHref = location.href.split('#');
			newHref = newHref[0] + '#ImagePopupManagerDiv';
		}
		else var newHref = location.href + '#ImagePopupManagerDiv';
		location.href = newHref;
	},
	
	centerDisplay: function(){
		if(this.display == null) return;
		
		var SM        = new ScreenManager();
		var size      = SM.getSize();
		var scroll    = SM.getScroll();
		
		var divWidth  = parseInt(this.display.getStyle('width'));
		var divHeight = parseInt(this.display.getStyle('height'));
		
		
		if(!divWidth)  divWidth  = (this.image ? this.image.width : 0);
		if(!divHeight) divHeight = (this.image ? this.image.height + 20 : 0);
		
		if(divWidth) this.display.setStyle('width', (divWidth + 10) + 'px');
		
		this.display.
		setStyle('left', Math.round((size.width + scroll.scrollX) / 2 - (divWidth / 2)) + 'px').
		setStyle('top', Math.round((size.height + scroll.scrollY) / 2 - (divHeight / 2)) + 'px');
	},
	
	removeDisplay: function(){
		if(this.display == null) return;
		
		new Xu( this.display.parentItem() ).removeItem(this.display.get());
		this.display = null;
	}
}

function SwfPopupManager(link, swfWidth, swfHeight){
	link = new Xu(link);
	if(link.isNull()) return;
	
	this.link        = link;
	this.swfPath     = null;
	this.swfWidth    = swfWidth;
	this.swfHeight   = swfHeight;
	this.display     = null;
	
	this.updateLink();
}

SwfPopupManager.prototype = {
	updateLink: function(){
		this.swfPath   = this.link.getVar('href');
		
		var func = function(e, source, type){
			new Xu(source).getData('main').displaySwf();
		}
		
		this.link.
		setVar('href', 'javascript:;').
		setVar('target', '_self').
		setData('main', this).
		addEvent('onclick', func);
	},
	
	displaySwf: function(){
		var func = function(e, source, type){
			new Xu(source).getData('main').removeDisplay();
		}
		
		var closeLink =
		new Xu('a', true).
		setVar('href', 'javascript:;').
		setData('main', this).
		addEvent('onclick', func).
		append( new Xu('text', true, 'Chiudi') );
		
		this.display =
		new Xu('div', true).
		setVar('id', 'ImagePopupManagerDiv').
		setStyle('position', 'absolute').
		append( closeLink.get() ).
		append(
			new Xu('div', true).
			setVar('id', 'swf-holder').
			get()
		).
		appendTo( new Xu().getByTag('body', 0) );
		
		this.centerDisplay();
		if(location.href.indexOf('#') != -1){
			var newHref = location.href.split('#');
			newHref = newHref[0] + '#ImagePopupManagerDiv';
		}
		else var newHref = location.href + '#ImagePopupManagerDiv';
		location.href = newHref;
		
		var swfDiv = new SWFObject('../MostraImmagine.swf?filename=' + this.swfPath, 'MostraImmagine', this.swfWidth, this.swfHeight, '7');
		swfDiv.write('swf-holder');
	},
	
	centerDisplay: function(){
		if(this.display == null) return;
		
		var SM        = new ScreenManager();
		var size      = SM.getSize();
		var scroll    = SM.getScroll();
		
		var divWidth  = parseInt(this.display.getStyle('width'));
		var divHeight = parseInt(this.display.getStyle('height'));
		
		
		if(!divWidth)  divWidth  = this.swfWidth;
		if(!divHeight) divHeight = this.swfHeight + 20;
		
		if(divWidth) this.display.setStyle('width', (divWidth + 10) + 'px');
		
		this.display.
		setStyle('left', Math.round((size.width + scroll.scrollX) / 2 - (divWidth / 2)) + 'px').
		setStyle('top', Math.round((size.height + scroll.scrollY) / 2 - (divHeight / 2)) + 'px');
	},
	
	removeDisplay: function(){
		if(this.display == null) return;
		
		this.display.removeSelf();
		this.display = null;
	}
}

function applyPopupViewers(){
	new ImagePopupManager('immagine-50');
	new SwfPopupManager('immagine-100', 500, 350);
}

function applyDefaultStringHider(id, defaultString){
	var tmp = new Xu(id);
	if(tmp.isNull()) return;
	
	var onFocus = function(e, source, type){
		var item = new Xu(source);
		if(item.getVar('value') == item.getData('defaultString')) item.setVar('value', '');
	}
	
	var onBlur = function(e, source, type){
		var item = new Xu(source);
		if(item.getVar('value') == '') item.setVar('value', item.getData('defaultString'));
	}
	
	tmp.
	setData('defaultString', defaultString).
	addEvent('onfocus', onFocus).
	addEvent('onblur', onBlur).
	fireEvent('onblur');
}

function applyBlankTargetLinks(){
	var links = new Xu().getByClass('a', 'blankLink');
	if(!links) return;
	
	for(var i = 0; i < links.length; ++i) new Xu(links[i]).setVar('target', '_blank');
}

function applyNewsletterOnSubmit(){
	var form = new Xu('form_newsletter');
	if(form.isNull()) return;
	
	var onSubmit = function(e, source, type){
		
		var self = new Xu('form_newsletter');
		var windowWidth  = 300;
		var windowHeight = 600;
		var posX = (screen.width - windowWidth) / 2;
		var posY = (screen.height - windowHeight) / 2 - 60;
		
		var email = new Xu('email_newsletter').getVar('value');
		var consenso = (new Xu('consenso').getVar('checked') ? 'true' : 'false');
		if(new Xu('newsletter_rimozione').getVar('checked')) var sign = 2;
		else var sign = 1;
		
		window.open(
			self.getData('URL') +
			'?name=' +
			'&email=' + email +
			'&sign=' + sign +
			'&consenso=' + consenso,
			'join',
			'width=' + windowWidth +
			',height=' + windowHeight +
			',top=' + posY +
			',left=' + posX
		);
		
		return false;
	}
	
	form.
	setData('URL', form.getVar('action')).
	setVar('action', '#').
	addEvent('onsubmit', onSubmit);
}

function applySlidingText(path, frasi){
	var swfDiv = new SWFObject(path + 'TestoScorrevole.swf?phrases=' + frasi + '&amp;waitTime=10000&amp;color=0xe9cd87&amp;bold=1&amp;phraseChangeSpeed=3000&amp;size=15', 'frasi', '870', '20', '7', '#ffffff');
	swfDiv.write('testo-scorrevole');
}

function applyReferences(path){
	var item = new Xu('catalogo');
	if(item.isNull()) return;
	
	
	new Xu('div', true).
	setVar('id', 'referenze-banner').
	append(
		new Xu('span', true).
		setVar('className', 'bordi-arrotondati-superiori')
	).
	append(
		new Xu('p', true).
		append(
			new Xu('text', true, 'CI HANNO DATO FIDUCIA')
		)
	).
	append(
		new Xu('div', true).
		setVar('id', 'referenze-banner-contenuto')
	).
	append(
		new Xu('span', true).
		setVar('className', 'bordi-arrotondati-inferiori')
	).
	insertAfterItem(item);
	
	var swfDiv = new SWFObject(path + 'ReferenzeBanner.swf', 'referenze-banner-flash', '260', '80', '7', '#ffffff');
	swfDiv.addParam('wmode', 'opaque');
	swfDiv.write('referenze-banner-contenuto');
}

function RotatingBanner(){
	this.banner = new Xu('customlogo-link');
	if(this.banner.isNull()) return;
	
	this.current = 0;
	
	this.prepare();
}

RotatingBanner.prototype = {
	prepare: function(){
		new Xi(this, 'interval', this.rotate, 2000).start();
	},
	
	rotate: function(){
		if(this.current < 4) ++this.current;
		else this.current = 0;
		
		this.banner.get().className = 'i' + this.current;
	}
}
	

function pageEnancher(path, page, frasi){
	applyBlankTargetLinks();
	applyDefaultStringHider('ricerca_semplice_stringa_libera', 'Digita qualcosa...');
	applyShoppingCartFormConfirm();
	applyArticleDescriptionHider();
	applyNewsletterOnSubmit();
	
	switch(page){
		case 'scheda_prodotto':
		applyTechnicalInfoHider();
		applyPopupViewers();
		applyPrintButton();
		break;
		
		case 'chiedi-preventivo':
		applyShoppingCartExcludeEffect();
		break;
		
		case 'ricerca':
		applySearchPanelHider();
		applyDefaultStringHider('ricerca_avanzata_stringa_libera', 'Digita qualcosa...');
		break;
	}
	new RotatingBanner();
	applySlidingText(path, frasi);
	applyReferences(path);
}
