function Overlay(id, width, height, screenClass, contentClass, cbresize, myname, resultArray, date, rawdate)
{
	this.id = id;
	this.width = width;
	this.height = height;//deprecated
	this.padding = { left : 0, right : 0, top : 0, bottom : 0 };
	this.cbresize = cbresize;
	this.myname = myname;

	this.screen = document.createElement( "DIV" );

	if( this.screen.className !== undefined )
		this.screen.className = screenClass;
	else if( this.screen.setAttribute )
		this.screen.setAttribute( "class", screenClass );

	this.screen.style.position = "absolute";
	this.screen.style.display = "block";

	this.content = document.createElement( "DIV" );
	
	if( this.content.className !== undefined )
		this.content.className = contentClass;
	else if( this.content.setAttribute )
		this.content.setAttribute( "class", contentClass );
		
	this.content.style.position = "absolute";
	this.content.style.display = "block";
	
	var upperbar = document.createElement("DIV");
	upperbar.id = "divOverlayUpperBar";
	
	var uppertitle = document.createElement("DIV");
	uppertitle.id = "divOverlayUpperTitle";
	
	var upperclose = document.createElement("DIV");
	upperclose.id = "divOverlayUpperClose";
	
	var upperdate = document.createElement("DIV");
	upperdate.id = "divOverlayUpperDate";
	
	this.killcal = document.createElement("A");
	this.killcal.className = "closeCal";
	this.killcal.href = "javascript:" + this.myname + ".destroy();";
	
	upperbar.appendChild( uppertitle );
	upperbar.appendChild( upperclose );
	upperbar.appendChild( upperdate );
	if( date != '' ){
		upperdate.innerHTML = date;
	}
	upperclose.appendChild( this.killcal );
	
	this.addChild( upperbar );
	this.listScadenze( resultArray );
	
	var lowerdate = document.createElement("DIV");
	lowerdate.id = "divOverlayLowerDate";
	var nextdate = document.createElement("A");
	nextdate.innerHTML = 'Prossima scadenza &raquo;';
	nextdate.href = 'javascript:goNext(\'' + rawdate + '\')';
	
	lowerdate.appendChild( nextdate );
	
	this.addChild( lowerdate );
	
	Overlay.instances[ id ] = this;
	
}

Overlay.instances = new Array();
Overlay.active = null;

Overlay.prototype.create = function()
{
	document.body.appendChild( this.screen );
	document.body.appendChild( this.content );
	
	this.positionElements();
	Overlay.active = this;
	
    setEventHandler( window, "resize", Overlay.onResize );
}

Overlay.prototype.destroy = function()
{
	var mtx = this.getMetrix();
	Overlay.active = null;
	
	document.body.removeChild( this.content );
	document.body.removeChild( this.screen );
	
	var tmp = new Array();
	var instance;
	
	for( iid in Overlay.instances ) {
		if( iid != this.id )
			tmp[ iid ] = Overlay.instances[ iid ];
	}
	
	Overlay.instances = tmp;
	document.body.style.overflow = "";
	document.getElementsByTagName('html')[0].style.overflow = '';
	
	scroll(0,mtx.offY);
}

Overlay.prototype.addChild = function(element)
{
	this.content.appendChild( element );
}

Overlay.prototype.removeChild = function(element)
{
	this.content.removeChild( element );
}

Overlay.prototype.setContent = function(html)
{
	this.content.innerHTML = html;
}

Overlay.prototype.setPadding = function(p)
{
	this.padding = p;
}

