$(function() {
	Roxa.load();
});

Roxa = {
	load: function()
	{
		switch ($('body').attr('rel')) {
			case 'mod_homepage':		Roxa.HomePage.load();			break;
			case 'mod_pagina':			Roxa.Pagine.load();				break;
			case 'mod_catHome':			Roxa.CatHome.load();			break;
			case 'mod_catCategorie':	Roxa.CatCategorie.load();		break;
			case 'mod_catProdotti':		Roxa.CatProdotti.load();		break;
			case 'mod_maps':			Roxa.Map.load();				break;
		}
	},
	HomePage: {
		load: function() {
			if ($('#homeSplash a').size() > 1)
				Roxa.HomePage.Slide.start();
		},
		Slide: {
			start: function() {
				setTimeout('Roxa.HomePage.Slide.next()', 4000);
			},
			next: function() {
				$('#homeSplash div:hidden').css('z-index', 97);
				var sel = $('#homeSplash div:visible');
				var selNext = sel.next(0).size() ? sel.next(0) : $('#homeSplash div:first');
				
				sel.css('z-index', 99);
				selNext.css('z-index', 98).show();
				sel.fadeOut('slow');
				
				Roxa.HomePage.Slide.start();
			}
		}
	},
	Map: {
		load: function()
		{
			if (GBrowserIsCompatible()) {
				var map = new GMap2(document.getElementById('mapBox'));
				var marker = new GMarker(new GLatLng(45.784517, 11.908901));
				var html = '<div style="width:210px; padding-right:10px; font-size:10px; text-align:left">'+
					'<b>ROCCA GROUP S.R.L.</b><br/>'+
					'VIA G. GALILEI, 58<br/>'+
					'31011 ASOLO (TV) - ITALY <br/>'+
					'T. 0039-0423-55621<br/>'+
					'F. 0039-0423-950410<br/>'+
					'mail. info@roxa.com</div>';

				map.setCenter(new GLatLng(45.784517, 11.908901), 7);
				map.addControl(new GLargeMapControl3D());
				map.addControl(new GMapTypeControl());
				map.addOverlay(marker);
				marker.openInfoWindowHtml(html);
			}
		}
	},
	Pagine: {
		load: function() {
			if ($('#pgSlide > div').size() > 1)
				Roxa.Pagine.SlidePages.start();
		},
		SlidePages: {
			start: function() {
				setTimeout('Roxa.Pagine.SlidePages.next()', 4000);
			},
			next: function() {
				$('#pgSlide div:hidden').css('z-index', 97);
				var sel = $('#pgSlide div:visible');
				var selNext = sel.next(0).size() ? sel.next(0) : $('#pgSlide div:first');
			
				sel.css('z-index', 99).hide();
				selNext.css('z-index', 98).show();
			
				Roxa.Pagine.SlidePages.start();
			}
		}
	},
	CatHome: {
		load: function() {
			$('#pgCatHome li').hover(function() {
				var ele = $('#pgCatHome li');
				var perEle = Math.floor((924-370)/(ele.size()-1));
				var selID = this.id;
				ele.width(perEle);
				$(this).width(924-((ele.size()-1)*perEle));
				ele.each(function() {
					$(this).stop();
					if (this.id != selID)
						$(this).fadeTo(300, 0.3);
					else
						$(this).fadeTo(300, 1);
				})
			}, function() {
				var ele = $('#pgCatHome li');
				var perEle = Math.floor(924/ele.size());
				ele.width(perEle);
				$('#pgCatHome li:last').width(924-((ele.size()-1)*perEle));
			});
			$('#pgCatHome').hover(function() { }, function() {
				$('#pgCatHome li').fadeTo('fast', 1);
			})
		}
	},
	CatCategorie: {
		load: function() {
			$('.pages img').click(Roxa.changePage);
		}
	},
	CatProdotti: {
		load: function() {
			$('.prdVariant li[class!=noclick]').css('cursor', 'pointer').click(function() {
				$('.prdMainPict').css('backgroundImage', 'url(/img/prod/'+this.id.substring(4)+'.big.jpg)');
				$('.prdMainType').html($(this).children('div').html());
				$('.prdMainPict a').attr('href', '/img/prod/'+this.id.substring(4)+'.extra.jpg').attr('title', $(this).children('div').html());
			});
			$('.pages span').click(Roxa.changePage);
			$('.prdSwitchBox span').click(function()
			{
				var selA = $(this).attr('rel');
				var showEle = (selA == 'Var' ? 'Photo' : 'Var');
				$(this).parent().children('span').removeClass('sel');
				$(this).addClass('sel');
				
				if ($('.prdSwitch'+selA+':visible').size() == 0)
				{
					$('.prdSwitch'+showEle).css('z-index', 99).hide();
					$('.prdSwitch'+selA).css('z-index', 98).show();
				}
				return false;
			});
			$('.prdPhoto a').lightBox({ fixedNavigation:true });
			$('.prdMainPict a').lightBox({ fixedNavigation:false });
		}
	},
	changePage: function() {
		if (!$(this).hasClass('sel'))
		{
			var index = $(this).attr('rel');
			var pagesEle = $(this).parent().attr('rel');
			var pagesID = $(this).parent().attr('id');
			
			if (pagesID != '')
				$.cookie('menu_'+pagesID, index);
			
			$('.'+pagesEle).css('z-index', 97).fadeOut('normal');
			$('.'+pagesEle+':visible').css('z-index', 99);
			$('.'+pagesEle+':eq('+index+')').css('z-index', 98).fadeIn('normal');
			$(this).parent().children($(this).attr('tagname')).attr('src', '/img/overview.gif');
			$(this).attr('src', '/img/overviewSel.gif');
			$(this).parent().children($(this).attr('tagname')).removeClass('sel');
			$(this).addClass('sel');
		}
	},
	
	Form: {
		sendContatto: function(form, ita) {
			if (form.azienda && $.trim(form.azienda.value) == '') {
				alert(ita ? 'Il campo "Azienda" non è stato compilato correttamente!' : 'The "Company" has not been completed correctly!');
				form.azienda.focus();
				return false;
			}
			if (form.nome && $.trim(form.nome.value) == '') {
				alert(ita ? 'Il campo "Nome" non è stato compilato correttamente!' : 'The "Name" has not been completed correctly!');
				form.nome.focus();
				return false;
			}
			if (form.telefono && $.trim(form.telefono.value) == '') {
				alert(ita ? 'Il campo "Telefono" non è stato compilato correttamente!' : 'The "Phone" has not been completed correctly!');
				form.telefono.focus();
				return false;
			}
			if (form.email) {
				var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
				var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
				var reg1 = new RegExp(reg1str);
				var reg2 = new RegExp(reg2str);
				if ($.trim(form.email.value) == '' || (!reg1.test(form.email.value) && !reg2.test(form.email.value))) {
					alert(ita ? 'Il campo "E-Mail" non è stato compilato correttamente!' : 'The "E-Mail" has not been completed correctly!');
					form.email.focus();
					return false;
				}
			}
			if ($.trim(form.richiesta.value) == '') {
				alert(ita ? 'Il campo "Richiesta" non è stato compilato correttamente!' : 'The "Richiesta" has not been completed correctly!');
				form.richiesta.focus();
				return false;
			}
			if (!form.autorizzazione.checked) {
				alert('Devi ancora dare l\'autorizzazione al trattamento dei dati personali!');
				return false;
			}
			
			form.action = '/sm.php?c='+form.sF.value;
			form.method = 'POST';
			form.submit();
	
			return false;
		},
		login: function(form, ita) {
			if (form.user && $.trim(form.user.value) == '') {
				alert(ita ? 'Il campo "User" non è stato compilato correttamente!' : 'The "User" has not been completed correctly!');
				form.user.focus();
				return false;
			}
			if (form.password && $.trim(form.password.value) == '') {
				alert(ita ? 'Il campo "Password" non è stato compilato correttamente!' : 'The "Password" has not been completed correctly!');
				form.password.focus();
				return false;
			}
			
			form.action = './login.do?c='+form.sF.value;
			form.method = 'POST';
			form.submit();
	
			return false;
		}
	}
}