/* TESTATE IMMAGINI */
var refreshinterval = 5000;
var fadeimages = new Array();
//fadeimages[0] = ["/img/testata1.jpg", "/", ""];
//fadeimages[1] = ["/img/testata2.jpg", "/", ""];
fadeimages[0] = ["/img/testata3.jpg", "/", ""];
fadeimages[1] = ["/img/testata4.jpg", "/", ""];
fadeimages[2] = ["/img/testata5.jpg", "/", ""];
fadeimages[3] = ["/img/testata6.jpg", "/", ""];
fadeimages[4] = ["/img/testata7.jpg", "/", ""];
fadeimages[5] = ["/img/testata8.jpg", "/", ""];
/* TESTATE IMMAGINI */

/* PRELOAD IMAGES */
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_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_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];}
}
/* PRELOAD IMAGES */



function mmenu(mID) {
	var menu = document.getElementById(mID);
	var display = menu.style.display;
	menu.style.display = (display == "block")?"none":"block";
}

function checkForm(formname){
	formname = document.getElementById(formname);
	countbox = 0;
	boxchecked = 1;
	for(i=0; i<formname.length; i++){
		if(formname.elements[i].type == "text" || formname.elements[i].type == "textarea"){
			css = formname.elements[i].className;
			if(css.indexOf('required') != -1){
				if(formname.elements[i].value == ""){
					alert("il campo "+formname.elements[i].id+" è vuoto.");
					return false;
				}
			}
		}
		if(formname.elements[i].type == "select-one"){
			css = formname.elements[i].className;
			if(css.indexOf('required') != -1){
				if(formname.elements[i].options[formname.elements[i].selectedIndex].value == ""){
					alert("il campo "+formname.elements[i].id+" è vuoto.");
					return false;
				}
			}
		}
		if(formname.elements[i].type == "radio"){
			if(formname.elements[i].className == "required"){
				radiochecked = 0;
				radiogroup = formname.elements[formname.elements[i].name];				
				for(j = 0; j < radiogroup.length; j++){
					if(radiogroup[j].checked){
						radiochecked = 1;
					}
				}
				if(radiochecked == 0) {
					alert("il campo "+formname.elements[i].name+" è vuoto.");
					return false;
				}
			}
		}
		if(formname.elements[i].type == "checkbox"){
			if(formname.elements[i].className == "required"){
				if(countbox == 0){
					countbox = 1;
					boxchecked = 0;
				}
				if(formname.elements[i].checked){
					boxchecked = 1;
				}
			}
		}
		if(formname.elements[i].id == "email"){
			css = formname.elements[i].className;
			if(css.indexOf('required') != -1){
				if(checkEmail(formname.elements[i].value) == false){
					alert("il campo "+formname.elements[i].id+" non è un indirizzo di posta valido.");
					return false;
				}
			}
		}
	}
	if(boxchecked == 0){
		alert("Alcuni campi sono stati lasciati vuoti.");
		return false;
	}
	return true;
}

function checkEmail(email){
	var str = Trim(email);
	var val_mail = "^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$";
	var link_reg_exp = new RegExp(val_mail);
	if(str != ""){
		if(link_reg_exp.test(str)){
			return true;
		} else{
			return false;
		}
	}
}

function Trim(field) { 
return field.replace(/\s+$|^\s+/g,""); 
}

function ApriImmagini(file, w, h){
	//w = w + 20;
	//h = h + 20;
	var prop = "width=" + w + ", height=" + h + ", top=100, left=100"; // Altre proprietà... eccetto width ed height!
	window.open(file, null, prop);
}

function formSend(frm) {
	if(checkForm(frm)){
		formname = document.getElementById(frm);
		formname.submit();
	}
}


function switchDivGraph(origin) {
	var orig = document.getElementById(origin);
	var dest = document.getElementById('graficiSfondo');
	dest.innerHTML = orig.innerHTML;
	switchLabelGraph(origin);
}

function switchLabelGraph(label) {
	var labelsDiv = document.getElementById('graficiMenu');
	var labels = labelsDiv.getElementsByTagName('a');
	for( var i = 0; i < labels.length; i++ ) {
	if ( labels[i].id == 'grafico-' + label ) {
			labelsDiv.style.backgroundImage = 'url(/img/graficiMenu_' + label + '.jpg)';
		}
	}
}

function showFaqOnLoad(){
	if(window.location.hash != ''){
		if(window.location.hash.indexOf('#faq-') > -1) {
			id = window.location.hash.replace('#faq-', "");
			if(document.getElementById('submenu-' + id))
				document.getElementById('submenu-' + id).style.display = 'block';
		}
	}
}

function showGlossarioOnLoad(){
	if(window.location.hash != ''){
		if(window.location.hash.indexOf('#glossario-') > -1) {
			id = window.location.hash.replace('#glossario-', "");
			if(document.getElementById('submenu-' + id))
				document.getElementById('submenu-' + id).style.display = 'block';
		}
	}
}

