// Set var for IE
var firstTime	= true;
var loadImageObj;

// Create xmlHttp request
function createRequest(){

	if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if(window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	}
	return xmlHttp;
}

function openPopup(url, w, h, parameters) {

	var popupDiv		= document.getElementById('popup');
	var popupBg			= document.getElementById('popupBackground');
	var popupDivContent	= document.getElementById('popupContent');
	var xmlHttp 		= createRequest();
	loadImageObj		= xmlHttp;

	// Add content to popup
	var iLoadingTimer	= setTimeout(function () {
		popupDivContent.innerHTML			= '<div id="closeButton" onclick="closePopup(loadImageObj);">[x]</div><div style="display:table-cell;height:150px;width:188px;text-align:center;vertical-align:middle;font-size:150px;"><img src="img/loader.gif" style="vertical-align:middle;" /></div>';
		// Fix png, ie check is in function
		fixPopup();
		// Show popup
		popupDiv.style.visibility			= "visible";
	}, 100);

	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){

			popupDivContent.innerHTML			= '<div id="closeButton" onclick="closePopup();">[x]</div>'+xmlHttp.responseText;

			if (popupDivContent.clientHeight>600) {
				popupDivContent.style.height	= 'auto';
			}

			// Fix png, ie check is in function
			correctPNG();

			fixPopup();

			clearTimeout(iLoadingTimer);

			popupDiv.style.visibility			= "visible";
		}
	}
	xmlHttp.send(null);
}

function closePopup() {

	if (arguments[0]) arguments[0].abort();

	document.getElementById('popup').style.visibility	= 'hidden';
	document.getElementById('popupContent').innerHTML	= '';
	div2 = document.getElementById('popupContent');
	div2.style.width	= '';
	div2.style.height	= '';
	fixPopup();
}

function fixPopup() {

	var popupBg			= document.getElementById('popupBackground');
	var popupDivContent	= document.getElementById('popupContent');

	// Center popup
	var h					= popupDivContent.clientHeight;
	var w					= popupDivContent.clientWidth;

	popupDivContent.style.marginTop		= -(h/2)+'px';
	popupDivContent.style.marginLeft	= -(w/2)+'px';

	// Check if top goes outside of browser
	if (popupDivContent.offsetTop<0) {
		popupDivContent.style.marginTop		= '0px';
		popupDivContent.style.top			= '0px';
	}
	if (popupDivContent.offsetLeft<0) {
		popupDivContent.style.marginLeft	= '0px';
		popupDivContent.style.left			= '0px';
	}

	$("#popupBackground").height(0);
	$("#popupBackground").height($(document).height());
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters))
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }
}

function resizeVideo(width, height){
	// div
	var div	= document.getElementById('videoDiv');
	div.style.width		= width+'px';
	div.style.height	= height+'px';

	var div2	= document.getElementById('popupContent');
	div2.style.width	= width+40+'px';
	div2.style.height	= height+40+'px';

	fixPopup();
}

function resizeImg(width, height){

	var div2	= document.getElementById('popupContent');
	div2.style.width	= width+40+'px';
	div2.style.height	= height+40+'px';

	fixPopup();
}