// nurun Accessible Video Player
// Needed: jQuery 1.4.2

var nurunAVP = function nurunAVP(){
	
	this.array = {};
	this.increment = 0;
	
	// Add a player
	this.add = function(id, player){
		this.array[id] = player;
	};
	// Get a player
	this.get = function(id){
		return this.array[id];
	};
	// Return a unique id for the nurunAVP
	this.getId = function(){
		this.increment = this.increment + 10;
		return "nurunAVP_Video_" + this.increment;
	};
	
	this.remove = function(el){
		this.array[el.id].remove();
		delete this.array[el.id];
	}
	
	this.stopHandler = function(params){
		var nurunAVP_Player = this.get(params.id)
		nurunAVP_Player.stop();
	}
	
	
	this.bufferHandler = function(params){
		var nurunAVP_Player = this.get(params.id);
		nurunAVP_Player.setVideoLoaded(params.percentage)
	}


	this.loadedHandler = function(params){
		/*var nurunAVP_Player = this.get(params.id);
		nurunAVP_Player.setVideoLoaded(100)*/
	}


	this.stateHandler = function(params){
		var nurunAVP_Player = this.get(params.id);
		if(params.newstate === 'PLAYING'){
			if(nurunAVP_Player.knowMoreButton){
				nurunAVP_Player.knowMoreButton.hide();
			}
			nurunAVP_Player.setStatePlayPause(true);
		}else if(params.newstate === 'PAUSED' || params.newstate === 'IDLE'){
			nurunAVP_Player.setStatePlayPause(false);
		}else if(params.newstate === 'COMPLETED'){
			nurunAVP_Player.endVideo();
		}
	}


	this.timerHandler = function(params){
		var nurunAVP_Player = this.get(params.id)
		nurunAVP_Player.setTime(params.position, params.duration);
		if(params.bufferPercentage){
			nurunAVP_Player.setVideoLoaded(params.bufferPercentage)
		} 
	}


	this.volumeHandler = function(params){
		var nurunAVP_Player = this.get(params.id)
		if(params.percentage == 0 && nurunAVP_Player.volume != 0){
			nurunAVP_Player.setStateMute(true, true);
		}
		if(params.percentage > 0){
			nurunAVP_Player.unmute(true);
		}
	}


	this.showCaptions = function(params){
		var nurunAVP_Player = this.get(params.id)
		nurunAVP_Player.setCaption(params.captions);
	}
	
	
	
	return this;
};
var nurunAVP = new nurunAVP();