shallowCopy = new Array();
function myScrollbar(id, obj){
	this.id = id;
	this.width = obj.width;
	this.height = obj.height;
	this.speed = obj.speed;
	
	// imposta altezza e larghezza del div di contenuto
	$(this.id).style.width = this.width + 'px';
	$(this.id).style.height = this.height + 'px';
	
	// HTML scroller
	var html = '<div id="scrollerBar_' + id + '" class="myScollerBar" style="visibility: hidden; float: right; width: 15px; height: ' + this.height + 'px; padding: 0px;">';
	html += '<span id="up_' + id + '" class="myScrollerUp" style="display: block; width: 100%; height: 15px; overflow: hidden;"><span></span></span>';
	html += '<span id="trace_' + id + '" class="myScrollerTrace" style="display: block; position: relative; overflow: hidden; width: 100%; padding: 0px; height: ' + (this.height - 34) + 'px;"><span id="cursor_' + id + '" class="myScrollerCursor" style="display: block; width: 100%; padding: 0px; position: absolute; top: 0px; left: 0px;"><span>&nbsp;</span></span></span>';
	html += '<span id="down_' + id + '" class="myScrollerDown" style="display: block; width: 100%; height: 15px; overflow: hidden;"><span></span></span>';
	html += '</div>';
	
	// HTML container
	html += '<div class="myScrollerContainer" id="container_' + id + '" style="position: relative; width: ' + (this.width - 22) + 'px; height: ' + this.height + 'px; overflow: hidden; padding: 0px; float: left;">';
	html += '<div class="myScrollerContent" id="content_' + id + '" style="position: absolute; width: ' + (this.width - 22) + 'px; left: 0px; top: 0px; padding: 0px;">';
	html += $(this.id).innerHTML;
	html += '</div></div><br style="clear: both;" />';
	
	$(this.id).innerHTML = html; // inserisco tutto nel div di contenuto
	
	
	this.objContainer = $("container_" + id);
	this.objContent = $("content_" + id);
	this.objScroller = $("scrollerBar_" + id);
	this.objUp = $("up_" + id);
	this.objDown = $("down_" + id);
	this.objTrace = $("trace_" + id);
	this.objCursor = $("cursor_" + id);
	
	this.containerHeight = parseInt(this.objContainer.style.height);
	this.contentHeight = this.objContent.offsetHeight;
	
	if(this.contentHeight > this.containerHeight){
		this.traceHeight = parseInt(this.objTrace.style.height); // altezza scrollbar
		this.cursorHeight = Math.ceil((this.traceHeight * this.containerHeight) / this.contentHeight); // altezza cursore
		this.traceVoid = this.traceHeight - this.cursorHeight; // differenza tra altezza scrollbar e altezza cursore
		this.objCursor.style.height = this.cursorHeight + 'px'; // imposta altezza cursore
		
		this.objCursor.style.top = '0px';
		this.objScroller.style.visibility = 'visible';
		
		Drag.init(this.objCursor, null, 0, 0, 0, this.traceVoid); // imposto il drag del cursore
		
		this.t;
		
		shallowCopy[id] = Object.clone(this); // clone dell'oggetto
		
		// Events
		this.objUp.onmouseover = function(){
			shallowCopy[id].moveUp(shallowCopy[id]);
		}
		
		this.objDown.onmouseover = function(){
			shallowCopy[id].moveDown(shallowCopy[id]);
		}
		
		this.objUp.onmouseout = function(){
			clearTimeout(shallowCopy[id].t);
		}
		
		this.objDown.onmouseout = function(){
			clearTimeout(shallowCopy[id].t);
		}
		
		this.objCursor.onDrag = function(x,y) {
			var scrollY = parseInt(shallowCopy[id].objCursor.style.top);
			var docY = 0 - (scrollY * (shallowCopy[id].contentHeight - shallowCopy[id].containerHeight) / shallowCopy[id].traceVoid);
			shallowCopy[id].objContent.style.top = docY + "px";
		}
	}
}

myScrollbar.prototype.moveDown = function(objCopy){
	if(parseInt(objCopy.objContent.style.top) >= (objCopy.contentHeight * (-1) + objCopy.containerHeight)){
		var delTop = parseInt(objCopy.objContent.style.top) - parseInt(objCopy.speed);
		objCopy.objContent.style.top = delTop + "px";
		var addTop = (((0 - delTop) * objCopy.traceVoid) / (objCopy.contentHeight - objCopy.containerHeight));
		objCopy.objCursor.style.top = addTop + 'px';
		
	}
	objCopy.t = setTimeout(function(){ objCopy.moveDown(objCopy); }, 20);
}

myScrollbar.prototype.moveUp = function(objCopy){
	if(parseInt(objCopy.objContent.style.top) <= 0){
		var addTop = parseInt(objCopy.objContent.style.top) + parseInt(objCopy.speed);
		objCopy.objContent.style.top = addTop + "px";
		var delTop = (((0 - addTop) * objCopy.traceVoid) / (objCopy.contentHeight - objCopy.containerHeight));
		objCopy.objCursor.style.top = delTop + 'px';
	}
	objCopy.t = setTimeout(function(){ objCopy.moveUp(objCopy); }, 20);
}

function mngContatti(id){
    com = 'comunica2_' + id;
	
	layersID = new Array('telefono','fax','emails','posta');
	
	for(i = 0; i < layersID.length; i++){
		if(com == 'comunica2_' + layersID[i]) $('comunica2_' + layersID[i]).style.visibility = 'visible';
		else $('comunica2_' + layersID[i]).style.visibility = 'hidden';
		if(id == layersID[i]) $(layersID[i]).style.visibility = 'visible';
		else $(layersID[i]).style.visibility = 'hidden';
	}
}