// JavaScript Document
/* 
REQUISITI: ajax_request.js
*/
function URLEncode(CODE){

var plaintext = CODE;

	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					
	var HEX = "0123456789ABCDEF";
	
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" + ch + "' cannot be encoded using standard URL encoding.\n" +
				        "(URL encoding only supports 8-bit characters.)\n" +
						"A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	// VALORE CODIFICATO
	return encoded;
};

function ZoomImage(image, description,id){
		if(id=='undefined'){
			id_image=image;
		}else{
			id_image=id;
		}
		img_description=description;
		showOverPage();
		document.getElementById('overlay').style.width='600px';	
		document.getElementById('overlay').style.height='500px';	
		centerOverlay();
		
		
		document.getElementById('overlay').innerHTML='<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>'+getLoadingImage();
		AyaxRequest('ajax_request.php', 'POST', 'filetocall=zoom_image&image='+URLEncode(image)+'&id_image='+id_image, ZoomImageHTML);		
}

function ZoomImageHTML(){
	//creo il contenitore
	var objContainer = document.createElement("div");
	objContainer.setAttribute('id','imgContainer');
	var objBody = document.getElementsByTagName("body").item(0);
	objBody.appendChild(objContainer);
	document.getElementById('imgContainer').style.left='0px';
	document.getElementById('imgContainer').style.top='0px';	
	document.getElementById('imgContainer').style.width='0px';	
	document.getElementById('imgContainer').style.height='0px';	
	objContainer.className = 'div_basic';

	// assegno al contenitore il codice html proveniente dal server
	document.getElementById('imgContainer').innerHTML=this.req.responseText;
	
	// assegno all envento onLoad dell' immagine la relativa funzione
		document.getElementById(id_image).onload = function() { imageOnLoad(); }	
}

function imageOnMouseOver(){
	try{
		if (img_description){
			setAltDiv();
			changeOpac(0, 'altContainer');
			opacity('altContainer', 0, 100, 100);
			id2follow='altContainer';
			document.onmousemove=followmouse;
		}
	}catch(e){alert(e)}
}


function imageOnLoad(){
	opacity(id_image, 100, 0, 100);
	window.setTimeout('setOverlay()',100);
}

function imageOnClick(){
	imageOnMouseOut();	
	hideOverPage();
}


function imageOnMouseOut(){
	try{
		var objBody = document.getElementsByTagName("body").item(0);
		objBody.removeChild(document.getElementById('altContainer'));
		document.onmousemove='';
	}catch(e){	}
}

function setAltDiv(){
	
	// creo il contenitore per l'alt div
	var objContainer = document.createElement("div");
	objContainer.setAttribute('id','altContainer');
	var objBody = document.getElementsByTagName("body").item(0);
	objBody.appendChild(objContainer);
	
	var immagine=document.getElementById(id_image);

	document.getElementById('altContainer').style.left=mouseX + 'px';
	document.getElementById('altContainer').style.top=mouseY + 'px';
	document.getElementById('altContainer').className = 'div_alt';
	document.getElementById('altContainer').innerHTML=img_description;

}



function setOverlay(){
	
	// nascondo l' overlay
	changeOpac(0, id_image);

	// dimensiono l' overlay
	document.getElementById('overlay').style.width=(document.getElementById(id_image).width+20)+'px';
	document.getElementById('overlay').style.height=(document.getElementById(id_image).height+20)+'px';
	document.getElementById('overlay').innerHTML=document.getElementById('imgContainer').innerHTML;
	centerOverlay();
	
	// fade in
	opacity(id_image, 0, 100, 300);
	
	// eventi per l'immagine
	document.getElementById(id_image).onclick = function() { imageOnClick(); }	
	//document.getElementById(id_image).onmouseover = function() { imageOnMouseOver(); }	
	//document.getElementById(id_image).onmouseout = function() { imageOnMouseOut(); }	
	

	// distruggo il contenitore dell' immagine
	var objBody = document.getElementsByTagName("body").item(0);
	objBody.removeChild(document.getElementById('imgContainer'));

}

