//USAGE 
//  <img src="image1_thumb.jpg" popup_image="image1.jpg">
//
//additional options through body tag:
//
//enable print icon
//  <body popup_image_print="print_icon_path"
//
//disable progress window
//  <body popup_image_disable_progress
//
//change progress window text
//  <body popup_image_progress="progress window text"

//!!!!! IMPORTANT !!!!!!!
//		var max_w = 770;
//		var max_h = 600;
// change this in the code below ... to allow bigger popups



function HPopup(){

	function prepare_events(img){
		img.HPopup_click = img.onclick;
		img.onclick = imageClick;
	}

	function init(){
		var imgs = document.images;
		
		for (var i=0; i<imgs.length; i++){
			var img = imgs[i];
			if (img.getAttribute("popup_image") == null) continue;
			
			img.style.cursor = "hand";
			img.HPopup_src = img.getAttribute("popup_image");

			prepare_events(img);
		}
		if (window.HPopup_onload != null) window.HPopup_onload();
	}

	function imageClick(){
		//var w = window;//avoid popup blocker
		var pwin = HPopup.prototype.pwin;
		var progress = document.body.getAttribute("popup_image_progress");
		var text = "Loading image..";
		if (document.body.getAttribute("loading_image")) text = document.body.getAttribute("loading_image").value;
		if (document.body.getAttribute("popup_image_disable_progress") == null){
			if (pwin == null){
				pwin = document.createElement("DIV"); 
				document.body.appendChild(pwin);
				pwin.innerHTML = text;
				HPopup.prototype.pwin = pwin;
				pwin.style.border = "solid 1px black";
				pwin.style.backgroundColor = "#FFFFFF";
				pwin.style.position = "absolute";
				pwin.className = "text";
			}
			pwin.style.display = "";
			pwin.style.top  = event.y + document.body.scrollTop - 16;
			pwin.style.left	= event.x + document.body.scrollLeft;
		}

		window.status = text;
		this.popup_image = document.createElement("IMG");
		this.popup_image.onload = show_popup;
		this.popup_image.src = this.HPopup_src;
		if (this.HPopup_click != null) this.HPopup_click();
	}


	function show_popup(){
		var max_w = 770;
		var max_h = 600;

		var width = this.width;
		var height = this.height;
		var resizable = "0";
		var scrollbars = "0";
		if (width > max_w || height > max_h){
			resizable= "1";
			scrollbars = "1";
		}
		if (width > max_w) width = max_w;
		if (height > max_h) height = max_h;
		if (scrollbars == "1"){
			width += 24;
			height += 24;
		}
		var win = window.open('','_blank','width='+width+' height='+height+' toolbars=no resizable='+resizable+' scrollbars='+scrollbars);
		win.document.open();
		win.document.write("<html>");
		win.document.write("<head>");
		win.document.write("<title>"+document.title+"</title>");
		win.document.write("</head>");
		win.document.write("<script>");
		win.document.write("function myKey(evt){");
		win.document.write("	if (evt == null) evt = event;");
		win.document.write("	if (evt.keyCode==27 ");
		win.document.write("	|| evt.keyCode==32");
		win.document.write("	) window.close();");
		win.document.write("}");
		win.document.write("document.onkeypress = myKey;");
		win.document.write("</script>");
		if (document.body.getAttribute("popup_image_print") != null){
			var print_img = document.body.getAttribute("popup_image_print");
			if (print_img == "") print_img="slike/print.jpg";
			win.document.write("<body margins=0 margintop=0 marginleft=0 topmargin=0 leftmargin=0 ");
			win.document.write("onbeforeprint='document.getElementById(\"print\").style.display=\"none\"'");
			win.document.write("onafterprint='document.getElementById(\"print\").style.display=\"\"'");
			win.document.write("onmouseover='document.getElementById(\"print\").style.display = \"\"'");
			win.document.write("onmouseout='document.getElementById(\"print\").style.display =  \"none\"'");
			win.document.write(">");
			win.document.write("<a id=print style='position:absolute;display:none' class=link_pregled href='#' onclick='window.print();return false;'><img src='"+print_img+"' width='22' height='19' border='0' alt='Isprintaj'></a>");
		}else{
			win.document.write("<body margins=0 margintop=0 marginleft=0 topmargin=0 leftmargin=0>");
		}
		win.document.write("<img src='"+this.src+"'>");
		win.document.write("</body>");
		win.document.write("</html>");
		win.document.close();
		win.focus();
		window.status = "";
		if (HPopup.prototype.pwin != null) HPopup.prototype.pwin.style.display = "none";
	}

	window.HPopup_onload = window.onload;
	window.onload = init;
	window.HPopup_called = true;
}

if(!window.HPopup_called) HPopup();
