(function($) {
	
	$(document).bind('url.set-video', function() {
		
		var media = $.url._urlInfos['video'];
		
		if(media == undefined)
			return;
		
		$.url._urlInfos['audio'] = undefined;
		$(document).trigger('url.set-audio');
		
		if(!media)
			return
		
		var module = media[0];
		var file = media[1];
			
		$.getJSON('./libs/js/audio/loadVideo.ajax.php', {module: module, file: file}, function(media) {
			$.box.open({href: media.file, width: 550, height: 400, mode: 'content', onLoad: function(div) {
			
				if($.flash != undefined && $.flash.version != undefined) {
				
					var version = $.flash.version.major;
					if(version < 11)
						$("#mov").html("<div class=\"FlashError\"><h2>Une mise à jour du Player Flash est nécessaire.</h2>Vous devez télécharger et installer la dernière version d'Adobe Flash Player pour afficher ce contenu.<br /><a href=\"http://www.adobe.com/flashplayer\">Mettre à jour ma version de Flash</a></div>");
					else {
						showFlash();
					}
				} else 
					showFlash();
				
				function showFlash() {
							
				jwplayer("mov").setup({
					flashplayer: "http://philipmorax.com/libs/mediaplayer-5.8/player.swf",
					file: $("#mov").data('flv'),
					width: "550",
    				height: "400"
				});
				}
					/*	
				$(".mov").flash({
						swf: "http://philipmorax.com/libs/flvplayer/player_flv_multi.swf",
						width: 550,
						height: 400,
						allowFullScreen: true,
						flashvars: {
							flv: $(".mov").data('flv'),
							width: 550,
							height: 400,		
							autoplay: 1,
							autoload: 1,
							autonext: 0,
							margin: 0,
							loadingcolor: "d20500",
							buttonovercolor: "d20500",
							sliderovercolor: "d20500"
						}
					});
					console.log($(".mov"));
					
					*/		
				findVideos(div);
			}});
		});	
	});
	
	$(document).bind('url.parsed url.set-audio', function() {
		
		var media = $.url._urlInfos['audio'];
		
		if($.url == undefined) {
			if(window.console !== undefined) {
				console.warn('The url module is not defined');
			}
		} else {
		
			if(media) {
				
				$.url._urlInfos['video'] = undefined;
				$(document).trigger('url.set-video');
		
				var module = media[0];
				var field = media[1];
				var id = media[2]; 
	 
	 			$.getJSON('./libs/js/audio/loadAudio.ajax.php', {module: module, file: field, entry: id}, function(options) {
	 				$("#Player").setAudio(options);
	 			});
	 	} else {
	 		
	 			
	 		$("#Player").setAudio({
	 			title: 'Cliquez sur un fichier pour le jouer'
	 		});
	 	}
	 	}
	});
	
	$.setAudioDefaults = {
		
		count: true,
		showCover: true,
		coverDefault: './design/images/no-image.jpg',
		addToUrl: false,
		title: '',
		file: ''
	}
	
	$.fn.setAudio = function(options) {
	
		var options = $.extend(true, {}, $.setAudioDefaults, options);
		var html = $(this).empty();
		options.showCover = true;
	//	console.log(options);
		// The cover of the player
		if(options.showCover) {
			var coverUrl = options.cover != null ? options.cover : options.coverDefault;
			var img = new Image();
			
			$(img).attr('src', coverUrl).error(function() {
				$(this).remove();
			});
			
			$(img).appendTo(html);
		}
		var title = options.title;
		
		if(title.length > 0) {
			var marquee = $('<ul class="marquee"><li>' + title + '</li></ul>').marquee({
				   yScroll: "top",
			       showSpeed: 850,
			       scrollSpeed: 10,
			       pauseSpeed: 5000,
			       pauseOnHover: true,                       
			       loop: -1,
			       fxEasingShow: "swing",
			       fxEasingScroll: "linear",
			       // define the class statements 
			       cssShowing: "marquee-showing"
				}).appendTo(html);
		}
		
		var filedom = $('<div>').addClass('FlashPlayer').appendTo(html).flash({
	      	swf: './design/structure/player.swf',
	      	flashvars: {
	      		son: options.file
	      	},
	      	wmode: 'transparent',
	      	width: 250,
	      	height: 25
		});
		
		if(options.count == true) {
			if($.count == undefined) {
				if(window.console !== undefined) {
					console.warn('The counting module is not defined');
				}
			} else if(options.id !== undefined && options.module !== undefined) {
				var opts = {
					method: 'audio',
					id: options.id,
					module: options.module
				}
				$.count({
					method: 'audio', 
					id: options.id, 
					module: options.module, 
					file: options.file
				});
					
			} else {
				if(window.console !== undefined) {
					console.warn('The required options are not defined to apply a count');
				}
			}
		}
	}
	
}) (jQuery);
