function Init() {
	// controllo browser / explorer
	BrowserDetect.init();
	if (BrowserDetect.browser == "Explorer") {
		// avviso explorer
		setTimeout (ApriBarra, 500);
	}
	
	// oggetti e variabili
	form = document.getElementById("myForm");
	preventivo = document.getElementById("div-preventivo");
	barraBrowser = document.getElementById("div-barra");
	boxGallery = document.getElementById('div-gallery-box-interna');
		
	// nascondo i menu di selezione
	document.getElementById("tipologiaProgettoGrafico").style.display = "none";
	document.getElementById("tipologiaProgettoWeb").style.display = "none";
	document.getElementById("tempisticaProgetto").style.display = "none";
	document.getElementById("dimensioniProgettoWeb").style.display = "none";		
	document.getElementById("mantenimentoProgettoWeb").style.display = "none";
	document.getElementById("hostingProgettoWeb").style.display = "none";
	
	// gallery
	totImg = 20;
	counterImg = 0;
	velGallerySingola = 600;
	velGalleryTotale = 2000;
	boxGallery.style.width = (totImg * 900) + "px";
	boxGallery.style.left = ((window.innerWidth - 900) / 2) - (900 * counterImg) + "px";
	// illumino la prima img e oscuro le altre - e i pallini
	for (var i=1; i<=totImg; i++) {
		$(document.getElementById("i"+i)).animate({ opacity:0.2 }, 0);
		$(document.getElementById("p"+i)).animate({ opacity:0.4 }, 0);
	}
	$(document.getElementById("i1")).animate({ opacity:1 }, 0);
	$(document.getElementById("p1")).animate({ opacity:0.8 }, 0);

	// segnalo quali pallini sono spenti o accesi
	for (var i=1; i<=totImg; i++) {
		document.getElementById("p" + i).acceso = false;	
	}
	document.getElementById("p1").acceso = true;
}


function OnResize() {
	boxGallery.style.left = ((window.innerWidth - 900) / 2) - (900 * counterImg) + "px";
}


function ApriBarra() {
	$(barraBrowser).slideDown(400);
}


function ChiudiBarra() {
	$(barraBrowser).slideUp(400);
}



// -------- ROLLOVER SAGOME -------- //
function RollSagoma1(stato) {
	switch (stato) {
		case "over":
			$(document.getElementById("div-fumetto-1")).stop().animate({opacity:1, top:"-40px", left:"0px"}, 200);
			break;
		case "out":
			$(document.getElementById("div-fumetto-1")).stop().animate({opacity:0, top:"-30px", left:"10px"}, 200);
			break;
	}
}
function RollSagoma2(stato) {
	switch (stato) {
		case "over":
			$(document.getElementById("div-fumetto-2")).stop().animate({opacity:1, top:"-20px", left:"270px"}, 200);
			break;
		case "out":
			$(document.getElementById("div-fumetto-2")).stop().animate({opacity:0, top:"-10px", left:"280px"}, 200);
			break;
	}
}
function RollSagoma3(stato) {
	switch (stato) {
		case "over":
			$(document.getElementById("div-fumetto-3")).stop().animate({opacity:1, top:"-30px", left:"510px"}, 200);
			break;
		case "out":
			$(document.getElementById("div-fumetto-3")).stop().animate({opacity:0, top:"-20px", left:"520px"}, 200);
			break;
	}
}
function RollSagoma4(stato) {
	switch (stato) {
		case "over":
			$(document.getElementById("div-fumetto-4")).stop().animate({opacity:1, top:"-40px", left:"750px"}, 200);
			break;
		case "out":
			$(document.getElementById("div-fumetto-4")).stop().animate({opacity:0, top:"-30px", left:"760px"}, 200);
			break;
	}
}
// ---------------------------------- //



