// 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	= '600px';
			}

			// 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';
	}

	// Set height popup background (ie has bug height 100%)
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		var imgw	= window.availWidth;
		var imgh 	= window.innerHeight;

	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		var imgw	= document.documentElement.clientWidth;
		var imgh 	= document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		var imgw	= document.body.clientWidth;
		var imgh	= document.body.clientHeight;
	}

	// Fix background to cover full screen
	if (navigator.appName.indexOf('Netscape')>=0) {
		popupBg.style.height		= (imgh < document.body.scrollHeight) ? (document.body.scrollHeight+7)+'px' : imgh+'px';
		popupBg.style.width			= (imgw < document.body.scrollWidth) ? (document.body.scrollWidth-100)+'px' : imgw+'px';
	} else if (navigator.appName.indexOf('Internet Explorer')>=0) {
		if (imgh < document.body.scrollHeight) {
			popupBg.style.height			=  document.body.scrollHeight+'px';
		} else {
			popupBg.style.height			= /*(firstTime) ? (imgh+16)+'px' : */imgh+'px';
			firstTime					 	= false;
		}
		popupBg.style.width			= (imgw < document.body.scrollWidth) ? document.body.scrollWidth+'px' : imgw+'px';
	} else {
		popupBg.style.height		= (imgh < document.body.scrollHeight) ? document.body.scrollHeight+'px' : imgh+'px';
		popupBg.style.width			= (imgw < document.body.scrollWidth) ? document.body.scrollWidth+'px' : imgw+'px';
	}
}

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();
}

