// JavaScript Document
// Popup handling

function initPopup(id,pw,ph){
	id = '#' + id;
	var oldScrollY = 0; var resizeEvent = 0;
	var bw = $('body').width();
	var bh = $('body').height();
	var ww = $(window).width();
	var wh = $(window).height();
	
	$('#popupBg').css({
		display: 'block',
		width: bw,
		height: bh,
		opacity: '0'
	}).fadeTo('normal', 0.8);

	if ( pw != 'auto' || ph != 'auto' ) {
		pw = pw + 'px'; ph = ph + 'px';
	}

	$(id).css({ width: pw, height: ph });
	if ( pw = 'auto' ) pw = $(id).width();
	if ( ph = 'auto' ) ph = $(id).height();
	
	$(id).css({
		top: (wh - ph)/2 + 'px',
		left: (ww - pw)/2 + 'px'
	}).fadeIn('normal');
	
	$(window).bind('resize',function(){
		adjustPBg(); adjustP(id,pw,ph,'undefined');
	});
	$(document).bind('scroll',function(){
		oldScrollY = adjustP(id,pw,ph,oldScrollY);
	});
}

function adjustPBg(){
	var bw = $('body').width();
	var bh = $('body').height();
	$('#popupBg').width(bw + 'px');
	$('#popupBg').height(bh + 'px');
}

function adjustP(id,pw,ph,oldScrollY) {
	var scrollY = $(window).scrollTop();
	var offsetY = parseInt($(id).css('top'));
	var ww = $(window).width();
	var wh = $(window).height();
	var pPos;

	if ( oldScrollY != 'undefined' ) {
		pPos = oldScrollY - scrollY;
		$(id).css('top',offsetY - pPos + 'px');
		return scrollY;
	}
	if ( oldScrollY == 'undefined' ) {
		$(id).css({
			top: (wh - ph)/2 + 'px',
			left: (ww - pw)/2 + 'px'
		});
		return;
	}
}

function hideP() {
	$('#popupBg').fadeOut('normal');
	$('.popup').fadeOut('normal');
	$(window).unbind('resize');
	$(document).unbind('scroll');
}