liwe.lightbox = {};

liwe.lightbox.opacity = 70;
liwe.lightbox.fade = true;
liwe.lightbox._is_show = false;

liwe.lightbox._back_div  = null;
liwe.lightbox._front_div = [];

liwe.lightbox.events = {
	"click" : null
};

liwe.lightbox.create = function ( id, w, h, x, y )
{
	if ( liwe.lightbox._front_div.length == 0 )
	{
		var ua = navigator.userAgent.toLowerCase ();
		var an = navigator.appName.toLowerCase ();
		if ( ua.indexOf ( "msie 6" ) >= 0 )
			liwe.lightbox._save_windowed_status ();
		else if ( an.indexOf ( "netscape" ) >= 0 )
			liwe.lightbox._save_overflow_status ();
	}

	var back = liwe.lightbox._add_back_div ();
	var div  = liwe.lightbox._add_front_div ( id, w, h, x, y );

	return div;
};

liwe.lightbox.created = function ()
{
	return liwe.lightbox._back_div != null;
};

liwe.lightbox.close = function ()
{
	var d;

	d = liwe.lightbox._front_div.pop ();
	liwe.dom.remove_element ( d );

	if ( ! liwe.lightbox._front_div.length )
	{
		var ua = navigator.userAgent.toLowerCase ();
		var an = navigator.appName.toLowerCase ();
		if ( ua.indexOf ( "msie 6" ) >= 0 )
			liwe.lightbox._restore_windowed_status ();
		else if ( an.indexOf ( "netscape" ) >= 0 )
			liwe.lightbox._restore_overflow_status ();

		liwe.lightbox._back_div.style.display = "none";
	}

	liwe.dom.remove_element ( liwe.lightbox._back_div );
	liwe.lightbox._back_div = null;
};

liwe.lightbox.show = function ()
{
	var div = liwe.lightbox._front_div [ liwe.lightbox._front_div.length -1 ];

	liwe.fx.set_opacity ( liwe.lightbox._back_div, liwe.lightbox.opacity );
	liwe.lightbox._back_div.style.display = "block";

	if ( liwe.lightbox.fade )
	{
		liwe.fx.set_opacity ( div, 0 );
		div.style.display = "block";

		liwe.fx.fade_in ( div );
	} else 
		liwe.fx.set_opacity ( div, 100 );
};

liwe.lightbox.easy = function ( id, title, w, h, x, y )
{
	var div = liwe.lightbox.create ( id + "_win", w, h, x, y );
	var s;

	div.className = "lightbox_easy";

	s  = '<div class="title">' + title;
	s += '<div class="close" onclick="liwe.lightbox.close()"></div></div><div id="' + id + '"></div>';

	div.innerHTML = s;

	// TODO: fare il "pallino"

	liwe.lightbox.show ();
};

liwe.lightbox._add_back_div = function ()
{
	var d = liwe.lightbox._back_div;
	var h;
	
	if ( ! d ) 
	{
		d = liwe.dom.create_element ( "div", "liwe_lightbox" );

		d.style.display = 'none';
		d.style.backgroundColor = "black";

		liwe.lightbox._back_div = d;
	}

	var ua = navigator.userAgent;
	if ( ua.indexOf ( "MSIE" ) >= 0 )
		h = document.documentElement.clientHeight;
	else
		h = window.innerHeight;

	if ( document.body.parentNode.scrollHeight > h )
		h = document.body.parentNode.scrollHeight;

	d.style.zIndex = 10;
	d.style.width  = "100%";
	d.style.height = h + "px";

	liwe.events.add ( d, "click", liwe.lightbox._back_click );

	return d;
};

liwe.lightbox._back_click = function ()
{
	if ( liwe.lightbox.events [ 'click' ] ) liwe.lightbox.events [ 'click' ] ();
};

liwe.lightbox._add_front_div = function ( id, w, h, x, y )
{
	var d = liwe.lightbox._back_div;
	var div = liwe.dom.create_element ( "div", id );
	var bw, bh;

	var ua = navigator.userAgent;
	if ( ua.indexOf ( "MSIE" ) >= 0 )
	{
		bw = document.documentElement.clientWidth;
		bh = document.documentElement.clientHeight;
	}
	else
	{
		bw = window.innerWidth;
		bh = window.innerHeight;
	}

	if ( ( x == undefined ) || ( x == -1 ) ) x = Math.abs ( bw - w ) / 2;
	if ( ( y == undefined ) || ( y == -1 ) ) 
		y = Math.abs ( bh - h ) / 2;
	else
		if ( window.pageYOffset ) y += window.pageYOffset;

	x += document.body.parentNode.scrollLeft;
	y += document.body.parentNode.scrollTop;
	
	div.style.zIndex = document.body.childNodes.length + 10;
	div.style.width  = w + "px";
	div.style.height = h + "px";
	div.style.left = x + "px";
	div.style.top  = y + "px";

	liwe.lightbox._front_div.push ( div ); // = div;

	return div;
};

liwe.lightbox._save_overflow_status = function ()
{
	liwe.lightbox._overflow_status = [];

	liwe.lightbox._iterate_tree ( document.body, function ( item ) {

		if ( item.style )
		{
			liwe.lightbox._overflow_status.push ( [ item, item.style.overflow, item.style.overflowX, item.style.overflowY ] );
			item.style.overflow = "hidden";
		}

	} );
};

liwe.lightbox._restore_overflow_status = function ()
{
	var t, l = liwe.lightbox._overflow_status.length;

	for ( t = 0; t < l; t ++ )
	{
		var ost = liwe.lightbox._overflow_status [ t ];
		var item = ost [ 0 ];
		item.style.overflow = ost [ 1 ];
		item.style.overflowX = ost [ 2 ];
		item.style.overflowY = ost [ 3 ];
	}
};

liwe.lightbox._iterate_tree = function ( parent, cback )
{
	var children = parent.childNodes;
	if ( ! children || children.length == 0 ) return;

	var t, l = children.length;
	for ( t = 0; t < l; t ++ )
	{
		var child = children [ t ];
		cback ( child );

		liwe.lightbox._iterate_tree ( child, cback );
	}
};

liwe.lightbox._save_windowed_status = function ()
{
	liwe.lightbox._windowed_status = [];

	var windowed = [ "select", "object", "iframe", "applet", "plugin", "embed" ];
	var k, len = windowed.length;
	for ( k = 0; k < len; k++ )
	{
		var els = document.getElementsByTagName ( windowed [ k ] );

		var t, l = els.length;
		for ( t = 0; t < l; t++ )
		{
			var el = els [ t ];
			var vis = el.style.visibility;
			liwe.lightbox._windowed_status.push ( [ el, vis ] );
			el.style.visibility = "hidden";
		}
	}
};

liwe.lightbox._restore_windowed_status = function ()
{
	var t, l = liwe.lightbox._windowed_status.length;
	for ( t = 0; t < l; t ++ )
	{
		var item = liwe.lightbox._windowed_status [ t ];
		var el = item [ 0 ];
		var vis = item [ 1 ];
		el.style.visibility = vis;
	}
};

