var imgPrevActive = 0;
var imgPrevCurrentId = 0;
var imgPrevOneImageOnly = 0;
var imgPrevCurrenltyLoading = '';
function imgPrevClose()
{
	imgPrevLoadImage(imgPrevLoading);
	imgPrevActive = 0;
	var obj = document.getElementById('auto24ImgPrevBg');
	if (obj != null) obj.style.display='none';
	var obj = document.getElementById('auto24ImgPreviewer');
	if (obj != null) obj.style.display='none';
	if (document.body.addEventListener)
	{
		document.body.removeEventListener("keyup", imgPrevHandleKeys, false);
	}
	else if (document.body.attachEvent)
	{
		document.body.detachEvent("onkeyup", imgPrevHandleKeys);
	}
}

function imgPrevInit()
{
	var obj = document.getElementById('auto24ImgPrevBg');
	if (obj == null) return false;
	var b = document.body;
	if (b == null) return false;
	if (b.offsetHeight)
	{
		var h = b.offsetHeight;
		if (window.innerHeight != null) h = window.innerHeight;
		if (b.scrollHeight > h) h = b.scrollHeight;
		if (b.clientHeight > h) h = b.clientHeight;
		obj.style.height= h+'px';
		var w = b.clientWidth
		if (document.body.scrollLeft) w += document.body.scrollLeft;
		else if (document.documentElement.scrollLeft) w += document.documentElement.scrollLeft;
		obj.style.width= w+'px';
		obj.style.display='';
	}
	var obj = document.getElementById('auto24ImgPreviewer');
	if (obj == null) return false;
	obj.style.display='';
	if (document.body.addEventListener)
	{
		document.body.addEventListener("keyup", imgPrevHandleKeys, false);
	}
	else if (document.body.attachEvent)
	{
		document.body.attachEvent("onkeyup", imgPrevHandleKeys);
	}
	
	document.body.focus();
	
	imgPrevActive = 1;
	return true;
}

function imgPrevHandleKeys(e)
{
	if (imgPrevActive==0) return false;
	else if (e.keyCode == 27) imgPrevClose(); //ESC
	else if (imgPrevOneImageOnly) return false;
	else if (e.keyCode == 39 || e.keyCode == 102) imgPrevNext();//right_arrow, numbad6
	else if (e.keyCode == 37 || e.keyCode == 100) imgPrevPrevious();//left_arrow, numbad4
}

function imgPrevNext()
{
	var obj = imgPrevGetNextObject();
	imgPrevLoadImage(obj.href);
	imgPrevPreLoadImages(obj.id);
	return true;
}

function imgPrevPrevious()
{
	var obj = imgPrevGetPreviousObject();
	imgPrevLoadImage(obj.href);
	imgPrevPreLoadImages(obj.id);
	return true;
}

function imgPrevInitPosition(img_width, img_height)
{
	var obj = document.getElementById('auto24ImgPreviewer');
	if (obj == null) return false;
	var b = document.body;
	if (b == null) return false;
	if (b.clientWidth < img_width) img_width = b.clientWidth;
	var l = img_width/2;
	var scrLeft = b.scrollLeft;
	if (scrLeft == 0 && document.documentElement.scrollLeft > scrLeft) scrLeft = document.documentElement.scrollLeft;
	l -= scrLeft;
	var winHeight = window.innerHeight;
	if (winHeight == null) 
	{
		winHeight = document.documentElement.clientHeight
		if (!winHeight || winHeight == null) winHeight = b.clientHeight;
	}
	var scrollBarTop = window.pageYOffset;
	if (scrollBarTop == null) 
	{
		scrollBarTop = document.documentElement.scrollTop;
		if (!scrollBarTop || scrollBarTop == null) scrollBarTop = b.scrollTop;
	}
	var t = scrollBarTop+(winHeight-img_height)/2;
	if (t < 0 || !t) t = 0;
	obj.style.marginLeft = (l*-1)+"px";
	obj.style.top = t+"px";
	var next_obj = imgPrevGetNextObject();
	if (next_obj != null && next_obj.id == imgPrevCurrentId) imgPrevOneImageOnly = 1;
	else imgPrevOneImageOnly = 0;
	var obj = document.getElementById('imgPrevNextLink');
	if (obj != null) 
	{
		obj.style.height = img_height+"px";
		if (imgPrevOneImageOnly) obj.style.display = 'none';
		else obj.style.display = '';
	}
	var obj = document.getElementById('imgPrevPreviousLink');
	if (obj != null) 
	{
		obj.style.height = img_height+"px";
		if (imgPrevOneImageOnly) obj.style.display = 'none';
		else obj.style.display = '';
	}
	var obj = document.getElementById('imgPrevCloseLink');
	if (obj != null) 
	{
		obj.style.height = img_height+"px";
	}
	return true;
}

