//=============================================================================
// Generic lightbox popup function
//=============================================================================

//var box = CreatePopup( "auto", "auto", "myboxid" );

function CreatePopup( x, y, id )
{
	// Setup popup container

	var popupContainer = $( "<div />" );

	popupContainer.attr( "class", "pop_container" );
	popupContainer.attr( "id", id );

	popupContainer.css( "z-index", 100 );
	popupContainer.css( "top", 0 );
	popupContainer.css( "left", 0 );
	popupContainer.css( "width", $( "body" ).width() );

	if( msieversion() >= 6 && msieversion() < 8 )
	{
		popupContainer.css( "height", $( window ).height() );
		popupContainer.css( "position", "absolute" );

		$( window ).scroll( function() { OnScroll( popupContainer ); } );
	}
	else
	{
		popupContainer.css( "height", Math.max( $( "body" ).height(), $( window ).height() ) );
		popupContainer.css( "position", "fixed" );
	}

	// Setup popup background

	var popupBG = $( "<div />" );

	popupBG.attr( "class", "pop_bg" );
	popupBG.css( "z-index", 101 );
	popupBG.css( "top", 0 );
	popupBG.css( "left", 0 );
	popupBG.css( "width", popupContainer.css( "width" ) );
	popupBG.css( "height", popupContainer.css( "height" ) );
	popupBG.css( "position", "relative" );
	popupBG.css( "top", 0 );
	popupBG.css( "left", 0 );
	popupBG.click( function() { OnCloseClick( popupContainer ); } );
	
	// Allow escape key to close window

	$( window ).keydown( function( e )
	{
		if( e.keyCode == 27 )
		{
			$( window ).unbind( "keydown" );
			OnCloseClick( popupContainer );
		}
	});

	// Setup the popup box that will hold the actual content

	var popupBox = $( "<div />" );

	popupBox.attr( "class", "pop_box" );
	popupBox.css( "z-index", 102 );
	popupBox.css( "position", "absolute" );

	var popupBoxInner = $( "<div />" );

	popupBoxInner.attr( "class", "pop_boxinner" );
	popupBoxInner.css( "position", "relative" );

	popupBox.append( popupBoxInner );

	// Setup close button

	var closeButton = $( "<a />" );

	closeButton.attr( "class", "pop_closebutton" );
	closeButton.css( "display", "block" );
	closeButton.css( "z-index", 103 );
	closeButton.css( "position", "absolute" );
	closeButton.click( function() { OnCloseClick( popupContainer ); } );

	popupBoxInner.append( closeButton );

	// Place all elements into container, then onto body

	popupContainer.append( popupBG );
	popupContainer.append( popupBox );

	$( "body" ).append( popupContainer );

	PositionPopup( popupContainer, x, y, "auto", "auto", false );	// grabbing width/height from css because at this point nothing is inside the lightbox

	var shadowAlpha = ( popupBG.css( "widows" ) != "" ? ( parseInt( popupBG.css( "widows" ) ) / 100 ) : popupBG.css( "opacity" ) );

	popupContainer.children( "div.pop_bg" ).css( "opacity", shadowAlpha );

	if( msieversion() >= 6 && msieversion() < 7 )
	{
		popupContainer.children( "div.pop_bg, div.pop_boxout" ).css( "display", "block" );
	}
	else
	{
		popupContainer.children( "div.pop_bg, div.pop_boxout" ).css( "display", "none" ).fadeIn( "fast" );
	}

	if( msieversion() >= 6 && msieversion() < 8 )
	{
		OnScroll( popupContainer );
	}

	return( popupBoxInner );
}

function PositionPopup( popupContainer, x, y, width, height, animate )
{
	var popupBox = popupContainer.children( "div.pop_box" );

	if( width == "auto" || width == null )
	{
		width = parseInt( popupBox.css( "width" ) );
	}

	if( height == "auto" || height == null )
	{
		height = parseInt( popupBox.css( "height" ) );
	}

	if( x == "auto" || x == null )
	{
		x = parseInt( ( $( window ).width() / 2 ) - ( width / 2 ) );
	}

	if( y == "auto" || y == null )
	{
		y = parseInt( ( $( window ).height() / 2 ) - ( height / 2 ) );
	}

	if( animate )
	{
		popupBox.animate( { width: width + "px", height: height + "px", left: x, top: y }, 200 );
	}
	else
	{
		popupBox.css( { width: width + "px", height: height + "px", left: x, top: y } );
	}

	var popupBoxInner = popupBox.children( "div.pop_boxinner" );

	popupBoxInner.css( "width", width + "px" );
	popupBoxInner.css( "height", height + "px" );
}

function OnScroll( popupContainer )
{
	popupContainer.css( "top", $( window ).scrollTop() );
}

function OnCloseClick( popupContainer )
{
	popupContainer.children( "div.pop_bg, div.pop_boxout" ).each( function()
	{
		if( msieversion() >= 6 && msieversion() < 7 )
		{
			WipeContents( popupContainer );
		}
		else
		{
			$( this ).fadeOut( "fast", function() { WipeContents( popupContainer ); } );
		}
	});
}

function WipeContents( popupContainer )
{
	popupContainer.find( "object" ).each( function( i )
	{
		swfobject.removeSWF( $( this ).attr( "id" ) );
	});

	popupContainer.remove();
}
