/** thumb preview **/

function ThumbPreview(parentsrc)
{
    return {
	thumbElement : undefined,
	thumbLink : null,
	parentDiv : parentsrc,
	hider : null,
	preloaded : new Array(),
	preIndex : 0,
	preDone : 0,
	isActive : false,
	showThumb : function(srcElement) {
	    if( !this.isActive ) return;
	    if( srcElement == null ) {
		srcElement = this.thumbElement;
	    }

	    if( this.thumbElement != null ) {
		if( this.thumbElement == srcElement ) {
		    if( this.hider != null ) {
			clearTimeout( this.hider );
			this.hider = null;
		    }
		    return;
		}

		this.thumbElement.style.display = "none";
		this.thumbElement = null;
	    }

	    var srcXY = getPageCoords( srcElement );

	    this.thumbElement = document.createElement( "IMG" );
	    this.thumbElement.src = srcElement.getAttribute( "preview" );
	    this.thumbElement.width = srcElement.getAttribute( "tcx" );
	    this.thumbElement.height = srcElement.getAttribute( "tcy" );
	    this.thumbElement.style.position = "absolute";
	    this.thumbElement.style.left = (srcXY.x + 12) + "px";
	    this.thumbElement.style.top = (srcXY.y + 12) + "px";
	    this.thumbElement.style.border = "solid 2px #a24246";
	    this.thumbElement.style.zIndex = 101;
	    this.thumbElement.style.cursor = "pointer";
	    this.thumbElement.title = "Clicca per ingrandire";
	    this.thumbLink = srcElement.getAttribute( "tl" );
	    setEventHandler( this.thumbElement, "mouseover", function(e) { tp.showThumb(null); } );
	    setEventHandler( this.thumbElement, "mouseout", function(e) { tp.hideThumb(null); } );
	    setEventHandler( this.thumbElement, "click", function(e) { tp.clickThumb(); } );
	    this.parentDiv.appendChild( this.thumbElement );
	},
	clickThumb : function() {
	    if( this.thumbLink != null ) {
		document.location = this.thumbLink;
	    }
	},
	finallyHideThumb : function() {
	    if( this.thumbElement != null ) {
		this.thumbElement.style.display = "none";
		this.thumbElement = null;
		this.thumbLink = null;
	    }
	    hider = null;
	},
	hideThumb : function(srcElement) {
	    if( !this.isActive ) return;
	    if( this.thumbElement != null ) {
		if( this.hider == null )
		    this.hider = setTimeout( function (e) { tp.finallyHideThumb(); }, 800 );
	    }
	},
	onImageLoaded : function() {
	    if( ++this.preDone >= this.preIndex ) {
		this.isActive = true;
	    }
	},
	preloadImage : function(src) {
	    this.preloaded[ this.preIndex ] = new Image();
	    this.preloaded[ this.preIndex ].src = src;
	    this.preloaded[ this.preIndex ].onLoad = this.onImageLoaded();
	    this.preIndex ++;
	}
    };
}

