var visionneuse = null;
var visionneuse_obj = null;
var visionneuse_players_ready = {};
var visionneuse_players_listener = function(obj){
	
	if((obj.newstate == 'IDLE') && (obj.oldstate == 'PAUSED')){
		visionneuse_players_stop_handler
		return;
	}
	
	if( 
		(
			(obj.newstate == 'COMPLETED' ) && ( (obj.oldstate == 'IDLE') || (obj.oldstate == 'PLAYING') ) // video is finished
		)
		||
		(
			(obj.newstate == 'IDLE') && (obj.oldstate == 'PAUSED') // stop clicked once paused
		)
	){
		$(obj.id).parentNode.style.position = 'absolute';
		$(obj.id).parentNode.style.top = '-1000px';
		var index = /(\d+)$/.exec(obj.id);
		index = parseInt(index[1]);
		if(visionneuse_obj.current_slide_index == index){
			//document.getElementById('visio_legend_' + index).style.display = '';
			//visionneuse_obj.slides[index].video_btn.setStyle('display', '');
			
			$('visio_legend_' + index).fade('in');
			//visionneuse_obj.slides[index].video_btn.fade('in');
		}
		else{
			if(!Browser.Engine.trident){	// blurfast replaces the image by a canvas on capable browsers. Adds filter on ie. We need to revert the canvas but not the image
				Pixastic.revert(document.getElementById('visio_pic_' + index));			
			}
		}
		visionneuse_obj.is_playing = false;
		visionneuse_obj.slides[index].unclick();
	}
};
var visionneuse_players_stop_handler = function(obj){
	$(obj.id).parentNode.style.position = 'absolute';
	$(obj.id).parentNode.style.top = '-1000px';
	var index = /(\d+)$/.exec(obj.id);
	index = parseInt(index[1]);
	if(visionneuse_obj.current_slide_index == index){
		//document.getElementById('visio_legend_' + index).style.display = '';
		//visionneuse_obj.slides[index].video_btn.setStyle('display', '');
		$('visio_legend_' + index).fade('in');
		visionneuse_obj.slides[index].video_btn.fade('in');
		
		Pixastic.revert(document.getElementById('visio_pic_' + index));
	}
	else{
		if(!Browser.Engine.trident){	// blurfast replaces the image by a canvas on capable browsers. Adds filter on ie. We need to revert the canvas but not the image
			Pixastic.revert(document.getElementById('visio_pic_' + index));			
		}
	}
	visionneuse_obj.is_playing = false;
	visionneuse_obj.slides[index].unclick();
};
function playerReady(obj){
	if(typeof(visionneuse_players_ready[obj.id]) == 'undefined'){
		var p = $(obj.id);
		visionneuse_players_ready[obj.id] = true;
	}
}


