var popUpImageViewer = false;
var popUpImageBackground = false;
var imgWin = false;
var opacity = 85;

function openPreviewImage(imageURL, w, h, caption)
{
	winW = w;// + 20;
	winH = h + 70;

	content = "<div align='center'>";
	if(caption && caption.length > 0) content+= "<b>"+caption+"</b><br>";
	content+= "<img src='"+imageURL+"' width="+w+" height="+h+"><br><a href='javaScript:hideWin(\"imageViewer\")'>Close</a></div>";

	popupWindow(content, winW, winH);

	/*if(window.innerWidth)
	{
		windowW   = window.innerWidth;
		windowH   = window.innerHeight;
		scrollTop = window.pageYOffset;
	}else{
		windowW   = document.body.clientWidth;
		windowH   = document.body.clientHeight;
		scrollTop = document.body.scrollTop;
	}


	if(winW > windowW || winH > windowH)
	{
		try{
			imgWin.window.close();
		}catch(e)
		{
		}
		//if(!document.all && imgWin) alert("IMGWIN");// && imgWin.document) imgWin.window.close();
		imgWin = window.open('/blank.html','imgWin','scrollbars=no,menubar=no,width='+winW+',height='+winH+',resizable=yes,toolbar=no,location=no,status=no');
		content = "<html><head><script>\nfunction hideWin(){window.close();}\n</script></head><body marginwidth=0 marginheight=0 leftmargin=0 topmargin=0>"+content+"</body></html>";
		imgWin.document.write(content);
		imgWin.focus();
	}else{
		if(!popUpImageViewer)
		{
			popUpImageBackground = createDiv('imageBackground', 'xxx');
			popUpImageViewer = createDiv('imageViewer','xxx');
		}

		// set up background
		popUpImageBackground.style.position="absolute";
		popUpImageBackground.style.top=scrollTop+"px";
		popUpImageBackground.style.left="0px";

		popUpImageBackground.style.width='100%';
		popUpImageBackground.style.height='100%';
		popUpImageBackground.style.visibility='visible';

		// now fill it and make it semi-transparent
		popUpImageBackground.innerHTML= "<div style='opacity:."+opacity+";filter:alpha(opacity="+opacity+"); -moz-opacity: 0."+opacity+";width:100%;height:100%;background-color:#000000;'>&nbsp;</div>";

		x = (windowW-winW)/2;
		y = (windowH-winH)/2+scrollTop;
		popUpImageViewer.style.left= x+'px';
		popUpImageViewer.style.top=  y+'px';
		popUpImageViewer.style.visibility='visible';
		popUpImageViewer.style.position = "absolute";
		popUpImageViewer.style.backgroundColor = "#ffffff";
		popUpImageViewer.style.border = "2px solid #999999";
		popUpImageViewer.style.padding = "2px";
		popUpImageViewer.innerHTML = content;
	}*/
}


function openYoutubeWindow(youtubeID, x, y, caption)
{
	winW = x;// + 20;
	winH = y + 70;
	youtubeSource = "http://www.youtube.com/v/"+youtubeID;
	content = "<div align='center'>";
	if(caption && caption.length > 0) content+= "<b>"+caption+"</b><br>";
	content+= "<object width=\""+x+"\" height=\""+y+"\"><param name=\"movie\" value=\""+youtubeSource+"\"></param>\n";
	content+= "<param name=\"allowFullScreen\" value=\"true\"></param>\n";
	content+= "<embed src=\""+youtubeSource+"\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\""+x+"\" height=\""+y+"\"></embed>\n";
	content+= "</object>\n";
	content+= "<br><a href='javaScript:hideWin(\"imageViewer\")'>Close</a></div>";

	popupWindow(content, winW, winH);
}


function popupWindow(content, winW, winH)
{

	if(window.innerWidth)
	{
		windowW   = window.innerWidth;
		windowH   = window.innerHeight;
		scrollTop = window.pageYOffset;
	}else{
		windowW   = document.body.clientWidth;
		windowH   = document.body.clientHeight;
		scrollTop = document.body.scrollTop;
	}

	if(winW > windowW || winH > windowH)
	{
		try{
			imgWin.window.close();
		}catch(e)
		{
		}
		//if(!document.all && imgWin) alert("IMGWIN");// && imgWin.document) imgWin.window.close();
		imgWin = window.open('/blank.html','imgWin','scrollbars=no,menubar=no,width='+winW+',height='+winH+',resizable=yes,toolbar=no,location=no,status=no');
		content = "<html><head><script>\nfunction hideWin(){window.close();}\n</script></head><body marginwidth=0 marginheight=0 leftmargin=0 topmargin=0>"+content+"</body></html>";
		imgWin.document.write(content);
		imgWin.focus();
	}else{
		if(!popUpImageViewer)
		{
			popUpImageBackground = createDiv('imageBackground', 'xxx');
			popUpImageViewer = createDiv('imageViewer','xxx');
		}

		// set up background
		popUpImageBackground.style.position="absolute";
		popUpImageBackground.style.top=scrollTop+"px";
		popUpImageBackground.style.left="0px";

		popUpImageBackground.style.width='100%';
		popUpImageBackground.style.height='100%';
		popUpImageBackground.style.visibility='visible';

		// now fill it and make it semi-transparent
		popUpImageBackground.innerHTML= "<div style='opacity:."+opacity+";filter:alpha(opacity="+opacity+"); -moz-opacity: 0."+opacity+";width:100%;height:100%;background-color:#000000;'>&nbsp;</div>";

		x = (windowW-winW)/2;
		y = (windowH-winH)/2+scrollTop;
		popUpImageViewer.style.left= x+'px';
		popUpImageViewer.style.top=  y+'px';
		popUpImageViewer.style.visibility='visible';
		popUpImageViewer.style.position = "absolute";
		popUpImageViewer.style.backgroundColor = "#ffffff";
		popUpImageViewer.style.border = "2px solid #999999";
		popUpImageViewer.style.padding = "2px";
		popUpImageViewer.innerHTML = content;
	}
}


function hideWin(name)
{
	if(popUpImageViewer)
	{
		popUpImageViewer.style.visibility='hidden';
		popUpImageViewer.innerHTML=""; // destroy what is there so videos stop
		popUpImageBackground.style.visibility='hidden';
	}else{
		window.close();
	}
}


/* function createDiv(divName)
get a div, or if it doesn't exist, create it
*/

function createDiv(divName, className)
{
	style="";
	if(!className || className.length == 0) className = "";

	if(!getElemById(divName)) // check if it exists 
	{
		// write the new div to the document body
		showDiv = document.createElement("div");
		showDiv.setAttribute("id", divName);
		showDiv.setAttribute("style", style);
		showDiv.setAttribute("className", className);
		document.body.appendChild(showDiv);
	}

	target = getElemById(divName);
	return target;
}


function getElemById(id)
{
	elem = null;
	if(document.getElementById)
	{
		elem = document.getElementById(id);
	}else if(document.all)
	{
		elem = document.all[id];
	}
	return elem;
}