// -------- POP OUT POP IN SAGOME -------- //
function PopSagoma(oggetto, evento) {
	var sagoma = document.getElementById(oggetto);
	switch (evento) {
		case "up":
			$(sagoma).animate({top:"-280px"}, 300);
			break;
		
		case "down":
			$(sagoma).animate({top:"0"}, 300);
			break;
	}
}
// ---------------------------------- //




function ApriPreventivo() {
	$(preventivo).slideDown(500);
	$(document.getElementById("div-testi-container")).animate({height:"870px"}, 500);
	$(document.getElementById("div-gallery-container")).animate({top:"430px"}, 500);
	$(document.getElementById("div-footer")).animate({top:"430px"}, 500);
}


function ChiudiPreventivo() {
	$(preventivo).slideUp(500);
	$(document.getElementById("div-testi-container")).animate({height:"450px"}, 500);
	$(document.getElementById("div-gallery-container")).animate({top:"0"}, 500);
	$(document.getElementById("div-footer")).animate({top:"0"}, 500);
	
	form.nome.value = "";
	form.email.value = "";
	form.telefono.value = "";
	form.descrizione.value = "";
	
	form.progetto[0].selected = "selected";
	// nascondo
	document.getElementById("tipologiaProgettoGrafico").style.display = "none";
	document.getElementById("tipologiaProgettoWeb").style.display = "none";
	document.getElementById("tempisticaProgetto").style.display = "none";
	document.getElementById("dimensioniProgettoWeb").style.display = "none";
	document.getElementById("mantenimentoProgettoWeb").style.display = "none";
	document.getElementById("hostingProgettoWeb").style.display = "none";
	// azzero
	form.tipologiaProgettoGrafico[0].selected = "selected";
	form.tipologiaProgettoWeb[0].selected = "selected";
	form.tempisticaProgetto[0].selected = "selected";
	form.dimensioniProgettoWeb[0].selected = "selected";
	form.mantenimentoProgettoWeb[0].selected = "selected";
	form.hostingProgettoWeb[0].selected = "selected";
}


