end_opacity = 70; //end opacity, 25 = 25%, 50 = 50%, 100 = 100%, etc.
increase_opacity_by = 10; //how much to increase by each time the timeout ends
timeout = 50; //timeout in milliseconds, 0 = instant fade-out

win = document.getElementById('popin');
winbackground = document.getElementById('popinbackground');
wincontent = document.getElementById('popincontent');
winpicture = document.getElementById('popinpicture');
cur_opacity = 0;

var timer = null;
var ffmac = IsFirefoxMac();
var width = 300;
var height = 193;
var fadedin = false;

function makePOSTRequest(url, parameters, whereto) {
   http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
             // set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }

   eval ("http_request.onreadystatechange = "+whereto+";");
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
}

function showPic(img) {
	fadedin = false;
	var params = 'imgid='+img;
	winpicture.src = 'assets/images/gala2010/pic'+img+'.jpg';
	makePOSTRequest('getimagesize.php', params, 'setsize');
}

function showPic2011(img) {
	fadedin = false;
	var params = 'imgid='+img+'&year=2011';
	winpicture.src = 'assets/images/gala2011/pic'+img+'.jpg';
	makePOSTRequest('getimagesize.php', params, 'setsize');
}

function setsize() {
  var dims = new Array();
  http_request.onreadystatechange=function() {
    if(http_request.readyState==4) {
      dimensions = http_request.responseText;
      dims = dimensions.split(';');
      width = parseInt(dims[1]);
      height = parseInt(dims[0]);
      showWindow();
      locate = setTimeout("initial_locate()",timeout);
    }
  }
}

function showWindow() {
	if(timeout > 0) {
		cur_opacity = 0;
		if (ffmac) {
			winbackground.style.background = 'transparent';
			winbackground.style.opacity = 1;
			winbackground.style.filter = "alpha(opacity=100)";
		} else {
			winbackground.style.opacity = cur_opacity / 100;
			winbackground.style.filter = "alpha(opacity=" + cur_opacity + ")";
		}
		win.style.display = 'block';
		wincontent.style.display = 'none';

		timer = setTimeout("increase_opacity()",timeout);
	} else {
		winbackground.style.opacity = end_opacity / 100;
		winbackground.style.filter = "alpha(opacity=" + end_opacity + ")";
		win.style.display = 'block';
		wincontent.style.display = 'block';
	}
	CalibratePos();
	if (winpicture.height > 0) {
		wincontent.style.height = winpicture.height + 20 + 'px';
		wincontent.style.height = (height + 20) + 'px';
		wincontent.style.width = winpicture.width  + 'px';
		wincontent.style.width = (width + 2) + 'px';
	} else {
		wincontent.style.height = winpicture.style.innerHeight + 20 + 'px';
		wincontent.style.height = (height + 20) + 'px';
		wincontent.style.width = winpicture.style.innerWidth  + 'px';
		wincontent.style.width = (width + 2) + 'px';
		/*wincontent.style.paddingLeft = '6px';
		wincontent.style.paddingRight = '6px';
		wincontent.style.paddingBottom = '16px';*/
	}
	CalibratePos();
	SetEventHandler("resize", CalibratePos);
	SetEventHandler("scroll", CalibratePos);
	SetEventHandler("DOMMouseScroll", CalibratePos);
}

function increase_opacity() {
	cur_opacity += increase_opacity_by;

	if (!ffmac) {
		winbackground.style.opacity = cur_opacity / 100;
		winbackground.style.filter = "alpha(opacity=" + cur_opacity + ")";
	} else {
		winbackground.style.background = "url('assets/images/fadeout/fade"+cur_opacity+".png')";
	}

	if(cur_opacity < end_opacity) {
		timer = setTimeout("increase_opacity()",timeout);
	} else {
		wincontent.style.display = 'block';
		fadedin = true;
	}
}

function initial_locate() {
	if (fadedin) {
		CalibratePos();
	} else {
		locate = setTimeout("initial_locate()",timeout);
	}
}

function hideWindow() {
	win.style.display = 'none';
	fadedin = false;
	RemoveEventHandler("resize", CalibratePos);
	RemoveEventHandler("scroll", CalibratePos);
	RemoveEventHandler("DOMMouseScroll", CalibratePos);
	CalibratePos();
}



// Below this line is borrowed from newgrounds.com and edited or improved for our use

