var PopupWindow;
function PopupWin(title, source, height, width, url, type) {
	/*var gogo = false;
	if(CheckPageLoaded) {
		if(isPageLoaded) gogo = true;
	} else {
		gogo = true;	
	}*/
	
	if(isPageLoaded) {
		try {
			if(Browser.Engine.trident4) {
				this.goToCatch();
			} else {
				//If dimensions are bigger than window size, shrink it.
				BodyDimensions = $$('BODY')[0].getSize();
				if(height > BodyDimensions.y) height = BodyDimensions.y;
				if(width > BodyDimensions.x) width = BodyDimensions.x;
				
				var BlurBgDiv = BlurBg(true);
				/*BlurBgDiv.addEvent('click', function() {
					closePopup();
				});*/
				
				var sourceDiv = $$('DIV.PopupWin[title='+source+']')[0];
				var PopupDivHTML  = "<div class='TitleBar CnrTL CnrTR'>";
					PopupDivHTML += "	<h1>"+title+"</h1>";
					PopupDivHTML += "	<span class='CnrTopLeft'></span>";
					PopupDivHTML += "	<span class='CnrTopRight'></span>";
					PopupDivHTML += "</div>";
					PopupDivHTML += "<a class='Close' href='javascript:void(0);' onclick='closePopup();' title='(Esc)'>Close</a>";
					PopupDivHTML += "<div class='PopupContent'>";
					if($type(sourceDiv) != false) {
						PopupDivHTML += sourceDiv.innerHTML;
					} else {
						//PopupDivHTML += "<iframe class='PopupWin' frameborder='0' src='"+source+"' scrolling='auto' width='100%' height='100%'>iframe</iframe>";
						PopupDivHTML += "<div class='Loading'>&nbsp;</div>";
					}
					PopupDivHTML += "</div>";
				
				PopupDiv = new Element('DIV', {
					'id':				'PopupWin',
					'class':			'PopupWin',
					'html':				PopupDivHTML,
					'styles': {
						//'position':		'absolute',
						'display':		'block',
						'position':		'relative',
						'height':		height,
						'width':		width,
						'z-index':		'999'
					}
				}).inject($$('BODY')[0]);

				//PopupDiv.set('html', PopupDivHTML);
				//PopupDiv.injectInside(document.body);
				PopupDiv.getElement('.PopupContent').setStyles({
					'height':		(PopupDiv.offsetHeight-PopupDiv.getElement('.PopupContent').getStyle('margin-top').toInt()-5)+'px',
					'width':		(PopupDiv.offsetWidth-4)+'px'
				});
				
				/* PopupDiv.parentNode == Shadow WRAPPER */
				PopupDivShadow = new Shadow(PopupDiv);
				BodyDimensions = $$('BODY')[0].getSize();
				PopupDiv.parentNode.setStyles({
					'position':		'fixed',
					'top':			'-' + height + 'px',
					'left':			((BodyDimensions.x/2)-(width/2)),
					'z-index':		999
				});
				
				var PopupTween = new Fx.Tween(PopupDiv.parentNode, {duration: 750, transition: Fx.Transitions.Quad.easeOut});
				
				PopupTween.addEvent('onComplete', function() {
					if(!$type(sourceDiv)) {
						var PopupContentHTML = "<iframe class='PopupWin' frameborder='0' src='"+source+"' scrolling='auto' width='100%' height='100%'>iframe</iframe>";
						PopupDiv.getElement('.PopupContent').set('html', PopupContentHTML);
						
						PopupWindow = (Browser.Engine.trident5)?PopupDiv.getElement('.PopupWin'):PopupDiv.getElement('.PopupWin').contentWindow;
						
						document.addEvent('keyup', function(e) {
							if(e.key == 'esc') closePopup();
						});
						
						
						// Make popup draggable
						PopupDiv.getElement(".TitleBar").addEvent('mousedown', function(event) {
							PopupWindow.mouseY = event.page.y - PopupDiv.getPosition().y;
							PopupWindow.mouseX = event.page.x - PopupDiv.getPosition().x;
							
							PopupDiv.setStyle('opacity', 0.5);
							PopupDiv.parentNode.getElement(".ShadowClass").setStyle('display', 'none');
							$('BlurBg').setStyle('opacity', 0.2);
							PopupDiv.getElement('.PopupWin').setStyle('display', 'none');
							
							document.body.addEvent('mousemove', dragPopup);
							document.body.addEvent('mouseup', dragPopupStop);
							PopupWindow.Dragged = true;
						}.bind(PopupDiv));
					}

					if(source == 'PleaseWait') CrawlRequest(url, type);
				}.pass(PopupDiv, source));
			
				PopupTween.start('top', ((BodyDimensions.y/4)-(height/4)));
				return PopupTween;
			}
		} catch(err) {
			PopupWindow = window.open(source, 'PopupWin', "top="+((document.documentElement.clientHeight/4)-(height/4)+window.screenTop)+", left="+((document.documentElement.clientWidth/2)-(width/2)+window.screenLeft+10)+", height="+(height-30)+", width="+(width-4)+", resizeable=0, scrollbars=no, location=0, menubar=0, status=0, toolbar=0");
		}
	}
}

function resizePopup(difference) {
	if(Browser.Engine.trident4) {
		window.resizeBy(0, difference);
	} else {
		var frameElement = window.parent.document.body.getElement('IFRAME.PopupWin');
		if(!Browser.Engine.trident4)
			frameElement.getParent().setStyle('height', frameElement.getParent().offsetHeight+difference-2);
		else
			frameElement.setStyle('height', frameElement.offsetHeight+difference);
	
		var shadowElement = frameElement.getParent().getParent().getParent().getElement('.ShadowClass');
		shadowElement.setStyle('height', shadowElement.offsetHeight+difference);
	
		shadowElement.getElement('.ml').setStyle('height', shadowElement.getElement('.ml').offsetHeight+difference);
		shadowElement.getElement('.mc').setStyle('height', shadowElement.getElement('.mc').offsetHeight+difference);
		shadowElement.getElement('.mr').setStyle('height', shadowElement.getElement('.mr').offsetHeight+difference);
	}
}

function dragPopup(event) {
	$('PopupWin').parentNode.setStyles({
		'top':			event.page.y - PopupWindow.mouseY,
		'left':			event.page.x - PopupWindow.mouseX
	});
	
	if(Browser.Engine.trident) document.selection.empty();
	else window.getSelection().removeAllRanges();
}

function dragPopupStop() {
	if(PopupDiv) PopupDiv.setStyle('opacity', 1);
	if(PopupDiv.parentNode.getElement(".ShadowClass")) PopupDiv.parentNode.getElement(".ShadowClass").setStyle('display', 'block');
	if($('BlurBg')) $('BlurBg').setStyle('opacity', 0.8);
	if(PopupDiv.getElement('.PopupWin')) PopupDiv.getElement('.PopupWin').setStyle('display', 'block');
					
	document.body.removeEvent('mousemove', dragPopup);
}

function closePopup(redirect) {
	//alert("closepopup");
	if(Browser.Engine.trident4 && opener) {
		if(redirect) opener.location = redirect;
		self.close();
	} else {
		if(redirect) {
			window.parent.location = redirect;
		} else {
			var PopupWin = $('PopupWin');
			var BlurBg = $('BlurBg');
			
			// Remove dragging event
			document.body.removeEvent('mouseup', dragPopupStop);
			// If it was a Gallery Slideshow, remove left/right key events
			document.removeEvents('keydown');
			
			if(PopupWin) PopupWin.parentNode.destroy();
			if(BlurBg) closeBlurBg();
		}
	}
}