function AggiornaForm() {
	if (form.progetto.value == "--") { //-- progetto
		// nascondo
		document.getElementById("tipologiaProgettoGrafico").style.display = "none";
		document.getElementById("tempisticaProgetto").style.display = "none";
		document.getElementById("tipologiaProgettoWeb").style.display = "none";
		document.getElementById("dimensioniProgettoWeb").style.display = "none";
		document.getElementById("mantenimentoProgettoWeb").style.display = "none";
		document.getElementById("hostingProgettoWeb").style.display = "none"
		// azzero
		form.tipologiaProgettoGrafico[0].selected = "selected";
		form.tipologiaProgettoWeb[0].selected = "selected";
		form.tempisticaProgetto[0].selected = "selected";
		form.dimensioniProgettoWeb[0].selected = "selected";
		form.mantenimentoProgettoWeb[0].selected = "selected";
		form.hostingProgettoWeb[0].selected = "selected";
	}
	
	if (form.tipologiaProgettoWeb.value == "--" && form.progetto.value != "--") { //-- tipologia progetto web
		// nascondo
		document.getElementById("tempisticaProgetto").style.display = "none";
		document.getElementById("dimensioniProgettoWeb").style.display = "none";
		document.getElementById("mantenimentoProgettoWeb").style.display = "none";
		document.getElementById("hostingProgettoWeb").style.display = "none";
		// azzero
		form.tempisticaProgetto[0].selected = "selected";
		form.dimensioniProgettoWeb[0].selected = "selected";
		form.mantenimentoProgettoWeb[0].selected = "selected";
		form.hostingProgettoWeb[0].selected = "selected";
	}
	
	if (form.progetto.value == "Grafica Applicata") { // GRAFICA
		// mostro
		document.getElementById("tipologiaProgettoGrafico").style.display = "block";
		document.getElementById("tempisticaProgetto").style.display = "block";
		
		// nascondo
		document.getElementById("tipologiaProgettoWeb").style.display = "none";
		document.getElementById("dimensioniProgettoWeb").style.display = "none";
		document.getElementById("mantenimentoProgettoWeb").style.display = "none";
		document.getElementById("hostingProgettoWeb").style.display = "none";
		// azzero
		form.tipologiaProgettoWeb[0].selected = "selected";
		form.tempisticaProgetto[0].selected = "selected";
		form.dimensioniProgettoWeb[0].selected = "selected";
		form.mantenimentoProgettoWeb[0].selected = "selected";
		form.hostingProgettoWeb[0].selected = "selected";
	}
	
	if (form.progetto.value == "Web Design / User Interface") { // WEB
		// mostro
		document.getElementById("tipologiaProgettoWeb").style.display = "block";
		// nascondo
		document.getElementById("tipologiaProgettoGrafico").style.display = "none";
		document.getElementById("tempisticaProgetto").style.display = "none";
		// azzero
		form.tipologiaProgettoGrafico[0].selected = "selected";
		form.tempisticaProgetto[0].selected = "selected";
	}
	
	if (form.tipologiaProgettoWeb.value == "Nuovo Sito Web" || form.tipologiaProgettoWeb.value == "Restyling Sito Web Esistente") { // NUOVO - RESTYLING
		//mostro
		document.getElementById("tempisticaProgetto").style.display = "block";
		document.getElementById("dimensioniProgettoWeb").style.display = "block";
		document.getElementById("mantenimentoProgettoWeb").style.display = "block";
		document.getElementById("hostingProgettoWeb").style.display = "block";
	}
	
	if (form.tipologiaProgettoWeb.value == "User Interface" || form.tipologiaProgettoWeb.value == "Altro") { // USER INTERFACE E ALTRO
		// mostro
		document.getElementById("tempisticaProgetto").style.display = "block";
		// nascondo
		document.getElementById("tipologiaProgettoGrafico").style.display = "none";
		document.getElementById("dimensioniProgettoWeb").style.display = "none";
		document.getElementById("mantenimentoProgettoWeb").style.display = "none";
		document.getElementById("hostingProgettoWeb").style.display = "none";
		// azzero
		form.tipologiaProgettoGrafico[0].selected = "selected";
		form.tempisticaProgetto[0].selected = "selected";
		form.dimensioniProgettoWeb[0].selected = "selected";
		form.mantenimentoProgettoWeb[0].selected = "selected";
		form.hostingProgettoWeb[0].selected = "selected";
	}
	
	if (form.progetto.value == "Ho dell'altro in mente") { // ALTRE IDEE
		// mostro
		document.getElementById("tempisticaProgetto").style.display = "block";
		
		// nascondo
		document.getElementById("tipologiaProgettoGrafico").style.display = "none";
		document.getElementById("tipologiaProgettoWeb").style.display = "none";
		document.getElementById("dimensioniProgettoWeb").style.display = "none";
		document.getElementById("mantenimentoProgettoWeb").style.display = "none";
		document.getElementById("hostingProgettoWeb").style.display = "none";
		// azzero
		form.tipologiaProgettoGrafico[0].selected = "selected";
		form.tipologiaProgettoWeb[0].selected = "selected";
		form.tempisticaProgetto[0].selected = "selected";
		form.dimensioniProgettoWeb[0].selected = "selected";
		form.mantenimentoProgettoWeb[0].selected = "selected";
		form.hostingProgettoWeb[0].selected = "selected";
	}
}


