/**
 * JS Image Viewer v. 1.0
 * © André Laszlo 2007
 * andre.laszlo@gmail.com
 **/

function image_load() {
	var viewer = document.getElementById("image_viewer");
	var picture = document.getElementById("image_viewer_img");
	
	var width = picture.width;
	var height = picture.height;
	
	var window_width = 0, window_height = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		// Non-IE
		window_width = window.innerWidth;
		window_height = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		// IE 6+ in 'standards compliant mode'
		window_width = document.documentElement.clientWidth;
		window_height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		// IE 4 compatible
		window_width = document.body.clientWidth;
		window_height = document.body.clientHeight;
	}
	
	if ((width+200 > window_width) || (height+200 > window_height)) {
		if ((width-window_width) > (height-window_height)) {
			picture.width = window_width*0.7;
			picture.height = (picture.width/width) * height;
		} else {
			picture.height = window_height*0.7;
			picture.width = (picture.height/height) * width;
		}
		var width = picture.width;
		var height = picture.height;
	}

	var xoff = width / 2;
	var yoff = height / 2;
	
	// center viewer
	viewer.style.marginLeft = "-" + xoff + "px";
	viewer.style.marginTop = "-" + yoff + "px";
	viewer.style.left = "50%";
	viewer.style.top = "50%";
	
	viewer.style.visibility = "visible";
}

function show_image(image) {
	var viewer = document.getElementById("image_viewer");
	// add the viewer
	if (viewer == null) {
		var viewer_html = "<div style=\"visibility: hidden; z-index: 10; position: absolute; left: 0; top: 0; background-color: white; text-align: center;\" id=\"image_viewer\" onclick=\"hide_image()\"><img src=\"\" style=\"border:1px solid black;\" alt=\"\" id=\"image_viewer_img\" onload=\"image_load()\" /><br /><div style=\"text-align: center; border: 1px solid black; border-top: none;\">Click to close</div></div>";
		//var main = document.getElementById("main");
		var main = document.getElementsByTagName("body")[0];
		main.innerHTML = main.innerHTML + viewer_html;
		var viewer = document.getElementById("image_viewer");
	}
	var picture = document.getElementById("image_viewer_img");
	// load picture
	picture.src = image;
	//picture.src = "http://www.sussex.ac.uk/about/images/campusmap/campus-map.jpg";
}

function hide_image() {
	var viewer = document.getElementById("image_viewer");
	viewer.style.visibility = "hidden";
}