/*
 * 
 * Différentes fonctions utiles
 * 
 */


/* **********************************************************
 * *
 * *	extend Array: ajoute de fonction à la class Array   *
 * *		
 * **********************************************************/
Array.implement({
	
	// équivalent de l'implode en PHP //
	implode: function ( car )
	{
		var chaine = '';
		if ( typeof car == 'string' )
		{
			var i = 0;
			this.each(
				function ( infos )
				{
					if ( typeof infos == 'string' )
					{
						if ( i > 0 )
							chaine += car;
						chaine += infos;
						i++;
					}
				}
			);
		}
		return chaine;
	}


});


/* ************************************************************************
 * *
 * *	Class Window : permet de voir l'affichage d'une fenêtre virtuelle *
 * *		
 * ************************************************************************/

/* * On étend la class évènement pour la création d'évènement mousewheel * */
Element.Events.extend({
	'wheelup': {
		type: Element.Events.mousewheel.type,
		map: function(event){
			event = new Event(event);
			if (event.wheel >= 0) {
				this.fireEvent('wheelup', event);
			}
		}
	},
 
	'wheeldown': {
		type: Element.Events.mousewheel.type,
		map: function(event){
			event = new Event(event);
			if (event.wheel <= 0) {
				this.fireEvent('wheeldown', event);
			}
		}
	}
});



/* **********************************************************
 * *
 * *	Class Alert : création d'alert personalisée         *
 * *		
 * **********************************************************/

var Alert = new Class({

	options: {
		mainClassName: 'alert-perso',
		className: null
	},
	
	/* *	Constructeur
	 * *
	 * ******************************************/	
	initialize: function ()
	{
		// Boite global contenant les éléments d'affichage //
		this.boite_alert = new Element('div',{
			'id': 'alert-boite',
			'styles': {
				'position'			: 'absolute',
				'width'				: '300px',
				'z-index'			: 9001,
				'top'				: '50%',
				'left'				: '50%',
				'visibility'		: 'hidden'
			}
		});
		
		// Boite contenant le titre //
		this.title = new Element('div',{
			'id':'alert-title',
			'styles': {
				'width'				: '100%',
				'margin'			: '0px'
			}
		}).set('text', 'Message d\'alerte !');


		// Boite contient les boites du texte et de l'icone //		
		this.content = new Element('div',{
			'id':'alert-content',
			'styles': {
				'width'		: '100%',
				'position'	: 'relative'
			}
		});

		// Boite contient l'icone //		
		this.content_icone = new Element('div',{'id':'alert-content-icone'});

		// Boite contient le texte //		
		this.content_text = new Element('div',{'id':'alert-content-text'});
		
		
		// Anti-focus : empèche le clic sur la page tant que la boite est présente //
		this.background = new Element('div',{
			'id': 'alert-anti-focus',
			'styles': {
				'position'			: 'absolute',
				'top'				: '0px',
				'left'				: '0px',
				'width'				: '100%',
				'background-image'	: 'url(g.gif)',
				'z-index'			: 5000
			}
		});
		
		// Bouton de fermeture de l'alert //
		this.bouton_fermer = new Element('div',{
			'id': 'alert-bouton-fermer',
			'styles': {
				'width'				: '50px',
				'margin-left'		: 'auto',
				'margin-right'		: 'auto',
				'margin-top'		: '5px',
				'margin-bottom'		: '5px'
			}
		}).set('text', 'Fermer').addEvent( 'click', function () { this.fermer(); }.bind(this) );
		
		this.content.adopt( this.content_icone, this.content_text, new Element('span', {'styles': {'clear':'both', 'display':'block'}}) );
		this.boite_alert.adopt( this.title, this.content, this.bouton_fermer );		
	},
	
	/* *	Affichage de la boite d'alert
	 * *
	 * ******************************************/	
	afficher: function ( message, options )
	{
		this.boite_alert.removeClass(this.options.className);

		this.setOptions(options);
		
		// Définition du texte de la boite d'alert //
		this.content_text.set('html', message);
		
		// On définit la taille de l'anti-focus à 100% de la hauteur de la page //
		this.background.setStyle('height', window.getScrollHeight());
		
		// Insertion de la boite dans le corp de la page //
		var body = document.getElementsByTagName('body')[0];
		this.background.injectInside( body );
		this.boite_alert.addClass( this.options.mainClassName ).injectInside( this.background );
		
		if ( this.options.className )
			this.boite_alert.addClass( this.options.className );
		
		// On positionne la boite d'alert au mileu de la fenètre et on la rend visible //
		this.boite_alert.setStyles({
			'margin-left'	: - this.boite_alert.getStyle('width').toInt()/2,
			'margin-top'	: - 50 - this.boite_alert.getStyle('height').toInt()/2,
			'visibility'	: 'visible'
		});
		
	},
	
	/* *	Affichage de la boite d'alert
	 * *
	 * ******************************************/	
	fermer: function ()
	{
		if ( this.options.className )
		{
			this.boite_alert.removeClass( this.options.className );
			this.options.className = null;
		}
		this.boite_alert.dispose();
		this.background.dispose();
	}

});

Alert.implement(new Options);
var MYalert = new Alert();







// Supprime les espaces inutiles en début et fin de la chaîne passée en paramètre.
function trim (aString) {
	return aString.replace(/^\s+/, "").replace(/\s+$/, "");
}


// Supprime les tags HTML d'un texte
function stripHTML(txt){ 
	return txt.replace(/<\S[^><]*>/g, "")
}





/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function print_r (arr,level)
{
	var dumped_text = "";
	if (!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for (var j=0;j<level+1;j++) level_padding += "    ";

	if (typeof(arr) == 'object')
	{ //Array/Hashes/Objects
		for(var item in arr)
		{
			var value = arr[item];
			if(typeof(value) == 'object')
			{ //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += print_r(value,level+1);
			}
			else
			{
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	}
	else
	{ //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

