function mmenu(mID) {
	var menu = document.getElementById(mID);
	var display = menu.style.display;
	menu.style.display = (display == "block")?"none":"block";
}

/* Apertura pop-up */
function openPopup(url, name, w, h, s){
	var prop = "width=" + w + ", height=" + h + ", scrollbars=" + s + ", top=100, left=100";
	window.open(url, name, prop);
}

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';
		}
	}
}

/* Prototype Ajax */
function setAjaxProto(method, fload, ferror, formId){
	bindArgs = new Object();
	method = (method != null)?method:"post";
	bindArgs.method = method;
	if(fload != null) bindArgs.onSuccess = fload;
	if(ferror != null) bindArgs.onFailure = ferror;
	if(formId != null) bindArgs.parameters = Form.serialize($(formId));
	return bindArgs;
}

function setRespProto(fcreate, fcomplete){
	bindArgs = new Object();
	if(fcreate != null) bindArgs.onCreate = fcreate;
	if(fcomplete != null) bindArgs.onComplete = fcomplete;
	return bindArgs;
}

/* mia funzione */
function myAjaxFunc(url){
	if(url == null) return;
	params = setAjaxProto('get', fload, ferror, null);
	var req = new Ajax.Request(url, params);
	
	// opzionale
	other = setRespProto(fcreate, fcomplete);
	Ajax.Responders.register(other);
}

/* Tipo fload */
function floadExemple(resp){
	// qualcosa	succede
}

/* Tipo ferror */
function ferrorExemple(error){
	// qualcosa	succede
}

/* Tipo fcreate */
function fcreateExemple(){
	// qualcosa	succede
}

/* Tipo fcomplete */
function fcompleteExemple(){
	if(Ajax.activeRequestCount == 0){
		// qualcosa	succede
	}
}

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){
    
    $('comunica2').style.backgroundImage = 'url(/img/comunica_' + id + '_smock.gif)';
	
	layersID = new Array('telefono','fax','email','posta','reclami');
	
	for(i = 0; i < layersID.length; i++){
		if(id == layersID[i]) $(layersID[i]).style.visibility = 'visible';
		else $(layersID[i]).style.visibility = 'hidden';
	}
}

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)';
		}
	}
}


