/**
 * darmaVideoSlider plugin
 *
 * Copyright (c) 2009 Armelle Coquart (darma.fr)
 *
 */
 
(function($) {
	$.fn.darmaVideoSlider = function(options) {
		
	var defaults = {
		slider : '#darmavideoslider', //element conteneur
		videos : new Array(), //videos
		width: 100,	
		height: 100			
	};

	this.each(function() {
		
		var obj = $(this);
		var o = $.extend(defaults, options);
		var moveleft = $(o.slider + '-moveleft');
		var moveright = $(o.slider + '-moveright');
		var capturescontainer = $(o.slider + '-capturescontainer');
		var captures = $(o.slider + '-captures');
		var videocontainer = $(o.slider + '-videocontainer');
		var page = $(o.slider + '-page');	
		var title = $(o.slider + '-title');				
		var video = o.slider + '-video';
		video = video.substring(1, video.length);
		var numelem = o.videos.length;
		var totalwidth = numelem * o.width; //largeur totale panel
		var currentVideo = 0;

		$(o.slider).height(o.height);
		$(o.slider).width(o.width);
		capturescontainer.height(o.height);
		capturescontainer.width(o.width);
		capturescontainer.css('overflow', 'hidden');		
		captures.width(totalwidth);
		videocontainer.height(0);
        $(o.slider + '-captures img').css('float', 'left');
        $(o.slider + '-captures img').css('cursor', 'pointer');
        $(o.slider + '-captures img').height(o.height);
        $(o.slider + '-captures img').width(o.width);		
		moveleft.css('visibility', 'hidden');	
		moveleft.css('cursor', 'pointer');				
		moveright.css('visibility', 'hidden');			
		moveright.css('cursor', 'pointer');				

        if(numelem > 1){
			moveright.css('visibility', 'visible');		
			title.html(o.videos[0].title);	
			page.html(1 + '/' + numelem);						
		}	
	    moveleft.click(function(){
		    if(currentVideo <= 0) return;		
		    setCaptures();
			currentVideo--;
			moveright.css('visibility', 'visible');			
			if(currentVideo == 0) moveleft.css('visibility', 'hidden');					
			videoListMoveTo(currentVideo);
	    });
	    moveright.click(function(){
		    if(currentVideo >= numelem-1) return;		
		    setCaptures();		
			currentVideo++;
			moveleft.css('visibility', 'visible');			
			if(currentVideo == numelem-1) moveright.css('visibility', 'hidden');								
			videoListMoveTo(currentVideo);			
	    });		
	    $(o.slider + '-captures img').click(function(){
			playVideo(currentVideo);		
	    });	
		
		function setCaptures(){
			videocontainer.height(0);		
			videocontainer.html('<div id="' + video + '"></div>');	
			capturescontainer.height(o.height);		
		}
		function setVideos(){
			capturescontainer.height(0);		
			videocontainer.height(o.height);		
		}
		function videoListMoveTo(pos){
			title.html(o.videos[currentVideo].title);					
			page.html(currentVideo+1 + '/' + numelem);			
			captures.animate({'marginLeft': '-' + pos * o.width + 'px'}, 180, 'swing');	
		}
		function playVideo(indice){
			setVideos();
			var flashvars = {};
			var params = {};
			params.allowFullScreen = "true";
			params.allowScriptAccess = "always";			
			params.bgcolor = "#000000";
			swfobject.embedSWF(o.videos[indice].url, video, o.width, o.height, "9.0.0", false, flashvars, params);
			var request = $.ajax({
				type: 'POST',
				url: '/ajax.video.counter.php',
				data: 'id=' + o.videos[indice].id,
				success: function(response){},
				error: function(obj, msg, e){}
			});	
		}
		
	});
	
}})(jQuery);