var baloon = null;
var popup_image_buffer = new Array();

// funzione per assegnare l'oggetto XMLHttpRequest
// compatibile con i browsers pił recenti e diffusi
function ajax_request() {

	var	XHR = null,
	browserUtente = navigator.userAgent.toUpperCase();

	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
		XHR = new XMLHttpRequest();

	else if(
		window.ActiveXObject &&
		browserUtente.indexOf("MSIE 4") < 0
	) {

		if(browserUtente.indexOf("MSIE 5") < 0)
			XHR = new ActiveXObject("Msxml2.XMLHTTP");

		else
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
	}

	return XHR;
}

function get_object_pos(act_object){
	
	var top = act_object.offsetTop, left = act_object.offsetLeft;
	
	do{
		act_object = act_object.offsetParent;
		
		top += act_object.offsetTop;
		left += act_object.offsetLeft;
		
	}while(act_object.offsetParent && (act_object.tagName != 'BODY'));
	
	return new Array(left, top);
}

function get_page_scroll(){

	var yScroll;
	
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	
	// arrayPageScroll = new Array('',yScroll)
	return yScroll;
}

function get_page_size(){

	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}else if(document.documentElement){
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} 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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function menu_current_page(location){
	var links = document.getElementById('menu').getElementsByTagName('a'), i;
	
	if(location == null)
		location = document.location.href;
	
	for(i=0; i<links.length; i++){
		if(links[i].href == location)
			links[i].className = 'current';
		else
			links[i].className = null;
		
		links[i].onclick = function() { menu_current_page(this.href); };
	}
		
		
	return void(0);
}

var alt_buffer = '';

function image_show_baloon(img_object){

	if(img_object.alt == '')
		return void(0);

	var position = get_object_pos(img_object);
	baloon.set_target_pos(position[0]+img_object.offsetWidth-10, position[1]+10);
	baloon.set_text('<span style="white-space: nowrap;">'+img_object.alt+'<'+'/span>');
	
	//alt_buffer = img_object.alt;
	//img_object.alt = '';
	
	baloon.show();
	
}

function image_hide_baloon(img_object){

	//img_object.alt = alt_buffer;
	//alt_buffer = '';

	baloon.hide();
	
}

function set_opacity(obj, opacity){
	// IE filter opacity:
	obj.style.filter = "alpha(opacity:" + opacity + ")";
	// Safari and Konqueror:
	obj.style.KHTMLOpacity = opacity / 100;
	// Old Mozilla and Firefox:
	obj.style.MozOpacity = opacity / 100;
	// CSS3 opacity for browsers that support it:
	obj.style.opacity = opacity / 100;
}

function show_cover(){
	var layer_overlay = document.getElementById('cover');
	var layer_suboverlay = document.getElementById('cover_mask');
	var page_size = get_page_size();

	// Visualizzo l'overlay
	layer_overlay.style.height = page_size[1]+'px';
	layer_suboverlay.style.height = page_size[1]+'px';
		
	layer_overlay.style.display = 'block';
	layer_suboverlay.style.display = 'block';
}

function hide_cover(){
	document.getElementById('cover').style.display = 'none';
	document.getElementById('cover_mask').style.display = 'none';
	/*
	document.body.removeChild(document.getElementById('popup_info'));
	document.body.removeChild(document.getElementById('popup_alt'));
	document.body.removeChild(document.getElementById('popup_img'));
	*/
}

