// Pop-up other web pages

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

var page_done = false;
var frameWidth, frameHeight, windowHeight;
var backgroundOpacity = 0.7 ;

$( document ).ready ( function ()
	{
		$( ".page" ).each ( function ()
			{
				var tip_text = $( this ).attr ( "title" );
				if ( tip_text ) $( this ).removeAttr ( "title" ).attr ( "tip_title", tip_text );
			}
		);
		$( ".page" ).click ( function ()
			{
				page_show ( $( this ).attr ( "tip_title" ), this.href, 0, 800 );
				return false;
			}
		);
	}
);

function page_show ( caption, url, height, width ) {
	frameWidth = width;
	if ( !page_done ) {
		$( document.body ).append( "<div id='page_overlay'></div><div id='page_window'><div id='page_caption'></div><img src='img/close.gif' alt='Close window' /></div>" );
		$( "#page_window img" ).click ( page_hide );
		$( "#page_overlay" ).click ( page_hide );
		$( window ).resize ( page_position ).scroll ( page_position );
		page_done = true;
	}
	$( "#page_frame" ).remove();
	$( "#page_window" ).append ( "<iframe id='page_frame' src='"+url+"'></iframe>" );
	$( "#page_caption" ).html ( caption );
	page_position();
}

function page_hide() {
	$( '#page_window' ).slideUp ( "medium", function ()
		{
			$( '#page_overlay' ).fadeOut ( "fast" );
			page_done = false;
		}
	);
	return false;
}

function page_position() {
	if ( page_done ) {
		$( "#page_overlay" ).css (
			{
				height: $( document ).height() + "px",
				width: $( document ).width() + "px",
				opacity: backgroundOpacity
			}
		).fadeIn ( "fast", function ()
			{
				windowHeight = $( window ).height();
				frameHeight = windowHeight - 30;
				$( "#page_window" ).css (
					{
						width: frameWidth + "px",
						height: frameHeight + "px",
						left: (( $( window ).width() - frameWidth ) / 2 ) + "px",
						top: ((( windowHeight - frameHeight ) / 2 ) + $( window ).scrollTop() ) + "px",
						display: "none",
						opacity: 1
					}
				);
				$( "#page_frame" ).css ( "height", frameHeight - 32 + "px" );
				$( "#page_window" ).slideDown ( "medium" );
			}
		);
	}
}
