function WindowPopup( sUrl, iWidth, iHeight, sOptions, sName, iLeft, iTop )
{
    // Options=Defaultwerk
    // dependent        // close the parent, close the popup, omit if you want otherwise
    // resizable=no     // yes|no
    // menubar=no       // yes|no
    // toolbar=no       // yes|no
    // location=no      // yes|no
    // status=no        // yes|no
    // scrollbars=no    // yes|no
    // directories=no   // yes|no

    iLeft = parseInt( iLeft );
    if ( !iLeft || isNaN( iLeft ) )
        iLeft = 0;

    iTop = parseInt( iTop );
    if ( !iTop || isNaN( iTop ) )
        iTop = 0;

    iWidth = parseInt( iWidth );
    iHeight = parseInt( iHeight );

	var iMaxWidth = screen.availWidth - 11;
	var iMaxHeight = screen.availHeight - 51;

	if ( !sOptions )
	    sOptions = '';

    if ( iWidth == -1 )
        iWidth = iMaxWidth;
    if ( iHeight == -1 )
        iHeight = iMaxHeight;

    // Scrollbar einschalten, wenn Fenster zu groß.
    if ( iLeft + iWidth > iMaxWidth || iTop + iHeight > iMaxHeight )
    {
        if ( sOptions.indexOf( 'scrollbars' ) == -1 )
        {
            if ( sOptions )
                sOptions = sOptions + ',';
            sOptions = sOptions + 'scrollbars=yes,resizable=yes';
        }

        if ( iWidth + iLeft > iMaxWidth )
            iWidth = iMaxWidth - iLeft;

        if ( iHeight + iTop > iMaxHeight )
        {
            iHeight = iMaxHeight - iTop;

            // add some width because auf scrollbar
            iWidth = iWidth + 19;

            // check width again to aviod windows wider then the screen
            if ( iWidth + iLeft > iMaxWidth )
                iWidth = iMaxWidth - iLeft;
        }
    }

    // Fenster zentrieren.
    if ( !iLeft )
    {
        iLeft = parseInt( ( iMaxWidth - iWidth ) / 2 );
        if ( iLeft < 0 )
            iLeft = 0;
    }
    if ( !iTop )
    {
        iTop = parseInt( ( iMaxHeight - iHeight ) / 2 );
        if ( iTop < 0 )
            iTop = 0;
    }

    if ( sOptions != '' )
        sOptions = 'width=' + iWidth + ',height=' + iHeight + ",left=" + iLeft + ',top=' + iTop + ',' + sOptions;
    else
        sOptions = 'width=' + iWidth + ',height=' + iHeight + ",left=" + iLeft + ',top=' + iTop;

    oWindow = window.open( sUrl, sName, sOptions );
    if ( oWindow )
        oWindow.focus();

    return( oWindow );
}

function WindowWidth( oWindow )
{
    if( typeof( window.innerWidth ) == 'number' )
        //Non-IE
        return( window.innerWidth );
    else
        if ( document.documentElement && document.documentElement.clientWidth )
            //IE 6+ in 'standards compliant mode'
            return( document.documentElement.clientWidth );
        else
            if ( document.body && document.body.clientWidth )
                //IE 4 compatible
                return( document.body.clientWidth );
            else
                return( 1024 );
}

function WindowHeight( oWindow )
{
    if( typeof( window.innerHeight ) == 'number' )
        //Non-IE
        return( window.innerHeight );
    else
        if ( document.documentElement && document.documentElement.clientHeight )
            //IE 6+ in 'standards compliant mode'
            return( document.documentElement.clientHeight );
        else
            if ( document.body && document.body.clientHeight )
                //IE 4 compatible
                return( document.body.clientHeight );
            else
                return( 768 );
}

function WindowResize( oWindow, iWidth, iHeight, fCenter )
{
	var iMaxWidth = screen.availWidth;
	var iMaxHeight = screen.availHeight;

    if ( iWidth > iMaxWidth )
        iWidth = iMaxWidth;
    if ( iHeight > iMaxHeight )
        iHeight = iMaxHeight

    oWindow.resizeTo( iWidth, iHeight );

    if ( fCenter )
    {
        iLeft = parseInt( ( iMaxWidth - iWidth ) / 2 );
        iTop = parseInt( ( iMaxHeight - iHeight ) / 2 );
        WindowMove( oWindow, iLeft, iTop );
    }
}

function WindowMove( oWindow, iLeft, iTop )
{
    oWindow.moveTo( iLeft, iTop );
}

function WindowFrameCheck( sPath )
{
    if ( top == self )
    {
        if ( sPath )
        {
            sLocation = self.location.pathname;
            top.location = sPath + '?content=' + encodeURIComponent( sLocation + self.location.search );
        }
        else
        {
            sLocation = self.location.pathname;
            i = sLocation.lastIndexOf( '/' );
            top.location = sLocation.substring( 0, i + 1 ) + '?content=' + encodeURIComponent( sLocation.substring( i + 1 ) + self.location.search );
        }
    }
}

function WindowPrint( oWindow )
{
    if ( !oWindow )
        oWindow = self;

    sUrl = oWindow.location.protocol + '//';
    sUrl = sUrl + oWindow.location.host;
    sUrl = sUrl + oWindow.location.pathname;

    sSearch = oWindow.location.search;
    if ( sSearch.length == 0 )
        sSearch = '?';
    else
        sSearch = sSearch + '&';
    sSearch = sSearch + 'print=1';
    sUrl = sUrl + sSearch;

    WindowPopup( sUrl, 780, 550, 'menubar=yes,scrollbars,resizable=yes,status=yes', 'print' );
}