Overlay.prototype.positionElements = function()
{
    var mtx = this.getMetrix();
    var x, finalWidth, finalHeight;

    this.screen.style.left = mtx.offX + "px";
    this.screen.style.top = mtx.offY + "px";
    this.screen.style.width = mtx.width + "px";
    this.screen.style.height = mtx.height + "px";

	if( this.width > 0 ) {
		x = (this.width <= mtx.width) ? this.width : mtx.width;
		this.content.style.width = x + "px";
		finalWidth = x;
		x = (this.width <= mtx.width) ? Math.round((mtx.width - this.width) / 2) : 0;
		this.content.style.left = (mtx.offX + x) + "px";
	} else if( this.width < 0 ) {
		x = mtx.width + this.width;
		if( x < 0 )
			x = Math.abs( this.width );
		this.content.style.width = x + "px";
		finalWidth = x;
		
		x = Math.abs( Math.abs( this.width ) - Math.abs( this.padding.left + this.padding.right ) ) / 2;
		this.content.style.left = (mtx.offX + x) + "px";
	} else {
		this.content.style.width = mtx.width + "px";
		this.content.style.left = mtx.offX + "px";
		finalWidth = mtx.width;
	}
	
	/*
	if( this.height > 0 ) {
		x = (this.height <= mtx.height) ? this.height : mtx.height;
		this.content.style.height = "auto";
		//this.content.style.height = x + "px";
		finalHeight = x;
		x = (this.height <= mtx.height) ? Math.round((mtx.height - this.height) / 2) : 0;
		this.content.style.top = (mtx.offY + x) + "px";
	} else if( this.height < 0 ) {
		x = mtx.height + this.height;
		if( x < 0 )
			x = Math.abs( this.height );
		this.content.style.height = x + "px";
		finalHeight = x;
		
		x = Math.abs( Math.abs( this.height ) - Math.abs( this.padding.top + this.padding.bottom ) ) / 2;
		this.content.style.top = (mtx.offY + x) + "px";
	} else {
		//this.content.style.height = mtx.height + "px";
		this.content.style.height = "auto";
		
		if( mtx.offY > this.content.offsetHeight ){
			this.content.style.top = (mtx.offY - this.content.offsetHeight )/2;
		}
		else{
			this.content.style.top = mtx.offY + "px";
		}
		
		this.content.style.top = mtx.offY + "px";
		finalHeight = mtx.height;
	}
	*/
	
	//alert(this.content.offsetHeight);
	
	if( mtx.height < this.content.offsetHeight ){//our content is higher than the screen
		/*
		this.content.style.top = mtx.offY + "px";
		document.body.style.overflow = "visible";
		this.screen.style.height = (mtx.offY * 2) + this.content.offsetHeight + "px";
		this.screen.style.top = "0px";
		*/
		
		//too much stuff, make the box scroll
		//alert("cache");
		document.body.style.overflow = "hidden";
		document.getElementsByTagName('html')[0].style.overflow = 'hidden';//for IE, obviously
		this.content.style.height = "400px";
		this.content.style.width = this.width + 20 + "px";
		this.content.style.overflowY = "scroll";
		this.content.style.overflowX = "hidden";
		var add = Math.round( ( mtx.height - 400 ) / 2 );
		this.content.style.top = mtx.offY + add + "px";
		this.screen.style.width = mtx.width + 30 + "px";
		this.screen.style.height = (mtx.offY * 2) + this.content.offsetHeight + "px";
		this.screen.style.top = "0px";
		scroll(0,mtx.offY);
	}
	else{
		var add = Math.round( ( mtx.height - this.content.offsetHeight ) / 2 );
		this.content.style.top = mtx.offY + add + "px"
		document.body.style.overflow = "hidden";
		document.getElementsByTagName('html')[0].style.overflow = 'hidden';//for IE, obviously
		this.screen.style.width = mtx.width + 30 + "px";
		if( this.content.style.overflowY == 'scroll' ){
			this.content.style.width = this.width + 20 + "px";
		}
		scroll(0,mtx.offY);
		//this.content.style.overflow = "hidden";
		//alert(mtx.offY);
	}
	
	//document.body.style.overflow = "hidden";
	
	if( this.cbresize != null )
		this.cbresize( finalWidth, finalHeight );
	
}

Overlay.prototype.getMetrix = function()
{
    var metrics = {
        offX : 0,
        offY : 0,
        width : 0,
        height: 0
    };

    if( window.pageXOffset !== undefined ) {
        metrics.offX = window.pageXOffset;
    } else if( document.documentElement.scrollLeft !== undefined ) {
        metrics.offX = document.documentElement.scrollLeft;
    } else if( document.body.scrollLeft !== undefined ) {
        metrics.offX = document.body.scrollLeft;
    }

    if( window.pageYOffset !== undefined ) {
        metrics.offY = window.pageYOffset;
    } else if( document.documentElement.scrollTop !== undefined ) {
        metrics.offY = document.documentElement.scrollTop;
    } else if( document.body.scrollTop !== undefined ) {
        metrics.offY = document.body.scrollTop;
    }

    if( window.innerWidth ) {
        metrics.width = window.innerWidth ;
        metrics.height = window.innerHeight;
    } else if( document.documentElement.clientWidth ) {
        metrics.width = document.documentElement.clientWidth;
        metrics.height = document.documentElement.clientHeight;
    } else if( document.body.clientWidth ) {
        metrics.width = document.body.clientWidth;
        metrics.height = document.body.clientHeight;
    }
        
    return metrics;
}

Overlay.onResize = function()
{
	if( Overlay.active != null )
		Overlay.active.positionElements();
}

Overlay.prototype.listScadenze = function(resultArray)
{
	var container = document.createElement("DIV");
	container.id = "divScadenzaContainer";
	
	for( var i in resultArray ){
		var block = document.createElement( "DIV" );
		block.className = "scadenzaBlock";
		
		var cat = document.createElement("SPAN");
		cat.className = "scadenzaCat";
		
		if( resultArray[i]["category"] != '' ){
			cat.innerHTML = "SCADENZE - " + resultArray[i]["category"] + "&nbsp;»&nbsp;&nbsp;";
		}
		block.appendChild(cat);
		
		var title = document.createElement("SPAN");
		title.className = "scadenzaTitle";
		if( resultArray[i]["title"] != '' ){
			title.innerHTML = resultArray[i]["title"];
		}
		
		block.appendChild(title);
		
		var content = document.createElement("DIV");
		content.className = "scadenzaContent";
		if( resultArray[i]["description"] != '' ){
			content.innerHTML = resultArray[i]["description"];
		}
		block.appendChild(content);
		
		container.appendChild(block);
	}
	
	this.addChild(container);
}