function nurunAVP_Video(params){
	var $ = jQuery;
	
	this.id = nurunAVP.getId();
	this.params = params;
	
	this.maxVideo = 400;
	
	// default values	
	this.durationInSec = 0;
	this.volume   = 50;
	this.duration = '0:00';
	this.position = 0;
	this.timeIncrement = 10; // in seconds
	this.volumeIncrement = 10;
	
	this.volumeIsOnDrag = false;
	this.volumeDragParams = {}
	
	this.timeIsOnDrag = false;
	this.setDragVideoParams = {}
	this.percentBuffer = 0;
	
	this.actualCaption = '';
	this.subtitleShow = false;
	
	
	this.init = function(){
		// set Volume if not defined in params
		if(!this.params.volume){
			this.params.volume = this.volume;
		}else{
			this.volume = this.params.volume;
		}
		
		nurunAVP.add(this.id, this);
		
		this.divPlayer = $($(this.params.id)[0]);
		this.zoneFlashPlayer = $(this.params.divPlayer)[0];
		this.zoneControls = $(this.params.divControls)[0];
		
		this.resizePlayer(this.divPlayer, this.params.width);
		var haveFlash = this.createFlashPlayer(this.params, this.id, this.zoneFlashPlayer);
		
		if(haveFlash){
			
			this.addPlayPauseButtons();
			this.addStopButton();
			
			var right = this.addAlignRightBar($(this.zoneControls));
			
			var totalDuration = this.duration;
			if(this.params.totalDuration){
				totalDuration = this.params.totalDuration;
			}
			this.addTimerZone(right, totalDuration);
			this.addMuteUnmuteButtons(right);
			this.addVolumeSlider(right);
			
			this.addTimeSlider(right);
			
			if(this.params.captions ||this.params.urlTranscript || this.params.knowMoreLink){
				this.addAccessibilityButtonsBar();
				var divButtonsRight = this.addAlignRightBar(this.accessibilityBar);
			}
			if(this.params.captions){
				this.addSubtitleButton(divButtonsRight);
			}
			if(this.params.urlTranscript){
				this.addTranscriptButton(divButtonsRight);
			}
			if(this.params.knowMoreLink){
				this.addKnowMoreButton(this.accessibilityBar);
			}
			
			this.setDragVideoParams();
			
			this.setDragVolumeParams();
			this.moveVolumeTo(this.volume);
			
		}
		
	};
	
	
	this.remove = function(){
		$(this.zoneFlashPlayer).empty()
		$(this.zoneControls).empty()
		if(this.accessibilityBar){
			$(this.accessibilityBar).remove()
		}
		if(this.zoneCaptions){
			this.zoneCaptions.remove();
		}
	}
	
	this.endVideo = function(){
		nurunAVP_trace('Action', 'Affiche Bouton Savoir-Plus')
		if(this.knowMoreButton){
			this.knowMoreButton.show();
			this.playBtn.btn.blur();
			this.pauseBtn.btn.blur();
			this.knowMoreButton.focus();
		}
	}

	this.startDrag = function(){
		$(document).mousemove(jQuery.proxy( this.moveElements, this ));	
	}
	
	this.stopDrag = function(){
		$(document).unbind('mousemove');
	}
	
	
	this.resizePlayer = function(theDiv, theWidth){
		theDiv.css('width',theWidth);
	};
	
	
	this.createFlashPlayer = function(){
		var color = "#000000";
		if(this.params.color){
			color = this.params.color;
		}
		var so = new SWFObject(this.params.player, this.id, this.params.width, this.params.height, "9", color);
		so.addParam("wmode", "transparent");
		so.addParam("allowScriptAccess", "always");
		so.addParam("allowFullscreen", "true");
		so.addVariable("file", this.params.video);
		so.addVariable("image", this.params.image);
		so.addVariable("controlbar", "none");
		so.addVariable("volume", this.params.volume);
		so.addVariable('plugins', this.params.captionsPlugin);
		so.addVariable('captions.file', this.params.captions);
		so.addVariable('captions.state', 'true');
		so.addVariable("captions.callback", "nurunAVP_showCaptions");
		if(this.params.autostart){
			so.addVariable('autostart','true');	
		}
		
		var isOk =  so.write(this.zoneFlashPlayer);
		this.flashPlayer = $('#'+ this.id)[0];
		
		return isOk;
	};
	
	
	this.addAlignRightBar = function(theDiv){
		var bar = $('<span class="nurunAVP_alignRightBar" />');
		theDiv.append(bar);
		
		return bar;
	};
	
	
	this.addPlayPauseButtons = function(){
		var playBtn = this.newButton('nurunAVP_play', 'nurunAVP_play_act', 'nurunAVP_play_focus', this.params.altTexts.play, false, 'nurunAVP_span_button_first' );
		var pauseBtn = this.newButton('nurunAVP_pause', 'nurunAVP_pause_act', 'nurunAVP_pause_focus', this.params.altTexts.pause, true, 'nurunAVP_span_button_first' );
		
		pauseBtn.btn.attr('tabindex', '-1');
		playBtn.invertBtn = pauseBtn;
		pauseBtn.invertBtn = playBtn;
		
		
		playBtn.toggle = this.toggleButton;
		pauseBtn.toggle = this.toggleButton;
			
		playBtn.click( $.proxy( this.play, this ));
		pauseBtn.click( $.proxy( this.pause, this ));
		
		$(this.zoneControls).append(playBtn);
		$(this.zoneControls).append(pauseBtn);
		
		this.playBtn = playBtn;
		this.pauseBtn = pauseBtn;
	};
	
	
	this.addStopButton = function(){
		var stopBtn = this.newButton('nurunAVP_stop', 'nurunAVP_stop_act', 'nurunAVP_stop_focus', this.params.altTexts.stop, false, 'nurunAVP_span_button_stop' );
		
		
		stopBtn.click( jQuery.proxy( this.stop, this ));
		
		$(this.zoneControls).append(stopBtn);
		
		this.stopBtn = stopBtn;
	};
	
	
	this.addTimerZone = function(theDiv, totalTime){
		if(this.params.width >= this.maxVideo){
			var timer = $('<span class="nurunAVP_timer">0:00/'+ totalTime +'</span>');
			theDiv.append(timer);
			
			this.timer = timer;
		}
	};
	
	
	this.addMuteUnmuteButtons = function(theDiv){
		var muteBtn = this.newButton('nurunAVP_mute', 'nurunAVP_mute_act', 'nurunAVP_mute_focus', this.params.altTexts.mute, false );
		var unmuteBtn = this.newButton('nurunAVP_unmute', 'nurunAVP_unmute_act', 'nurunAVP_unmute_focus', this.params.altTexts.unmute, true );
		
		unmuteBtn.btn.attr('tabindex', '-1');
		muteBtn.invertBtn = unmuteBtn;
		unmuteBtn.invertBtn = muteBtn;
		
		muteBtn.toggle = this.toggleButton;
		unmuteBtn.toggle = this.toggleButton;
			
		muteBtn.click( $.proxy( this.mute, this ));
		unmuteBtn.click( $.proxy( this.unmute, this ));
		
		theDiv.append(muteBtn);
		theDiv.append(unmuteBtn);
		
		this.muteBtn = muteBtn;
		this.unmuteBtn = unmuteBtn;
	};
	
	
	this.addVolumeSlider = function(theDiv){
		var zoneVolume = $('<span class="nurunAVP_volume" />');
		var volumeScrub = $('<span class="nurunAVP_volumeBtn" />');
		
		zoneVolume.append(volumeScrub);
		
		volumeScrub.mousedown(jQuery.proxy( this.dragVolume, this ));
		$(document).mouseup(jQuery.proxy( this.dropVolume, this ));
		
		var volDown = $('<button class="nurunAVP_volumeDown">'+ this.params.altTexts.volumeDown +'</button>')
		var volUp = $('<button class="nurunAVP_volumeUp">'+this.params.altTexts.volumeUp+'</button>')
		
		zoneVolume.append(volDown);
		zoneVolume.append(volUp);
		
		
		volDown.click(jQuery.proxy( this.volumeDown, this ));
		volUp.click(jQuery.proxy( this.volumeUp, this ));
		volDown.focus(jQuery.proxy( this.focusVolumeDownBtn, this ));
		volUp.focus(jQuery.proxy( this.focusVolumeUpBtn, this ));
		volDown.blur(jQuery.proxy( this.focusVolumeDownBtn, this ));
		volUp.blur(jQuery.proxy( this.focusVolumeUpBtn, this ));
			
		this.volDown = volDown;
		this.volUp = volUp;
			
		this.volumeScrub = volumeScrub;
		this.zoneVolume = zoneVolume;
		
		theDiv.append(zoneVolume);
	};
	
	
	this.focusVolumeDownBtn = function(e){
		
		this.focusClass($(e.target), 'nurunAVP_volumeDownFocus', e)
	}
	
	this.focusVolumeUpBtn = function(e){
		
		this.focusClass($(e.target), 'nurunAVP_volumeUpFocus', e)
	}
	
	
	this.volumeUp = function(e){
		
		nurunAVP_trace('Action','augmente le volume')
		e.preventDefault(); 
		if(this.volume < 90){
			this.volume = this.volume + this.volumeIncrement;
		}else{
			this.volume = 100;
		}
		this.moveVolumeTo(this.volume);
		this.flashPlayer.sendEvent("volume", this.volume);
		this.muteBtn.btn.blur();
		this.volUp.focus();
	}
	
	
	this.volumeDown = function(e){
		nurunAVP_trace('Action','diminue le volume')
		e.preventDefault(); 
		if(this.volume >= 10){
			this.volume = this.volume - this.volumeIncrement;
		}else{
			this.volume = 0;
		}
		this.moveVolumeTo(this.volume);
		this.flashPlayer.sendEvent("volume", this.volume);
		this.muteBtn.btn.blur();
		this.volDown.focus();
	}
	
	
	this.focusClass = function(el, theClass, e){
		if(e.type !== 'focus'){
			el.removeClass(theClass);
		}else{
			nurunAVP_trace('Focus', theClass)
			el.addClass(theClass);
		}
	}
	
	
	this.addAccessibilityButtonsBar = function(){
		this.accessibilityBar = $('<div class="nurunAVP_accessibilityButtons"></div>');
		$(this.zoneControls).after(this.accessibilityBar)
	};
	
	
	this.addSubtitleButton = function(theDiv){
		
		
		this.subtitleButton = this.newTextButton('button', this.params.altTexts.subtitles, false, 'nurunAVP_textButton_subtitles');
		
		this.subtitleButton.click(jQuery.proxy( this.toggleCaptions, this ))
		
		
		this.zoneCaptions = $('<div class="nurunAVP_captions"/>')
		$(this.accessibilityBar).after(this.zoneCaptions)
		
		this.zoneSubCaptions = $('<div class="nurunAVP_subCaptions"/>')
		$(this.zoneFlashPlayer).after(this.zoneSubCaptions)
		
		theDiv.append(this.subtitleButton);
	};
	
	
	this.addTranscriptButton = function(theDiv){
		
		this.transcriptButton = this.newTextButton('button', this.params.altTexts.transcript, this.params.altTexts.transcriptHidden, 'nurunAVP_textButton_transcript');
		
		if(this.params.id!="#monVideo") {
			this.transcriptButton.click(jQuery.proxy( function(){
				if($(".nurunAVP_captions").hasClass("nurunAVP_captions_focus")==true) {
					window.location = this.params.urlTranscript + "?soustitre";			
				}else{
					window.location = this.params.urlTranscript
				}
			}, this ));
		}
		
		theDiv.append(this.transcriptButton);
	};
	
	
	this.addKnowMoreButton = function(theDiv){
		
		this.knowMoreButton = $('<a href="http://www.google.com" class="nurunAVP_knowMore"><span class="nurunAVP_knowMore_inside">'+ this.params.altTexts.knowMore +'</span><span class="nurunAVP_knowMore_insideB"></span><span class="nurunAVP_knowMore_outsideB"></span></a>')
		this.knowMoreButton.focus(function(){
			nurunAVP_trace('Focus','Savoir plus')
			if($(this).hasClass('nurunAVP_knowMore_visible')){
				$(this).addClass('nurunAVP_knowMore_visible_focus')
			}else{
				$(this).addClass('nurunAVP_knowMore_focus')
			}
		})
		this.knowMoreButton.blur(function(){
			$(this).removeClass('nurunAVP_knowMore_visible_focus')
			$(this).removeClass('nurunAVP_knowMore_focus')
		})
		
		
		this.knowMoreButton.show = function(){
			this.addClass('nurunAVP_knowMore_visible')
		}
		
		this.knowMoreButton.hide = function(){
			this.removeClass('nurunAVP_knowMore_visible')
		}
		
		this.knowMoreButton.attr('href', this.params.knowMoreLink)
		
		theDiv.append(this.knowMoreButton);
	};
	
	
	this.toggleCaptions = function(){
		if(!this.subtitleShow){
			nurunAVP_trace('Action','affiche captions');
			this.subtitleShow = true;
			this.zoneCaptions.addClass('nurunAVP_captions_focus');
			$(this.zoneSubCaptions).addClass('nurunAVP_subCaptions_show');
			this.zoneCaptions.css('top', $(this.zoneFlashPlayer).outerHeight() + $(this.zoneFlashPlayer).position().top )
			this.setCaption(this.actualCaption);
		}else{
			//this.setCaption('');
			nurunAVP_trace('Action','cache captions');
			this.subtitleShow = false;
			this.zoneCaptions.removeClass('nurunAVP_captions_focus');
			$(this.zoneSubCaptions).removeClass('nurunAVP_subCaptions_show');
		}
	}
	
	
	this.newTextButton = function(tag, text, hiddenText, uniqueclass){
		if(uniqueclass){
			var uniqueclass = ' '+uniqueclass;
		}else{
			uniqueclass = '';
		}
		var btn = $('<'+ tag +' class="nurunAVP_textButton'+uniqueclass+'" />');
		if(hiddenText){
			text = text + '<span class="nurunAVP_hiddenText">'+hiddenText+'</span>';
		}
		btn.html( text );
		
		
		btn.mouseenter($.proxy( this.rollOverTextButton, btn ));
		btn.mouseleave($.proxy( this.rollOverTextButton, btn ));
		btn.focus($.proxy( this.rollOverTextButton, btn ));
		btn.blur($.proxy( this.rollOverTextButton, btn ));
		
		return btn;
	};
	
	this.rollOverTextButton = function(e){
		var btn = $(e.target);
		if(e.type=="mouseenter"){
			btn.addClass('nurunAVP_textButton_hover');
			btn.removeClass('nurunAVP_textButton_focus');
		}else if(e.type=="focus"){
			nurunAVP_trace('Focus ', btn.html())
			btn.removeClass('nurunAVP_textButton_hover');
			btn.addClass('nurunAVP_textButton_focus');
		}else{
			btn.removeClass('nurunAVP_textButton_hover');
			btn.removeClass('nurunAVP_textButton_focus');
		}
	};
	
	
	this.setDragVolumeParams = function(){
		var zoneScrub = this.volumeScrub.parent();
		
	 	// Set general params for dragging
		var halfButton = Math.floor(parseInt(this.volumeScrub.css('width')) /2) -1;
		var minAbsX = zoneScrub.position().left + parseInt(zoneScrub.css('padding-left')) + halfButton;
		var maxX = parseInt(zoneScrub.css('width')) - parseInt(this.volumeScrub.css('width'));
		
		this.volumeDragParams = {
			'minAbsX' : minAbsX,
			'maxX' : maxX,
			'halfButton' : halfButton
		};
	};
	
	this.dragVolume = function(e){
		e.preventDefault();
		this.setDragVolumeParams();
		this.volumeIsOnDrag = true;
		this.startDrag();
	}
	
	
	this.dropVolume = function(e){
		this.volumeIsOnDrag = false;
		this.stopDrag();
	}
	
	
	this.moveElements = function(e){
		
		var cursorX = e.pageX - $(this.zoneControls).offset().left;
		
		if(this.volumeIsOnDrag){
			
			e.preventDefault();
			var x = cursorX - this.volumeDragParams.minAbsX;

			if(x < 0){ x = 0; }
			if(x > this.volumeDragParams.maxX){ x = this.volumeDragParams.maxX; }

			this.volumeScrub.css('left', x);
			
			var volume = parseInt(( x / this.volumeDragParams.maxX) * 100);
			this.volume = volume;
		
			this.flashPlayer.sendEvent("volume", this.volume);
		}
		
		if(this.timeIsOnDrag && this.durationInSec != 0 && this.params.width >= this.maxVideo){
			e.preventDefault();
			var x = cursorX - this.dragVideoParams.minAbsX - (this.dragVideoParams.widthButton /2);
			
			var maxLoaded = parseInt(this.timeSliderLoad.width() - this.dragVideoParams.widthButton);
			if(x < 0){ x = 0; }
			if(x > maxLoaded){ x = maxLoaded; }
			
			this.timeSliderBtn.css('left', x);
			
			var percentageTime = x / this.dragVideoParams.maxX;
			
			this.play();
			var sec = percentageTime * this.durationInSec;
			this.seekVideo(sec);
			this.setTime(sec,0);
			this.pauseBtn.btn.blur();
		}
	}
	
	
	this.play = function(e){
		if(this.knowMoreButton){
			this.knowMoreButton.hide()
		}
		nurunAVP_trace('Action','play');
		this.flashPlayer.sendEvent("play", "true");
	};
	
	
	this.pause = function(e){
		nurunAVP_trace('Action','pause');
		this.flashPlayer.sendEvent("play", "false");
	};
	
	
	this.setStatePlayPause = function(state){
		this.pauseBtn.blur();
		this.playBtn.blur();
		this.stopBtn.blur();
		this.muteBtn.blur();
		this.unmuteBtn.blur();
		this.volDown.blur();
		this.volUp.blur();
		if(this.subtitleButton){
			this.subtitleButton.blur();
		}
		if(this.transcriptButton){
			this.transcriptButton.blur();
		}
		if(this.knowMoreButton){
			this.knowMoreButton.blur();
		}
		if(this.timeSliderFocusLeft){
			this.timeSliderFocusLeft.blur();
			this.timeSliderFocusRight.blur();
		}
		if(state){
			nurunAVP_trace('Action','show Pause');
			this.playBtn.toggle();
		}else{
			nurunAVP_trace('Action','show Play');
			this.pauseBtn.toggle();
		}
	};
	
	
	this.stop = function(){
		nurunAVP_trace('Action','stop')
		this.flashPlayer.sendEvent("stop", "true");
		this.resetCaption();
		this.setTime(0,0);
		this.stopBtn.btn.blur();
		this.playBtn.btn.focus();
	};
	
	
	this.mute = function(){
		nurunAVP_trace('Action','mute')
		this.flashPlayer.sendEvent("mute", "true");
		this.setStateMute(true);
	};
	
	
	this.unmute = function(noButton){
		nurunAVP_trace('Action','unmute')
		this.flashPlayer.sendEvent("mute");
		this.setStateMute(false, noButton);
	};
	
	
	this.setStateMute = function(state, dontFocus){
		if(state){
			this.muteBtn.toggle(dontFocus);
		}else{
			this.unmuteBtn.toggle(dontFocus);
		}	
	};
	
	
	this.newButton = function(theClass, classAct, classFocus, altText, isHidden, spanClass){
		if(spanClass){
			var spanClass = ' '+spanClass;
		}else{
			var spanClass = '';
		}
		
		var btn = $('<button class="nurunAVP_button '+ theClass +'" title="'+ altText +'">'+altText+'</button>');
		var span = $('<span class="nurunAVP_span_button'+ spanClass +'" />');
		
		span.btn = btn;
		span.append(btn);
		btn.classAct = classAct;
		btn.classFocus = classFocus;
		
		btn.mouseenter($.proxy( this.rollOverButton, btn ));
		btn.mouseleave($.proxy( this.rollOverButton, btn ));
		btn.focus($.proxy( this.rollOverButton, btn ));
		btn.blur($.proxy( this.rollOverButton, btn ));
		
		span.show = this.showButton;
		span.hide = this.hideButton;
		if(isHidden){ span.hide(); }
		
		
		return span;
	};
	
	
	this.rollOverButton = function(e){
		if(e.type=="mouseenter"){
			this.addClass(this.classAct);
			this.removeClass(this.classFocus);
		}else if(e.type=="focus"){
			nurunAVP_trace('Focus',this.classAct);
			this.removeClass(this.classAct);
			this.addClass(this.classFocus);
		}else{
			this.removeClass(this.classAct);
			this.removeClass(this.classFocus);
		}
	};
	
	
	this.toggleButton = function(dontFocus){
		this.invertBtn.removeClass('nurunAVP_button_hide');
		this.addClass('nurunAVP_button_hide');
		this.invertBtn.btn.removeAttr('tabindex');
		this.btn.attr('tabindex', "-1");
		
		if(dontFocus !== true){
			this.invertBtn.btn.focus();
		}
	};
	
	
	this.showButton = function(){
		this.removeClass('nurunAVP_button_hide');
	};
	
	
	this.hideButton = function(){
		this.addClass('nurunAVP_button_hide');
	};
	
	this.setTime = function(position, duration){
		
		if(this.timer){
			this.scrubVideo(position, duration)
			this.position = position;
			
			if(this.duration == '0:00'){
			
				this.durationInSec = duration;
				
				durationMinutes = Math.floor(duration / 60)
				durationSeconds = Math.floor(duration % 60)
				
				if(durationSeconds < 10){
					durationSeconds = "0" + durationSeconds;
				}
				
				this.duration = durationMinutes + ':' + durationSeconds
			}
			
			positionMinutes = Math.floor(position / 60)
			positionSeconds = Math.floor(position % 60)
			
			if(positionSeconds < 10){
				positionSeconds = "0" + positionSeconds;
			}
			
			position = positionMinutes + ':' + positionSeconds
		
			this.timer.html(position + '/' + this.duration);
		}
	}
	
	this.moveVolumeTo = function(pos){
		// Mettre en px
		var x = pos / 100 * this.volumeDragParams.maxX;
		this.volumeScrub.css('left', x);
	}
	
	
	this.setCaption = function(text){
		this.actualCaption = text;
		if(this.subtitleShow){
			this.zoneCaptions.html(text);
		}
	}
	
	
	this.resetCaption = function(){
		this.setCaption('');
	}
	
	this.setVideoLoaded = function(percentage){
		if(percentage - this.percentBuffer >= 0.5){
			this.timeSliderLoad.css('width',percentage + '%');
		}
	}
	
	
	this.addTimeSlider = function(theDiv){
		
		if(this.params.width >= this.maxVideo){
			var timeSlider = $('<span class="nurunAVP_timeSlider"/>');
			
			var widthOfOthers = parseInt(this.playBtn.outerWidth()) + parseInt(this.stopBtn.outerWidth()) + parseInt(this.timer.outerWidth()) + parseInt(this.muteBtn.outerWidth()) + parseInt(this.zoneVolume.outerWidth());
			theDiv.before(timeSlider);
			var sliderStyles = {
				'borderLeft' : 0,
				'borderRight' : 0
			}

			if(!isNaN(parseInt(timeSlider.css('border-right-width')))){
				sliderStyles.borderRight = parseInt(timeSlider.css('border-right-width'));
			}
			if(!isNaN(parseInt(timeSlider.css('border-left-width')))){
				sliderStyles.borderLeft = parseInt(timeSlider.css('border-left-width'));
			}

			
			var theWidth = parseInt($(this.zoneControls).innerWidth()) - widthOfOthers - parseInt(timeSlider.css('padding-left')) -  parseInt(timeSlider.css('padding-right')) - sliderStyles.borderRight - sliderStyles.borderLeft;
			
			$(timeSlider).css('width', theWidth );
			
			this.timeSliderZone =  $('<span class="nurunAVP_timeSliderZone"/>');
			this.timeSliderLoad =  $('<span class="nurunAVP_timeSliderLoaded"/>');
			this.timeSliderBtn =  $('<span class="nurunAVP_timeSliderBtn"/>');
			
			this.timeSliderBtn.mousedown(jQuery.proxy( this.dragTime, this ));
			$(document).mouseup(jQuery.proxy( this.dropTime, this ));
			
			this.timeSliderFocus = $('<span class="nurunAVP_timeSliderFocusBtns"/>');
			this.timeSliderFocusLeft = $('<button class="nurunAVP_timeSliderFocusBtnLeft">'+this.params.altTexts.backward+'</button>');
			this.timeSliderFocusRight = $('<button class="nurunAVP_timeSliderFocusBtnRight">'+this.params.altTexts.forward+'</button>');

			this.timeSliderFocusLeft.focusClass = 'nurunAVP_timeSliderFocusBtnLeftFocus'
			this.timeSliderFocusRight.focusClass = 'nurunAVP_timeSliderFocusBtnRightFocus'
			this.timeSliderFocusLeft.playerVideo = this;
			this.timeSliderFocusRight.playerVideo = this;
			
			this.timeSliderFocusLeft.click($.proxy( this.rewindVideo, this ));
			this.timeSliderFocusRight.click($.proxy( this.forwardVideo, this ));
			this.timeSliderFocusRight.blur($.proxy( this.rollOverTimeButton, this.timeSliderFocusRight ));
			this.timeSliderFocusLeft.blur($.proxy( this.rollOverTimeButton, this.timeSliderFocusLeft ));
			this.timeSliderFocusLeft.focus($.proxy( this.rollOverTimeButton, this.timeSliderFocusLeft ));
			this.timeSliderFocusRight.focus($.proxy( this.rollOverTimeButton, this.timeSliderFocusRight ));
			
			timeSlider.append(this.timeSliderZone);
			this.timeSliderZone.append(this.timeSliderLoad);
			this.timeSliderLoad.append(this.timeSliderBtn);
			this.timeSliderBtn.append(this.timeSliderFocus);
			
			
			this.timeSliderFocus.append(this.timeSliderFocusLeft);
			this.timeSliderFocus.append(this.timeSliderFocusRight);
		}
		
	};
	
	
	this.rewindVideo = function(e){
		nurunAVP_trace('Action','recule video')
		e.preventDefault(); 
		var pos = this.position - this.timeIncrement -1;
		if(pos < 0){ pos = 0; }
		this.seekVideo(pos);
		this.timeSliderFocusLeft.focus();
	}
	
	
	this.forwardVideo = function(e){
		nurunAVP_trace('Action','avance video')
		e.preventDefault(); 
		var pos = this.position + this.timeIncrement -1;
		this.seekVideo(pos);
		this.timeSliderFocusRight.focus();
	}
	
	
	this.rollOverTimeButton = function(e){
		el = $(this);
		if(e.type !== 'focus'){
			el.parent().removeClass('nurunAVP_timeSliderFocusBtnsFocus');
			el.removeClass(this.focusClass)
		}else{
			nurunAVP_trace('Focus',this.focusClass)
			el.parent().addClass('nurunAVP_timeSliderFocusBtnsFocus');
			el.addClass(this.focusClass)
			this.playerVideo.pauseBtn.btn.blur();
		}
	}
	
	
	this.setDragVideoParams = function(){
		if(this.params.width >= this.maxVideo){
			var widthButton = parseInt(this.timeSliderBtn.css('width'));
			var minAbsX = this.timeSliderZone.position().left;
			var maxX = parseInt(this.timeSliderZone.css('width')) - parseInt(this.timeSliderBtn.css('width'));
			
			this.dragVideoParams = {
				'minAbsX' : minAbsX,
				'maxX' : maxX,
				'widthButton': widthButton
			}
		}
	}
	
	this.dragTime = function(e){
		e.preventDefault();
		this.setDragVideoParams();
		this.timeIsOnDrag = true;
		this.startDrag();
	}
	
	
	this.dropTime = function(e){
		this.timeIsOnDrag = false;
		if(this.position == 0){
			this.scrubVideo(0,0)
		}
		this.stopDrag()
	}
	
	
	this.scrubVideo = function(position, duration){
		if(!this.timeIsOnDrag){
			if(duration == 0){
				duration = 1;
			}
			var percentage = position/duration;
			

			var posX = parseInt(this.dragVideoParams.maxX * percentage);
			this.timeSliderBtn.css('left', posX)
		}
	}
	
	this.seekVideo = function(time){
		this.flashPlayer.sendEvent("seek", time);
	}
	
	
	this.init();
}

function nurunAVP_trace(action, msg){
/*	if(nurunAVP_gup('debug') == 1){
		if(typeof(console) !== "undefined"){
			console.log(action +' : '+ msg);
		}else{
			jQuery('#trace').html(msg + '<br>'+ jQuery('#trace').html());
		}
	}*/
}


function nurunAVP_gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

	
// the name "PlayerReady" is mandatory for FW FLV Player
function playerReady(obj){
	var player = document.getElementById(obj.id);
	player.addControllerListener("VOLUME", 'nurunAVP.volumeHandler');
	player.addModelListener("STATE", "nurunAVP.stateHandler");
	player.addModelListener("TIME", "nurunAVP.timerHandler");
	player.addModelListener("BUFFER", "nurunAVP.bufferHandler");
	player.addModelListener("LOADED", "nurunAVP.loadedHandler");
	player.addControllerListener("STOP", "nurunAVP.stopHandler");
}


function nurunAVP_showCaptions(params){
	nurunAVP.showCaptions(params);
}
