window.addEvent('domready', function() {

	var url_default	= "http://www.castingmachine.com";
	
	if( $('ficheComedien') ) {

		//-- Liens avec popup pour messagerie
		
		if( $('message') ) {
	
			$('message').addEvent('click', function(e) {
				e.stop();
				
				if( Browser.Engine.trident && getInternetExplorerVersion() < 8 ) {
					SqueezeBox.open(this.get('href')+'&ajax=1', {size: {x: 570, y: 535}, ajaxOptions: { evalResponse: true }} );
				} else if( Browser.Engine.trident ) {
					SqueezeBox.open(this.get('href')+'&ajax=1', {size: {x: 570, y: 500}, ajaxOptions: { evalResponse: true }} );
				} else {
					SqueezeBox.open(this.get('href')+'&ajax=1', {size: {x: 570, y: 500}, ajaxOptions: { evalResponse: true }} );
				}
					
			});
		}
		
		
		
		
		var	list 	= $$('#ficheComedien_onglets li a');
		
		list.each(function(link) { //pour chaque lien des onglets
			
			//on ajoute un événement
			link.addEvent('click', function(e){
				e.stop();
				
				if( link.getParent().hasClass('ongletOff') ) {
					return;
				}
				
				list.each(function(item) {
					//on supprime les liens "current"
					item.getParent().removeClass('current');
				});
				
				//l'onglet cliqué passe en current
				link.getParent().addClass('current');
				
				$('infos_content').empty();
				
				var newElement = new Element('span');
				newElement.addClass('spinnerContainer');
				
				$('infos_content').adopt(newElement);
				
				onglet	= link.get('id');

				var zone	= window.location.pathname;
				if( !zone.test("^\/(.)+\/", "g") ) {
					zone	= zone + '/';
				}
				
				new Request.HTML({
					method: 'get',
					url: url_default + zone + '?do=' + onglet,
					data: 'ajax=1',
					update: 'infos_content',
					evalResponse: true,
					noCache: true,
					onSuccess: function() {
						if( $('session_timeout') ){
							window.location.reload();
						}
					}
				}).send();
			});
		
		});
		
		if( $$('div.pagination ul li a') ) {
			$$('div.pagination ul li a').each(function(link) {
				link.addEvent('click', function(e){
					e.stop();
					
					new Request.HTML({
						method: 'get',
						url: this.get('href'),
						data: 'ajax=1',
						update: 'infos_content',
						evalResponse: true,
						noCache: true,
						onSuccess: function() {
							if( $('session_timeout') ){
								window.location.reload();
							}
						}
					}).send();
				});
			});
		}
		
		if( $$('a.son_selection') ) {			
			$$('a.son_selection').each(function(link) {
				link.addEvent('click', function(e){
					e.stop();
					
					this.addClass('waiting');
					
					new Request.JSON({
						method: 'get',
						url: this.get('href'),
						data: 'json=1',
						onSuccess: function(response) {
							if( response.error == 600 ) {
								window.location.reload();
								return;
							}
							
							if( response.error == 0 ) {
								if( this.hasClass('son_add_to_selection') ) {
									this.set('title', "Supprimer l'extrait de la sélection");										
									
									this.set('href', this.get('href').replace(/select/g, 'remove'));
									
									this.addClass('son_remove_from_selection');
									this.removeClass('son_add_to_selection');
								} else if( this.hasClass('son_remove_from_selection') ) {
									this.set('title', "Ajouter l'extrait à la sélection");
									this.set('href', this.get('href').replace(/remove/g, 'select'));
									
									this.addClass('son_add_to_selection');
									this.removeClass('son_remove_from_selection');
								}
							}
							
							if( $('selectionTotal') ) {
								new Request.HTML({
									method: 'get',
									url: url_default + '/',
									data: 'action=get_selection_total&ajax=1',
									update: 'selectionTotal',
									noCache: true,
									onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
										this.removeClass('waiting');
										
										if( responseHTML != '' ) {
											$('selectionTotal').getParent().getParent().addClass('last');
										}
										
										$('selectionTotal').tween('color', ['#FF9900', '#007292' ]);

									}.bind(this)
								}).send();
							}
						}.bind(this)
					}).send();
				});
			});
		}
	}	

});