var ispopped = false;
var ww = hh = 0;
var automaticCenter = 0;

function pop(img,w,h,contentElement,content,bgcolor) {
	var waspopped = ispopped;
	if (!bgcolor) var bgcolor = '#000';
	ispopped = true;
	var s = document.viewport.getScrollOffsets();
	ww = w;
	hh = h;
	// fond
	$('popup_image_background').absolutize();
/*	$('body').setStyle({
		overflow: 'hidden'
	});*/
	if (Prototype.Browser.IE && navigator.userAgent.indexOf('MSIE 8.0') === -1) {
		var bodyHeight = '10000px';
	} else {
		var bodyHeight = document.body.getHeight() + 'px';
	}

	$('popup_image_background').setStyle({
		top: '0',
		left: '0',
		width: '100%',
		height: bodyHeight,
		backgroundColor: bgcolor,
		textAlign: 'center',
//		display: 'block',
		zIndex: '98'
	});
	if (!waspopped) $('popup_image_background').setOpacity(0);
	var top = s.top+10;
	if (!waspopped) $('popup_image_background').appear({ duration: 0.5, from: 0.0, to: 0.7 });
	
	//
	// Content of popup window
	//
	
	// if no content element or content given, just popup the image
	if (contentElement == '' && content=='') content = '<img src="'+img+'" width="'+w+'" height="'+h+'" onclick="unpop();" title="Cliquez sur l\'image pour fermer" style="cursor: pointer; border: 1px solid white;" />';
	if (contentElement) {
		content = $(contentElement).innerHTML;
	}
	$('popup_image_window').innerHTML = content;
	$('popup_image_window').setStyle({
		zIndex: '99',
		display: 'block',
		width: w + 'px',
		height: h + 'px'
	});
	if (!waspopped) $('popup_image_window').setOpacity(0);
	if (!waspopped) $('popup_image_window').appear({ duration: 0.5, from: 0.0, to: 1 });
	$('popup_image_window').absolutize();
	var t = Math.ceil((document.viewport.getHeight() / 2) - (h / 2)) + s.top;
	var l = Math.ceil((document.viewport.getWidth() / 2) - (w / 2)) + s.left;
	$('popup_image_window').setStyle({
		top: t +'px',
		left: l +'px'
	});
	
	automaticCenter = window.setInterval('centerpop()',2000);
}

function centerpop() {
	if (ispopped) {
		var s = document.viewport.getScrollOffsets();
		var t = Math.ceil((document.viewport.getHeight() / 2) - (hh / 2)) + s.top;
		var l = Math.ceil((document.viewport.getWidth() / 2) - (ww / 2)) + s.left;
		new Effect.Move('popup_image_window', {x: l, y: t, mode: 'absolute', transition: Effect.Transitions.sinoidal});
	}
}
function unpop() {
	ispopped = false;
	$('popup_image_background').fade({ duration: 0.5, from: 0.7, to: 0 });
	$('popup_image_window').fade({ duration: 0.5, from: 1, to: 0 });
	window.clearInterval(automaticCenter);
	return true;
}
