/* author: Variance */
/* Creation date: 09/10/2004 */

function appear_gerarchia(element){
  elemento_img = "piu_"+element
  elemento_div = "div_"+element
  if(document.getElementById(elemento_img)){
		var img_url = document.getElementById(elemento_img).src;
  	if(img_url.indexOf("piu.gif")=="-1"){document.getElementById(elemento_img).src="images/piu.gif"}
  	else{document.getElementById(elemento_img).src="images/meno.gif"}		
	}else{alert('Immagine + non trovata - id cercato '+elemento_img);}

 	appear(elemento_div);
}

function appear(obj){
	if(document.getElementById(obj)){	
	  var oggetto = document.getElementById(obj); 
	  if ((oggetto.style.display=='')||(oggetto.style.display=='none')){
	    document.getElementById(obj).style.display='block';
	  }
	  else{
	    document.getElementById(obj).style.display='none';
	  }
	}else{alert('Elemento da far apparire non trovato - id cercato '+elemento_div);}  
}

function disappear(obj){
	if(document.getElementById(obj)){	
	  var oggetto = document.getElementById(obj); 
	  if ((oggetto.style.display=='')||(oggetto.style.display=='block')){
	    document.getElementById(obj).style.display='none';
	  }
	  else{
	    document.getElementById(obj).style.display='block';
	  }
	}else{alert('Elemento da far apparire non trovato - id cercato '+elemento_div);}  
}

/* END Funzioni JS modificate e ottimizzate */

function hide_plus(element){
  elemento=element+'_td';
  document.getElementById(elemento).style.background="url('images/tab_pagine_no_select_bg.jpg')";
  document.getElementById(elemento).style.border="1px solid rgb(192,192,192)";
  hide(element);
}

function show_plus(element){
  elemento=element+'_td';
  document.getElementById(elemento).style.background="url('images/tab_pagine_bg.jpg')";
  document.getElementById(elemento).style.border="1px solid rgb(22,69,162)";
  show(element);
}

function show(obj) {
  document.getElementById(obj).style.display='block';
}

function hide(obj) {
  document.getElementById(obj).style.display='none';
}

function scroll_position(coordinata){
	if(coordinata=='x'){
		if (navigator.appName == 'Microsoft Internet Explorer'){valore = document.body.scrollLeft;}
		else{valore = window.pageXOffset;}		
	}
	else{
		if (navigator.appName == 'Microsoft Internet Explorer'){valore = document.body.scrollTop;}
		else{valore = window.pageYOffset;}	
	}
return valore;
}


/* da usare nei moduli fusion per problemi di duplicazione nomi javascript*/
function show_fusion(obj, width) {
  document.getElementById(obj).style.display='block';
  bodyX = document.body.offsetWidth;
  bodyY = document.body.offsetHeight;

  if(width != ''){
  	document.getElementById(obj).style.width=width+'px';
  	x = document.getElementById(obj);
  	xposition = trova_posizione(x);
  	tmp = parseInt(xposition[0])+parseInt(width);
  	if(bodyX < tmp){
  	  tmp2 = (bodyX - width) - 10;
			x.style.left = tmp2+'px';
		}
		
		height = x.offsetHeight;
		tmp = parseInt(xposition[1])+parseInt(height);
		if(bodyY < tmp){
			tmp2 = (bodyY - (2*height)) - 20;
			x.style.top = tmp2+'px';
		}
	}
}

function hide_fusion(obj){
  document.getElementById(obj).style.display='none';
}


