overlay_toggle = function(arg) {
	if (arg && !popup_lock) {
		flash_toggle(false);
		popup_lock = true;
		$('#overlay').show().fadeTo(effects_time,0.7);
	} else if (!arg && popup_lock) {
		$('#overlay').stop().fadeTo(effects_time,0,function() {
			$('#overlay').hide();
			popup_lock = false;
			flash_toggle(true);
			});
	}
	return false;
}
popup_toggle = function(arg,image) {
	if (arg) {
		if (image) {
			var mw = minmax('min',$('html').innerWidth(),$('body').innerWidth()) - 70;
			var mh = minmax('min',$('html').innerHeight(),$('body').innerHeight()) - 70;
			var preload = new Array();
			$('#popup-content img').each(function() {
					preload.push($(this).attr('src'));
				});
			$('#popup').stop().hide().fadeTo(0,0).css({
					'width':'auto',
					'height':'auto'
				}).show(1, function() {
				$('<img>').bind('load', function() {
						if (preload[0]) {
							this.src = preload.shift();
						} else {
							w = this.width + 65;
							h = this.height + 65;
							$('#popup').hide().fadeTo(0,0).css({
								'width'		: w + 'px',
								'height'	: h + 'px',
								'top'		: Math.round((mh-h) / 2) + minmax('max',$('html').scrollTop(),$('body').scrollTop()) + 'px',
								'left'		: Math.round((mw-w) / 2) + minmax('max',$('html').scrollLeft(),$('body').scrollLeft()) + 'px'
								}).show().fadeTo(effects_time,1);
						}
					}).trigger('load');
				});
		} else {
			$('#popup').stop().css({
				'opacity'	: 0,
				'max-height'	: (minmax('min',$('html').innerHeight(),$('body').innerHeight()) - 70) + 'px',
				'width'		: (minmax('min',$('html').innerWidth(),$('body').innerWidth()) - 70) + 'px',
				'top'		: minmax('max',$('html').scrollTop(),$('body').scrollTop()) + 'px',
				'left'		: minmax('max',$('html').scrollLeft(),$('body').scrollLeft()) + 'px'
				}).show().fadeTo(effects_time,1);
				//todo: fix for ie
		}
	} else {
		$('#popup').stop().fadeTo(effects_time,0,function() {
			$('#popup').hide();
			$('#popup-content').html('');
			});
	}
	return false;
}
popup_close = function() {
	popup_toggle(false);
	return overlay_toggle(false);
}
popup_show = function(code,image) {
	overlay_toggle(true);
	$('#popup-content').html(code);
	popup_toggle(true,image || false);
	return false;
}
popup_image = function(path) {
	return popup_show('<div style="width:100%; text-align:center;"><a href="' + path + '" onclick="return popup_close();"><img src="' + path + '.(' + ($('html').innerWidth() - 130) + 'x' + ($('html').innerHeight() - 130) + ')?nocache=1" alt="" /></a></div>',true);
}
popup_selector = function(selector) {
	return popup_show($(selector).html());
}

$(function() {
	$('body').append('<div id="overlay"></div><div id="popup"><div id="popup-close"><a href="'+self_path+'" onclick="return popup_close();"><img src="'+style_path_generic+'images/button-close.png" alt="" /></a></div><div id="popup-content"></div></div>');
});