function create_cover(){
	// ----------------------------------------------------------------------
	// Creo i layer per il catch dell'onclick sul body
	
    var layer_overlay = document.createElement('div');
    var layer_suboverlay = document.createElement('iframe');
 	
    layer_overlay.setAttribute('id', 'cover', 0);
    layer_overlay.setAttribute('src', 'about:blank', 0);
	with(layer_overlay.style){
		position = 'absolute';
        top = '0px';
        left = '0px';
        width = '100%';
        backgroundColor = '#ffffff';
        display = 'none';
        zIndex = 1001;
    }
	layer_overlay.inneHTML = '&nbsp;';
	set_opacity(layer_overlay, 90);
	document.body.appendChild(layer_overlay);
	
    layer_suboverlay.setAttribute('frameborder', '0', 0);
    layer_suboverlay.setAttribute('id', 'cover_mask', 0);
    with(layer_suboverlay.style){
        position = 'absolute';
        top = '0px';
        left = '0px';
        width = '100%';
        display = 'none';
        zIndex = 1000;
        filter = 'mask()';
    }
	set_opacity(layer_suboverlay, 0);
	document.body.appendChild(layer_suboverlay);
	
	layer_overlay.onclick = function(){
		hide_image();
		hide_cover();
	}

}

function hide_image(){
	document.body.removeChild(document.getElementById('popup_container'));
}

function show_image(img_id){

	var img_container = document.createElement('div');
	img_container.setAttribute('id', 'popup_container', 0);
	document.body.appendChild(img_container);
	
	var img = document.createElement('img');
	img_container.appendChild(img);
	img.onclick = function(){
		hide_image();
		hide_cover();
	};
	
	var alt = document.createElement('div');
	alt.innerHTML = document.getElementById(img_id).alt;
	alt.onclick = function(){
		hide_image();
		hide_cover();
	};
	img_container.appendChild(alt);

	var info = document.createElement('div');
	info.innerHTML = 'Clicca per chiudere';
	info.onclick = function(){
		hide_image();
		hide_cover();
	};
	img_container.appendChild(info);

	var page_size = get_page_size();
	var img_buffer = popup_image_buffer[img_id];
	
	var img_max_width = parseInt(Math.min(img_buffer.width, (page_size[2]*.75)));
	var img_max_height = parseInt(Math.min(img_buffer.height, (page_size[3]*.75)));
	
	if((img_buffer.width > img_max_width) || (img_buffer.height > img_max_height)){
		if(img_buffer.width > img_buffer.height){
			var img_width = img_max_width;
			var img_height = parseInt(img_max_width/img_buffer.width*img_buffer.height);
		}else{ 
			var img_width = parseInt(img_max_height/img_buffer.height*img_buffer.width);
			var img_height = img_max_height;
		}
	}else{
		var img_width = img_buffer.width;
		var img_height = img_buffer.height;
	}
	
	img.src = img_buffer.src;
	with (img_container.style){
		width = img_width+'px';
		height = img_height+'px';
		left = parseInt((page_size[2]-img_width)/2)+'px';
		top = (parseInt((page_size[3]-img_height)/2)+get_page_scroll())+'px';
		position = 'absolute';
		zIndex = 1003;
		display = 'block';
		cursor = 'default';
	}
	
	with (img.style){
		width = img_width+'px';
		height = img_height+'px';
		left = '0px';
		top = '0px';
		position = 'absolute';
		zIndex = 1;
		display = 'block';
		cursor = 'default';
		border = 'solid 2px #404040';
	}

	with (alt.style){
		width = img_width+'px';
		height = '30px';
		left = '0px';
		top = '-30px';
		position = 'absolute';
		zIndex = 2;
		textAlign = 'center';
		fontSize = '22px';
		fontWeight = 'bold';
		cursor = 'default';
	}
	
	with (info.style){
		width = img_width+'px';
		left = '0px';
		bottom = '-22px';
		position = 'absolute';
		zIndex = 3;
		textAlign = 'center';
		fontSize = '14px';
		fontWeight = 'bold';
		cursor = 'default';
	}
	
	window.onscroll = function() {
		img_container.style.top = parseInt((page_size[3]-img_height)/2)+get_page_scroll()+'px';
	};
	document.body.removeChild(document.getElementById('loading_bar'));
	document.body.removeChild(document.getElementById('loading_string'));
	img_container.style.display = 'block';
}

