// globale Variablen

var WINDOWS_VERSION_UNKNOWN = 0;
var WINDOWS_VERSION_XP      = 1;
var WINDOWS_VERSION_VISTA   = 2;

// benoetigte Functionen

function windowTitle( documentToWriteTo, additionalTitle )
{
	if ( documentToWriteTo == null )
	{
		return;
	}
	
	var theTitle = '';
	
	theTitle += '<title>';
	
	if ( ( additionalTitle != null ) && ( additionalTitle != '' ) )
	{
		theTitle += additionalTitle;
		theTitle += ' - ';
	}
	
	theTitle += globalSettings.mainTitle;
	theTitle += '</title>';
	
	documentToWriteTo.write( theTitle );
}

function writeStyle(documentToWriteTo, newLocation)
{
	if ( documentToWriteTo == null )
	{
		return;
	}
	
	if ( newLocation == null )
	{
		newLocation = 'css/';
	}
	
	if ( newLocation.lastIndexOf('/') != (newLocation.length - 1) )
	{
		newLocation += '/';
	}
	
	var theHTML = '';
	
	theHTML += '<link href="{location}{styleName}_mozilla.css" rel="stylesheet" type="text/css" />';
	theHTML += '<!--[if lt IE 7]><link href="{location}{styleName}_ie6.css" rel="stylesheet" type="text/css" /><![endif]-->';
	theHTML += '<!--[if IE 7]><link href="{location}{styleName}_ie7.css" rel="stylesheet" type="text/css" /><![endif]-->';
	
	var styleName = 'default';
	
	if ( (globalSettings != null) &&
	     (globalSettings.stylename != null) )
	{
		styleName = globalSettings.stylename;
	}
	
	theHTML = theHTML.replace( /{location}/g, newLocation );
	theHTML = theHTML.replace( /{styleName}/g, styleName );
	
	documentToWriteTo.writeln( theHTML );
}

function openBrowserWindow( resourceHref, resourcewin, width, height, opener, fullscreen )
{
	fullscreen = ( ('' + fullscreen) == 'true' );
	
	var windowAttributes = globalSettings.windowAttributes;
	
	if ( windowAttributes == '' )
	{
		windowAttributes = 'location=no,menubar=no,toolbar=no,scrollbars=yes,status=no,resizable=yes';
	}
	
	if ( fullscreen )
	{
		windowAttributes += ',fullscreen=1';
	}
	
	var globalWidth  = null;
	var globalHeight = null;
	
	// Breite berechnen
	
	globalWidth  = parseInt( width,  10);
	
	if ( isNaN(globalWidth) )
	{
		var desiredWidth = Math.min( 1024, screen.availWidth );
	}
	else
	{
		var desiredWidth = Math.min( globalWidth,  screen.availWidth );
	}
	
	// Hoehe berechnen
	
	var globalHeight  = parseInt( height,  10);
	
	if ( isNaN(globalHeight) )
	{
		var desiredHeight = Math.min( 740, screen.availHeight );
	}
	else
	{
		var desiredHeight = Math.min( globalHeight, screen.availHeight );
	}
	
	// Anpassungen an Hoehe und Breite aufgrund von Browser
	
	if ( browserunterscheidung() == 'netscape' )
	{
		desiredWidth  = desiredWidth - 12;
		desiredHeight = desiredHeight + 20;	
	}
		
	// Anpassungen an Hoehe und Breite aufgrund vom Betriebssystem
	
	if ( checkWindowsVersion() == 2 )
	{
		desiredWidth  = desiredWidth + 8;
		
		// die Hoehe wird zunaechst nicht angepasst
		// desiredHeight = desiredHeight + 20;
	}
	
	// Startkoordinaten berechnen
	
	var left = screen.availWidth  / 2 - desiredWidth  / 2;
	var top  = screen.availHeight / 2 - desiredHeight / 2;
	
	// Fenster oeffnen

	var openedWindow = opener.open( resourceHref, resourcewin, windowAttributes );
	
	if ( ( openedWindow == null ) || ( openedWindow.closed == true ) )
	{
		alert( language.getString( 'popup_blocker1' ) + '\n\n' + language.getString( 'popup_blocker2' ) );
		return ( null );
	}
	
	// Fenster resizen und verschieben
	
	if ( (  browserunterscheidung() == 'explorer' ) || (  browserunterscheidung() == 'explorer7' ) )
	{
		// Fullscreen fuer IE
		
		if ( ! fullscreen )
		{
			openedWindow.resizeTo( desiredWidth, desiredHeight );
			openedWindow.moveTo( left, top );
		}
	}
	else
	{
		openedWindow.resizeTo( desiredWidth, desiredHeight );
		openedWindow.moveTo( left, top );
	}
	
	openedWindow.focus();
	
	return ( openedWindow );
}

function checkWindowsVersion()
{
	var windowsVerion = WINDOWS_VERSION_UNKNOWN;
	
	if ( navigator.platform.toLowerCase().indexOf('win32') >= 0 )
	{
		var userAgentValue = navigator.userAgent.toLowerCase();

		if ( userAgentValue.indexOf('windows nt ') > -1 )
		{
			var tempValue = userAgentValue.substring( userAgentValue.indexOf('windows nt ') );
			
			var operatingSystemValue = tempValue.substr( 0 , tempValue.indexOf( ';' ) );
			
			var versionValue = operatingSystemValue.substr( 11, 1 );
										
			if ( parseInt( versionValue, 10 ) > 5 ) 
			{
				windowsVerion = WINDOWS_VERSION_VISTA;
			}
			else
			{
				windowsVerion = WINDOWS_VERSION_XP;
			}
		}
	}
	
	return( windowsVerion );
}

function splitTitle( oldTitle, maxChars )
{
	var textArray = oldTitle.split(' / ');
	var newTitle = '';
	
	if ( ( textArray != null ) && ( textArray.length != 0 ) ) 
	{
		// Es gibt nur einen verdammt langen Titel!
		if ( textArray.length == 1 ) 
		{
			return ( '... ' + textArray[0].substring( ( textArray[0].length - ( maxChars - 4 ) ), textArray[0].length ) );
		}
		// Titel in umgekehrter Reiherfolge wieder zusammensetzen
		else
		{
			textArray.reverse();
			
			newTitle = textArray[0];
			
			if ( newTitle.length > ( maxChars - 4 ) ) 
			{
				return ( '... / ' + newTitle.substring( ( newTitle.length - ( maxChars - 6 ) ), newTitle.length ) );
			}
			
			for ( var counter = 1; counter < textArray.length; counter++ )
			{
				if( ( textArray[counter].length + newTitle.length ) < ( maxChars - 9 ) ) 
				{
					newTitle = textArray[counter] + ' / ' + newTitle;
				}
				else 
				{
					return ( '... / ' + newTitle );
				}
			}
		}
	}
	return ( newTitle );
}

function Fensterweite()
{
	if ( window.innerWidth )
	{	
		return window.innerWidth;
	}
	else if ( document.body && document.body.offsetWidth )
	{
		return document.body.offsetWidth;
	}
	else
	{
		return 0;
	}
}

function neuAufbau()
{
	if ( Weite != Fensterweite() )
	{
		window.history.go(0);
	}
}

if( !window.Weite && window.innerWidth )
{
	window.onresize = neuAufbau;
	Weite = Fensterweite();
}