function CheckForm(lingua) {
	if (lingua == "italiano") var avviso = "Tutti i campi sono obbgligatori";
	else if (lingua == "inglese") avviso = "All fields are required"
	if (form.nome.value == "" || form.email.value == "" || form.telefono.value == "" || form.progetto.value == "--" || form.descrizione.value == "") {
		alert (avviso);
		return false;
	}
	
	if (form.progetto.value == "Grafica Applicata") { // GRAFICA
		if (form.tipologiaProgettoGrafico.value == "--" || form.tempisticaProgetto.value == "--") {
			alert (avviso);
			return false;
		}
	}
	
	if (form.progetto.value == "Web Design / User Interface") { // WEB
		if (form.tipologiaProgettoWeb.value == "--") {
			alert (avviso);
			return false;
		} else if (form.tipologiaProgettoWeb.value == "User Interface") {
			if (form.tempisticaProgetto.value == "--") {
				alert (avviso);
				return false;
			}
		} else if (form.tipologiaProgettoWeb.value == "Altro") {
			if (form.tempisticaProgetto.value == "--") {
				alert (avviso);
				return false;
			}
		}
	}
	
	if (form.progetto.value == "Ho dell'altro in mente") { // HO DELL'ALTRO IN MENTE
		if (form.tempisticaProgetto.value == "--") {
			alert (avviso);
			return false;
		}
	}
	
	if (form.tipologiaProgettoWeb.value != "--" && form.tipologiaProgettoWeb.value != "User Interface" && form.tipologiaProgettoWeb.value != "Altro") {
		if (form.dimensioniProgettoWeb.value == "--" || form.mantenimentoProgettoWeb.value == "--" || form.tempisticaProgetto.value == "--" || form.hostingProgettoWeb.value == "--" ) {
			alert (avviso);
			return false;
		}
	}

	SendMail();
	return false;
}


function SendMail() {
	var descrizione = form.descrizione.value.replace(/\n/g,'<br />');
	descrizione = encodeURIComponent(descrizione);
	var progetto = encodeURIComponent(form.progetto.value);
	
	var parametri = "nome=" + form.nome.value;
	
	parametri += "&email=" + form.email.value;
	parametri += "&telefono=" + form.telefono.value;
	parametri += "&progetto=" + progetto;
	parametri += "&descrizione=" + descrizione;
	
	if (form.tipologiaProgettoGrafico.value != "--") {
		parametri += "&tipologia=" + form.tipologiaProgettoGrafico.value;
	}
	
	if (form.tipologiaProgettoWeb.value != "--") {
		parametri += "&tipologia=" + form.tipologiaProgettoWeb.value;
	}
	
	if (form.tempisticaProgetto.value != "--") {
		parametri += "&tempistica=" + form.tempisticaProgetto.value;	
	}
	
	if (form.dimensioniProgettoWeb.value != "--") {
		parametri += "&dimensioni=" + form.dimensioniProgettoWeb.value;
	}
	
	if (form.mantenimentoProgettoWeb.value != "--") {
		parametri += "&mantenimento=" + form.mantenimentoProgettoWeb.value;
	}
	
	if (form.hostingProgettoWeb.value != "--") {
		parametri += "&hosting=" + form.hostingProgettoWeb.value;
	}
			
	$.get ( form.action, parametri, function(data) { ajaxSuccess(data); } );

	function ajaxSuccess (data) {
		if (data == "positivo") {
			alert ("Richiesta di preventivo inviata con successo.\nSarai ricontattato al più presto");
			ChiudiPreventivo();
		} else {
			alert ("Errore nell'invio del preventivo per possibili problemi al server.\nSe l'errore persiste, contattaci direttamente a hello@fishcanfly.com");
		}
	}
}



