// JavaScript Document

// imposto le altezze dei vari elementi
//$(window).load(function (){
	// imposto altezza del contenitore
	//var altezzaContenitorePrincipale = 1080
	//var altezzaContenitorePrincipaleAzienda = 1400

	/*var altezzaMenuSchede = $("#schede").height()
	var altezzaMenuWrapper = $("#menuWrapper").height()
	var altezzaContenitore300px = $("#contenitore300px").height()
	var altezzaContenitoreContenuti = $("#contenitoreContenuti").height()
	if(navigator.appName=='Netscape'){
		var altezzaContenitorePreventiviIn640px = 250
	}
	else{
		var altezzaContenitorePreventiviIn640px = 246
	}
	
	var altezzaContenitorePrincipaleAzienda = parseInt(altezzaContenitoreContenuti)+parseInt(altezzaMenuSchede)+parseInt(altezzaMenuWrapper)-40+10
	
	if(parseInt(altezzaContenitore300px)>parseInt(altezzaContenitoreContenuti)){
		var altezzaContenitorePrincipale = parseInt(altezzaMenuSchede)+parseInt(altezzaMenuWrapper)+parseInt(altezzaContenitore300px)-30
	}
	else {
		// caso nostro
		if(navigator.appName=='Netscape'){
			var altezzaContenitorePrincipale = parseInt(altezzaMenuSchede)+parseInt(altezzaMenuWrapper)+parseInt(altezzaContenitoreContenuti)-80
		}
		else{
			var altezzaContenitorePrincipale = parseInt(altezzaMenuSchede)+parseInt(altezzaMenuWrapper)+parseInt(altezzaContenitoreContenuti)-60
		}
	}
	
	var altezzaContenitoreInformazioniIn640px = parseInt(altezzaContenitorePrincipale)-parseInt(altezzaMenuSchede)-parseInt(altezzaMenuWrapper)-parseInt(altezzaContenitorePreventiviIn640px)
	
	$("#contenitorePrincipale").height(altezzaContenitorePrincipale)
	$("#contenitorePrincipaleAzienda").height(altezzaContenitorePrincipaleAzienda)
	$("#contenitorePreventiviIn640px").height(altezzaContenitorePreventiviIn640px)
	//$(".colonna3da200px").height(altezzaColonna3da200px)
	//$(".colonna3da200pxLast").height(altezzaColonna3da200pxLast)
	//alert($("#contenitorePreventiviIn640px").height());
	//alert("OK"+document.getElementById("contenitorePreventiviIn640px").style.height);
	
	//alert(altezzaContenitoreInformazioniIn640px)
	//alert($("#contenitorePrincipale").height());
	// accoda footer al contenitore
	
	$("#contenitoreInformazioniIn640px").height(altezzaContenitoreInformazioniIn640px)
	$("#contenitoreInformazioniIn640px").css('top',$("#contenitorePreventiviIn640px").height())
	$("#footer").css('top',$("#contenitorePrincipale").height())
	$("#footerAzienda").css('top',$("#contenitorePrincipaleAzienda").height())*/
	
//});
// fine impostazioni altezze

//Scorrimento giu/su
$(function() {
	var $elem = $('#contenitorePrincipale');
	if($elem.height()==null){
		var $elem = $('#contenitorePrincipaleAzienda');
	}
	
	$('#nav_up').fadeIn('slow');
	$('#nav_down').fadeIn('slow');  
	
	$(window).bind('scrollstart', function(){
		$('#nav_up,#nav_down').stop().animate({'opacity':'0.2'});
	});
	$(window).bind('scrollstop', function(){
		$('#nav_up,#nav_down').stop().animate({'opacity':'1'});
	});
	
	$('#nav_down').click(
		function (e) {
			$('html, body').animate({scrollTop: $elem.height()}, 800);
		}
	);
	$('#nav_up').click(
		function (e) {
			$('html, body').animate({scrollTop: '0px'}, 800);
		}
	);
});
// fine scorrimento giu/su

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return 4;
  else
    return results[1];
}

