var xmlHttp = getXmlHttpObject();
function load_image(negozio)
{
	xmlHttp.open('GET', 'caricatore_immagini.php?negozio='+negozio+"&random=" + Math.random(), true);
	xmlHttp.onreadystatechange = image_loaded;
	xmlHttp.send(null);
}
function image_loaded() 
{
	 if(xmlHttp.readyState == 4) 
	 {
		 if (xmlHttp.status == 200) 
		 {
			 var resp = xmlHttp.responseText;
			 if(resp) 
			 {
				var values = resp.split(';');		
 				var thumbnails = document.getElementById("thumbnails");
				while(thumbnails.firstChild) thumbnails.removeChild(thumbnails.firstChild);
				
				 var limit = values.length;
				 for(i=0; i < limit; i++) 
				 {
					addImage(values[i]);
				 } 
			 }
		 } 
		 else 
		 {
			alert(xmlHttp.responseText);
		 }
 	}
 }
function getXmlHttpObject()
{
	var xmlHttp=null;
	try
	 {
		//Firefox e Opera
		xmlHttp=new XMLHttpRequest();
	 }
	 catch(e)
	 {
		//Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	 }
	return xmlHttp;
} 
function addImage(src) {
	var src_array = src.split('/');
	
	var src_big = src_array[0]+'/'+src_array[1]+'/'+src_array[3];
	var imghref = document.createElement('a');
	imghref.setAttribute('class','paginatore');
	imghref.setAttribute('href',src_big);
	
	var newImg = document.createElement("img");
	newImg.setAttribute('id','thumbnail');
	newImg.style.margin = "5px";

	imghref.appendChild(newImg);
	document.getElementById("thumbnails").appendChild(imghref);
	
	if (newImg.filters) {
		try {
			newImg.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 0;
		} catch (e) {
			// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
			newImg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + 0 + ')';
		}
	} else {
		newImg.style.opacity = 0;
	}

	newImg.onload = function () {
		fadeIn(newImg, 0);
	};
	newImg.src = src;
	$('#thumbnails a').lightBox();
}

function fadeIn(element, opacity) {
	var reduceOpacityBy = 5;
	var rate = 30;	// 15 fps


	if (opacity < 100) {
		opacity += reduceOpacityBy;
		if (opacity > 100) {
			opacity = 100;
		}

		if (element.filters) {
			try {
				element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';
			}
		} else {
			element.style.opacity = opacity / 100;
		}
	}

	if (opacity < 100) {
		setTimeout(function () {
			fadeIn(element, opacity);
		}, rate);
	}
}