function WindowZoom( oEvent, sName, sImage, iWidth, iHeight, sPosition )
{
	if ( !sPosition )
	    sPosition = 'WindowZoomPosition';

	oImage = document.getElementById( sName + ':zoomimage' );

    // destroy the old image, to avoid an ugly effect caused by the delayed loading of the next image
	if ( oImage != null )
    {
        oNode = document.getElementById( sName + ':zoom' );
        if ( oNode == null )
        {
            // legacy code, do not use #zoom in tempmlate any more
            // because # is an invalid char for ids
            oNode = document.getElementById( sName + '#zoom' );
            if ( oNode == null )
                return( false );
        }
	    oNode.removeChild( oImage );
	    oImage = null;
    }

	if ( oImage == null )
	{
		var oNewNode;
		var oNode;

		oNewNode = document.createElement( 'IMG' );
		oNewNode.id = sName + ':zoomimage';
		oNewNode.style.position = 'absolute';
		oNewNode.style.zIndex = 110;
		oNewNode.className = 'border';

        oNode = document.getElementById( sName + ':zoom' );
        if ( oNode == null )
        {
            // legacy code, do not use #zoom in tempmlate any more
            // because # is an invalid char for ids
            oNode = document.getElementById( sName + '#zoom' );
            if ( oNode == null )
                return( false );
        }

	    oImage = oNode.appendChild( oNewNode );
	}

    // set image src
	oImage.src = sImage;

    // set position calculator
	oImage.Position = sPosition + '( oEvent, oImage )';
    oImage.Position_Width = iWidth;
    oImage.Position_Height = iHeight;

    // set initial width and height
	oImage.style.width = iWidth + 'px';
	oImage.style.height = iHeight + 'px';

	// call position calculator
	eval( oImage.Position );

    // show image
    oImage.style.visibility = 'visible';
}

function WindowZoomFollow( oEvent, sName )
{
	var oImage = document.getElementById( sName + ':zoomimage' );
	if ( oImage == null )
	    return;

    eval( oImage.Position );
}

function WindowZoomPosition( oEvent, oImage )
{
    oImage.style.left = oEvent.clientX + 10 + 'px';
    oImage.style.top = oEvent.clientY + 10 + 'px';
}

function WindowZoomPositionZoom( oEvent, oImage )
{
    var iWidth;
    var iHeight;
    var iWindowWidth;
    var iWindowHeight;
    var iOffsetLeft;
    var iOffsetTop;
    var iLeft;
    var iTop;
    var iProportionX;
    var iProportionY;
    var oImage;

    iWidth = oImage.Position_Width;
    iHeight = oImage.Position_Height;

    iWindowWidth = WindowWidth( window );
    iWindowHeight = WindowHeight( window );

    if ( document.all )
    {
        iOffsetTop = document.body.scrollTop;
        iOffsetLeft = document.body.scrollLeft;
    }
    else
    {
        iOffsetTop = window.pageYOffset;
        iOffsetLeft = window.pageXOffset;
    }

    // window.status = 'windows: w ' + iWindowWidth + ' h ' + iWindowHeight + ' offset: left ' + iOffsetLeft + ' top ' + iOffsetTop + ' oEvent: x ' + oEvent.clientX + ' y ' + oEvent.clientY + ' Image: w ' + iWidth + ' h ' + iHeight;

    // find bigger side
    iProportionX = 1;
    if ( oEvent.clientX - 15 > iWindowWidth - oEvent.clientX - 15 )
    {
        // left side is bigger
        if ( oEvent.clientX - 25 < iWidth )
            iProportionX = ( oEvent.clientX - 25 ) / iWidth;
    }
    else
    {
        // right side is bigger
        if ( iWindowWidth - oEvent.clientX - 25 < iWidth )
            iProportionX = ( iWindowWidth - oEvent.clientX - 25 ) / iWidth;
    }

    // find bigger side
    iProportionY = 1;
    if ( oEvent.clientY - 15 > iWindowHeight - oEvent.clientY - 15 )
    {
        // top side is bigger
        if ( oEvent.clientY - 25 < iHeight )
            iProportionY = ( oEvent.clientY - 25 ) / iHeight;
    }
    else
    {
        // bottom side is bigger
        if ( iWindowHeight - oEvent.clientY - 25 < iHeight )
            iProportionY = ( iWindowHeight - oEvent.clientY - 25 ) / iHeight;
    }

    // resize image
    iWidth = Math.floor( iWidth * Math.min( iProportionX, iProportionY ) );
    iHeight = Math.floor( iHeight * Math.min( iProportionX, iProportionY ) );

    // calc positions
    if ( oEvent.clientX - 15 > iWindowWidth - oEvent.clientX - 15 )
        iLeft = oEvent.clientX + iOffsetLeft - iWidth - 15;
    else
        iLeft = oEvent.clientX + iOffsetLeft + 15;

    if ( oEvent.clientY - 15 > iWindowHeight - oEvent.clientY - 15 )
        iTop = oEvent.clientY + iOffsetTop - iHeight - 15;
    else
        iTop = oEvent.clientY + iOffsetTop + 15;

	// set width and height
	oImage.style.width = iWidth + 'px';
	oImage.style.height = iHeight + 'px';

    oImage.style.left = iLeft + 'px';
    oImage.style.top = iTop + 'px';
}

function WindowZoomHide( oEvent, sName )
{
	var oImage = document.getElementById( sName + ':zoomimage' );

	if ( oImage == null )
	    return( false );

    oImage.style.visibility = 'hidden';
}