// animazione banner e menu
$(function() {
	var current = -1;
	var loaded  = 0;
	var last = 0;
	
	
	for(var i = 1; i < 6; ++i)
		$('<img />').load(function(){
			++loaded;
			if(loaded == 5){
				
				$('#bg1,#bg2,#bg3,#bg4,#bg5').mouseout(function(){
					
					var $this = $(this);
					var MenuTop = gup('MenuTop')
					
					//alert('ITEM CLICCATO=<'+MenuID+'>')
					$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(200px 0)"},300,function(){
						$(this).find('li').hide();
						last = current;
						current = -1;
						//alert("LAST "+last)
						if(MenuTop!=''){
							var itemCliccato = parseInt(MenuTop)-3;
							
							if(itemCliccato>5){
								itemCliccato=parseInt(MenuTop)-8;
								//alert(itemCliccato);
							}
							//alert(lastItem);
							itemMio = 'bg'+(itemCliccato);
							//alert("ITEMIO "+itemMio)
							
							if(itemMio == 'bg1' || itemMio == 'bg3' || itemMio == 'bg5'){
							//alert(itemMio);
								$('#menu > li').animate({backgroundPosition:"(-1000px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5').addClass(itemMio);									
								move(1,itemMio);
							}
							else{
								$('#menu > li').animate({backgroundPosition:"(1000px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5').addClass(itemMio);
								move(0,itemMio);
							}
						}
					});
				});	
				
				//funzione su mouseover
				$('#bg1,#bg2,#bg3,#bg4,#bg5').mouseover(function(e){
					
					var $this = $(this);
					var item = e.target.id;
					
					if((parseInt(current)+1)==parseInt(item.charAt(2))){
					}
					// gestione dei menu 
					
						$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(-200px 0)"},300,function(){
							$(this).find('li').hide();
							
						});
						
					//alert("CURRENT "+current)
					// gestione delle immagini
					if(item == 'bg1' || item == 'bg3' || item == 'bg5'){
						$('#menu > li').animate({backgroundPosition:"(-1000px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5').addClass(item);									
						if(parseInt(current)==-1){
							//if((parseInt(last)+1)!=parseInt(item.charAt(2))){
								move(1,item);
								//alert("CASO 1")
							//}
							//else
								//alert(parseInt(last)+1+"\n"+item.charAt(2)+"\n"+"CASO 5")
						}
						else{
							if((parseInt(current)+1)!=parseInt(item.charAt(2))){
								move(1,item);
								//alert("CASO 2")
							}
							//else
								//alert("CASO 6")
						}
					}
					else{
						$('#menu > li').animate({backgroundPosition:"(1000px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5').addClass(item);
						if(parseInt(current)==-1){
							//if((parseInt(last)+1)!=parseInt(item.charAt(2))){
								move(0,item);
								//alert("CASO 3")
							//}
						}
						else{
							if((parseInt(current)+1)!=parseInt(item.charAt(2))){
								move(0,item);
								//alert("CASO 4")
							}
						}
					}
					current = $this.parent().index();
					
					$('#menu .sub'+parseInt(current+1)).mouseover(function(e){
						$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(0 0)"},200,function(){
								$(this).find('li').fadeIn();
							});
					});
					$('#menu .sub'+parseInt(current+1)).mouseout(function(e){
						$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(200px 0)"},300,function(){
							$(this).find('li').hide();
							last = current;
							current = -1;
							var MenuTop = gup('MenuTop')
				
							//alert('ITEM CLICCATO=<'+MenuID+'>')
							//alert("LAST "+last)
							if(MenuTop!=''){
								var itemCliccato = parseInt(MenuTop)-3;
								//alert(lastItem);
								itemMio = 'bg'+(itemCliccato)
								//alert("ITEMIO "+itemMio)
								
								if(itemMio == 'bg1' || itemMio == 'bg3' || itemMio == 'bg5'){
								//alert(itemMio);
									$('#menu > li').animate({backgroundPosition:"(-1000px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5').addClass(itemMio);									
									move(1,itemMio);
								}
								else{
									$('#menu > li').animate({backgroundPosition:"(1000px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5').addClass(itemMio);
									move(0,itemMio);
								}
							}
						});
					});
					
					$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(0 0)"},200,function(){
						$(this).find('li').fadeIn();
					});
				});
			}	
		}).attr('src', 'images/'+i+'.jpg');
	
					
		/*
		dir:1 - move left->right
		dir:0 - move right->left
		*/
		
		// immagine di sfondo
		function move(dir,item){
			if(dir){
				//alert(item)
				$('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},200);
				$('#bg2').parent().stop().animate({backgroundPosition:"(-200px 0)"},300);
				$('#bg3').parent().stop().animate({backgroundPosition:"(-400px 0)"},400);
				$('#bg4').parent().stop().animate({backgroundPosition:"(-600px 0)"},500);
				$('#bg5').parent().stop().animate({backgroundPosition:"(-800px 0)"},600,function(){
					$('#menuWrapper').removeClass('bg1 bg2 bg3 bg4 bg5').addClass(item);
				});
			}
			else{
				$('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},600,function(){
					$('#menuWrapper').removeClass('bg1 bg2 bg3 bg4 bg5').addClass(item);
				});
				$('#bg2').parent().stop().animate({backgroundPosition:"(-200px 0)"},500);
				$('#bg3').parent().stop().animate({backgroundPosition:"(-400px 0)"},400);
				$('#bg4').parent().stop().animate({backgroundPosition:"(-600px 0)"},300);
				$('#bg5').parent().stop().animate({backgroundPosition:"(-800px 0)"},200);
			}
		}
	});
// fine animazione banner e menu

// animazione schede
$(function() {
	$('#menu2 > li').hover(
		function () {
			var $this = $(this);
			$('a',$this).stop(true,true).animate({
					'bottom':'-15px'
				}, 300);
			$('i',$this).stop(true,true).animate({
					'top':'-10px'
				}, 400);
		},
		function () {
			var $this = $(this);
			$('a',$this).stop(true,true).animate({
					'bottom':'-35px'
				}, 300);
			$('i',$this).stop(true,true).animate({
					'top':'50px'
				}, 400);
		}
	);
});
// fine animazione schede

//funzione per ridimensionare l'immagine di sfondo in base alla grandezza della finestra

//function resize()
//{
//    var myWidth = 0; 
//    var myHeight = 0;
//    var Width = 1000, Height= 1000;
//    var AspectRatioImg = Width / Height; 
//    
//    var img = document.getElementById("immagineSfondo");
//
//    
//    if( typeof( window.innerWidth ) == 'number' ) 
//    {
//        //Non-IE
//        myWidth = window.innerWidth-20;
//        myHeight = window.innerHeight-20;
//		//alert(myWidth);
//    } 
//    else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
//    {
//        //IE 6+ in 'standards compliant mode'
//        myWidth = document.documentElement.clientWidth;
//        myHeight = document.documentElement.clientHeight;
//    } 
//    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
//    {
//        //IE  compatible
//        myWidth = document.body.clientWidth;
//        myHeight = document.body.clientHeight;
//    }
//
//  var AspectRatioWindow = myWidth / myHeight;
//  
//  if (AspectRatioImg < AspectRatioWindow) {
//  	 img.width = myWidth;
//     img.height = myWidth / AspectRatioImg; 
//  }
//  else{
//     img.width = myHeight * AspectRatioImg;
//     img.height = myHeight;
//  } 
//}

// swap immagini su rollover
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
// fine

function leggiNews(str)
{
	news = window.open (str, "visualizzanews", "width=650, height=650, left=50, top=50, scrollbars=yes");
}

function controllomail(mail)
{	
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;	
	if (!espressione.test(mail))	
		return "La mail inserita non è valida!\n";	
	return '';
}

function ValidaDatiNewsletter() 
{
	var message="";
	var err="";
	var vContattoNome = document.form_news.ContattoNome.value;
	var vContattoIndirizzoEmail = document.form_news.ContattoIndirizzoEmail.value;
	if (vContattoNome=='') {
		message = message + "Il campo nome è obbligatorio:\n";
	}	
	if (vContattoIndirizzoEmail=='') {
		message = message + "Il campo email è obbligatorio:\n";
	}
	if (vContattoIndirizzoEmail != '') 
		err = controllomail(vContattoIndirizzoEmail);
		if (err != '')
			message = message + err; 

	if (message != '') {
		alert(message);
	} else {
		
		document.form_news.submit();
	}	
}

function messaggio(errmsg)
{	
	alert(errmsg);
	document.form_news.ContattoNome.select();
	document.form_news.ContattoNome.focus();
}

function svuotaCampi()
{
	document.getElementById('form_news').ContattoNome.value=''; 
	document.getElementById('form_news').ContattoIndirizzoEmail.value='';
}
