var loaded = false;

Event.add(window, 'domloaded', function(){
	
	var s = Env.pageSize();
	
	if (!loaded) {
		BODY.append(['div', {
			id: 'loading',
			style: {
				height: s[1] + 'px'
			}
		}, ['div', 'Caricamento in corso...']]);
	}
	
});

Event.add(window, 'load', function(){
	
	loaded = true;
	
	if (I('loading')) {
		I('loading').remove();
	}
	
	
	
	$('#menu li').each(function(o){
		
		o.addEvent('mouseover', function(){
			this.addClass('hover');
		}).addEvent('mouseout', function(){
			this.removeClass('hover');
		});
		
	});
	
	
	// Top images
	var list = [], listIds = {}, actual = 0, i = 0;
	$('#topimages div.top-image').each(function(o) {
		list[i] = o;
		listIds[(o.id + '')] = i;
		i++;
		o.styles({
			left: '1000px'
		})
	});
	list[0].styles({
		left: '0px'
	});
	var total = list.length;
	
	I('topimages_right').addEvent('click', function(){
		
		list[actual].morph({
			styles: {
				left: ['px', 0, -900]
			},
			time: 0.5,
			effect: EE.outCubic
		});
		
		actual = (total + (actual + 1)) % total;
		
		list[actual].morph({
			styles: {
				left: ['px', 900, 0]
			},
			time: 0.5,
			effect: EE.outCubic
		});
		
	});
	
	I('topimages_left').addEvent('click', function(){
		
		list[actual].morph({
			styles: {
				left: ['px', 0, 900]
			},
			time: 0.5,
			effect: EE.outCubic
		});
		
		actual = (total + (actual - 1)) % total;
		
		list[actual].morph({
			styles: {
				left: ['px', -900, 0]
			},
			time: 0.5,
			effect: EE.outCubic
		});
		
	});
	
	if(I('topimages_thumbs')) {
		$('#topimages_thumbs a').each(function(o){
			var id = (o.href + '').split('#')[1];
			o.addEvent('click', function(e){
				list[actual].morph({
					styles: {
						left: ['px', 0, 900]
					},
					time: 0.5,
					effect: EE.outCubic
				});
				
				actual = listIds[id];
				
				list[actual].morph({
					styles: {
						left: ['px', -900, 0]
					},
					time: 0.5,
					effect: EE.outCubic
				});
				
				return e.stop();
			});
		});
	}
	
	// vetrine
	$('#vetrina_static div.vetrina').each(function(o){
		
		var title = o.byClass('title')[0], a = o.byTag('a')[0];
		
		a.addEvent('mouseover', function(){
			title.morph({
				styles: {
					bottom: ['px', ACTUAL, 0]
				},
				time: 0.5,
				effect: EE.outCubic,
				stop: true
			});
		}).addEvent('mouseout', function(){
			title.morph({
				styles: {
					bottom: ['px', ACTUAL, -47]
				},
				time: 0.5,
				effect: EE.outCubic,
				stop: true
			});
		});
		
		title.styles({
			bottom: '-47px'
		});
		
		if(Agent.MSIE && Agent.MSIE < 7) {
			
			var bg = I(o.byClass('bg')[0]);
			bg.alphaBackground(bg.backgroundImageUrl()).styles({
				background: ''
			});
			title.alphaBackground(title.backgroundImageUrl()).styles({
				background: ''
			});
		}
		
	});
	
	I('search_input').addEvent('focus', function(){
		if(this.value == 'Cerca nel sito') {
			this.value = '';
		}
	}).addEvent('blur', function(){
		if(this.value == '') {
			this.value = 'Cerca nel sito';
		}
	}).value = 'Cerca nel sito';
	
	if(Agent.MSIE && Agent.MSIE < 7) {
		
		I('menu_sfondo').alphaBackground(I('menu_sfondo').backgroundImageUrl()).styles({
			background: ''
		});
		
		if(I('topimages_left')) {
			I('topimages_left').alphaBackground(I('topimages_left').backgroundImageUrl()).styles({
				background: ''
			});
		}
		if(I('topimages_right')) {
			I('topimages_right').alphaBackground(I('topimages_right').backgroundImageUrl()).styles({
				background: ''
			});
		}
		
		I('right_bg').alphaBackground(I('right_bg').backgroundImageUrl()).styles({
			background: ''
		});
	}
	
	Require.module('lightshow', function(){
		
		LightShow.init();
		
	});
	
});

