var preloadimg = new Image();
preloadimg.src = '/images/zoom_icon.png';
var preloadimg2 = new Image();
preloadimg2.src = '/images/unzoom_icon.png';
var imgenlarged = false;
var imghover;

function imagebig(id)  {
	var url = (document.getElementById(id).src).replace(/\.(?!.*\.)/,"_big.");
	document.getElementById('enlargedimagebox').style.display='none';
	var a = document.getElementById('enlargedimageimg');
	a.src = "";
	a.onload = function() { imagebigloaded(); } // setTimeout("imagebigloaded();",1000); }
	a.src = url;
	var b = document.getElementById("imgtxt");
	b.innerHTML ="<span style='background-color: #ffffff'>loading...</span>";
}

function imagebigloaded() {
	imgenlarged = true;
	var bodyscrolltop = window.pageYOffset; // document.body.scrollTop;
	if (!bodyscrolltop) { bodyscrolltop = 15; }
	var t=15;
	var a = document.getElementById("enlargedimagebox");
	var bworig= document.body.clientWidth;
	a.style.display = "";
	var x = ((bworig/2)-(a.offsetWidth/2));
	a.style.left = x+"px";
	if (a.offsetHeight >= (document.body.clientHeight-30)) { t=5; } 
	a.style.top = t+bodyscrolltop+"px"; 
	//var str = "document.getElementById('enlargedimagebox').style.top='"+(t+bodyscrolltop)+"px';";
	//setTimeout(str,10); // solves chrome rendering glitch
	var bw = document.body.clientWidth;
	if (bw < bworig) {
		document.body.style.paddingLeft = (bworig-bw)+"px";
	}
	var b = document.getElementById("imgtxt");
	b.innerHTML = "<img src='/images/unzoom_icon.png' alt=''>";
	b.style.left = x+15+"px";
	b.style.top = t+bodyscrolltop+15+"px";
	
	var c = document.getElementById("youtubecontainer");
	if (c != null) { c.style.visibility = "hidden"; }

}

function imageclose() {
	if (!imgenlarged) {
		imagebig(imghover);
	}
	else {
		imgenlarged = false;
		document.getElementById('enlargedimagebox').style.display='none';
		a=document.getElementById('enlargedimageimg');
		a.onload = null;
		a.src = "";
		document.body.style.paddingLeft = "0px";
		document.getElementById("imgtxt").style.display = "none";
		var b = document.getElementById("youtubecontainer");
		if (b != null) { b.style.visibility = "visible"; }
	}
}

function imgbigover() {
	document.getElementById("imgtxt").style.display = "";
}

function imgbigout() {
	document.getElementById("imgtxt").style.display = "none";
}

function imageover(id) {
	if (!imgenlarged) {
		imghover = id;
		var a = document.getElementById("imgtxt");
		a.style.display = "";
		a.innerHTML = "<img src='/images/zoom_icon.png' alt=''>";
		var b= document.getElementById(id);
		var bp = findPos(b);
		a.style.left = (bp[0]+20)+"px";
		a.style.top = (bp[1]+20) + "px";
	}
}

function imageout(id) {
	if (!imgenlarged) {
		var a = document.getElementById("imgtxt");
		if (a.innerHTML != "loading...") {
			a.style.display = "none";
		}
	}
}

function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
  return [curleft,curtop];
  }
}

function imagepopup(url)  {
	document.getElementById('enlargedimagebox').style.display='none';
	var a = document.getElementById('enlargedimageimg');
	a.src = "";
	a.onload = function() { imagebigloaded(); } // setTimeout("imagebigloaded();",1000); }
	a.src = url;
}

