var OverLayer;
var DummyLayer;
var PicLayer;
var imgPreloader;

function DoPopup(src, caption){

	OverLayer  = document.getElementById('SA_OVER_LAYER');
	DummyLayer = document.getElementById('SA_DUMMY_LAYER');
	PicLayer   = document.getElementById('SA_PIC_LAYER');

	var cap = document.getElementById('SA_IMAGE_CAPTION');
	if(cap){
		if(caption){ cap.innerHTML = caption; }
	}

	SetOpacity(PicLayer, 0);
	with(PicLayer.style){
		zIndex  = 303;
		display = '';
	}

	imgPreloader = new Image();
	imgPreloader.onload = function(){ StartPopup(src); }
	imgPreloader.src = src;

}

function StartPopup(src){
	SetOpacity(PicLayer, 0);
	with(PicLayer.style){
		zIndex  = 303;
		display = '';
	}
    document.images['SA_POPUP_PIC'].src = src;
	SetPicLayer();
}

function SetPicLayer(){

	var ps = GetPageSize();
	posX = Math.floor((ps[2] - PicLayer.offsetWidth) / 2);  if(posX < 0){ posX = 0; } posX += ps[4];
	posY = Math.floor((ps[3] - PicLayer.offsetHeight) / 2); if(posY < 0){ posY = 0; } posY += ps[5];
	PicLayer.style.left = posX + 'px';
	PicLayer.style.top  = posY + 'px';
	if(window.popup_background_opacity == undefined){
		SetOpacity(OverLayer, 70);
	} else {
		SetOpacity(OverLayer, popup_background_opacity);
	}

	with(OverLayer.style){
		zIndex  = 301;
		width   = ps[0] + 'px';
		height  = ps[1] + 'px';
		left    = '0px';
		top     = '0px';
		display = '';
	}

	with(DummyLayer.style){
		zIndex  = 302;
		width   = ps[0] + 'px';
		height  = ps[1] + 'px';
		left    = '0px';
		top     = '0px';
	}

	setTimeout("ZoomIn(0)", 50);

}

function ZoomIn(zoom){
	zoom += 10;
	if(zoom > 100){ zoom = 100; }
    SetOpacity(PicLayer, zoom);
	if(zoom < 100){
		setTimeout("ZoomIn(" + zoom + ")",50);
	}
}

function CloseLayer(){

	DummyLayer.style.display = 'none';
	OverLayer.style.display  = 'none';
	PicLayer.style.display   = 'none';

}

function SetOpacity(elem, opacity){
	if(document.all){
		elem.style.filter = 'Alpha(opacity=' + opacity + ')';
	} else {
		opacity = opacity/100;
		elem.style.opacity = opacity;
	}
}

function GetPageSize(){
	var pageWidth, pageHeight;
	if (window.innerHeight && window.scrollMaxY) {	
		pageWidth = window.innerWidth + window.scrollMaxX;
		pageHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		pageWidth = document.body.offsetWidth;
		pageHeight = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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;
	}	
	var scrollX, scrollY;
	scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
	scrollY = document.documentElement.scrollTop || document.body.scrollTop;
	if(pageWidth  < windowWidth) { pageWidth  = windowWidth;  }
	if(pageHeight < windowHeight){ pageHeight = windowHeight; }
	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight, scrollX, scrollY) 
	return arrayPageSize;
}

