if( typeof at == "undefined" )	at = {};
if( typeof at.treemotion == "undefined" ) at.treemotion = {}
/**************************************************************/
/*              ADMIN                                         */
/**************************************************************/
at.treemotion.CMS = {
	loadingCode: '<div id="loaderhorizon"><div id="loadercont"><img src="layout/img/spinner.gif" alt="Lade..."></div></div>'
	,	backend: false
	,	tinymce: false
	,	mainPage: 'index.php'
	,	realPath: ''
	,	relToRealPath: ''
	,	init: function()
	{
		this.setRealPath();
		this.autoSetMainPage();
	}
	,	setMainPage: function( p )
	{
		this.mainPage = p;
	}
	,	autoSetMainPage: function()
	{
		var p = document.URL;
		if( p.lastIndexOf( '/' ) != '-1' )
			p = p.substring( p.lastIndexOf( '/' ) + 1, p.length );
		if( p.substring( p.length - 1, p.length ) == "#" )
			p = p.substring( 0, p.length - 1 );
		this.setMainPage( p );
	}
	,	setRealPath: function()
	{
		var p = document.URL;
		if( p.lastIndexOf( 'page' ) != '-1' )
			p = p.substring( 0, p.lastIndexOf( 'page' ) + ( 'page' ).length + 1 );
		this.realPath = p;
		var lp = document.URL.substring( p.length, document.URL.length );
		this.relToRealPath = '';
		var index = lp.indexOf( '/' ) == -1 ? lp.length : lp.indexOf( '/' );
		lp = lp.substring( index + 1, lp.length );
		while( lp.length > 0 )
		{
			var index = lp.indexOf( '/' ) == -1 ? lp.length : lp.indexOf( '/' );
			lp = lp.substring( index + 1, lp.length );
			this.relToRealPath += '../';
		}
	}
	,	clean: function( div )
	{
		if( typeof at.treemotion.FFUtils != "undefined" )
			at.treemotion.FFUtils.cleans( div );
		if( typeof Sortable != "undefined" )
		{
			var sortables = $A( $( div ).getElementsByClassName( 'sortableList' ) );
			sortables.each( function( item ){ Sortable.destroy( item ); } );
		}
	}

	// Standard Element code
	,	registeredRandomPic: {}
	,	initRandomPic: function( id, container, path, files, seconds )
	{
		at.treemotion.CMS.destroyRandomPic( id );
		if( isNaN( seconds ) || seconds < 0.2 ) seconds = 3;
		seconds *= 1000;
		at.treemotion.CMS.registeredRandomPic[ id ] = {
			container: container
			,	counter: 0
			,	path: path
			,	files: files
			, 	seconds: seconds
			,	interval: window.setInterval( "at.treemotion.CMS.changeRandomPic( " + id + " )", seconds )
		};

	}
	,	changeRandomPic : function( id )
	{
		if( !$( at.treemotion.CMS.registeredRandomPic[ id ].container ) )
		{
			at.treemotion.CMS.destroyRandomPic();
			return;
		}
		var o = at.treemotion.CMS.registeredRandomPic[ id ];
		$( o.container ).src = o.path + o.files[ o.counter++ ];
		if( o.counter == o.files.length )
			o.counter = 0;
		at.treemotion.CMS.registeredRandomPic[ id ].counter = o.counter;
	}
	,	destroyRandomPic: function( id )
	{
		if( typeof at.treemotion.CMS.registeredRandomPic[ id ] == "undefined" ) return;
		window.clearInterval( at.treemotion.CMS.registeredRandomPic[ id ].interval );
		delete at.treemotion.CMS.registeredRandomPic[ id ];
	}

	//Highlight Box
	,	highlightedBox : {}
	,	highlightInterval : []
	/**
	 * Initialises a highlightboxset in a given container
	 * @param id // id of the container
	 * @param number // index which should be shown first
	 * @param time // optional parameter defining the time of the interval, when no time is given no interval will be started
	 */
	,	initHighlightBox: function( id, number, time )
	{
		if( typeof number == "undefined" ) number = 0;
		var boxes = $( id ).down( '.box' ).getElementsByClassName( 'checkABox' );
		if( boxes.length  == 0 ) return;
		for( var i = 0; i < boxes.length; i++ )
		{
			if( i == number ) Effect.Appear( boxes[ i ].addClassName( 'selectedBox' ).hide() );
			else boxes[ i ].hide();
		}
		at.treemotion.CMS.highlightedBox[ id ] = number;
		if( typeof time != "undefined" )
			at.treemotion.CMS.highlightInterval[ id ]
				= window.setInterval( 'at.treemotion.CMS.nextHighlightBox( "' + id + '", true )', time * 1000 );
	}
	/**
	 * shows a certain box in a highlightbox
	 * @param id // id of the container
	 * @param number // index to give
	 * @param noclear // should be given when interval should not be stopped
	 */
	,	showHighlightBox: function( id, number, noclear )
	{
		if( typeof noclear == "undefined" || !noclear ) at.treemotion.CMS.clearHighlightBoxInterval( id );
		if( $( id ).down( '.selectedBox' ) )
		{
			Effect.Fade( $( id ).down( '.selectedBox' ).removeClassName( 'selectedBox' ) );
			Effect.Appear.delay( 1, $( id ).down( '.box' ).getElementsByClassName( 'checkABox' )[ number ].addClassName( 'selectedBox' ) );
		}
		else
			Effect.Appear( $( id ).down( '.box' ).getElementsByClassName( 'checkABox' )[ number ].addClassName( 'selectedBox' ) );
		at.treemotion.CMS.highlightedBox[ id ] = number;
	}
	/**
	 * shows the last highlightbox
	 * @param id // id of the given container
	 * @param noclear // should be passed when interval should stop
	 */
	,	lastHighlightBox: function( id, noclear )
	{
		if( ! $( id ).down( '.selectedBox' ) ) return;
		if( typeof noclear == "undefined" || !noclear ) at.treemotion.CMS.clearHighlightBoxInterval( id );
		Effect.Fade( $( id ).down( '.selectedBox' ).removeClassName( 'selectedBox' ) );
		if( $( id ).down( '.box' ).getElementsByClassName( 'checkABox' ).length == at.treemotion.CMS.highlightedBox[ id ] + 1 )
			at.treemotion.CMS.highlightedBox[ id ] = 0;
		else at.treemotion.CMS.highlightedBox[ id ]++;
		var box = $( id ).down( '.box' ).getElementsByClassName( 'checkABox' )[ at.treemotion.CMS.highlightedBox[ id ] ];
		Effect.Appear.delay( 1, box.addClassName( 'selectedBox' ) );
	}
	/**
	 * shows the next highlightbox
	 * @param id // id of the given container
	 * @param noclear // should not be rest
	 */
	,	nextHighlightBox: function( id, noclear )
	{
		if( !$( id ).down( '.selectedBox' ) ) return;
		if( typeof noclear == "undefined" || !noclear ) at.treemotion.CMS.clearHighlightBoxInterval( id );
		Effect.Fade( $( id ).down( '.selectedBox' ).removeClassName( 'selectedBox' ) );
		if( 0 == at.treemotion.CMS.highlightedBox[ id ] )
			at.treemotion.CMS.highlightedBox[ id ] = $( id ).down( '.box' ).getElementsByClassName( 'checkABox' ).length - 1;
		else at.treemotion.CMS.highlightedBox[ id ]--;
		var box = $( id ).down( '.box' ).getElementsByClassName( 'checkABox' )[ at.treemotion.CMS.highlightedBox[ id ] ];
		Effect.Appear.delay( 1, box.addClassName( 'selectedBox' ) );
	}
	/**
	 * stops a certain highlight box from changing automatically
	 * @param id // id of the given container
	 */
	,	clearHighlightBoxInterval: function( id )
	{
		window.clearInterval( at.treemotion.CMS.highlightInterval[ id ] );
	}

	// Searcher
	/**
	 *
	 */
	,	search: function( target, url, form )
	{
		Element.update( target, 'Suche l&auml;uft...');
		new Ajax.Updater(
			target
			, url
			, {
				parameters: Form.serialize(document.forms[form])
				,	method: 'post'
			}
		);
	}
	,	searchCont: function( search, target, url, form )
	{
		Position.clone(
			search
			,	target
			,	{
				setWidth: false
				,	setHeight: false
				,	offsetTop: search.getHeight()
			}
		);
		if( !target.visible() )
		{
			target.show();
			Effect.BlindDown( target );
		}
			//else Effect.Shake( target, { distance: 3, duration: 0.3 } );
		at.treemotion.CMS.search( search, target.down( '.cont' ), url, form );
	}
	//Popups
	,	openExtPopUp: function( module, options, view )
	{
		var url = 'extPopup.php?module=' + module;
		if( typeof view != "undefined" ) url += '&view=' + view;
		window.open( url, 'missRedakPopUp' );
	}
	,	openPopUp: function()
	{
	}
}