var LOADING_IMAGE_FILE = 'exhibox-loading.gif';
var LOADING_IMAGE_WIDTH = 125;
var LOADING_IMAGE_HEIGHT = 22;

var $obj_preloader = new Image();

function win_get_width() {
	if (window.innerWidth) return window.innerWidth;
	if (document.documentElement) if (document.documentElement.clientWidth) return document.documentElement.clientWidth;
	if (document.body) if (document.body.clientWidth) return document.body.clientWidth;
	return 0;
}

function win_get_height() {
	if (window.innerHeight) return window.innerHeight;
	if (document.documentElement) if (document.documentElement.clientHeight) return document.documentElement.clientHeight;
	if (document.body) if (document.body.clientHeight) return document.body.clientHeight;
	return 0;
}

function page_get_hscroll() {
	if (window.pageXOffset) return window.pageXOffset;
	if (document.documentElement) if (document.documentElement.scrollLeft) return document.documentElement.scrollLeft;
	if (document.body) if (document.body.scrollLeft) return document.body.scrollLeft;
	return 0;
}

function page_get_vscroll() {
	if (window.pageYOffset) return window.pageYOffset;
	if (document.documentElement) if (document.documentElement.scrollTop) return document.documentElement.scrollTop;
	if (document.body) if (document.body.scrollTop) return document.body.scrollTop;
	return 0;
}

function exhibox_show($pobj_link) {
	
	var $obj_exhibox = document.getElementById('exhibox');
	var $obj_label = document.getElementById('exhiboxLabel');
	if ($obj_label) $obj_exhibox.removeChild($obj_label);
	
	var $title = $pobj_link.getAttribute('exhiboxlabel');
	if ($title != null) {
		var $a = $title.split('{,}');
		$obj_label = document.createElement('div');
		$obj_label.setAttribute('id', 'exhiboxLabel');
		var $strong = document.createElement('strong');
		$strong.appendChild(document.createTextNode($a[0] + '. '));
		$obj_label.appendChild($strong);
		$obj_label.appendChild(document.createTextNode($a[1]));
		$obj_exhibox.appendChild($obj_label);
	}
	
	var $obj_loading = document.getElementById('loadingImage');
	if ($obj_loading) {
		$obj_loading.style.top = Math.round(page_get_vscroll() + (win_get_height() / 2) - (LOADING_IMAGE_HEIGHT / 2)).toString() + 'px';
		$obj_loading.style.left = Math.round(page_get_hscroll() + (win_get_width() / 2) - (LOADING_IMAGE_WIDTH / 2)).toString() + 'px';
		$obj_loading.style.display = '';
	}
	
	with (document.getElementById('panel').style) {
			width = win_get_width().toString() + 'px';
			height = win_get_height().toString() + 'px';
			display = '';
	}
	
	$obj_preloader.onload = function() {
		var $exhibox_top = Math.round(page_get_vscroll() + (win_get_height() / 2) - (this.height / 2));
		var $exhibox_left = Math.round(page_get_hscroll() + (win_get_width() / 2) - (this.width / 2));
		with (document.getElementById('exhibox').style) {
			top = Math.max(0, $exhibox_top).toString() + 'px';
			left = Math.max(0, $exhibox_left).toString() + 'px';
			width = this.width.toString() + 'px';
			height = this.height.toString() + 'px';
		}
		document.getElementById('exhiboxImage').src = this.src;
		window.setTimeout(function() { document.getElementById('exhibox').style.display = ''; if ($obj_loading = document.getElementById('loadingImage')) $obj_loading.style.display = 'none'; }, 500);
	}
	$obj_preloader.src = $pobj_link.href;
	
	var $selects = document.getElementsByTagName('select');
	var $n = $selects.length;
	var $i;
	for ($i = 0; $i < $n; $i++) $selects[$i].style.visibility = 'hidden';
}

function exhibox_onresize() {
	$obj_exhibox = document.getElementById('exhibox');
	if ($obj_exhibox.style.display == '') {
		with (document.getElementById('panel').style) {
				width = win_get_width().toString() + 'px';
				height = win_get_height().toString() + 'px';
				display = '';
		}
		var $obj_image = document.getElementById('exhiboxImage');
		var $exhibox_top = Math.round(page_get_vscroll() + (win_get_height() / 2) - ($obj_image.height / 2));
		var $exhibox_left = Math.round(page_get_hscroll() + (win_get_width() / 2) - ($obj_image.width / 2));
		with($obj_exhibox.style) {
			top = Math.max(0, $exhibox_top).toString() + 'px';
			left = Math.max(0, $exhibox_left).toString() + 'px';
		}
	}
}

function exhibox_hide() {
	
	document.getElementById('panel').style.display = 'none';
	document.getElementById('exhibox').style.display = 'none';
	
	$selects = document.getElementsByTagName('select');
	var $n = $selects.length;
	var $i;
	for ($i = 0; $i < $n; $i++) $selects[$i].style.visibility = 'visible';
}

function exhibox_init() {
	if (!document.getElementsByTagName) return;
	
	var $links = document.links;
	var $n = $links.length;
	var $link, $i;
	
	for ($i = 0; $i < $n; $i++) {
		$link = $links[$i];
		if ($link.getAttribute('href') && ($link.getAttribute('rel') == 'exhibox')) $link.onclick = function() { exhibox_show(this); return false; }
	}
	
	var $obj_body = document.getElementsByTagName('body')[0];
	
	var $obj_panel = document.createElement('div');
	$obj_panel.setAttribute('id','panel');
	$obj_panel.onclick = function() { exhibox_hide(); return false; }
	with ($obj_panel.style) {
		display = 'none';
		position = 'absolute';
		top = '0';
		left = '0';
		zIndex = '90';
	}
	$obj_body.insertBefore($obj_panel, $obj_body.firstChild);
	
	var $obj_loading = document.createElement('img');
	$obj_loading.src = LOADING_IMAGE_FILE;
	$obj_loading.setAttribute('id', 'loadingImage');
	with ($obj_loading.style) {
			display = 'none';
			position = 'absolute';
			top = '0';
			left = '0';
			zIndex = '150';
	}
	$obj_panel.appendChild($obj_loading);
	
	var $obj_exhibox = document.createElement('div');
	$obj_exhibox.setAttribute('id','exhibox');
	$obj_exhibox.onclick = function() { exhibox_hide(); return false; }
	with ($obj_exhibox.style) {
		display = 'none';
		position = 'absolute';
		zIndex = '100';
	}
	$obj_body.insertBefore($obj_exhibox, $obj_body.firstChild);
	
	var $obj_image = document.createElement('img');
	$obj_image.setAttribute('id', 'exhiboxImage');
	$obj_exhibox.appendChild($obj_image);
}

/*
function exhibox_setup() {
	var $function;
	if (window.onload) {
		$function = window.onload;
		window.onload = function() {
			$function();
			exhibox_init();
		}
	} else {
		window.onload = exhibox_init;
	}
	
	if (window.onresize) {
		$function = window.onresize;
		window.onresize = function() {
			$function();
			exhibox_onresize();
		}
	} else {
		window.onresize = exhibox_onresize;
	}
	
	if (window.onscroll) {
		$function = window.onscroll;
		window.onscroll = function() {
			$function();
			exhibox_hide();
		}
	} else {
		window.onscroll = exhibox_hide;
	}
}

exhibox_setup();
*/