(function(){
	var slide_tag = function(elem){
		var o = {
			elem: null,
				
			init: function(elem){
				this.elem = elem;
			}
		};
		o.init(elem);
		
		return o;
	};
	
	var slide_content = function(elem){
		var o = {
			elem: null,
			pic: null,
			legend: null,
			copyright: null,
			
			init: function(elem){
				this.elem = elem;
				this.pic = this.elem.getChildren('a')[0].getChildren('img')[0];
				this.legend = this.elem.getChildren('div.infos')[0];
				
				var searchCopy = this.elem.getChildren('span.copyright');
				if(searchCopy.length > 0)
					this.copyright = searchCopy[0];
				
				// ellipsis
				// surtitre
				
				/*var n = this.legend.getChildren('span.surtitre')[0];
				var t = n.clone();
				t.setStyles({
					'height': 'auto',
					'overflow': 'visible',
					'white-space': 'normal'
				});
				t.inject(n.getParent());
				var txt = n.get('text');
				while(txt.length > 0 && t.getHeight() > n.getHeight()){
					txt = txt.substr(0, txt.length - 1);
					t.set('text', txt + '...');
				}
				n.set('text', t.get('text'));
				t.destroy();

				// link
				var n = this.legend.getChildren('strong')[0];
				var t = n.clone();
				t.setStyles({
					'height': 'auto',
					'overflow': 'visible',
					'white-space': 'normal'
				});
				t.inject(n.getParent());
				var txt = n.get('text');
				while(txt.length > 0 && t.getHeight() > n.getHeight()){
					txt = txt.substr(0, txt.length - 1);
					if(t.getChildren('a').length > 0){
						t.getChildren('a')[0].set('text', txt + '...');
					}
					else{
						t.set('text', txt + '...');
					}
				}
				n.set('html', t.get('html'));
				t.destroy();*/
				
			},
			show: function(){
				var that = this;
				
				// we show the picture
				//this.elem.setStyle('display', '');
				//this.pic.setStyle('display', '');
				//this.legend.setStyle('display', 'block');
				
				this.elem.fade('in');
				this.pic.fade('in');
				this.legend.fade('in');
				this.pic.getParent().getParent().setStyle("z-index", "5");
				
				// we slide the legend
				/*var target_h = 43;
				
				var fx = new Fx.Morph(this.legend, {
					duration: 500,
					link: 'ignore',
					transition: Fx.Transitions.linear,
					onComplete: function(){
						that.shown = true;
					}
				});
				this.legend.setStyles({
					display: '',
					height: 0,
					overflow: 'hidden'
				});
				fx.start({
					height: target_h
				});*/
			},
			hide: function(){
				//this.pic.setStyle('display', 'none');
				//this.legend.setStyle('display', 'none');
				
				this.pic.fade('out');
				this.legend.fade('out');
				this.pic.getParent().getParent().setStyle("z-index", "1");
			},
			blur_image: function(){
				Pixastic.process(this.pic, 'blurfast', {amount: 0.5});
			},
			unblur_image: function(){
				Pixastic.revert(this.pic);
			}
		};
		o.init(elem);
		return o;
	};

	var slide = function(index, parent){
		var o = {
			index: null,
			parent: null,
			tag: null,
			content: null,
			shown: false,
		
			init: function(index, parent){
				var that = this;
				
				this.index = index;
				this.parent = parent;

				if($('visio_copy_'+index)){
					$('visio_copy_'+index).fade('out');
				
					$('visio_pic_'+index).addEvents({
						'mouseenter': function(e){
							$('visio_copy_'+index).fade('in');
						},
						'mouseleave': function(e){
							$('visio_copy_'+index).fade('out');
						}
					});
				}
				
				this.tag = new slide_tag(this.parent.el.getChildren('ul.ancres').getChildren('li')[0][this.index]);
				this.content = new slide_content(this.parent.el.getChildren('ul.content_visionneuse').getChildren('li')[0][this.index]);
				this.tag.elem.addEvents({
					'mouseenter': function(e){
						e.stop();
						that = that.parent.slides[that.index]; // < we refresh the object state
						if(that.parent.is_playing){
							return;
						}

						that.show();
						that.parent.current_slide_index = that.index;
						that.parent.slideshow_stop();
						for(var i = 0; i < that.parent.slides.length; i++){
							that.parent.slides[i].tag.elem.removeClass('selected');
						}
					},
					'mouseleave': function(e){
						e.stop();
						that = that.parent.slides[that.index];
						if(that.parent.is_playing){
							return;
						}
						if( that.parent.current_slide_index != that.index ){
							that.hide();
						}
						else{
							that.select();
						}
						//if(that.parent.selected){
						if(false){
							that.parent.slides[that.parent.selected].click();
						}
						else{
							that.parent.slideshow_start();
						}
					},
					'click': function(e){
						return; // finalement, pas d'action click
						e.stop();
						that = that.parent.slides[that.index];
						if(!that.parent.selected || that.parent.selected != that.index){
							that.click();
						}
					}
				});
				this.tag.elem.getChildren('a')[0].addEvents({
					'focus': function(e){
						this.blur();
					}
				});
						
				
				// ellipsis
				/*var tag_txt_nodes = this.tag.elem.getChildren('a').getChildren('span')[0];
				for(var i = 0; i < tag_txt_nodes.length; i++){
					var n = tag_txt_nodes[i];
					var t = n.clone();
					t.setStyles({
						'overflow': 'visible',
						'height': 'auto',
						'width': '325px'
					});
					t.inject(n.getParent());
					var txt = n.get('text');
					while(txt.length > 0 && t.getHeight() > n.getHeight()){
						txt = txt.substr(0, txt.length - 1);
						t.set('text', txt + '...');
					}
					n.set('text', t.get('text'));
					t.destroy();
				}*/
				
			},
			show: function(){
				if(this.shown){
					return;
				}
				this.parent.hide_all();
				this.content.show();
				this.shown = true;
			},
			hide: function(){
				this.content.hide();
				this.shown = false;
			},
			select: function(){
				this.show();
				for(var i = 0; i < this.parent.slides.length; i++){
					this.parent.slides[i].tag.elem.removeClass('selected');
				}
				this.tag.elem.addClass('selected');
			},
			selected: function(){
				return (this.shown && this.tag.elem.hasClass('selected'));
			},
			has_video: function(){
				return ( this.content.elem.getChildren('.vid').length > 0 );
			},
			click: function(){
				this.parent.slideshow_stop();				
				this.parent.selected = this.index;
				this.parent.current_slide_index = this.index;
				this.parent.is_playing = false;

				this.show();
				for(var i = 0; i < this.parent.slides.length; i++){
					this.parent.slides[i].tag.elem.removeClass('clicked');
					this.parent.slides[i].tag.elem.removeClass('selected');
				}
				this.tag.elem.removeClass('selected');
				this.tag.elem.addClass('clicked');
				
			},
			unclick: function(){
				this.parent.selected = false;
				this.parent.slideshow_start();
				
				for(var i = 0; i < this.parent.slides.length; i++){
					this.parent.slides[i].tag.elem.removeClass('clicked');
					this.parent.slides[i].tag.elem.removeClass('selected');
				}
				this.tag.elem.addClass('selected');
			}
		};
		o.init(index, parent);
		
		if(o.has_video()){
			return new slide_video(o);
		}
		return o;
		
	};
	
	var slide_video = function(slide){
		var o = Object.beget(slide);
		var _listener_added = false;
		
		o.video = o.content.elem.getChildren('.vid')[0];
		o.video_btn = o.video.getChildren('a')[0];
		o.video_vid = o.video.getChildren('p')[0];
		o.player = $('visio_player_' + o.index);
		
		o.hide = function(){
			this.content.hide();
			//this.video_btn.setStyle('display', 'none');
			this.video_btn.fade('out');
			this.video_vid.setStyles({
				'position':'absolute',
				'top': '-1000px'
			});
			if(typeof(o.player.sendEvent) === 'function'){
				o.player.sendEvent('STOP');
			}
			//this.content.unblur_image();
			this.shown = false;
		};
		o.show = function(){
			if(this.shown){
				return;
			}
			this.parent.hide_all();
			this.content.show();
			//this.video_btn.setStyle('display', '');
			this.video_btn.fade('in');
			this.content.unblur_image();
			this.shown = true;
		};
		o.video_btn.addEvents({
			'focus': function(e){
				this.blur();
			},
			'click': function(e){
				e.stop();
				o.click();
				o.video_vid.setStyle('position', 'static');
				setTimeout(function(){
					o.play();
					o.add_listener();
					}, 100);
				o.parent.is_playing = true;
			}
		});
		o.add_listener = function(){
			var that = this;
			if(typeof(o.player.addModelListener) == 'undefined'){
				setTimeout(function(){
					that.add_listener();
				}, 100);
			}
			else{
				this.player.addModelListener('STATE', 'visionneuse_players_listener');
				this.player.addControllerListener('STOP', 'visionneuse_players_stop_handler');
			}
		};
		o.play = function(){
			var that = this;
			if(	
				typeof(visionneuse_players_ready['visio_player_' + o.index]) === 'undefined' ||
				typeof(this.player.sendEvent) == 'undefined'
			){
				setTimeout(function(){
					o.play();
				}, 100);
			}
			else{
				this.player.sendEvent('PLAY');
				//this.content.legend.setStyle('display', 'none');
				//this.video_btn.setStyle('display', 'none');
				this.content.legend.fade('out');
				this.video_btn.fade('out');
				this.content.blur_image();
			}
		};
		
		
		return o;
	};

	visionneuse = function(selector, timeout){
		
		var o = {
			el:						null,
			timeout:				null,
			slideshow:				false,
			current_slide_index:	0,
			slides:					[],
			timer:					null,
			selected:				false,
			is_playing:				false,
			
			init: function(selector, timeout){
				var that = this;
				
				this.el = $(selector);
				this.timeout = timeout;
				this.load_slides();
				
				this.slideshow_start();
			},
			load_slides: function(){
				this.slides = [];
				var slides_el = this.el.getChildren('ul.ancres').getChildren('li')[0];
				for(i=0; i < slides_el.length; i++){
					this.slides.push(new slide(i, this));
				}
			},
			hide_all: function(){
				for(i = 0; i < this.slides.length; i++){
					var slide = this.slides[i];
					slide.hide();
				}
			},
			slideshow_start: function(){
				if(this.slideshow || this.selected){
					return;
				}
				this.slideshow = true;
				this.slideshow_next();
			},
			slideshow_next: function(){
				var that = this;

				if(!this.slides[this.current_slide_index].selected()){
					this.hide_all();
					this.slides[this.current_slide_index].select();
				}

				this.timer = self.setTimeout(function(){
						that.current_slide_index++;
						if(that.current_slide_index >= that.slides.length){
							that.current_slide_index = 0;
						}
						that.slideshow_next();
					}, that.timeout
				);
			},
			slideshow_stop: function(){
				clearTimeout(this.timer);
				this.slideshow = false;
			},
			slideshow_pause: function(){
				
			},
			slideshow_resume: function(){
				
			}
		};
		
		o.init(selector, timeout);
		return o;
	};
	
})();