function imgPrevSetImage(img_obj)
{
	src_value = img_obj.src;
	var img = document.getElementById('a24ImgPrevImg');
	if (img== null) return false;
	img.src = src_value;
	imgPrevFadeIn(0);
	var w = img_obj.naturalWidth;
	var h = img_obj.naturalHeight;
	if (!w) w = img_obj.width;
	if (!h) h = img_obj.height;
	var b = document.getElementsByTagName('body');
	if (b == null) return false;
	img_width = w;
	img_height = h;
	if (b[0].clientWidth < img_width) 
	{
		img_width = b[0].clientWidth;
		img_height = Math.round(img_width*h/w);
	}
	if (b[0].clientHeight < img_height) 
	{
		img_height = b[0].clientHeight;
		img_width = Math.round(img_height*w/h);
	}
	w = img_width;
	h = img_height;
	img.style.width = img_width+"px";
	img.style.height = img_height+"px";
	return imgPrevInitPosition(w, h);
}

function imgPrevGetIdParts()
{
	var splited = imgPrevCurrentId.split('_', 3);
	var r = new Array();
	var nr = parseInt(splited[2]);
	if (!nr) return r;
	var id_start = splited[0]+"_"+splited[1]+"_";
	r[0] = nr;
	r[1] = id_start;
	return r;
}

function imgPrevGetNextObject()
{
	var r = imgPrevGetIdParts();
	var cur_object = document.getElementById(imgPrevCurrentId);
	if (r.length != 2) return cur_object;
	var nr = r[0];
	var id_start = r[1];
	var next_id = id_start+(nr+1);
	var obj = document.getElementById(next_id);
	if (obj == null) 
	{
		next_id = id_start+1;
		obj = document.getElementById(next_id);
		if (obj == null) return cur_object;
	}
	return obj;
}

function imgPrevGetPreviousObject()
{
	var r = imgPrevGetIdParts();
	var cur_object = document.getElementById(imgPrevCurrentId);
	if (r.length != 2) return cur_object;
	var nr = r[0];
	var id_start = r[1];
	var previous_id = id_start+(nr-1);
	if (nr-1 == 0)
	{
		var i = nr;
		while (true)
		{
			var obj = document.getElementById(id_start+i);
			if (obj == null) 
			{
				i--;
				break;
			}
			i++;
		}
		previous_id = id_start+i;
	}
	var obj = document.getElementById(previous_id);
	if (obj == null) return cur_object;
	return obj;
}

function imgPrevPreLoadImages(current_id)
{
	imgPrevCurrentId = current_id;
	
	var previous_obj = imgPrevGetPreviousObject();
	var next_obj = imgPrevGetNextObject();
	
	imgPrevPreLoadImage(next_obj.href, 1);
	imgPrevPreLoadImage(previous_obj.href, 2);
}

function imgPrevPreLoadImage(src_value, to)
{
	var obj = document.getElementById('auto24ImgPrevPreLoader'+to);
	if (obj == null) return false;
	obj.src = src_value;
}

function imgPrevLoadImage(src_value)
{
	if (src_value == '') src_value = imgPrevCurrenltyLoading;
	var obj = document.getElementById('auto24ImgPrevPreLoader0');
	var obj2 = document.getElementById('imgPrevLoading');
	if (obj == null) return false;
	if (obj.src == src_value)
	{
		if (obj.src==imgPrevEmpty) return true;
		if (obj.naturalWidth > 1 || obj.width > 1)
		{
			if (obj2 != null) obj2.style.display = 'none';
			imgPrevSetImage(obj);
			obj.src=imgPrevEmpty;
			imgPrevCurrenltyLoading = '';
		}
		else if (imgPrevActive==1) 
		{
			if (obj2 != null && src_value != '') obj2.style.display = '';
			setTimeout('imgPrevLoadImage("")', 100);
		}
		return true;
	}
	else if (src_value == '' || imgPrevActive==0) return false;
	obj.src = src_value;
	imgPrevCurrenltyLoading = src_value;
		
	setTimeout('imgPrevLoadImage("")', 100);
}

function imgPrevOpen(element)
{
	if (imgPrevActive == 1) return false;
	imgPrevCurrentId = element.id;
	if (!imgPrevInit()) 
	{
		imgPrevClose();
		return true;
	}
	imgPrevLoadImage(element.href);
	imgPrevPreLoadImages(element.id);
	return false;
}

function imgPrevFadeIn(op)
{
	if (op > 100) return false;
	var obj = document.getElementById('a24ImgPrevImg');
	if (obj == null) return false;
	obj.style.opacity = op/100;
	obj.style.filter = 'alpha(opacity='+op+')';
	setTimeout('imgPrevFadeIn('+parseInt(op+10)+')', 10);
}

function createGallery(parent_container_id, img_id_prefix)
{
	var obj = document.getElementById(parent_container_id);
	var imgs = obj.getElementsByTagName('img');
	str = '';
	for (i = 0; i < imgs.length; i++)
	{
		var par = imgs[i].parentNode;
		if (par.tagName != "A") continue;
		par.id = img_id_prefix+(i+1);
		par.onclick = function (e)
                {
                        return imgPrevOpen(this);
                }
	}
}

