// Pop-up text boxes

$( 'head' ).append ( '<link rel="stylesheet" type="text/css" href="css/pop.css" media="screen" />' );

var popOverlay, popFrame, frameHeight, frameWidth, id, borders;
var pop_done = false;
var backgroundOpacity = 0.7 ;

$( document ).ready ( function () {
	$( ".pop" ).click ( function () {
		if ( pop_done ) return false;
		popOverlay = $( this ).next( 'div' );
		popFrame = $( this ).next( 'div' ).next( 'div' );
 		borders = parseInt ( popFrame.children ( "img" ).css( 'borderTopWidth' )) * 2;
		frameWidth = parseInt ( popFrame.children ( "img" ).css ( "width" )) + borders;
		frameHeight = parseInt ( popFrame.children ( "img" ).css ( "height" )) + 54;
		pop_show ();
		return false;
	});
});

function pop_show () {
	if ( !pop_done ) {
		$( popOverlay ).click ( pop_hide );
 		$( popFrame ).click ( pop_hide );
		$( window ).resize ( pop_position ).scroll ( pop_position );
		pop_done = true;
	}
	pop_position ();
}

function pop_hide() {
	$( popFrame ).slideUp ( "medium", function ()
		{
			$( popOverlay ).fadeOut ( "fast" );
			pop_done = false;
		}
	);
	return false;
}

function pop_position() {
	if ( pop_done ) {
		$( popOverlay ).css (
			{
				height: $( document ).height() + "px",
				width: $( document ).width() + "px",
				opacity: backgroundOpacity
			}
		).fadeIn ( "fast", function ()
			{
				$( popFrame ).css (
					{
						width: frameWidth,
						left: (( $( window ).width() - frameWidth ) / 2 ) + "px",
						top: ((( $( window ).height() - frameHeight ) / 2 ) + $( window ).scrollTop() ) + "px",
						display: "none",
						opacity: 1
					}
				).slideDown ( "medium" );
			}
		);
	}
}

