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 = 30; //timeout in milliseconds, 0 = instant fade-out

win = getElement('popin');
winbackground = getElement('popinbackground');
wincontent = getElement('popincontent');
winpicture = getElement('popincontent');
cur_opacity = 0;

var timer = null;
var ffmac = IsFirefoxMac();

/* AJAX */
function populateeditdiv() {
  http_request.onreadystatechange=function() {
    if(http_request.readyState==4) {
      getElement('popinholder').innerHTML = http_request.responseText;
      showWindow();
    }
  }
}
function viewpic(id, dots) {
	var destination = dots + 'getpichtml.php';
	var params = 'id='+id+'&dots='+dots;
	var whereto = 'populateeditdiv';
	return makeGETRequest(destination, params, whereto);
}

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 + 36 + 'px';
		wincontent.style.width = winpicture.width + 12 + 'px';
	} else {
		if (winpicture.style.innerHeight > 0) {
			wincontent.style.height = winpicture.style.innerHeight + 36 + 'px';
			wincontent.style.width = winpicture.style.innerWidth + 12 + 'px';
		}
		wincontent.style.paddingLeft = '6px';
		wincontent.style.paddingRight = '6px';
		wincontent.style.paddingBottom = '8px';
		wincontent.style.paddingTop = '8px';
	}
	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';
	}
}

function hideWindow() {
	win.style.display = 'none';
	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));
}


