﻿document.onmousemove = showValues;
var mouse_x = 0;
var mouse_y = 0;

function showValues(e) {
	if (!e)
		var e = window.event;

	if (e.pageX || e.pageY) {
		mouse_x = e.pageX - getScrollXY()[0];
		mouse_y = e.pageY - getScrollXY()[1];
	} else if (e.clientX || e.clientY) {
		mouse_x = e.clientX + document.body.scrollLeft;
		mouse_y = e.clientY + document.body.scrollTop;
	}
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if (typeof (window.pageYOffset) == 'number') {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [scrOfX, scrOfY];
}

function getPageXY() {
	var myWidth = 0, myHeight = 0;
	if (typeof (window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth, myHeight];
}

/*function findPos(obj) { // ricava le coordinate x,y di un oggetto obj
    var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		alert("obj:" + obj + " curleft: " + curleft + " curtop:" + curtop);

		while (obj == obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	
	return [curleft, curtop];
}*/

function findPos(obj) {
 var curleft = curtop = 0;

 if (obj.offsetParent) {

     do {
         curleft += obj.offsetLeft;
         curtop += obj.offsetTop;
     } while (obj = obj.offsetParent);
 }


 return [curleft,curtop];
}

// Baloon -->
function showBaloon(image_url, title_str, descr_str) {
	if (image_url != "" || title_str != "" || descr_str != "") {
		document.getElementById("title_lay").innerHTML = title_str;
		document.getElementById("descr_lay").innerHTML = descr_str;
		var baloonObj = document.getElementById("baloon_lay");
		if (image_url != "") {
			document.getElementById("preloadImg").src = image_url;
			document.getElementById("ante_image_lay").style["visibility"] = "visible";
			document.getElementById("ante_image_lay").style["position"] = "relative";
			document.getElementById("ante_image").onload = function() {
				baloon_position(baloonObj);
			}
		} else {
			document.getElementById("ante_image_lay").style["visibility"] = "hidden";
			document.getElementById("ante_image_lay").style["position"] = "absolute";
		};
		baloon_position(baloonObj);
		baloonObj.style["visibility"] = "visible";
	}
}

function baloon_position(baloonObj) {
	var pos_x = 0;
	var pos_y = 0;
	pos_x = mouse_x + getScrollXY()[0];
	pos_y = mouse_y + getScrollXY()[1];
	// posizione di default
	var bal_left = pos_x - 58; // left del fumetto
	var bal_top = pos_y - baloonObj.clientHeight - 10; // top del fumetto
	if (getPageXY()[0] < bal_left + baloonObj.clientWidth) {
		bal_left = pos_x - baloonObj.clientWidth + 58;
		baloon_top_left();
		if (bal_top - getScrollXY()[1] < 0) {
			bal_top = pos_y + 10;
			baloon_bottom_left();
		}
	}
	else {
		baloon_top_right();
		if (bal_top - getScrollXY()[1] < 0) {
			bal_top = pos_y + 10;
			baloon_bottom_right();
		}
	}
	baloonObj.style["left"] = bal_left.toString() + "px";
	baloonObj.style["top"] = bal_top.toString() + "px";
}

function showFixedBaloon(image_url, title_str, descr_str, target_obj) {
	if (image_url != "" || title_str != "" || descr_str != "") {
		document.getElementById("title_lay_2").innerHTML = title_str;
		document.getElementById("descr_lay_2").innerHTML = descr_str;
		var baloonObj = document.getElementById("fixed_baloon_lay");
		if (image_url != "") {
			document.getElementById("preloadImg_2").src = image_url;
			document.getElementById("ante_image_lay_2").style["visibility"] = "visible";
			document.getElementById("ante_image_lay_2").style["position"] = "relative";
			document.getElementById("ante_image_2").onload = function() {
				fix_baloon_position(baloonObj, target_obj);
			}
		} else {
			document.getElementById("ante_image_lay_2").style["visibility"] = "hidden";
			document.getElementById("ante_image_lay_2").style["position"] = "absolute";
		};
		fix_baloon_position(baloonObj, target_obj);
		baloonObj.style["visibility"] = "visible";
	}
}

function fix_baloon_position(baloonObj, target_obj) {
   
	var pos_x = findPos(target_obj)[0];
	var pos_y = findPos(target_obj)[1];
	var bal_left = pos_x + target_obj.clientWidth;
	var bal_top = pos_y - baloonObj.clientHeight;
	if (getPageXY()[0] < bal_left + baloonObj.clientWidth) {
		bal_left = pos_x - baloonObj.clientWidth;
		fix_baloon_top_left();
		if (bal_top - getScrollXY()[1] < 0) {
			bal_top = pos_y + target_obj.clientHeight;
			fix_baloon_bottom_left();
		}
	}
	else {
		fix_baloon_top_right();
		if (bal_top - getScrollXY()[1] < 0) {
			bal_top = pos_y + target_obj.clientHeight;
			fix_baloon_bottom_right();
		}
	}
	baloonObj.style["left"] = (bal_left).toString() + "px";
	baloonObj.style["top"] = (bal_top).toString() + "px";
}

function hideBaloon() {
	var baloonObj = document.getElementById("baloon_lay");
	document.getElementById('ante_image').onload = null;
	document.getElementById('ante_image').src = please_wait;
	document.getElementById("ante_image_lay").style["visibility"] = "hidden";
	document.getElementById("ante_image_lay").style["position"] = "absolute";
	document.getElementById("top_arrow").style["visibility"] = "hidden";
	document.getElementById("bottom_arrow").style["visibility"] = "hidden";
	baloonObj.style["visibility"] = "hidden";
}

function hideFixedBaloon(delay) {
	if (typeof (delay) == "undefined") delay = 1000;
	window.setTimeout(function() {
		var baloonObj = document.getElementById("fixed_baloon_lay");
		document.getElementById('ante_image_2').onload = null;
		document.getElementById('ante_image_2').src = please_wait;
		document.getElementById("ante_image_lay_2").style["visibility"] = "hidden";
		document.getElementById("ante_image_lay_2").style["position"] = "absolute";
		document.getElementById("fix_top_arrow").style["visibility"] = "hidden";
		document.getElementById("fix_bottom_arrow").style["visibility"] = "hidden";
		baloonObj.style["visibility"] = "hidden";
	}, delay);
}

var please_wait = "./images/baloon/please_wait.gif"; // contiene url immagine please_wait

// private
function baloon_top_left() {
	// C'è posto in alto sulla sx
	document.getElementById("top_border_cell").style["textAlign"] = "right";
	document.getElementById("bottom_border_cell").style["textAlign"] = "right";
	img_replace(document.getElementById("top_arrow"), "sx-top_arrow.gif");
	img_replace(document.getElementById("bottom_arrow"), "sx-bottom_arrow.gif");
	document.getElementById("top_arrow").style["visibility"] = "hidden";
	document.getElementById("bottom_arrow").style["visibility"] = "visible";
	img_replace(document.getElementById("top_left_corner"), "sx-tp-top_left_corner.gif");
	img_replace(document.getElementById("top_right_corner"), "sx-tp-top_right_corner.gif");
	img_replace(document.getElementById("left_border"), "sx-left_border.gif");
	img_replace(document.getElementById("right_border"), "sx-right_border.gif");
	img_replace(document.getElementById("bottom_left_corner"), "sx-tp-bottom_left_corner.gif");
	img_replace(document.getElementById("bottom_right_corner"), "sx-tp-bottom_right_corner.gif");
	background_replace(document.getElementById("left_border_cell"), "sx-left_border.gif");
	background_replace(document.getElementById("right_border_cell"), "sx-right_border.gif");
	background_replace(document.getElementById("top_border_cell"), "tp-top_border.gif");
	background_replace(document.getElementById("bottom_border_cell"), "tp-bottom_border.gif");
}

function baloon_top_right() {
	// c'è posto in alto sulla dx
	document.getElementById("top_border_cell").style["textAlign"] = "left";
	document.getElementById("bottom_border_cell").style["textAlign"] = "left";
	img_replace(document.getElementById("top_arrow"), "dx-top_arrow.gif");
	img_replace(document.getElementById("bottom_arrow"), "dx-bottom_arrow.gif");
	document.getElementById("top_arrow").style["visibility"] = "hidden";
	document.getElementById("bottom_arrow").style["visibility"] = "visible";
	img_replace(document.getElementById("top_left_corner"), "dx-tp-top_left_corner.gif");
	img_replace(document.getElementById("top_right_corner"), "dx-tp-top_right_corner.gif");
	img_replace(document.getElementById("left_border"), "dx-left_border.gif");
	img_replace(document.getElementById("right_border"), "dx-right_border.gif");
	img_replace(document.getElementById("bottom_left_corner"), "dx-tp-bottom_left_corner.gif");
	img_replace(document.getElementById("bottom_right_corner"), "dx-tp-bottom_right_corner.gif");
	background_replace(document.getElementById("left_border_cell"), "dx-left_border.gif");
	background_replace(document.getElementById("right_border_cell"), "dx-right_border.gif");
	background_replace(document.getElementById("top_border_cell"), "tp-top_border.gif");
	background_replace(document.getElementById("bottom_border_cell"), "tp-bottom_border.gif");
}

function baloon_bottom_left() {
	// c'è posto in basso sulla sx
	document.getElementById("top_border_cell").style["textAlign"] = "right";
	document.getElementById("bottom_border_cell").style["textAlign"] = "right";
	img_replace(document.getElementById("top_arrow"), "sx-top_arrow.gif");
	img_replace(document.getElementById("bottom_arrow"), "sx-bottom_arrow.gif");
	document.getElementById("top_arrow").style["visibility"] = "visible";
	document.getElementById("bottom_arrow").style["visibility"] = "hidden";
	img_replace(document.getElementById("top_left_corner"), "sx-bt-top_left_corner.gif");
	img_replace(document.getElementById("top_right_corner"), "sx-bt-top_right_corner.gif");
	img_replace(document.getElementById("left_border"), "sx-left_border.gif");
	img_replace(document.getElementById("right_border"), "sx-right_border.gif");
	img_replace(document.getElementById("bottom_left_corner"), "sx-bt-bottom_left_corner.gif");
	img_replace(document.getElementById("bottom_right_corner"), "sx-bt-bottom_right_corner.gif");
	background_replace(document.getElementById("left_border_cell"), "sx-left_border.gif");
	background_replace(document.getElementById("right_border_cell"), "sx-right_border.gif");
	background_replace(document.getElementById("top_border_cell"), "bt-top_border.gif");
	background_replace(document.getElementById("bottom_border_cell"), "bt-bottom_border.gif");
}

function baloon_bottom_right() {
	// c'è posto in basso sulla sx
	document.getElementById("top_border_cell").style["textAlign"] = "left";
	document.getElementById("bottom_border_cell").style["textAlign"] = "left";
	img_replace(document.getElementById("top_arrow"), "dx-top_arrow.gif");
	img_replace(document.getElementById("bottom_arrow"), "dx-bottom_arrow.gif");
	document.getElementById("top_arrow").style["visibility"] = "visible";
	document.getElementById("bottom_arrow").style["visibility"] = "hidden";
	img_replace(document.getElementById("top_left_corner"), "dx-bt-top_left_corner.gif");
	img_replace(document.getElementById("top_right_corner"), "dx-bt-top_right_corner.gif");
	img_replace(document.getElementById("left_border"), "dx-left_border.gif");
	img_replace(document.getElementById("right_border"), "dx-right_border.gif");
	img_replace(document.getElementById("bottom_left_corner"), "dx-bt-bottom_left_corner.gif");
	img_replace(document.getElementById("bottom_right_corner"), "dx-bt-bottom_right_corner.gif");
	background_replace(document.getElementById("left_border_cell"), "dx-left_border.gif");
	background_replace(document.getElementById("right_border_cell"), "dx-right_border.gif");
	background_replace(document.getElementById("top_border_cell"), "bt-top_border.gif");
	background_replace(document.getElementById("bottom_border_cell"), "bt-bottom_border.gif");
}

// fixed baloon
function fix_baloon_top_left() {
	// c'è posto in alto sulla sx
	document.getElementById("fix_bottom_border_cell").style["textAlign"] = "right";
	img_replace(document.getElementById("fix_bottom_arrow"), "fix-sx-bottom_arrow_2.gif");
	document.getElementById("fix_top_arrow").style["visibility"] = "hidden";
	document.getElementById("fix_bottom_arrow").style["visibility"] = "visible";
	img_replace(document.getElementById("fix_top_left_corner"), "sx-tp-top_left_corner.gif");
	img_replace(document.getElementById("fix_top_right_corner"), "sx-tp-top_right_corner.gif");
	img_replace(document.getElementById("fix_left_border"), "sx-left_border.gif");
	img_replace(document.getElementById("fix_right_border"), "sx-right_border.gif");
	img_replace(document.getElementById("fix_bottom_left_corner"), "sx-tp-bottom_left_corner.gif");
	img_replace(document.getElementById("fix_bottom_right_corner"), "fix-sx-bottom_arrow_1.gif");
	background_replace(document.getElementById("fix_left_border_cell"), "sx-left_border.gif");
	background_replace(document.getElementById("fix_right_border_cell"), "sx-right_border.gif");
	background_replace(document.getElementById("fix_top_border_cell"), "tp-top_border.gif");
	background_replace(document.getElementById("fix_bottom_border_cell"), "tp-bottom_border.gif");
}

function fix_baloon_top_right() {
	// c'è posto in alto sulla dx
	document.getElementById("fix_bottom_border_cell").style["textAlign"] = "left";
	img_replace(document.getElementById("fix_bottom_arrow"), "fix-dx-bottom_arrow_2.gif");
	document.getElementById("fix_top_arrow").style["visibility"] = "hidden";
	document.getElementById("fix_bottom_arrow").style["visibility"] = "visible";
	img_replace(document.getElementById("fix_top_left_corner"), "dx-tp-top_left_corner.gif");
	img_replace(document.getElementById("fix_top_right_corner"), "dx-tp-top_right_corner.gif");
	img_replace(document.getElementById("fix_left_border"), "dx-left_border.gif");
	img_replace(document.getElementById("fix_right_border"), "dx-right_border.gif");
	img_replace(document.getElementById("fix_bottom_left_corner"), "fix-dx-bottom_arrow_1.gif");
	img_replace(document.getElementById("fix_bottom_right_corner"), "dx-tp-bottom_right_corner.gif");
	background_replace(document.getElementById("fix_left_border_cell"), "dx-left_border.gif");
	background_replace(document.getElementById("fix_right_border_cell"), "dx-right_border.gif");
	background_replace(document.getElementById("fix_top_border_cell"), "tp-top_border.gif");
	background_replace(document.getElementById("fix_bottom_border_cell"), "tp-bottom_border.gif");
}

function fix_baloon_bottom_right() {
	// c'è posto in basso sulla dx
	document.getElementById("fix_top_border_cell").style["textAlign"] = "left";
	img_replace(document.getElementById("fix_top_arrow"), "fix-dx-top_arrow_2.gif");
	document.getElementById("fix_bottom_arrow").style["visibility"] = "hidden";
	document.getElementById("fix_top_arrow").style["visibility"] = "visible";
	img_replace(document.getElementById("fix_top_left_corner"), "fix-dx-top_arrow_1.gif");
	img_replace(document.getElementById("fix_top_right_corner"), "dx-bt-top_right_corner.gif");
	img_replace(document.getElementById("fix_left_border"), "dx-left_border.gif");
	img_replace(document.getElementById("fix_right_border"), "dx-right_border.gif");
	img_replace(document.getElementById("fix_bottom_left_corner"), "dx-bt-bottom_left_corner.gif");
	img_replace(document.getElementById("fix_bottom_right_corner"), "dx-bt-bottom_right_corner.gif");
	background_replace(document.getElementById("fix_left_border_cell"), "dx-left_border.gif");
	background_replace(document.getElementById("fix_right_border_cell"), "dx-right_border.gif");
	background_replace(document.getElementById("fix_top_border_cell"), "bt-top_border.gif");
	background_replace(document.getElementById("fix_bottom_border_cell"), "bt-bottom_border.gif");
}

function fix_baloon_bottom_left() {
	// c'è posto in basso sulla sx
	document.getElementById("fix_top_border_cell").style["textAlign"] = "right";
	img_replace(document.getElementById("fix_top_arrow"), "fix-sx-top_arrow_2.gif");
	document.getElementById("fix_bottom_arrow").style["visibility"] = "hidden";
	document.getElementById("fix_top_arrow").style["visibility"] = "visible";
	img_replace(document.getElementById("fix_top_left_corner"), "sx-bt-top_left_corner.gif");
	img_replace(document.getElementById("fix_top_right_corner"), "fix-sx-top_arrow_1.gif");
	img_replace(document.getElementById("fix_left_border"), "sx-left_border.gif");
	img_replace(document.getElementById("fix_right_border"), "sx-right_border.gif");
	img_replace(document.getElementById("fix_bottom_left_corner"), "sx-bt-bottom_left_corner.gif");
	img_replace(document.getElementById("fix_bottom_right_corner"), "sx-bt-bottom_right_corner.gif");
	background_replace(document.getElementById("fix_left_border_cell"), "sx-left_border.gif");
	background_replace(document.getElementById("fix_right_border_cell"), "sx-right_border.gif");
	background_replace(document.getElementById("fix_top_border_cell"), "bt-top_border.gif");
	background_replace(document.getElementById("fix_bottom_border_cell"), "bt-bottom_border.gif");
}

function img_replace(img_obj, img_name) {
	img_obj.src = img_obj.src.substring(0, img_obj.src.lastIndexOf("/") + 1) + img_name;
}

function background_replace(obj, img_name) {
	var background_path = obj.style["backgroundImage"].substring(obj.style["backgroundImage"].indexOf("(") + 1, obj.style["backgroundImage"].lastIndexOf(")"));
	background_path = background_path.substring(0, background_path.lastIndexOf("/") + 1);
	obj.style["backgroundImage"] = "url(" + background_path + img_name + ")";
}
// <-- baloon

// img popup -->

document.writeln("<iframe id='img_pop_up_iframe' src=\"?req=empty\" frameborder='0' style='position:absolute; width:5px; height:5px; visibility:hidden'></iframe>");

function popup_obj(div_obj, frame_obj, pop_up_opt, pop_up_dida, rest_page_background, mainImg_obj) {
	document.getElementsByTagName("html")[0].style["overflow"] = "auto";
	// variabili globali
	var wait_image_url = "";
	var pop_up_original_width = "";
	var pop_up_original_height = "";
	var wait_image_original_margin = "";
	var width_ok = false; // vale true se il pop-up raggiunge la giusta larghezza
	var height_ok = false; // vale true se il pop-up raggiunge la giusta altezza
	var galleria_arr = new Array(); // array delle immagini di galleria
	var alt_array = new Array(); // array delle didascalie delle foto
	// all'iniziazione recupero le configurazioni originali
	if (wait_image_url == "") {
		wait_image_url = mainImg_obj.src; // recupero e salvo l'url dell'immagine di preload...
		pop_up_original_width = div_obj.clientWidth + "px"; // nonchè le dimensioni originali...
		pop_up_original_height = div_obj.clientHeight + "px";
		wait_image_original_margin = mainImg_obj.style["margin"]; // nonchè il margin iniziale...
	}
	// get obj width - height =>
	this.obj_width = div_obj.clientWidth;
	this.obj_height = div_obj.clientHeight;
	// <=
	// set obj top, left, width, height
	function set_top(pix) {
		frame_obj.style["top"] = pix.toString() + "px";
		div_obj.style["top"] = pix.toString() + "px";
	}
	function set_left(pix) {
		frame_obj.style["left"] = pix.toString() + "px";
		div_obj.style["left"] = pix.toString() + "px";
	}
	function set_width(pix) {
		frame_obj.style["width"] = pix.toString() + "px";
		div_obj.style["width"] = pix.toString() + "px";
		mainImg_obj.style["marginLeft"] = ((pix - parseInt(mainImg_obj.clientWidth)) / 2).toString() + "px";
		mainImg_obj.style["marginRight"] = (pix - parseInt(mainImg_obj.clientWidth) - parseInt(mainImg_obj.style["marginLeft"])).toString() + "px";
		set_middleWin();
	}
	function set_height(pix) {
		frame_obj.style["height"] = pix.toString() + "px";
		div_obj.style["height"] = pix.toString() + "px";
		mainImg_obj.style["marginTop"] = ((pix - parseInt(mainImg_obj.clientHeight)) / 2).toString() + "px";
		mainImg_obj.style["marginBottom"] = (pix - parseInt(mainImg_obj.clientHeight) - parseInt(mainImg_obj.style["marginTop"])).toString() + "px";
		set_middleWin();
	}
	// <=
	function set_middleWin() {
		var zIndex = 10000;
		div_obj.style["zIndex"] = zIndex;
		frame_obj.style["zIndex"] = zIndex - 1;
		pop_up_opt.style["zIndex"] = zIndex + 1;
		rest_page_background.style["zIndex"] = zIndex - 2;
		rest_page_background.style["left"] = parseInt(getScrollXY()[0]).toString() + "px";
		rest_page_background.style["top"] = parseInt(getScrollXY()[1]).toString() + "px";
		rest_page_background.style["width"] = parseInt(getPageXY()[0]).toString() + "px";
		rest_page_background.style["height"] = parseInt(getPageXY()[1]).toString() + "px";
		frame_obj.style["width"] = div_obj.clientWidth.toString() + "px";
		frame_obj.style["height"] = div_obj.clientHeight.toString() + "px";
		frame_obj.style["left"] = div_obj.style["left"] = (parseInt((parseInt(getPageXY()[0]) - parseInt(div_obj.clientWidth)) / 2) + (parseInt(getScrollXY()[0]))).toString() + "px";
		frame_obj.style["top"] = div_obj.style["top"] = (parseInt((parseInt(getPageXY()[1]) - parseInt(div_obj.clientHeight)) / 2) + (parseInt(getScrollXY()[1]))).toString() + "px";
		pop_up_opt.style["left"] = (parseInt(div_obj.style["left"]) + parseInt(wait_image_original_margin)).toString() + "px";
		pop_up_opt.style["top"] = (parseInt(div_obj.style["top"]) + parseInt(wait_image_original_margin)).toString() + "px";
	}
	this.open = function(image_url) {
		preload_main_image(image_url);
	}
	this.close = function() {
		div_obj.style["visibility"] = frame_obj.style["visibility"] = pop_up_opt.style["visibility"] = rest_page_background.style["visibility"] = "hidden";
		mainImg_obj.src = wait_image_url;
		mainImg_obj.style["margin"] = wait_image_original_margin;
		div_obj.style["width"] = frame_obj["width"] = pop_up_original_width;
		div_obj.style["height"] = frame_obj["height"] = pop_up_original_height;
		document.getElementById("forw_img").style["position"] = "absolute";
		document.getElementById("forw_img").style["visibility"] = "hidden";
		document.getElementById("backw_img").style["position"] = "absolute";
		document.getElementById("backw_img").style["visibility"] = "hidden";
		rest_page_background.style["left"] = rest_page_background.style["top"] = rest_page_background.style["width"] = rest_page_background.style["height"] = "0px";
		document.getElementsByTagName("html")[0].style["overflow"] = "visible";
	}
	// funzioni "private" =>
	function set_img_gallery_arr() {
		for (var i = 0; i < document.images.length; i++) {
			if (document.images[i].src.indexOf("small_") != -1 && document.images[i].className == "pop_up_img") {
				galleria_arr[galleria_arr.length] = document.images[i].src.replace("small_", "");
				alt_array[alt_array.length] = document.images[i].alt;
				document.images[i].onmouseover = function() {
					this.style["cursor"] = "pointer";
				}
				document.images[i].onclick = function() {
					preload_main_image(this.src);
				}
			}
		}
	}

	function get_image_gall_index(foto_src) {
		foto_src = foto_src.replace("small_", "").replace("disp_", "");
		var i = 0;
		while (i < galleria_arr.length && galleria_arr[i].replace("small_", "").replace("disp_", "") != foto_src.replace("small_", "").replace("disp_", "")) {
			i++;
		};
		return i;
	}

	function preload_main_image(foto_src) { // precarica ingrandimento su oggetto foto...
		document.getElementsByTagName("html")[0].style["overflow"] = "hidden"; // inibisce le scrollbar della pagina
		// carica immagine di preload
		pop_up_dida.innerHTML = "";
		pop_up_opt.style["visibility"] = "hidden";
		pop_up_opt.style["width"] = "0px";
		pop_up_opt.style["height"] = "0px";
		div_obj.style["visibility"] = frame_obj.style["visibility"] = rest_page_background.style["visibility"] = "visible";
		mainImg_obj.src = wait_image_url;
		set_width(div_obj.clientWidth);
		set_height(div_obj.clientHeight);
		// attiva il precaricamento dell'immagine passata a a parametro...

		window.setTimeout(function() {

			var preload_img = new Image();
			preload_img.onload = function() {
				get_main_image(preload_img);
			};
			preload_img.src = foto_src.replace("small_", "");
			preload_img.onerror = function() {
				if (foto_src.indexOf("disp_") == -1) {
					if (foto_src.indexOf("small_") == -1) {
						var tmp_str_1 = foto_src.substring(0, foto_src.lastIndexOf("/") + 1);
						var tmp_str_2 = foto_src.substring(foto_src.lastIndexOf("/") + 1, foto_src.length);
						foto_src = tmp_str_1 + "disp_" + tmp_str_2;
						preload_main_image(foto_src);
					}
					else {
						preload_main_image(foto_src.replace("small_", "disp_"));
					}
				}
				else {
					alert("impossibile caricare l'ingrandimento");
				}
			}
		}, 1);
	}

	function get_main_image(preload_img) {
		if (preload_img.width == 0 || preload_img.height == 0) {
			window.setTimeout(function() { get_main_image(preload_img) }, 1000); // il precaricamento non è ancora completato... attendo un secondo e provo di nuovo...
		}
		else {
			// adesso che ho le dimensioni dell'immagine da caricare posso ridimensionare...
			if (!width_ok) {
				width_grow(preload_img, 1);
			}
			else {
				// larghezza del pop-up ok
				if (!height_ok) {
					height_grow(preload_img, 1);
				}
				else {
					// altezza del pop-up ok
					width_ok = height_ok = false;
					mainImg_obj.style["margin"] = wait_image_original_margin;
					mainImg_obj.src = preload_img.src;
					window.setTimeout(function() { show_opt(preload_img); }, 1000);
				}
			}
		}
	}

	function width_grow(preload_img, inc) {
		pop_up_opt.style["visibility"] = "hidden";
		pop_up_opt.style["width"] = "0px";
		pop_up_opt.style["height"] = "0px";
		var final_width = preload_img.width + (parseInt(wait_image_original_margin) * 2);
		var abs_inc = Math.abs(inc) + 0.1;
		var inc = final_width > parseInt(div_obj.clientWidth) ? +abs_inc : -abs_inc;
		if (
				Math.abs(parseInt(div_obj.clientWidth) - final_width) <= Math.abs(inc)
			) {
			width_ok = true;
			set_width(final_width);
			get_main_image(preload_img);
		}
		else {
			set_width(parseInt(div_obj.clientWidth) + inc);
			window.setTimeout(function() {
				width_grow(preload_img, inc);
			}, 0);
		}
	}

	function height_grow(preload_img, inc) {
		pop_up_opt.style["visibility"] = "hidden";
		pop_up_opt.style["width"] = "0px";
		pop_up_opt.style["height"] = "0px";
		var final_height = preload_img.height + (parseInt(wait_image_original_margin) * 2);
		var abs_inc = Math.abs(inc) + 0.1;
		var inc = final_height > parseInt(div_obj.clientHeight) ? +abs_inc : -abs_inc;
		if (
				Math.abs(parseInt(div_obj.clientHeight) - final_height) <= Math.abs(inc)
			) {
			height_ok = true;
			set_height(final_height);
			get_main_image(preload_img);
		}
		else {
			set_height(parseInt(div_obj.clientHeight) + inc);
			window.setTimeout(function() {
				height_grow(preload_img, inc);
			}, 0);
		}
	}


	function show_opt(preload_img) {
		var image_index = get_image_gall_index(preload_img.src);
		pop_up_dida.innerHTML = alt_array[image_index];
		// link per immagine successiva...
		if (image_index < galleria_arr.length - 1) {
			document.getElementById("forw_img").style["position"] = "relative";
			document.getElementById("forw_img").style["visibility"] = "visible";
			document.getElementById("forw_img").onclick = function() {
				document.getElementById("forw_img").style["position"] = "absolute";
				document.getElementById("forw_img").style["visibility"] = "hidden";
				document.getElementById("backw_img").style["position"] = "absolute";
				document.getElementById("backw_img").style["visibility"] = "hidden";
				preload_main_image(galleria_arr[image_index + 1]);
			};
		}
		else {
			document.getElementById("forw_img").style["position"] = "absolute";
			document.getElementById("forw_img").style["visibility"] = "hidden";
		}
		// link per immagine precedente...
		if (image_index > 0) {
			document.getElementById("backw_img").style["position"] = "relative";
			document.getElementById("backw_img").style["visibility"] = "visible";
			document.getElementById("backw_img").onclick = function() {
				document.getElementById("forw_img").style["position"] = "absolute";
				document.getElementById("forw_img").style["visibility"] = "hidden";
				document.getElementById("backw_img").style["position"] = "absolute";
				document.getElementById("backw_img").style["visibility"] = "hidden";
				preload_main_image(galleria_arr[image_index - 1]);
			};
		}
		else {
			document.getElementById("backw_img").style["position"] = "absolute";
			document.getElementById("backw_img").style["visibility"] = "hidden";
		}
		pop_up_opt.style["left"] = (parseInt(div_obj.style["left"]) + parseInt(wait_image_original_margin)).toString() + "px";
		pop_up_opt.style["top"] = (parseInt(div_obj.style["top"]) + parseInt(wait_image_original_margin)).toString() + "px";
		pop_up_opt.style["width"] = (div_obj.clientWidth - (parseInt(wait_image_original_margin) * 2)).toString() + "px";
		pop_up_opt.style["height"] = (div_obj.clientHeight - (parseInt(wait_image_original_margin) * 2)).toString() + "px";
		pop_up_opt.style["visibility"] = "visible";
	}
	// <= "private"

	set_img_gallery_arr();
	set_middleWin();

	window.onresize = function() {
		set_middleWin();
	};
}


function pop_up_close() {
	myPopup_obj.close();
}
// <-- img popup