function CalibratePos() {
	// Let us set up the positioning of the background and the container,
	// prior to doing anything else.
//	wincontent.width = '250px';

	winbackground.style.width = (ScrollDimensionsInfo()[0] + GetPageScrolledPosition()[0]) + "px";
	winbackground.style.height = ScrollDimensionsInfo()[1] + "px";
	if (document.getElementById('ieframe')) document.getElementById('ieframe').style.height = ScrollDimensionsInfo()[1] + "px";
//	winbackground.style.zIndex = 20;

	var popup_height;
	var popup_width;

	// Little trick to get the dimensions of a possibly hidden element
	var element_hidden = (wincontent.style.display == "none");

	wincontent.style.visibility = "hidden";
	wincontent.style.display = "block";
	popup_height = wincontent.clientHeight;
	popup_width = wincontent.clientWidth;

	if(element_hidden) {
		wincontent.style.display = "none";
	}
	wincontent.style.visibility = "visible";

	// Now calculate our location based on scrolling and window size
	var top = Math.round((InnerDimensionsInfo()[1] - popup_height) / 2);
	if(top < 0) {
		top = 0;
	}
	top += GetPageScrolledPosition()[1];

	var left = Math.round((InnerDimensionsInfo()[0] - popup_width) / 2);
	if(left < 0) {
		left = 0;
	}
	left += GetPageScrolledPosition()[0];

	wincontent.style.top = top + "px";
	wincontent.style.left = left + "px";
//	wincontent.zIndex = 99;

}

// Browser related functions.
function InnerDimensionsInfo() {
	var dimensions = new Array(2);

	if(document.documentElement && document.documentElement.clientWidth) {
		dimensions[0] = document.documentElement.clientWidth;
		dimensions[1] = document.documentElement.clientHeight;
	} else if(self.innerWidth) {
		dimensions[0] = self.innerWidth;
		dimensions[1] = self.innerHeight;
	} else if(document.body) {
		dimensions[0] = document.body.clientWidth;
		dimensions[1] = document.body.clientHeight;
	}

	return(dimensions);
}

function ScrollDimensionsInfo() {
	var dimensions = new Array(2);
	var height;

	if((window.innerHeight && (window.scrollMaxY || window.pageYOffset))) { // || document.body.scrollWidth) {
		dimensions[0] = document.body.scrollWidth;
		height = (window.scrollMaxY?window.scrollMaxY:window.pageYOffset);
		dimensions[1] = window.innerHeight + height;
	} else if((document.body.scrollHeight > document.body.offsetHeight)) { // || document.body.scrollWidth) {
		dimensions[0] = document.body.scrollWidth;
		dimensions[1] = document.body.scrollHeight;
	} else {
		dimensions[0] = document.body.offsetWidth;
		dimensions[1] = document.body.offsetHeight;
		if (dimensions[1] < InnerDimensionsInfo()[1])
			dimensions[1] = InnerDimensionsInfo()[1];
	}

	return(dimensions);
}

function GetPageScrolledPosition() {
	var dimensions = new Array(2);

	if(self.pageYOffset || self.pageXOffset) {
		dimensions[0] = self.pageXOffset;
		dimensions[1] = self.pageYOffset;
	} else if((document.documentElement) && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) {
		dimensions[0] = document.documentElement.scrollLeft;
		dimensions[1] = document.documentElement.scrollTop;
	} else if(document.body) {
		dimensions[0] = document.body.scrollLeft;
		dimensions[1] = document.body.scrollTop;
	}

	return(dimensions);
}

function SetEventHandler(eventname, handler) {
	if(window.addEventListener) {
		window.addEventListener(eventname, handler, false);
	} else if(window.attachEvent) {
		window.attachEvent("on" + eventname, handler);
	}
}

function RemoveEventHandler(eventname, handler) {
	if(window.removeEventListener) {
		window.removeEventListener(eventname, handler, false);
	} else if(window.detachEvent) {
		window.detachEvent("on" + eventname, handler);
	}
}

// Necessary for an ugly hack - FF/Mac can't handle Flash combined with CSS opacity
function IsFirefoxMac() {
	var userAgent = navigator.userAgent.toLowerCase();
	return((userAgent.indexOf('mac') != -1) && (userAgent.indexOf('firefox') != -1));
}

