/**
 * LICENSE
 *
 * This source file is subject to the EWS Software License that is bundled
 * with this package in the file LICENSE.txt.
 * If you did not receive a copy of the license please send us an email
 * so we can send you a copy immediately.
 * Permission to copy and distribute verbatim copies of this source
 * are not granted.
 *
 * @copyright 	Copyright (c) 2008-2009 entrance web studio
 * @license	EWS Software License
 */

/**
 * Javascript library
 *
 * @author christoph lukas lindtner
 * @dependencies prototype, jQuery, global
 */
 
if ( ! ( include instanceof Function ) )
	throw new Error ( "No include function found" );

var Snoooze = 
{
	/* ------------------------------------------------------------------------------------------------------------ *
	 *							 																				     																										*
	 * Class variables																								     																					*
	 *																											     																										*
	 * ------------------------------------------------------------------------------------------------------------ */
	
	/**
	 * All functions included in this stack are called after all document content has been loaded.
	 *
	 * @var Array initFunctions
	 */  
	initFunctions : [],
	

	/* ------------------------------------------------------------------------------------------------------------ *
	 *							 																				     																										*
	 * Class methods																								     																						*
	 *																											     																										*
	 * ------------------------------------------------------------------------------------------------------------ */
	  
	/**
	 * @public
	 */
	 
	/**
	 * Navigates to a specific url
 	 *
 	 * @param String url
 	 * @return void
 	 */
 	redirect : function ( url ) {
 		var clean = url.strip().stripTags().stripScripts();
 		
 		document.location.href = clean;
 	},
 	
	
	/**
	 * Adds a window onload function.
	 *
	 * @param Function func
	 * @return void
	 */
	construct : function ( func ) {
		if ( typeof ( func ) != "function" )
			return;
		
		this.initFunctions.push ( func );
	},
	
	
	/**
	 * This function is called in the onload event. It executes all functions which have been added to the
	 * on-load stack
	 *
	 * Todo: Automate
	 *
	 * @return void
	 */
	initialize : function () {
		var n = this.initFunctions.length;
		
		if ( n <= 0 ) {
			return;
		}
		
		for ( var i = 0; i < n; i++ ) {
			var f = this.initFunctions [ i ];
			
			f.call ();
		}
	} ,
	
	/** 
 	 * Shows a content by id or url
 	 *
 	 * @param int id
 	 * @return void
 	 */
	requestContent : function ( url , id ) {
		var body = $ ( "body" );
		jQuery ( body ).hide ();
		
		$ ( "loader" ).show ();
		
		var data = null;
		if ( id != undefined )
			data = { "cid" : id };
		
		shell.wait ();
		jQuery ( body ).load ( url , data , onLoadContent );
	} ,
	
	
	/**
	 * Search
	 *
	 * @param Event e 
	 * @return void
	 */
	searchContent : function ( value , url ) {
		var body = $ ( "body" );
		jQuery ( body ).hide ();
		
		$ ( "loader" ).show ();
		
		shell.wait ();
		jQuery ( body ).load ( url , { "q" : String ( value ) } , this.onLoadContent );
	} ,
	
	
	/* ------------------------------------------------------------------------------------------------------------ *
	 *							 																				     																										*
	 * Class callbacks																							     																						*
	 *																											     																										*
	 * ------------------------------------------------------------------------------------------------------------ */
	
	/**
	 * Invokes if content has been loaded.
	 *
	 * @param String data
	 * @param String textStatus
	 * @param XMLHttpRequest request
	 * @return void
	 */
	onLoadContent : function ( data , textStatus , request ) {
		shell.resume ();
		$ ( "loader" ).hide ();
		
		var body = $ ( "body" );
		jQuery ( body ).show ();
		
		if ( textStatus != "success" )
			jAlert ( "Verbindung wurde unterbrochen!" , "Transport Fehler" );
	}
}