function show_div_popup(obj, altezza, sfondo, padre_relative){
	var x = document.getElementById(obj);
	
	bodyX = document.body.offsetWidth;
	bodyY = document.body.offsetHeight;

	array_dimensioni_pagina = dimensioni_pagina();
	
	if(document.getElementById(sfondo)){
		var contenitore = document.getElementById(sfondo);
		if(contenitore){
			contenitore.style.width = array_dimensioni_pagina[2]+'px';
			contenitore.style.height = array_dimensioni_pagina[3]+'px';
		}
	}
	
	if(document.getElementById(padre_relative)){
		bodyX = document.getElementById(padre_relative).offsetWidth;
		bodyY = document.getElementById(padre_relative).offsetHeight;
		if(document.getElementById(sfondo)){
			contenitore.style.width = bodyX+'px';
			contenitore.style.height = bodyY+'px';
		}
	}
	if(document.getElementById(sfondo)){
		contenitore.style.display = 'block';
	}

	divWidth = parseInt(x.style.width);
	div_position_X = parseInt(bodyX - divWidth) / 2;
	scrollY = altezza;
	x.style.top = scrollY+'px';
	x.style.left = div_position_X+'px';
	x.style.display = 'block';		
}

/*  */


function dimensioni_pagina(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function trova_posizione(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function mod_height(id, valore, operazione){
	if(document.getElementById(id)){
	 	var obj = document.getElementById(id);
	 	var height = parseInt(obj.style.height);
	 	alert(height);
	 	if(operazione == "+"){
	 		var new_height = height + parseInt(valore); 
			obj.style.height =  new_height+"px";
		}
	 	else if(operazione == "-"){
	 		var new_height = height - parseInt(valore); 
			obj.style.height =  new_height+"px";		 
		}
	 	else{
			alert('operatore chiamato errato');
		}
	}else{
		alert('elemento da modificare non presente');
	}
}












// La funzione per cambiare i fogli di stile
function templateChange(){
	//controllo browser
	if(!document.styleSheets){
		var ss = getAllSheets() //Opera
	}else{
		var ss = document.styleSheets; //Dom
	}
	// disabilita tutti i fogli di stile con un titolo 
	// tranne quello passato per argomento alla funzione
	for( var x = 0; x < ss.length; x++ ) {
		if( ss[x].title ) {
			ss[x].disabled=true;
		}
		for( var y = 0; y < arguments.length; y++ ) {
			//controlla ogni titolo ...
			if(ss[x].title == arguments[y]){
				//e riabilita il foglio di stile se ha il titolo scelto
				ss[x].disabled=false;
			}
		}
	}
	if( !ss.length ) { 
		alert( 'Il tuo browser non è abilitato a cambiare i fogli di stile CSS' );
	}
}

// ---------------------------------------------------------------------------------

// Funzione per Opera
function getAllSheets(){
	if( document.getElementsByTagName ) {
		var Lt = document.getElementsByTagName('LINK');
		var St = document.getElementsByTagName('STYLE');
	} else {
		// browser minori - restituisce array vuoto
		return []; 
	}
	//per tutti i tag link ...
	for( var x = 0, os = []; Lt[x]; x++ ) {
		//controlla l'attributo rel per vedere se contiene 'style'
		if( Lt[x].rel ) {
			var rel = Lt[x].rel;
		} else if( Lt[x].getAttribute ) {
			var rel = Lt[x].getAttribute('rel');
		} else {
			var rel = '';
		}
		if(typeof(rel)=='string'&&rel.toLowerCase().indexOf('style')+1){
			//riempe la variabile os con i stylesheets linkati
			os[os.length] = Lt[x];
		}
	}
	//include anche tutti i tags style e restituisce l'array
	for( var x = 0; St[x]; x++ ) {
		os[os.length] = St[x];
	}
	return os;
}

// ---------------------------------------------------------------------------------

// Questa funzione è soltanto accessoria e serve nel caso preferiate gestire
// la commutazione tramite una select nel documento, come nell' esempio
function temaChange(selObj){
	if(selObj.selectedIndex == 0){
		// disattiva tutti gli stili con il title
		templateChange();
	}else{
		// attiva soltanto lo stile scelto
		eval("templateChange('"+selObj.options[selObj.selectedIndex].value+"')");
	}
}

