var Delegate = {
	toString: function() {
		return '[Delegate]';
	},
	create : function( obj, methodName ) {
		var args = [];
		if ( arguments.length > 2 ) {
			for( var i=2; i<arguments.length; i++ ) {
				args.push( arguments[ i ] );
			}
		}
		return function() {
			var a = [];
			for( var i=0; i<arguments.length; i++ ) {
				a.push( arguments[ i ] );
			}
			for( var i=0; i<args.length; i++ ) {
				a.push( args[ i ] );
			}
			if ( typeof methodName == 'function' ) {
				methodName.apply( obj, a );
			} else {
				obj[ methodName ].apply( obj, a );
			}
		}
	}
}

function Observable() {
	this.observers		= [];

	this.addObserver	= function( o ) {
		this.removeObserver( o );
		this.observers.push( o );
	}
	this.removeObserver	= function( o ) {
		for( var i=0; i<this.observers.length; i++ ) {
			if ( this.observers[ i ] == o ) {
				delete this.observers[ i ];
				this.observers.splice( i, 1 );
			}
		}
	}
	this.removeAllObservers	= function() {
		for( var i=0; i<this.observers.length; i++ ) {
			delete this.observers[ i ];
			this.observers.splice( i, 1 );
		}
	}

	this.notify			= function( evt, context ) {
		for( var i=0; i<this.observers.length; i++ ) {
			if ( 'undefined' == typeof this.observers[ i ][ evt ] ) {
				alert( evt + ' not implemented !' );
				continue;
			}
			this.observers[ i ][ evt ].apply( this.observers[ i ], [ this, context ] );
		}
	}
}

/**
*	Exécute une expression rationnelle
*/
function regExec( pattern, txt ) {
	var rg		= new RegExp( pattern, 'gi' );
	return rg.exec( txt );
}
/**
*	Extrait l'uid du chemin d'un média
*/
function getUid( picture_Src ) {
	var rv		= null;
	var matches	= regExec( '[a-z0-9]{32}', picture_Src )
	if ( matches ) {
		rv	= matches[ 0 ];
	}
	return rv;
}
function getEntryPoint() {
	var matches	= regExec( 'http://[a-z0-9_\.-]+/([a-z_\.-]+\.php)', document.location );
	return matches ? ( '/' != matches[ 1 ].substr( -1 ) ? matches[ 1 ] + '/' : matches[ 1 ] ) : '';
}

/**
*	Affiche un message d'erreur
*/
function showErrorMessage( msg ) {
	new MessagePanel( 1500 ).setErrorMessage( msg ).center().splash();
}

function popit( name, href ) {
	if ( arguments.length > 2 ) {
		var w	= arguments[ 2 ];
		var h	= arguments[ 3 ];
		var t	= ( screen.height / 2 ) - ( h / 2 );
		var l	= ( screen.width / 2 ) - ( w / 2 );
		var opt	= 'width=' + w + ',height=' + h + ',top=' + t + ',left=' + l;
	} else {
		var opt	= 'width=' + screen.width + ',height=' + screen.height;
	}

	var win = window.open( href, name, opt );
	if ( !win ) {
		alert( 'La popup a \u00e9t\u00e9 bloqu\u00e9e' );
		return false;
	}

	win.focus();
	return true;
}

jQuery(document).ready( function() {
	//	Onglets principaux du menu
	jQuery( 'ol#menu a.trigger' ).click( function( e ) {
		e.preventDefault();
	} );
	//	** **
	/*gestion du sous menu, sur hover menu*/
	$("#menu li").hover(function(){
	   $(this).children("ul").addClass("hover");

		 },function(){
		   $(this).children("ul").removeClass("hover");
	 });
	/*gestion du sous menu, sur hover ssmenu*/
	$("#menu ul").hover(function(){
	   $(this).addClass("hover");
		 },function(){
		   $(this).removeClass("hover");
	 });
	//	** **

	//	Formulaire de recherche
	var keywords				= jQuery( '#keywords' );
	var keywords_default_text	= keywords.val();
	keywords.focus( function( e ) {
		if ( jQuery(this).val() == keywords_default_text ) {
			jQuery(this).val( '' );
		}
	} );
	keywords.blur( function( e ) {
		if ( '' == jQuery(this).val() ) {
			jQuery(this).val( keywords_default_text );
		}
	} );
	jQuery( 'form#search' ).submit( function( e ) {
		e.preventDefault();
		//	On modifie l'url pour y intégrer les données recherchées
		var formAction		= jQuery(this).attr( 'action' );
		var keywords_txt	= keywords.val().replace( '/', '' ).replace( '  ', ' ' );
		var new_url			= formAction.replace( '//', '/' + encodeURIComponent( keywords_txt ) + '/' );
		document.location.href = new_url;
	} );
	//	** **

	//	Formulaire d'identification
	var login					= jQuery( '#login_login' );
	if ( login ) {
		login.focus();
	}
	//	** **

	//	Pop-ups...
	jQuery( '#popup_credit' ).click( function( e ) {
		e.preventDefault();

		popit( 'popCredits', jQuery(this).attr('href'), 345, 305 );
	} );
	jQuery( 'a.external' ).click( function( e ) {
		e.preventDefault();

		popit( 'externalLink', jQuery(this).attr('href') );
	} );
	//	** **
} );