var isIE = navigator.appName == 'Microsoft Internet Explorer';
var isIE6 = false;
if (isIE) {
	var ua = navigator.userAgent;
	var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua) != null) {
		var ver = parseFloat(RegExp.$1);
		this.isIE6 = ver == 6;
	}
}
	
function drawOverlay(whichSrc, w, h) {
	window.overlayWidth = w == undefined ? 435 : w;
	window.overlayHeight = h == undefined ? 650 : h;
	var _outlineWidth = 10;
	
	window.overlayContainer = document.body;
	initResize();
	
	//draw shade
	var a = document.body.appendChild(document.createElement('div'));
	window.overlayShade = a;
	a.id = "overlay";
	var bodyElem = $$('body')[0];
	var totalHeight = bodyElem.getHeight()+20;
	with (a.style) {
		position = "absolute";
		top = "0px";
		left = "0px";
		zIndex = "100000";
		width = "100%";
		height = totalHeight + "px";
		backgroundColor = "#8cc63f";
	}
	if (isIE == true) {
		a.style.filter = "alpha(opacity=45)";
	} else {
		a.style.MozOpacity = 0.5;
	}
	
	//draw iframe
	var f = document.createElement('iframe');
	window.overlayBox = f;
	f.id = "overlayBox";
	f.frameBorder = 0;
	f.scrolling = "no";
	f.allowTransparency = "true";
	var fW = overlayWidth - (_outlineWidth * 2);
	var fH = overlayHeight - (_outlineWidth * 2);
	with (f.style) {
		position = "absolute";
		zIndex = "100001";
		width = overlayWidth + "px";
		height = overlayHeight + "px";
	}
	f.src = whichSrc;
	document.body.appendChild(f);
	
	
	doResize();
}

function doResize() {
	//we handle the nonexistence of this since ie6 doesn't actually unregister the events...
	if ($("overlayBox") != undefined) {
		var bodyWidth = document.viewport.getWidth();
		var bodyHeight = document.viewport.getHeight();
		var curScrollTop = document.viewport.getScrollOffsets().top;
		
		$("overlayBox").setStyle({
			left:((bodyWidth / 2) - (overlayWidth / 2))+'px',
			top:(curScrollTop + ((bodyHeight / 2)) - (overlayHeight / 2))+'px'});
		if (window.$onresize != undefined) {
			window.$onresize();
		}
	}
}

function initResize() {
	if (window.onresize != undefined) {
		window.$onresize = window.onresize;
	}

	window.onresize = doResize;
	window.onscroll = doResize;
}

function closeOverlayFromChild() {
	window.parent.closeOverlay();
}

function closeOverlay() {
	//this function is usually called from the child window - hence the references to "top"
	window.onresize = null;
	window.onscroll = null;
  
	if (window.$onresize != undefined) {
		window.onresize = window.$onresize;
	}
	
	window.overlayContainer.removeChild(window.overlayShade);
	window.overlayContainer.removeChild(window.overlayBox);
}

function fixBGs() {
	if (isIE6 == true) {
		var bgs = document.getElementsByTagName('div');
		for (var i=0; i<bgs.length; i++) {
			fixPNG(bgs[i]);
		}
	}
}

function fixPNG(whichElement){
	if (isIE6 == true) {
		bgImg = whichElement.style.backgroundImage;
		if (bgImg && bgImg != 'none') {
			if (bgImg.match(/^url[("']+(.*\.png)[)"']+$/i)) {
				var s = RegExp.$1;
				whichElement.style.backgroundImage = 'none';
				filt(whichElement, s, 'crop');
			}
		}
	}
}

function filt(whichElement, s, m){
	var f = 'DXImageTransform.Microsoft.AlphaImageLoader';
	whichElement.style.filter = 'progid:' + f + '(src="' + s + '",sizingMethod="' + m + '")';
}
