// Constructor code for all of the popup window elements

var g_popup_loading = false;
var g_popup_locked = false;

window.onresize = function() 
{
	popup_center();
}
/*
window.onscroll = function() 
{
	popup_center();
}
*/

function popup_open( obj, title, width, height )
{
	popup_bg_create();
	popup_create( title );
	
	var popup = document.getElementById( 'popup' );
	
	if( width ) { popup.style.width = width + 'px'; }
	if( height ) { popup.style.height = height + 'px'; }
	
	// add the content to the popup window
	if( obj )
	{
		popup.appendChild( obj );	
	}
	
	popup.style.display = "block";
	popup_center();
}

function popup_bg_create()
{
	if( !document.getElementById( 'popup_click' ) )
	{
		var obj = Builder.node( 'div', {	id: 'popup_click', 
											className: 'click',
											onclick: 'popup_close();' } );
		obj.style.display = "block";
		document.body.appendChild( obj );
		//Effect.Appear( 'popup_click', { duration: 0.2 } );
	}	
}

function testo()
{
	alert( 'transition doneo!' );
}

function popup_create( title )
{
	popup_destroy();
	var root = "";
	if( g_root ) root = g_root;
	
	popup_hide_loading();
	
	var obj = Builder.node( 'div', {	id: 'popup', 
										className: 'popup popup_window', 
										style: 'display:none' } );
	
	var header = Builder.node( 'div', { className: 'popup_header' } );
	if( title )
	{
		header.appendChild( Builder.node( 'h1' ).update( title ) );	
	}
	
	var clear = Builder.node( 'div', { className: 'popup_clear' }, '' );
	
	header.appendChild( Builder.node( 'div', { className: 'popup_close' } , [ 
		Builder.node( 'a', { href:'', onclick:'popup_close(); return false;' }, [ 
			Builder.node( 'img', { src: root + '/images/popup_close.gif', alt:'close' } )  
			] )
		] )  );
	
	header.appendChild( clear );
	obj.appendChild( header );
	
	document.body.appendChild( obj );
}

function popup_close()
{
	if( !g_popup_loading )
	{
		// fade out and delete the windows
		if( document.getElementById( 'popup_click' ) ) { Effect.Fade( 'popup_click', { duration: 0.2, afterFinish: popup_bg_destroy } ); }
		if( document.getElementById( 'popup' ) ) { Effect.Fade( 'popup', { duration: 0.2, afterFinish: popup_destroy } ); }
	}
}

function popup_kill()
{
	popup_loading_destroy();
	popup_destroy();
	if( document.getElementById( 'popup_click' ) ) { Effect.Fade( 'popup_click', { duration: 0.2, afterFinish: popup_bg_destroy } ); }
}

function popup_loading_destroy()
{
	var obj = document.getElementById( 'popup_loading' );
	if( obj )
	{
		document.body.removeChild( obj );
	}
}

function popup_bg_destroy()
{
	var obj = document.getElementById( 'popup_click' );
	if( obj )
	{
		document.body.removeChild( obj );
	}
}

function popup_destroy()
{
	var obj = document.getElementById( 'popup' );
	if( obj )
	{
		document.body.removeChild( obj );
	}
	g_popup_locked = false;
}

function popup_center( id )
{
	if( !id )
	{
		id = 'popup';	
	}
	var obj = document.getElementById( id );
	
	if( obj )
	{
		var w = 0;
		var h = 0;
		
		if( !window.innerWidth )
		{
			// Internet Explorer
			if( !( document.documentElement.clientWidth == 0 ) )
			{
				// Strict mode
				w = document.documentElement.clientWidth;
				h = document.documentElement.clientHeight;
				offset_x = document.documentElement.scrollLeft;
				offset_y = document.documentElement.scrollTop;
			}
			else
			{
				// Quirks mode
				w = document.body.clientWidth;
				h = document.body.clientHeight;
				offset_x = document.body.scrollLeft;
				offset_y = document.bodyscrollTop;
			}
		}
		else
		{
			// W3C
			w = window.innerWidth;
			h = window.innerHeight;
			offset_x = window.pageXOffset;
			offset_y = window.pageYOffset;
		}
		
		var x = parseInt( ( ( w - parseInt( obj.scrollWidth ) ) / 2 ) + offset_x );
		var y = parseInt( ( ( h - parseInt( obj.scrollHeight ) ) / 2 ) + offset_y );
		
		if( x < 0 ) { x = 0; }
		if( y < 0 ) { y = 0; }
		
		obj.style.top = y + 'px';
		obj.style.left = x + 'px';
	}
}

function popup_show_loading()
{
	if( !g_popup_locked )
	{
	g_popup_locked = true;
	g_popup_loading = true;
	var root = "";
	if( g_root ) root = g_root;
	
	popup_bg_create();
	
	var popup = document.getElementById( 'popup' );
	if( popup )
	{
		popup.style.display = "none";
	}
	document.body.appendChild( Builder.node( 'img', { id: 'popup_loading', src: root + '/images/loading_bar.gif', className: 'popup' } ) );
	popup_center( 'popup_loading' );
	}
}

function popup_hide_loading()
{
	var obj = document.getElementById( 'popup_loading' );
	if( obj )
	{
		obj.parentNode.removeChild( obj );
	}
	var popup = document.getElementById( 'popup' );
	if( popup )
	{
		popup.style.display = "block";
	}
	g_popup_loading = false;
}
