var lock=false;
var maxWidth=800;
var maxHeight=600;

var isInit=0;
var id=false;
var nbImages=0;
function prepareZonePhotobox () {

	$('body').innerHTML = '<div class="photobox"><table id="popup" style="display: none;"><tr><td><table width="0"><tr class="close"><td><a href="#" onclick="id=false;new Effect.Fade(\'popup\');return false;"><span>X</span></a></td></tr><tr><td><div id="img"></div></td></tr></table></td></tr></table></div>'+$('body').innerHTML;
	var popup = $("popup");
	if (popup) {
		popup.onclick=function () {
			if (!lock) {
				id=false;
				new Effect.Fade(this);
			}
			lock=false;
		}
		popup.hide();
	}
	var img = $("img");
	if (img) {
		img.onclick=function () {
			lock=true;
			//alert("done");
			//return false;
		}
	}
}

var tPopups = new Array();

function smartResize (elt) {
	getWindowSize();
	var newImg = new Image();
	newImg.src = elt.src;
	var height = newImg.height;
	var width = newImg.width;

	ratioW = width/maxWidth;
	ratioH = height/maxHeight;

	if (ratioW>ratioH) {
		if (width>maxWidth) {
			elt.width=width=maxWidth;
		}
		$("legend").style.width=(width+10)+"px";
	}
	else {
		if (height>maxHeight) {
			elt.height=height=maxHeight;
		}
	}





	new Effect.Appear("img");

}

function getWindowSize ()
{
	var myHeight=0;
	var myWidth=0;
	if (self.innerHeight) {
		myHeight=self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		myHeight=document.documentElement.clientHeight;
	}
	else if (document.body) {
		myHeight=document.body.clientHeight;
	}
	if (self.innerWidth) {
		myWidth=self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		myWidth=document.documentElement.clientWidth;
	}
	else if (document.body) {
		myWidth=document.body.clientWidth;
	}

	if (myHeight) {
		maxHeight = myHeight-200;
	}
	if (myWidth) {
		maxWidth = myWidth-20;
	}
}

function loadPhotobox () {
	if (isInit) return 1;
	isInit=1;
	getWindowSize();
	prepareZonePhotobox();
	var aElts = document.getElementsByClassName("photobox");
	nbImages = aElts.length;
	var cpt=-1;
	if (aElts) for (var i=0; i<aElts.length; i++) {
		if (!aElts[i].href || !aElts[i].href.length>3 || aElts[i].href=="undefined") {
			nbImages--;
			continue;
		}
		cpt++;
		if (!aElts[i].id) aElts[i].id="popup"+cpt;
		tPopups[cpt] = aElts[i].id;
		aElts[i].rel = cpt;
		aElts[i].onclick = function () {
			var popup = $("popup");
			var img = $("img");
			if (img) {
				img.hide();
				var prev="";
				var next="";
				id=parseInt(this.rel);
				if (id>=0) {
					if (this.rel>0) prev="<a href='#' class='prev' onclick='$(\""+tPopups[id-1]+"\").onclick();return false;'>&laquo;</a>";
					if (this.rel<tPopups.length-1) next="<a href='#' class='next' onclick='$(\""+tPopups[id+1]+"\").onclick();return false;'>&raquo;</a>";
				}

				img.update(
				"<a href=\""+this.href+"\" target=\"_blank\">"
				+"<span class=\"img\"><img src=\""+this.href+"\" alt=\"\" align=\"absmiddle\" onload=\"smartResize(this)\" /></span>"
				+"</a>"
				+ (this.title?"<p class='legend' id='legend'><span>"+this.title+"</span></p>":"")
				+ (
				tPopups.length>1
				?
				"<table width='100%' class='pagination'><tr>"
				+"<td>"+prev+"</td>"
				+"<td width='100%' class='pages'>"+(parseInt(this.rel)+1)+"/"+(tPopups.length)+"</td>"
				+"<td>"+next+"</td>"
				+"</tr></table>"
				:
				""
				)
				);
				if (popup) {
					new Effect.Appear(popup);
					return false;
				}
			}
		}

	}

}
document.observe("dom:loaded", function() {
	loadPhotobox();
});

Event.observe(window, "resize", function () {
	//alert("resize");
	if (id) {
		
		eltId = tPopups[id];
		if ($(eltId)) {
			
			$(eltId).onclick();
		}
	}
});
