/**
 * @author stefano
 */

// Variabile che contiene il codice HTML per il popup di errore
var dialogErrore = ''+
'<div id="dialog-errore" title="Errore riscontrato" class="ui-state-error ui-corner-all" style="display: none">'+
'	<p>'+
'		<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 50px 0;"></span>'+
'		<span id="messaggio"></span>'+
'	</p>'+
'</div>';

// Variabile che contiene il codice HTML per il popup di OK
var dialogAlert = ''+
'<div id="dialog-alert" title="Errore riscontrato" style="display: none">'+
'	<p>'+
'		<span id="icona" class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 50px 0;"></span>'+
'		<span id="messaggio"></span>'+
'	</p>'+
'</div>';

$(document).ready( function () {
	$("#buttonBar a").each(function(i) {
		$(this).hover(
			function(){
				$(this).removeClass("ui-state-default ui-corner-all");
				$(this).addClass("ui-state-hover ui-corner-all");
			},
			function(){
				$(this).removeClass("ui-state-hover ui-corner-all");
				$(this).addClass("ui-state-default ui-corner-all");
			}
		);
	});
	 
	try {
		if(altPag != undefined) {
			$("#content").height(altPag);
		}
	}
	catch (e) {}
	
	$("#dataOra").load(home + "assets/dataOra.html", function() {
		setTimeout(function(){    
			$("#middle").equalHeights();
		}, 500);
	});
});

function equalizza(altezza) {
	if (!altezza) {
		$("#middle").equalHeights();
	}
	else {
		try {
			$("#content").height($("#content").height() + altezza);
			$("#middle").equalHeights();
		} 
		catch (e) {}
	}
}

function estensione(valore, estensioni, inverso) 
{
	if (inverso) {
		var ris = true;
		
		if (valore == "") {
			return false;
		}
		
		for (var i = 0; i < estensioni.length; i++) {
			if (valore.toLowerCase().lastIndexOf(estensioni[i].toLowerCase()) != -1) {
				ris = false;
			}
		}
		
		if (!ris) {
			alert("ATTENZIONE:\nNon è consentito caricare questo tipo di file!");
		}
		return ris;
	}
	else {
		var ris = false;
		
		if (valore == "") {
			return false;
		}
		
		for (var i = 0; i < estensioni.length; i++) {
			if (valore.toLowerCase().lastIndexOf(estensioni[i].toLowerCase()) != -1) {
				ris = true;
			}
		}
		
		if (!ris) {
			alert("ATTENZIONE:\nNon è consentito caricare questo tipo di file!");
		}
		return ris;
	}
}

function throwErrore(messaggio, titolo) 
{
	if(!titolo)
		titolo = "Errore riscontrato";
	
	if ($('body').find("#dialog-errore").length > 0) {
		$("#dialog-errore").find("#messaggio").html("<b>ATTENZIONE:</b><br /><br />" + messaggio);
		$("#dialog-errore").dialog( 'option' , 'title' , titolo );
		$("#dialog-errore").dialog('open');
	}
	else {
		$('body').append(dialogErrore);
		
		$("#dialog-errore").find("#messaggio").html("<b>ATTENZIONE:</b><br /><br />" + messaggio);
		
		$("#dialog-errore").dialog({
			modal: true,
			title: titolo,
			width: 400,
			buttons: {
				'Ok': function(){
					$("#dialog-errore").dialog("close");
				}
			}
		});
	}
}

function throwAlert(messaggio, titolo, icona, callback) 
{
	if(!titolo)
		titolo = "Informazione";
	if(!icona)
		icona = "circle-check";
		
	if ($('body').find("#dialog-alert").length > 0) {
		$("#dialog-alert").find("#messaggio").html(messaggio);
		$("#dialog-alert").dialog( 'option' , 'title' , titolo );
		$("#dialog-alert").dialog('open');
	}
	else {
		$('body').append(dialogAlert);
		
		$("#dialog-alert").find("#messaggio").html(messaggio);
		
		$("#dialog-alert").dialog({
			modal: true,
			title: titolo,
			width: 400,
			buttons: {
				'Ok': function(){
					$("#dialog-alert").dialog("close");
					if(callback)
						callback();
				}
			}
		});
	}
	
	$("#dialog-alert").find("#icona").toggleClass("ui-icon-" + icona);
}

/*-----------------------------------------------------------*/
//	FUNZIONE CHE CONTROLLA IL CARATTERE DIGITATO DA TASTIERA //
/*-----------------------------------------------------------*/

function getkey(e)
{
	if (window.event)
	   return window.event.keyCode;
	else if (e)
	   return e.which;
	else
	   return null;
}

/*------------------------------------------------------*/
//	FUNZIONE CHE CONTROLLA SE IL CARATTERE DIGITATO		//
//	E' VALIDO PER IL CAMPO NEL QUALE E' STATO DIGITATO  //
/*------------------------------------------------------*/

function goodchars(e, goods)
{
	var key, keychar;
	key = getkey(e);
	
	if (key == null) return true;
	
	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();
	
	// check goodkeys
	if (goods.indexOf(keychar) != -1)
		return true;
	
	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==27 )
	   return true;
	
	// else return false
	return false;
}

/**
 * Funzione che apre il link corrispondente al bottone premuto nell'animazione di HOMEPAGE
 * @param {string} nome - il nome del bottone premuto
 */
function apriLink(nome) 
{
	var indice = '';

	switch(nome) {
		case 'mondoe':
			window.location = "prodotti/index.php?read=1";
			break;
		
		case 'steritrack':
			window.location = "prodotti/index.php?read=2"
			break;
		
		case 'ephone':
			window.location = "prodotti/index.php?read=3"
			break;
		
		case 'frantoi':
			window.location = "prodotti/index.php?read=4"
			break;
		
		case 'rottama':
			window.location = "prodotti/index.php?esa=documentale";//"iniziative/index.php?read=15";
			break;			
	}
}

