/**
 * JavaScript Toolkit
 */

function js()
{
	this.ns		= (document.layers);
	this.ie		= (document.all);
	this.w3 	= (document.getElementById && !this.ie);
	this.opera 	= ( navigator.userAgent.search( "Opera" ) != -1 );
	this.safari	= ( navigator.userAgent.search( "AppleWebKit" ) != -1 );
	
	/**
	 * Alias für getElementById
	 *
	 * @param String id ID des HTML-Elements
	 * @return Object HTML-Element
	 */	
	this.e = function( id )
	{
		return document.getElementById( id );		
	}

	/**
	 * Alias für getElementsByName
	 *
	 * @param String name Name der HTML-Elemente
	 * @return Array mit HTML-Elements
	 */	
	this.n = function( name )
	{
		return document.getElementsByName( name );
	}
	
	/**
	 * Alias für getElementsByTagName
	 *
	 * @param String name Der Tag-Name des gesuchten HTML-Elements
	 * @return Array mit HTML-Elementen
	 */
	this.tn = function( name )
	{
		return document.getElementsByTagName( name );
	}
	
	/**
	 * Ersetzt den innerHTML-Value eines HTML-Elements
	 *
	 * @param String id ID des HTML-Elements
	 * @param String value Neuer innerHTML-Value
	 */
	this.replace = function( id, value )
	{
		if ( !_idExists( id ) )
			return;
		
		// innerHTML setzen
		this.e( id ).innerHTML = value;
	}
	
	/**
	 * Blendet ein Element aus bzw. ein
	 *
	 * @param String id ID des HTML-Elements
	 */
	this.toggle = function( id )
	{
		if ( !_idExists( id ) )
			return;
		
		// Nicht ausgeblendet?
		if ( this.e( id ).style.display != 'none' )
			this.e( id ).style.display = 'none';
		else // Auf Standard zurücksetzen
			this.e( id ).style.display = '';
	}
	
	/**
	 * Gibt den Typ eines Elements zurück
	 *
	 * @param String id ID des Elements
	 * @return String Typ-Bezeichnung
	 */
	this.getType = function( id )
	{
		if ( !_idExists( id ) )
			return;
		
		// Rückgabe
		return typeof this.e( id );
	}
	
	/**
	 * Gibt den Tag-Namen eines Elements zurück
	 *
	 * @param String id ID des Elements
	 * @return String Tag-Name
	 */
	this.tagName = function( id )
	{
		if ( !_idExists( id ) )
			return;
		
		// Rückgabe
		return this.e( id ).tagName;
	}
	
	/**
	 * Prüft, ob sich ein bestimmtes Element im Array befindet
	 *
	 * @param Mixed elem zu suchendes Element
	 * @param Array array zu durchsuchendes Array
	 * @return Bool Element im Array enthalten ja/nein
	 */
	this.inArray = function( elem, array )
	{
		if ( !this.isObject( array ) )
			return false;
		
		for ( i = 0; array[i]; ++i )
			if ( array[i] == elem )
				return true;
				
		return false;
	}
	
	/**
	 * Prüft, ob es sich bei dem Parameter um einen String handelt
	 *
	 * @param Mixed value zu prüfender Parameter
	 * @return Bool
	 */
	this.isString = function( value )
	{
		return (typeof( value ) == 'string');
	}
	
	/**
	 * Prüft, ob es sich bei dem Parameter um ein Objekt handelt
	 *
	 * @param Mixed value zu prüfender Parameter
	 * @return Bool
	 */
	this.isObject = function( value )
	{
		return (typeof( value ) == 'object');
	}
	
	/**
	 * Prüft, ob es sich bei dem Parameter um eine Zahl handelt
	 *
	 * @param Mixed value zu prüfender Parameter
	 * @return Bool
	 */
	this.isNumber = function( value )
	{
		return (typeof( value ) == 'number');
	}
	
	/**
	 * Prüft, ob es sich bei dem Parameter um einen Bool'schen Wert handelt
	 *
	 * @param Mixed value zu prüfender Parameter
	 * @return Bool
	 */
	this.isBoolean = function( value )
	{
		return (typeof( value ) == 'boolean');
	}
	
	/**
	 * Prüft, ob es sich bei dem Parameter um einen undefinierten Typ handelt
	 *
	 * @param Mixed value zu prüfender Parameter
	 * @return Bool
	 */ 
	this.isUndefined = function( value )
	{
		return (typeof( value ) == 'undefined');
	}
	
	/**
	 * Prüft, ob der angegebene Parameter leer ist
	 *
	 * @param Mixed value zu prüfender Parameter
	 * @return Bool
	 */
	this.isEmpty = function( value )
	{
		if ( this.isString( value ) && ( value == '' || value == 'undefined' ) )
			return true;
			
		if ( this.isUndefined( value ) )
			return true;

		if ( this.isBoolean( value ) )
			return !value;
		
		if ( this.isObject( value ) && value.length == 0 )
			return true;
			
		// Rückgabe
		return false;
	}
	
	/**
	 * Öffnet ein Popup mit Focus
	 *
	 * @param String url URL der Seite im Popup
	 * @param String name Name des Popups
	 * @param Integer width Breite des Popups (Standard: 400)
	 * @param Integer height Höhe des Popups (Standard: 300)
	 * @param Bool scollbars Scrollbalken ja/nein
	 * @param Bool resizable Skalierbar ja/nein
	 * @param Bool status Statusbar ja/nein
	 * @return Window-Handle
	 */
	this.popup = function( url, name, width, height, scrollbars, resizable, status )
	{
		return _openWindow( url, name, width, height, scrollbars, resizable, status, true );
	}
	
	/**
	 * Öffnet ein Popup ohne Focus
	 *
	 * @param String url URL der Seite im Popup
	 * @param String name Name des Popups
	 * @param Integer width Breite des Popups (Standard: 400)
	 * @param Integer height Höhe des Popups (Standard: 300)
	 * @param Bool scollbars Scrollbalken ja/nein
	 * @param Bool resizable Skalierbar ja/nein
	 * @param Bool status Statusbar ja/nein
	 * @return Window-Handle
	 */
	this.popunder = function( url, name, width, height, scrollbars, resizable, status )
	{
		return _openWindow( url, name, width, height, scrollbars, resizable, status, false );
	}
	
	/**
	 * Legt einen Layer mit 85% Suchtbarkeit über die Seite
	 *
	 * @param Bool display Layer sichtbar ja/nein
	 * @param Integer zindex Z-Index
	 */	
 	this.contentOverlay = function( display, zindex )
 	{
		if ( this.e( 'jslib_layer_hidecontent' ) )
 			iframe = this.e( 'jslib_layer_hidecontent' );
 		else 
 			iframe = document.createElement( 'iframe' );
 		
		iframe.style.display	= ( display ) ? 'block' : 'none';
		
		if ( display ) 
		{
			var i 						= 85;
			var i_ff 					= i / 100;
			
			if ( this.ie ) 
			{
				xoffset 				= _trueBody().scrollLeft;
				yoffset 				= _trueBody().scrollTop;
				documentWidth			= _trueBody().offsetWidth;
				documentHeight			= _trueBody().offsetHeight;
			}
			else {
				xoffset 				= window.pageXOffset;
				yoffset 				= window.pageYOffset;
				documentWidth			= window.innerWidth;
				documentHeight			= window.innerHeight;		
			}			
			
			var contentHeight = Math.max( _trueBody().scrollHeight, documentHeight );
			var contentWidth  = Math.max( _trueBody().scrollWidth, documentWidth );
			
			if ( this.ie && !this.opera )
			{
				contentWidth -= 21;
				contentHeight -= 4;
			}
			else if ( _trueBody().scrollHeight > documentHeight )
				contentWidth -= 17;
				
			if ( _trueBody().scrollWidth > documentWidth )
				contentHeight -= (this.ie ? 21 : 17);
			
			iframe.id					= 'jslib_layer_hidecontent';
			iframe.setAttribute( 'border', '0' );
			iframe.setAttribute( 'frameborder', '0' );
			iframe.setAttribute( 'marginheight', '0' );
			iframe.setAttribute( 'marginwidth', '0' );
			iframe.frameborder 			= 0;
			iframe.marginheight			= 0;
			iframe.marginwidth			= 0;
			iframe.style.zIndex 		= ( zindex ? zindex : 1 ),
			iframe.style.position 		= 'absolute';
			iframe.style.background 	= '#FFFFFF';			
			iframe.style.border			= '0px none';			
			iframe.style.padding		= '0px';			
			iframe.style.margin			= '0px';
			iframe.style.left			= '0px';			
			iframe.style.top			= '0px';
			iframe.style.width 			= contentWidth + 'px';
			iframe.style.height 		= contentHeight + 'px';
			iframe.style.filter			= "alpha(opacity=" + i + ")";
			iframe.style.opacity 		= i_ff;
			iframe.style.mozopacity 	= i_ff
			iframe.style.khtmlopacity 	= i_ff	
		}
		
		// iFrame als erstes Element in den Body schreiben
		_trueBody().insertBefore( iframe, _trueBody().firstChild );
		
		this.moveElem( iframe.id, 'topLeft' );
		
		var self = this;
		window.onresize = function(){ self.contentOverlay( display, zindex ); };		
 	}	
	
	/**
	 * Positioniert ein Element an eine bestimmte Position
	 *
	 * @param String id ID des Elements
	 * @param String pos Positionierung (topLeft, topCenter, leftCenter, center (=centerCenter))
	 */
 	this.moveElem = function( id, pos )
	{
		if ( !_idExists( id ) )
			return;
		
		elem = this.e( id );
			
		if ( elem.style.display != 'block' )
			return;
			
		// Abmessungen
		elemHeight = elem.offsetHeight;
		elemWidth  = elem.offsetWidth;
		
		var yoffset 	= 0;
		var xoffset 	= 0;
		var docWidth 	= 0;
		var docHeight	= 0;
		
		if ( this.ie ) 
		{
			xoffset 	= _trueBody().scrollLeft;
			yoffset 	= _trueBody().scrollTop;
			docWidth	= _trueBody().offsetWidth;
			docHeight	= _trueBody().offsetHeight;
		}
		else 
		{
			xoffset 	= window.pageXOffset;
			yoffset 	= window.pageYOffset;
			docWidth	= self.innerWidth;
			docHeight	= self.innerHeight;		
		}	
		
		switch( pos )
		{
			case 'topLeft':
				elem.style.top 	= yoffset + 'px';
				elem.style.left	= xoffset; + 'px';
				break;
			case 'topCenter':
				elem.style.top	= yoffset + 'px';
				elem.style.left = ((docWidth / 2) - (elemWidth / 2) + xoffset) + 'px';
				break;
			case 'leftCenter':
				elem.style.top	= ((docHeight / 2) - (elemHeight / 2) + yoffset) + 'px';
				elem.style.left	= xoffset + 'px';
				break;
			case 'center':
			case 'centerCenter':
				docWidth 	= 780;
				docHeight   = 100 + elemHeight;
				elem.style.top	= ((docHeight / 2) - (elemHeight / 2) + yoffset) + 'px';
				elem.style.left = ((docWidth / 2) - (elemWidth / 2) + xoffset) + 'px';
				break;
		}
		setTimeout( 'js.moveElem(\'' + id + '\', \'' + pos + '\')', 300 );
	}
	
	/**
	 * Lädt ein JS-File in den Head nach
	 * --> Prüft, ob die Datei bereits included wurde
	 *
	 * @param String file Dateipfad
	 */
	this.loadJSFile = function( file )
	{
		if ( !this.isString( file ) )
			return;
		
		var script_tags = this.tn( 'script' );
		
		// Prüfen, ob die Datei schon geladen wurde
		for ( i = 0; script_tags[i]; ++i )
		{
			if ( !script_tags[i].src )
				continue;
			if ( script_tags[i].src.match(file) )
				return;
		}
		
		// Datei nachladen
		var head 	= js.tn( 'head' )[0];
		var script 	= document.createElement( 'script' );
			script.setAttribute( 'language', 'JavaScript' );
			script.setAttribute( 'type', 'text/javascript' );
			script.setAttribute( 'src', file );
			
		// Script in den Head schreiben
		head.appendChild( script );
	}
	
	/**
	 * Lädt ein JS-File in den Head nach
	 * --> Prüft, ob die Datei bereits included wurde
	 *
	 * @param String file Dateipfad
	 */
	this.loadCSSFile = function( file )
	{
		if ( !this.isString( file ) )
			return;
		
		var tags = this.tn( 'link' );
		
		// Prüfen, ob die Datei schon geladen wurde
		for ( i = 0; tags[i]; ++i )
		{
			if ( !tags[i].rel != 'stylesheet' )
				continue;
			if ( tags[i].href.match( file ) )
				return;
		}
		
		// Datei nachladen
		var body 	= js.tn( 'head' )[0];
		var link 	= document.createElement( 'link' );
			link.setAttribute( 'rel', 'stylesheet' );
			link.setAttribute( 'type', 'text/css' );
			link.setAttribute( 'href', file );
			
		// Script in den Head schreiben
		head.appendChild( link );
	}
 	
	//####################//
	//# Private Methoden #//
	//####################//
	
	/**
	 * Behebt IE fehler für die Fenstergröße
	 *
	 * @return Object
	 */	
	function _trueBody()
	{
		return ( document.compatMode && document.compatMode != 'BackCompat' ) 
				 ? document.documentElement : document.body;
	}	
	
	
	/**
	 * Prüft, ob ein Element zu einer angegebenen ID existiert
	 *
	 * @param String id ID des Elements
	 * @return Bool
	 */
	function _idExists( id )
	{
		if ( !js.e( id ) ) {
			alert( 'There is no Element with id "' + id + '"' );
			return false;
		}
			
		return true;
	}
	
	/**
	 * Öffnet ein Popup mit/ohne Focus
	 *
	 * @param String url URL der Seite im Popup
	 * @param String name Name des Popups
	 * @param Integer width Breite des Popups (Standard: 400)
	 * @param Integer height Höhe des Popups (Standard: 300)
	 * @param Bool scollbars Scrollbalken ja/nein
	 * @param Bool resizable Skalierbar ja/nein
	 * @param Bool status Statusbar ja/nein
	 * @return Window-Handle
	 */
	function _openWindow( url, name, width, height, scrollbars, resizable, status, focus )
	{
		var params = new Array(
			'width=' + ( width ? width : 400 ),
			'height=' + ( height ? height : 300 ),
			'scrollbars=' + ( scrollbars ? 'yes' : 'no' ),
			'resizable=' + ( resizable ? 'yes' : 'no' ),
			'status=' + ( status ? 'yes' : 'no' ),
			'menubar=no',
			'toolbar=no'
		);
		
		var parameter 	= params.join( ',' );
		var wnd 		= null;
		var command 	= "wnd = window.open('" + url + "', '" + name + "', '" + parameter + "');";
		
		// Popup erstellen
		eval( command );
		
		// Fokussieren?
		if ( wnd && focus )
			wnd.focus();
		else if ( wnd && !focus )
			wnd.blur();
		
		// Rückgabe
		return wnd;
	}
}

// Klasse instanziieren
var js = new js();
