var Site = {

	'curpage':	'main',
	'chngtit':	0,
	'cursect':	'info',
	'lang': 'it',

	// bg animation
	'path': '/assets/bg_to_scroll/',
	'animatebg': true,
	'totimages': 0,
	'backgrounds': new Array(),
	'bgsizes': new Array(),
	'totsize': 0,
	'duration': 0,
	'position': 1, // start from image #1
	'speed': 30, // 40 secs per page (page = 955px)

	// preloader
	'preload': new Array(),
	'prelcnt': 0,

	// gallery
	'current_pos': 1,
	'maxscroll': 1,
	'gfx': '',

	// IE Version
	'IEold': 0,

	// background images
	firstbg: {
		'sala_specchi': 	'sala_specchi_5.jpg',
		'sala_acqua': 		'sala_acqua_1.jpg',
		'sala_terra': 		'sala_terra_17.jpg',
		'labirinto': 		'labirinto_8.jpg',
		'sala_metalli': 	'sala_metalli_3.jpg',
		'sala_sfere': 		'sala_sfere_3.jpg',
		'tempio_azzurro': 	'tempio_azzurro_2.jpg',
		'storia': 			'storia_14.jpg',
		'futuro':			'futuro_4.jpg'
	},

	Start: function() {
		$('icons').setStyle('opacity', 0);
		$('internal').setStyle('opacity', 0);
		Site.CenterContainer();
		Site.BgAnimation();
		Site.AnimateBottomMenu();
		Site.AnimateTopMenu();
		Site.InitMiniPanel();
		Site.HomeScroll();
		Site.IconsMenu();
		Site.ExternalLinks();
		Site.Preloader();
	},

	Preloader: function() {
		new Asset.images(
			Site.preload[Site.prelcnt], {
			onComplete: function() {
				Site.prelcnt += 1;
				(function() { Site.Preloader() }).delay(333);
			}
		});
	},

	ExternalLinks: function() {
		$$('.external').each(function(el) { el.setProperty('target', '_blank') });
	},

	CenterContainer: function() {
		var bodyy = $(document.body).getSize().y;
		var conty = $('container').getSize().y;
		var newposy = Math.round((bodyy - conty) / 2);
		$('container').setStyle('margin-top', newposy);
	},

	InitMiniPanel: function() {
		Site.Private_InitMiniPanel('green');
		Site.Private_InitMiniPanel('blue');
	},

	AutoCenter: function() {
		var bodyy = $(document.body).getSize().y;
		var cntry = $('container').getSize().y;
		var newposy = Math.round((bodyy - cntry) / 2);
		$('container').setProperty('margin-top', newposy);
	},

	IconsMenu: function() {
		if (!$('icons')) return;
		$$('#icons a').each(function(el) {
			var rx = /^(.*)\.png/;
			var bg_on  = $(el.getProperty('id')).getStyle('background-image');
			var bg_off = bg_on.replace(rx, "$1_off.png");
			el.addEvents({
				'click': function(e) {
					e.stop();
					if (Site.cursect == el.getProperty('href')) return;
					Site.chngtit = 0;
					Site.cursect = el.getProperty('href');
					Site.Private_ShowInternal();
				},
				'mouseover': function() { el.setStyle('background', bg_off); },
				'mouseout': function() { el.setStyle('background', bg_on); }
			});
		});
	},

	AnimateBottomMenu: function() {
		if (!$('menu_swf')) return;
		$$('#menu_swf a').each( function(el) {
			el.addEvent('click', function(e) {
				e.stop();
				if (Site.curpage == el.getProperty('href')) return;
				Site.chngtit = 1;
				Site.curpage = el.getProperty('href');
				Site.Private_HideAll();
			})
		});
		$$('#menu_swf li').each( function(el) {
			var objlnk = el.getElement('a');
			var objdiv = el.getElement('div');
			var smallsize = objdiv.getSize().x;
			var bigsize = el.getSize().x - 20;
			var morphin = new Fx.Morph(objdiv, {duration: 100, transition: Fx.Transitions.Quad.easeOut});
			objlnk.addEvents({
				'mouseover': function() {
					morphin.cancel();
					morphin.start({'width': [smallsize, bigsize]});
				},
				'mouseout': function() {
					morphin.cancel();
					morphin.start({'width': [bigsize, smallsize]});
				}
			});
		});
	},

	AnimateTopMenu: function() {
		if (!$('menu')) return;
		$$('#menu a').each( function(el) {
			if (el.getProperty('href') == '/en') return;
			if (el.getProperty('href') == '/it') return;
			if (el.getProperty('href') == '/') return;
			if (el.getProperty('class').match('language')) return;
			if (el.getProperty('class').match('external')) return;
			el.addEvent('click', function(e) {
				e.stop();
				if (Site.curpage == el.getProperty('href')) return;
				Site.chngtit = 1;
				Site.curpage = el.getProperty('href');
				Site.Private_HideAll();
			})
		});
	},

	HomeScroll: function() {
		if (!$('scroller')) return;
		// init scroll effect
		var scrollsize = $('scroller').getSize().y - 10;
		var maxscroll = $('scroller').getScrollSize().y;
		var numclick = 0;
		var maxclick = Math.floor(maxscroll / scrollsize);
		
		var toscroll = 0;
		var fx = new Fx.Scroll('scroller', { wait: false, duration: 1000, transition: Fx.Transitions.Quad.easeInOut });
		// add actions
		$('prev').addEvent('click', function(e) {
			e.stop(); 
			numclick--;
			toscroll = numclick * scrollsize;
			if (numclick < 0) {
				numclick = 0;
				toscroll = 0;
			}
			fx.start(0, toscroll);
		});
		$('next').addEvent('click', function(e) { 
			e.stop(); 
			numclick++;
			toscroll = numclick * scrollsize;
			if (numclick > maxclick) {
				numclick = maxclick;
				toscroll = maxscroll;
			}
			fx.start(0, toscroll); 
		});
	},

	BgAnimation: function() {
		if (Site.IEold == true) return;

		// setup total duration
		Site.duration = Math.round(Site.totsize / $('cntflash').getSize().x * Site.speed) * 1000;
		// create ul container
		var ul = new Element('ul', {
			'id': 'bgscroll',
			'class': 'bgscroll',
			'styles': { 'width': Site.totsize +'px' }
		}).inject($('cntflash'), 'bottom');
		// IE opacity Patch
		var minopacity = 0.8;
		if (Browser.Engine.trident) minopacity = 1;
		// create first li
		var li = new Element('li', {
			'id': 'bg_0',
			'styles': {
				'width': Site.bgsizes[Site.position] +'px',
				'opacity': minopacity,
				'background': 'transparent url('+ Site.path + Site.backgrounds[Site.position] +') 0 0 no-repeat'
			}
		}).inject($('bgscroll'), 'bottom');
		// create all others
		for (i = 1; i <= Site.totimages; i++) {
			if (!Site.bgsizes[i]) continue;
			Site.position += 1;
			if (Site.position > Site.totimages) Site.position = 1;
			var li = new Element('li', {
				'id': 'bg_'+ i,
				'styles': {
					'width': Site.bgsizes[Site.position] +'px',
					'opacity': minopacity,
					'background': 'transparent url('+ Site.path + Site.backgrounds[Site.position] +') 0 0 no-repeat'
				}
			}).inject($('bgscroll'), 'bottom');
		}
		// change opacity
		$$('#cntflash li').each(function(el) {
			var fx = new Fx.Tween(el, {duration: 100, property: 'opacity'});
			el.addEvents({
				'mouseover': function() { fx.start(minopacity, 1) },
				'mouseout': function() { fx.start(1, minopacity) }
			});
		});
		// add effect on hover
		if ($('bgscroll') && !Browser.Engine.trident) {
			var bgscroll = $('bgscroll');
			var initopac = bgscroll.getStyle('opacity');
			var showa = new Fx.Morph(bgscroll, {'duration': 400, transition: Fx.Transitions.Quad.easeOut});
			bgscroll.addEvents({
				'mouseover': function() {
					showa.cancel();
					showa.start({'opacity': [initopac, 1]});
				},
				'mouseout': function() {
					showa.cancel();
					showa.start({'opacity': [1, initopac]});
				}
			});
		}
		// start animation
		Site.Private_BgAnimation();
	},


	// INTERNAL FUNCTIONS (private)

	Private_InitMiniPanel: function(name) {
		if (!$(name)) return;
		var el = $(name);
		var fx = new Fx.Tween(el, {duration: 200, link: 'cancel', 'property': 'background-position'});
		var nullpos = -140;
		if (name != 'green') nullpos = 0;
		el.addEvents({
			'mouseover': function(e) {
				e.stop();
				fx.start('7px '+ nullpos +'px');
			},
			'mouseout': function(e) { 
				e.stop();
				fx.start('7px -70px');
			}
		});
	},

	Private_HideAll: function() {
		// checks
		var icons = $('icons');
		if (icons.getStyle('opacity') > 0) return Site.Private_ShowInternal();
		// setup vars
		var todelay = 0;
		var cntflash = $('cntflash');
		var objtaj = $('text_ajax');
		var sizez = objtaj.getStyle('margin-left').toInt();
		// hide all
		$('scroller').fade('out');
		$('pages').fade('out');
		todelay += 400;
		// blue resize
		bel = $('blue');
		bsize = bel.getSize().x;
		bfx = new Fx.Morph(bel, {duration: 200, transition: Fx.Transitions.Quad.easeOut});
		bfx.start({'width': [bsize, 7]});
		bel.addEvents({
			'mouseenter': function() { bfx.cancel(); bfx.start({'width': [7, bsize]}) },
			'mouseleave': function() { bfx.cancel(); bfx.start({'width': [bsize, 7]}) }
		});
		// blue resize
		gel = $('green');
		gsize = gel.getSize().x;
		gfx = new Fx.Morph(gel, {duration: 200, transition: Fx.Transitions.Quad.easeOut});
		gfx.start({'width': [bsize, 7]});
		gel.addEvents({
			'mouseenter': function() { gfx.cancel(); gfx.start({'width': [7, gsize]}) },
			'mouseleave': function() { gfx.cancel(); gfx.start({'width': [gsize, 7]}) }
		});
		// reduce right sidebar and show icons
		(function() {
			new Fx.Morph(objtaj, {duration: 300, transition: Fx.Transitions.Quad.easeOut}).start({
				'margin-left': [sizez, (sizez + objtaj.getSize().x - 100)],
				'width': [objtaj.getSize().x.toInt(), 100]
			});
		}).delay(todelay);
		todelay += 300;
		// hide flash
		(function() { 
			$('cntflash').fade('out'); 
			if (Site.IEold == true) $('photos').setStyle('background', 'transparent');
		}).delay(todelay);
		Site.animatebg = false;
		todelay += 400;
		// ajax request
		(function() { Site.Private_ShowInternal() }).delay(todelay);
	},

	Private_ShowInternal: function() {
		var internal = $('internal');
		var ms = 300;
		var cnt = 0;
		// photo gallery
		if (Site.cursect == 'photo') return Site.Private_ShowGallery();
		else if ($('cnt_gallery')) {
			$('cnt_gallery').fade('out');
			cnt += 1;
		}
		// show internal
		if (internal.getStyle('opacity') == 0) {
			internal.setProperty('class', 'internal');
			$('cntflash').empty();
			new Fx.Tween(internal, {duration: ms, property: 'opacity'}).start(0, 0.9);
			cnt += 1;
		};
		// change title
		if (Site.chngtit == 1) Site.Private_ChangeTitle(ms);
		// show ajax loading then do request
		(function() {
			if ($('cnt_gallery')) $('cnt_gallery').destroy();
			internal.set({'html': ''});
			internal.set({'html': '<div id="ajaxload"></div>'});
		}).delay(ms * cnt);
		cnt += 1;
		(function() {
			var req = new Request.HTML({
				url: "/assets/"+ Site.lang +"/templates/"+ Site.cursect +"_"+ Site.curpage +".html", 
				onSuccess: function(html) {
					internal.set({'html': ''});
					internal.adopt(html);
					Site.Private_Antispam();
				},
				onFailure: function() {
					internal.set('html', 'cannot load '+ Site.cursect +"_"+ Site.curpage +".html");
				}
			}).send();
		}).delay(ms * cnt);
		// show icons
		var icons = $('icons');
		if (icons.getStyle('opacity') > 0) return;
		(function() { new Fx.Tween(icons, {duration: 300}).start('opacity', 0, 1)}).delay(ms * cnt);
	},

	Private_ChangeTitle: function(ms) {
		if (Site.chngtit == 0) return;
		Site.chngtit = 0;
		var title_room = $('title_room');
		var efx = new Fx.Tween(title_room, {duration: ms, link: 'chain'});
		efx.start('opacity', 1, 0); 
		(function() { title_room.setStyle("background-image", 'url(/assets/'+ Site.lang +'/images/title_'+ Site.curpage +'.png)')} ).delay(300);
		efx.start('opacity', 0, 1);
		// change background
		var cngbg = $('cntflash');
		var fx = new Fx.Tween(cngbg, {
			duration: 200,
			onComplete: function(){ 
				cngbg.setStyle('background-image','url(/assets/big_gallery/'+ Site.firstbg[Site.curpage] +')');
				cngbg.fade('in');
			}
		});
		fx.start('opacity',1,0);
	},

	Private_BgAnimation: function() {
		if (Site.curpage != 'main') return;
		var fx = new Fx.Tween($('bgscroll'), {'property': 'margin-left', 'duration': Site.duration, transition: Fx.Transitions.linear});
		fx.start(0, -(Site.totsize - Site.bgsizes[1]));
		if (Site.animatebg == true) Site.Private_BgAnimation.periodical(Site.duration);
	},

	Private_Antispam: function() {
		$$('.email').each(function(email) {
			var addr = email.getProperty('href');
			// IE patch
			if (addr.match('http')) {
				addr = addr.split("/");
				addr = addr[3];
			}
			var toks = addr.split("-_my_fake_-");
			var mail_real = toks[0] + "@" + toks[1];
			email.setProperty('html', mail_real);
			email.setProperty('href', 'mailto:' + mail_real);
			email.addEvents({
				'mouseover': function() {
					var addr = email.getProperty('href');
					var toks = addr.split("-_my_fake_-");
					if (toks[1] == null) return;
					mail_real = toks[0] + "@" + toks[1];
					mail_fake = toks[0] + "-_my_fake_-" + toks[1];
					email.setProperty('href', 'mailto:' + mail_real);
				}
			});
		});

	},

	Private_ShowGallery: function() {
		// change title
		Site.Private_ChangeTitle(300);
		var ms = 200;
		var cnt = 0;
		var internal = $('internal');
		// hide internal
		if (internal.getStyle('opacity') > 0) {
			new Fx.Tween(internal, {'property': 'opacity', 'duration': ms, transition: Fx.Transitions.Quad.easeOut}).start(1, 0);
			cnt += 1;
		}
		// make gallery
		if (!$('cnt_gallery')) {
			new Element('div', { 'id': 'cnt_gallery' }).inject($('photos'), 'bottom');
			new Element('div', { 'id': 'arrowu' }).inject($('cnt_gallery'), 'bottom');
			new Element('div', { 'id': 'arrowd' }).inject($('cnt_gallery'), 'bottom');
			new Element('div', { 'id': 'gallery' }).inject($('cnt_gallery'), 'bottom');
			new Element('a', {'id': 'scrolld', 'href': '#'}).inject($('arrowd'), 'bottom');
			new Element('a', {'id': 'scrollu', 'href': '#'}).inject($('arrowu'), 'bottom');
			new Fx.Tween($('cnt_gallery'), {'property': 'opacity', 'duration': ms, transition: Fx.Transitions.Quad.easeOut}).start(0, 1);
			// init scroll effect
			var scrollsize = 72;
			Site.gfx = new Fx.Scroll('gallery', { wait: false, duration: 500, transition: Fx.Transitions.Quad.easeInOut });
			// add actions
			$('scrolld').addEvent('click', function(e) { 
				e.stop(); 
				if (Site.current_pos == Site.maxscroll) return;
				Site.current_pos += 1; 
				Site.gfx.start(0, ((Site.current_pos - 1) * scrollsize)); 
				Site.Private_SetupGalleryButton();
			});
			$('scrollu').addEvent('click', function(e) { 
				e.stop(); 
				if (Site.current_pos == 1) return;
				Site.current_pos -= 1; 
				Site.gfx.start(0, ((Site.current_pos - 1) * scrollsize)); 
				Site.Private_SetupGalleryButton();
			});
		};
		// reset position
		Site.gfx.toTop();
		Site.current_pos = 1;
		// show ajax loading then do request
		var gallery = $('gallery');
		(function() {
			internal.set({'html': ''});
			gallery.set({'html': ''});
			gallery.set({'html': '<div id="ajaxload"></div>'});
		}).delay(ms * cnt);
		cnt += 1;
		// load images
		(function() {
			var req = new Request.HTML({
				url: "/photos.php?sect="+Site.curpage, 
				onSuccess: function(html) {
					gallery.set({'html': ''});
					gallery.adopt(html);
					var images = $$('#gallery a');
					Site.maxscroll = Math.ceil(images.length / 6);
					Site.Private_SetupGalleryButton();
					if (Site.maxscroll > 1) {
						$('arrowu').setStyle('opacity', 1);
						$('arrowd').setStyle('opacity', 1);
					} else {
						$('arrowu').setStyle('opacity', 0);
						$('arrowd').setStyle('opacity', 0);
					}
					images.each(function(el) {
						el.addEvent('click', function(e) {
							e.stop();
							// IE background patch
							imgpth = el.getProperty('href');
							if (el.getProperty('href').match('http')) {
								imgpth = el.getProperty('href').split("/");
								imgpth = imgpth [3];
							}
							// change background
							var cngbg = $('cntflash');
							var fx = new Fx.Tween(cngbg, {
								duration: 200,
								onComplete: function() { 
									cngbg.setStyle('background','transparent url(/assets/big_gallery/'+ imgpth +'.jpg)');
									cngbg.fade('in');
								}
							});
							fx.start('opacity',1,0);
						});
					});
				},
				onFailure: function() { gallery.set('html', "cannot load photo_"+ Site.curpage +".html"); }
			}).send();
		}).delay(ms * cnt);
	},

	Private_SetupGalleryButton: function() {
		if (Site.current_pos == Site.maxscroll) $('scrolld').setStyles({'background': 'url(/assets/images/arrowd_off.png)', 'cursor': 'default'});
		else $('scrolld').setStyles({'background': 'url(/assets/images/arrowd.png)', 'cursor': 'pointer'});
		if (Site.current_pos == 1) $('scrollu').setStyles({'background': 'url(/assets/images/arrowu_off.png)', 'cursor': 'default'});
		else $('scrollu').setStyles({'background': 'url(/assets/images/arrowu.png)', 'cursor': 'pointer'});
	},
	
	IEWarn: function() {
		return;
	}

}