function GoGallery (direzione, numero) {
	// SPOSTAMENTO CON FRECCE
	if (direzione != null) {
		switch (direzione) {
			case "sx":
				if (counterImg > 0) {
					counterImg --;
					for (var i=1; i<=totImg; i++) {
						var imgAttuale = document.getElementById("i" + i);
						if (imgAttuale.id.substr(1) != (counterImg + 1)) {
							$(imgAttuale).animate({ opacity:0.2 }, velGallerySingola);
						} else {
							$(imgAttuale).animate({ opacity:1 }, velGallerySingola);
						}
					}

					$(boxGallery).animate({left:"+=900px"}, velGallerySingola, "easeInOut");
				} else if (counterImg == 0) {
					counterImg = totImg - 1;
					for (var i=1; i<=totImg; i++) {
						$(document.getElementById("i"+i)).animate({ opacity:0.2 }, velGallerySingola);
					}
					$(document.getElementById("i"+totImg)).animate({ opacity:1 }, velGallerySingola);
					
					var posizioneGallery = ((window.innerWidth - 900) / 2) - (900 * counterImg) + "px";
					$(boxGallery).animate({left: posizioneGallery}, velGalleryTotale, "easeInOut");
				}
				for (var i=1; i<=totImg; i++) {
					if (i != counterImg + 1) {
						document.getElementById("p" + i).acceso = false;
						$(document.getElementById("p"+i)).animate({ opacity:0.4 }, 100);
					} else {
						document.getElementById("p" + i).acceso = true;
						$(document.getElementById("p"+i)).animate({ opacity:0.8 }, 100);
					}
				}
				break;
			case "dx":
				if (counterImg < totImg - 1) {
					counterImg ++;
					for (var i=1; i<=totImg; i++) {
						var imgAttuale = document.getElementById("i" + i);
						if (imgAttuale.id.substr(1) != (counterImg + 1)) {
							$(imgAttuale).animate({ opacity:0.2 }, velGallerySingola);
						} else {
							$(imgAttuale).animate({ opacity:1 }, velGallerySingola);
						}
					}
					$(boxGallery).animate({left:"-=900px"}, velGallerySingola, "easeInOut");
				} else if (counterImg == totImg -1) {
					counterImg = 0;
					for (var i=1; i<=totImg; i++) {
						$(document.getElementById("i"+i)).animate({ opacity:0.2 }, velGallerySingola);
					}
					$(document.getElementById("i1")).animate({ opacity:1 }, velGallerySingola);
					
					var posizioneGallery = ((window.innerWidth - 900) / 2) - (900 * counterImg) + "px";
					$(boxGallery).animate({left: posizioneGallery}, velGalleryTotale, "easeInOut");
				}
				for (var i=1; i<=totImg; i++) {
					if (i != counterImg + 1) {
						document.getElementById("p" + i).acceso = false;
						$(document.getElementById("p"+i)).animate({ opacity:0.4 }, 100);
					} else {
						document.getElementById("p" + i).acceso = true;
						$(document.getElementById("p"+i)).animate({ opacity:0.8 }, 100);
					}
				}
		}
	// SPOSTAMENTO CON PALLINI
	} else if (numero != null) {
		counterImg = numero - 1;
		var posizioneGallery = ((window.innerWidth - 900) / 2) - (900 * counterImg) + "px";
		$(boxGallery).stop().animate({left: posizioneGallery}, velGallerySingola, "easeInOut");
		for (var i=1; i<=totImg; i++) {
			if (i != numero) {
				document.getElementById("p" + i).acceso = false;
				$(document.getElementById("p"+i)).animate({ opacity:0.4 }, 100);
				var imgAttuale = document.getElementById("i"+ i);
				$(imgAttuale).animate({ opacity:0.2 }, velGallerySingola);
			} else { 
				document.getElementById("p" + numero).acceso = true;
				$(document.getElementById("p"+i)).animate({ opacity:0.8 }, 100);
				var imgAttuale = document.getElementById("i"+ i);
				$(imgAttuale).animate({ opacity:1 }, velGallerySingola);
			}
		}
	}
}


function RollPallini (pallino, stato) {
	if (document.getElementById(pallino).acceso == false) {
		switch (stato) {
			case "rollover":
				$(document.getElementById(pallino)).stop().animate({opacity:0.8}, 100);
				break;
			case "rollout":
				$(document.getElementById(pallino)).stop().animate({opacity:0.4}, 100);
				break;
		}
	}
}



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];}
}

