/*******************************************************
 **               ImageBox                            **
 **                                                   **
 **          Author :  Pasquale Di Stasio             **
 **          E-Mail :  info@pasqualedistasio.com      **
 **        Web-Site :  www.pasqualedistasio.com       **
 **                                                   **
 **            Date :  2007-10-18                     **
 **         Version :  1.0                            **
 *******************************************************/

ImageBox = {}

ImageBox.winSize = function() {
	if (!document.body) return null;
	w = h = t = l = 0;
	if (typeof(window.pageYOffset) == "number") {
		t = window.pageYOffset;
		l = window.pageXOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		t = document.body.scrollTop;
		l = document.body.scrollLeft;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		t = document.documentElement.scrollTop;
		l = document.documentElement.scrollLeft;
	}
	if (document.documentElement && document.documentElement.clientWidth) {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
		bw = document.documentElement.clientWidth;
		bh = document.documentElement.clientHeight;
	} else if (document.body.offsetWidth) {
		w = document.body.offsetWidth;
		h = document.body.offsetHeight;
		bw = document.body.clientWidth;
		bh = document.body.clientHeigh;
	} else {
		w = bw = screen.width;
		h = bh = screen.height;
	}
	w = Math.max(document.body.scrollWidth, w);
	h = Math.max(document.body.scrollHeight, h);
	return {
		bodyWidth	: bw,
		bodyHeight	: bh,
		width		: w,
		height		: h,
		scrollTop	: t,
		scrollLeft	: l
	}
}

ImageBox.addEvent = function(obj, type, fn) {
	if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	} else {
		obj.addEventListener( type, fn, false );
	}
}

ImageBox.removeEvent = function(obj, type, fn) {
	if ( obj.detachEvent ) {
		obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
	} else {
		obj.removeEventListener( type, fn, false );
	}
}

ImageBox.xId = function(id) {
	return document.getElementById(id);
}

ImageBox.openBox = function(img , w, h) {
	win = ImageBox.winSize();
	window.scrollTo(win.scrollLeft, win.scrollTop);
	ImageBox.xId('ImageBox_BOX').style.top = win.scrollTop + "px";
	ImageBox.xId('ImageBox_BOX').style.left = win.scrollLeft + "px";
	ImageBox.xId('ImageBox_BOX').style.height='100%';
	ImageBox.xId('ImageBox_BOX').style.width='100%';
	ImageBox.xId('ImageBox_BOX').style.display='block';
	ImageBox.xId('ImageBox_IMG').style.left = win.scrollLeft + parseInt((win.bodyWidth - 208 ) / 2) + "px";
	ImageBox.xId('ImageBox_IMG').style.top = win.scrollTop + parseInt((win.bodyHeight - 13 ) / 2) + "px";
	ImageBox.xId('ImageBox_IMG').innerHTML = '<img src="images/loading.gif" border="0" alt="Loading" style="opacity: 0.7; -moz-opacity: 0.7; -khtml-opacity: 0.7; filter: alpha(opacity=70);"/>';
	ImageBox.xId('ImageBox_IMG').style.display='block';
	var oImg = new Image();
	oImg.onload = function() {
		iw = (isNaN(w) || w<=0 || w=="") ? this.width : w;
		ih = (isNaN(h) || h<=0 || h=="") ? this.height : h;
		if (iw>win.bodyWidth)iw=win.bodyWidth;
		if (ih>win.bodyHeight)ih=win.bodyHeight;
		il = win.scrollLeft + parseInt((win.bodyWidth - iw ) / 2);
		it = win.scrollTop + parseInt((win.bodyHeight - ih ) / 2);
		if (il<0) il=0;
		if (it<0) it=0;
		ImageBox.xId('ImageBox_IMG').innerHTML='';
		ImageBox.xId('ImageBox_IMG').style.left = il + "px";
		ImageBox.xId('ImageBox_IMG').style.top = it + "px";
		setTimeout('ImageBox.setImage("'+img+'", "'+iw+'", "'+ih+'")', 100);
	}
	oImg.src = img;
	setTimeout('ImageBox.addEvent(document, "click", ImageBox.closeBox)', 100);
}

ImageBox.closeBox = function() {
	ImageBox.xId('ImageBox_BOX').style.display='none';
	ImageBox.xId('ImageBox_IMG').style.display='none';
	ImageBox.removeEvent(document, "click", ImageBox.closeBox);
}

ImageBox.setImage = function(img, w, h) {
	ImageBox.xId('ImageBox_IMG').innerHTML = '<img src="'+img+'" border="0" width="'+w+'" height="'+h+'" />';
}


ImageBox.addBoxToBody = function() {
	div = document.createElement("DIV");
	div.innerHTML = '<div id="ImageBox_BOX" style="display:none; top:0px; left:0px; position:absolute; background:#000000; opacity:0.6; -moz-opacity:0.6; -khtml-opacity:0.6; filter:alpha(opacity=60); width:100%; height:100%;"></div><div id="ImageBox_IMG" style="display:none; position:absolute; text-align:center; z-index:999;"></div>'
	document.body.appendChild(div)
}

ImageBox.addEvent(window, "load", ImageBox.addBoxToBody);

