var templateCorrente="";

function do_init(){

	accordionMenu(-1);

	if ($('login')){
		$('login').onclick=login;
	}
	
	
}

function login(){
	
	//Controllare se i campi sono stati compilati
	if (($('user').value.length>0) && ($('password').value.length>0)){
		
		var oggetto = new insertDipnet("login","debug",1);
		oggetto.post="user="+$('user').value;
		oggetto.post+="&password="+$('password').value;
		oggetto.settaOnComplete(onCompleteLogin);
		
		oggetto.caricaXML();
	
	}else{
		
		var oggetto = new insertDipnet("schermataLoginCampiNonInseriti","debug",1);
		oggetto.settaOnComplete(onCompleteLoginVuoto);
		oggetto.caricaXML();
		
	}
	
}

function onCompleteLogin(stringaXML){
	
	var doc=creaDocsXML(stringaXML);
	var controllo=doc.getElementsByTagName("templates");
	var controlloLogin=controllo[0].childNodes[2].firstChild.data;
	
	if (controlloLogin=="true"){
		
		window.location.reload(true);
	
	}else{
		
		//Messaggio overrlay con messaggio 
		var template=controllo[0].childNodes[1].firstChild.data;
		templateCorrente=template;
		activeOverlayLogin(template);
		
	}
	
}

function onCompleteLoginVuoto(stringaXML){
	
	var doc=creaDocsXML(stringaXML);
	var controllo=doc.getElementsByTagName("templates");
	var template=controllo[0].childNodes[1].firstChild.data;
	templateCorrente=template;
	activeOverlayLogin(template);
	
}

function activeOverlayLogin(template){
	 
	var overlay=document.createElement("div");
	overlay.id="overlay";
		
	var lightbox=document.createElement("div");
	lightbox.id="lightbox";
	
	var contents=document.createElement("div");
	contents.id="contsLightbox";
	
	document.body.appendChild(overlay);
	document.body.appendChild(lightbox);
	document.body.appendChild(contents);
	
	overlay.onclick=deactiveOverlay;
	
	$('overlay').setStyle('opacity', 0);
	$('contsLightbox').setStyle('opacity', 0);
	
	$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
	
	var arrayPageSize = this.getPageSize();
	$('overlay').setStyles({width: arrayPageSize[0], height: arrayPageSize[1] });
	
	var fxLogin=new Fx.Tween('overlay',{duration:'long'});
	fxLogin.start('opacity',0.6);
		
	fxLogin.onComplete=function(template){
		
		var fx4=new Fx.Tween('contsLightbox',{duration:'long'});
		fx4.start('opacity',1);
		
		$('contsLightbox').innerHTML=templateCorrente;
		
	}
}


function deactiveOverlay(){
	
	$('overlay').onclick=null;
	var fx2=new Fx.Tween('contsLightbox',{duration:'long',onComplete:fadeOutOverlay});
	fx2.start('opacity',0);
	
}

function fadeOutOverlay(){
	
	var fx3=new Fx.Tween('overlay',{duration:'long',onComplete:cancellaDivLightBox});
	fx3.start('opacity',0);
	
}

function cancellaDivLightBox(){

	$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
	document.body.removeChild($('contsLightbox'));
	document.body.removeChild($('overlay'));
	document.body.removeChild($('lightbox'));
	
}

function  getPageSize() {
	        
	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;
	
	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;
	}

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

	return [pageWidth,pageHeight];
}





