﻿// --- Klasse popup --- PS 17.9.07 ---

function popup( node )
{
	this.node			= node;
	this.node.popup	= this;
	this.popup			= this;
	this.activ			= false;
	this.div				= "";
	this.drag			= false;
	this.drag_start	= { x:0, y:0 };
	this.drag_current	= { x:0, y:0 };
	this.drag_obj		= null;
	
	this.init			= popupInit;
	this.start			= popupStart;
	this.stop			= popupStop;
	this.move			= popupMove;
	this.scale			= popupScale;
	this.isActiv		= popupIsActiv;
	this.mousemove		= popupMousemove;
	this.mousedown		= popupMousedown;
	this.mouseup		= popupMouseup;
	this.mouseclick	= popupMouseclick;

	this.node.onclick			= this.mouseclick;

	// --- Lupe einfügen ---
	
	var size = getNodeSize( node.parentNode );

	node.parentNode.style.backgroundImage = "url(/img/lupe.gif)";
	node.parentNode.style.backgroundRepeat = "no-repeat";
	node.parentNode.style.backgroundPosition = (size.w - 25) + "px " + (size.h - 25) + "px";
	
	node.parentNode.style.cursor = "help";
	
	this.init( document );
}

function popupInit( document )
{
	this.div = document.createElement( "div" );
	
	this.div.style.position				= "relative";
	this.div.style.visibility			= "hidden";
	this.div.style.backgroundColor	= "#fff";
	this.div.style.color					= "#fff";
	this.div.style.border				= "solid 3px #333";
	this.div.style.cursor				= "move";
	this.div.style.zIndex				= "1010";

	document.body.appendChild( this.div );
}

function popupStart( datei )
{
	//this.div.innerHTML = '<img style="position:absolute; left:0px; top:0px; cursor:move;" title="popup" src="' + datei + '/><img style="position:absolute; cursor:auto;" src="/img/close.png" onclick="popup.stop()"/>';

	var img1 = document.createElement( "img" );
	
	img1.style.position	= "absolute";
	img1.style.left		= "0px";
	img1.style.top			= "0px";
	img1.style.cursor		= "move";
	img1.title				= "popup";
	img1.src					= datei;
	img1.onload				= this.scale;
	img1.onmousedown		= this.mousedown;		
	img1.onmousemove		= this.mousemove;
	img1.onmouseup			= this.mouseup;
	img1.popup				= this;
	
	var img2 = document.createElement( "img" );
	
	img2.style.position	= "absolute";
	img2.style.cursor		= "auto";
	img2.src					= "/img/close.png";
	img2.onclick			= this.stop;
	img2.popup				= this;
	
		this.div.appendChild( img1 );
	this.div.appendChild( img2 );
	
	this.div.style.left		= 5;
	this.div.style.top		= 5;
	this.div.style.width		= 100;
	this.div.style.height	= 100;
	this.div.style.zIndex	= 1011;
	this.activ					= true;
	
	//alert( this.div.innerHTML );
}

function popupStop()
{
	this.popup.div.innerHTML = '';
	
	this.popup.div.style.left			= 0;
	this.popup.div.style.top			= 0;
	this.popup.div.style.width			= 0;
	this.popup.div.style.height		= 0;
	this.popup.div.style.zIndex		= 0;
	this.popup.div.style.visibility	= 'hidden';
	this.popup.activ						= false;
}

function popupMove( dx, dy )
{
	x = parseInt( this.div.style.left + 0 );
	y = parseInt( this.div.style.top + 0 );
	
	this.div.style.left = x + dx;
	this.div.style.top = y + dy;
}

function popupScale()
{
	var clientSize	= getClientSize();
	var scrollPos	= getScrollPos();

	bild = this.popup.div.getElementsByTagName("img")[0];
	
	this.popup.div.style.left			= scrollPos.x + ( clientSize.w - 35 - bild.width ) / 2;
	this.popup.div.style.top			= scrollPos.y + ( clientSize.h - 20 - bild.height ) / 2;
	this.popup.div.style.width			= bild.width;
	this.popup.div.style.height		= bild.height;
	this.popup.div.style.visibility	= 'visible';
}

function popupIsActiv()
{
	var ret = false;
	
	if( this.activ == true )
		ret = true;
		
	return( ret );
}

function popupMousedown(e)
{
	e ? evt = e : evt = event;
	target = evt.target ? evt.target : evt.srcElement;

	if( this.popup.isActiv() == true )
	{
		if( target.title == "popup" )
		{
			this.popup.drag				= true;
			this.popup.drag_start		= getEvtPos(evt);
			this.popup.drag_current		= this.popup.drag_start;
			this.popup.drag_obj			= target;
			
			return( false );
		}
	}
	
	return( true );
}

function popupMouseup(e)
{
	e ? evt = e : evt = event;
	target = evt.target ? evt.target : evt.srcElement;

	if( this.popup.drag )
		this.popup.drag = false;
}

function popupMousemove(e)
{
	e ? evt = e : evt = event;

	if( this.popup.drag )
	{
		evtPos = getEvtPos(evt);
		
		this.popup.move( evtPos.x - this.popup.drag_current.x, evtPos.y - this.popup.drag_current.y );
		
		this.popup.drag_current = evtPos;
	}
}

function popupMouseclick(e)
{
	e ? evt = e : evt = event;
	target = evt.target ? evt.target : evt.srcElement;

	if( this.popup.drag )
		this.popup.stop();
	else
	{
		if( target.nodeName == "IMG" )
		{
			if( target.title == "popup" )
				this.popup.stop();
			else if ( target.title && target.parentNode.className == "bild" )
			{
				if( this.popup.isActiv() == true )
					this.popup.stop();
				else
					this.popup.start( target.title );
			}
		}
	}
}
