var img_obj, index = 0;

function img_panel_size(show)
{
    var newheight, newwidth, width, height;
    var page_screen = document.getElementById('page_screen');
    var image_panel = document.getElementById('image_panel');
    var loading_screen = document.getElementById('loading_screen');
    var decatron = document.getElementById('decatron');

    newheight = img_obj[index].height + 100;
    newwidth = img_obj[index].width + 30;
    width = screen.width - 10;
    height = screen.height - 30;

	if(window.innerWidth)
		width = window.innerWidth;
	else if(document.body.clientWidth)
		width = document.body.clientWidth;
	else if(document.width)
		width = document.width;
	if(window.innerHeight)
		height = window.innerHeight - 5;

    if(newheight > height)
	newheight = height;
    if(newwidth > width)
	newwidth = width;

    page_screen.style.height = document.body.parentNode.scrollHeight + 'px';
    loading_screen.style.height = document.body.parentNode.scrollHeight + 'px';

    image_panel.style.width = newwidth + 'px';
	image_panel.style.marginLeft = -Math.round(newwidth / 2) + 'px';

    image_panel.style.height = newheight + 'px';
	image_panel.style.marginTop = -Math.round(newheight / 2) + window.pageYOffset - 5 + 'px';

	page_screen.style.display = 'block';
	image_panel.style.display = 'block';
	if(show) {
		loading_screen.style.display = 'block';
	}
}

function decatron_hide()
{
    var loading_screen = document.getElementById('loading_screen');
	loading_screen.style.display = 'none';
}

function photo_error()
{
	alert("Loading error");
	decatron_hide();
}

function show_photo(n, obj)
{
    var prev = document.getElementById('prev');
    var next = document.getElementById('next');
	var lnk = document.getElementById('img_link');
	var new_load = 1;
	var pth;

    index = n;
    img_obj = obj;
    image_mode = 1;
    pth = rp + '/' + obj[n].img + '.jpg';

    document.images.photo.src = pth;
    document.getElementById('comment').innerHTML = obj[n].comment;
    if(n == 0) prev.style.display = 'none';
	else {
		var prev_link = document.getElementById('prev_link');

		prev.style.display = 'block';
		prev_link.title = obj[n - 1].title;
	}
    if(n == obj.length - 1) {
		next.style.display = 'none';
		lnk.title = close_image;
	} else {
		var next_link = document.getElementById('next_link');

		next.style.display = 'block';
		lnk.title = next_image + obj[n + 1].title;
		next_link.title = obj[n + 1].title;
	}

	if(typeof show_photo.prev != 'undefined') {
		if(show_photo.prev == pth)
			new_load = 0;
	}
	show_photo.prev = pth;
	img_panel_size(new_load);
    document.title = oldtitle + ": " + obj[n].title;
    if(document.all) {
		document.body.scroll="no";
	} else {
	    document.body.style.overflow="hidden";
	}
}

function close_img() {
    var page_screen = document.getElementById('page_screen');
    var image_panel = document.getElementById('image_panel');

	image_panel.style.display = 'none';
    page_screen.style.display = 'none';
    document.title = oldtitle;
    image_mode = 0;

    if(document.all) {
		document.body.scroll="";
	} else {
	    document.body.style.overflow="";
	}
}

function show_prev_img() {
    if(index <= 0) return;
    show_photo(--index, img_obj);
}

function show_next_img() {
    if(index >= img_obj.length - 1)
		close_img();
	else
	    show_photo(++index, img_obj);
}