function show_loading(){

	var loading_bar = document.createElement('img');
	loading_bar.setAttribute('id', 'loading_bar', 0);
	var page_size = get_page_size();

	loading_bar.src = "/images/loading_bar.gif";
	
	with (loading_bar.style){
		width = '150px';
		height = '25px';
		left = parseInt((page_size[2]-150)/2)+'px';
		top = (parseInt((page_size[3]-25)/2)+get_page_scroll())+'px';
		position = 'absolute';
		zIndex = 1001;
	}

	var loading_string = document.createElement('div');
	loading_string.setAttribute('id', 'loading_string', 0);
	loading_string.innerHTML = 'Caricamento foto';
	with (loading_string.style){
		width = '150px';
		left = parseInt((page_size[2]-150)/2)+'px';
		top = (parseInt((page_size[3]-25)/2)-15+get_page_scroll())+'px';
		position = 'absolute';
		zIndex = 1002;
		textAlign = 'center';
		fontSize = '14px';
		fontWeight = 'bold';
	}
	
	document.body.appendChild(loading_string);
	document.body.appendChild(loading_bar);
}

function popup_image(img_obj){
	
	show_cover();
	show_loading();
	
	var img_src = img_obj.src.replace('/thumbs', '');
	
	var img_id = img_src.substr(img_src.lastIndexOf('/')+1, img_src.lastIndexOf('.'));
	img_obj.setAttribute('id', img_id, 0);
	
	if(popup_image_buffer[img_id] == null){
		popup_image_buffer[img_id] = new Image();
		popup_image_buffer[img_id].onload = function() {
			var img = document.getElementById('popup_img');
			show_image(img_id);
		};
		popup_image_buffer[img_id].src = img_src;
	}else
		show_image(img_id);
		
}

function ie6_png(){

	if(navigator.appVersion.indexOf('MSIE 6') == -1)
		return void(0);
		
	var i, immagini = document.body.getElementsByTagName('img');
	var buffer_immagini = new Array();
	
	for(i=0; i<immagini.length; i++)
		if(/\.png$/i.test(immagini[i].src))
			buffer_immagini[buffer_immagini.length] = immagini[i];
	
	for(i=0; i<buffer_immagini.length; i++){
		var fake_img = document.createElement('div');
		
		fake_img.style.position = 'absolute';
		fake_img.style.top = buffer_immagini[i].offsetTop+'px';
		fake_img.style.left = buffer_immagini[i].offsetLeft+'px';
		fake_img.style.width = buffer_immagini[i].offsetWidth+'px';
		fake_img.style.height = buffer_immagini[i].offsetHeight+'px';
		
		fake_img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+buffer_immagini[i].src+"',sizingMethod='scale')";
		
		buffer_immagini[i].parentNode.replaceChild(fake_img, buffer_immagini[i]);
	}
	
}

function opacity(obj, new_opacity){

	if((new_opacity == null) && (new_opacity !== 0)){
		if(obj.act_opacity == null){
			if(	(obj.style.display == 'none') || 
				(obj.style.visibility == 'hidden'))
				obj.act_opacity = 100;
			else
				obj.act_opacity = 0;
		}
		return obj.act_opacity;
	}else{
		new_opacity = Math.floor(new_opacity);
		if(new_opacity < 0)
			new_opacity = 0;
	
		if(new_opacity > 100)
			new_opacity = 100;
	
		obj.act_opacity = new_opacity;
		obj.style.mozOpacity = new_opacity/100;
		obj.style.opacity = new_opacity/100;
		obj.style.filter = 'alpha(opacity = '+new_opacity+')';
		
		if(new_opacity > 0)
			obj.style.visibility = 'visible';
	}
	
	return obj.act_opacity;

}

function init(){
	menu_current_page();
	//baloon = new kbaloon();
	create_cover();
	ie6_png();
}


