function monkeyPatchAutocomplete() {


      $.ui.autocomplete.prototype._renderItem = function( ul, item) {


          var t = item.label.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + this.term + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<b>$1</b>");
          return $( "<li></li>" )
              .data( "item.autocomplete", item )
              .append( "<a>" + t + "</a>" )
              .appendTo( ul );
      };
  }

$(document).ready(function() {
	
	if (jQuery().uploadify)
	{
		$('#uploadfile').uploadify({
			'uploader': '/js/uploadify/uploadify.swf',
			'script': '/js/uploadify/uploadify.php',
			'folder': '/upload/tmp',
			'cancelImg': '/js/uploadify/cancel.png',
			'scriptAccess' : 'always',
			'checkScript' : '/js/uploadify/check.php',
			'auto' : true,
			'fileDesc' : 'zip, rar',
			'fileExt' : '*.zip;*.rar',
			'buttonImg' : '/picture/submit.png',
			'width' : 164,
			'height' : 37,
			'displayData' : 'percentage',
			onComplete : function(event, queueID, fileObj, response, data) {
			$('.uploadfile').hide().after('L\'upload du fichier '+fileObj.name+' a reussi !');
			$('input[name=file]').attr("value",fileObj.name);
			}
		});
		
		$('#upload_button').uploadify({
			'uploader': '/js/uploadify/uploadify.swf',
			'script': '/js/uploadify/uploadify.php',
			'folder': '/upload/tmp',
			'cancelImg': '/js/uploadify/cancel.png',
			'scriptAccess' : 'always',
			'checkScript' : '/js/uploadify/check.php',
			'auto' : true,
			'buttonImg' : '/picture/submit.png',
			'width' : 164,
			'height' : 37,
			'displayData' : 'percentage',
			onComplete : function(event, queueID, fileObj, response, data) {
			
			var button = $("input[id=upload_button]");
			var image_type = button.attr("title");
			
			$.getJSON("js/uploadify/check_image.php", { name: fileObj.name, type: image_type },
				function(checkData){
				
				if ($("div.upload_message").length == 0) button.parent().after("<div class=\"upload_message\"></div>");
				$(".upload_message").html("");
				
				
					if (checkData == "0")
					{
						$("#uploaded_filename").attr("value",fileObj.name);
						button.parent().hide();
						
						$(".upload_message").append("<p>L'upload du fichier "+fileObj.name+" a réussi !</p>");
						$(".upload_message").image("/upload/tmp/"+fileObj.name);
						$(".upload_message").append("<a href=\"\" id=\"cancelUploadChange\">Annuler les changements</a>");
					}
					else
					{
						$(".upload_message").append("<ul>");
						$.each(checkData, function(i, error)
						{
							$(".upload_message ul").append("<li>"+error+"</li>");
						});
						$(".upload_message").append("</ul>");
					}
				});	
			}
		});
		
		$('#upload_button2').uploadify({
			'uploader': '/js/uploadify/uploadify.swf',
			'script': '/js/uploadify/uploadify.php',
			'folder': '/upload/tmp',
			'cancelImg': '/js/uploadify/cancel.png',
			'scriptAccess' : 'always',
			'checkScript' : '/js/uploadify/check.php',
			'auto' : true,
			'buttonImg' : '/picture/submit.png',
			'width' : 164,
			'height' : 37,
			'displayData' : 'percentage',
			onComplete : function(event, queueID, fileObj, response, data) {
			
			var button = $("input[id=upload_button2]");
			var image_type = button.attr("title");
			
			$.getJSON("js/uploadify/check_image.php", { name: fileObj.name, type: image_type },
				function(checkData){
				
				if ($("div.upload_message2").length == 0) button.parent().after("<div class=\"upload_message2\"></div>");
				$(".upload_message2").html("");
				
				
					if (checkData == "0")
					{
						$("#uploaded_filename2").attr("value",fileObj.name);
						button.parent().hide();
						
						$(".upload_message2").append("<p>L'upload du fichier "+fileObj.name+" a réussi !</p>");
						$(".upload_message2").image("/upload/tmp/"+fileObj.name);
						$(".upload_message2").append("<a href=\"\" id=\"cancelUploadChange2\">Annuler les changements</a>");
					}
					else
					{
						$(".upload_message2").append("<ul>");
						$.each(checkData, function(i, error)
						{
							$(".upload_message2 ul").append("<li>"+error+"</li>");
						});
						$(".upload_message2").append("</ul>");
					}
				});	
			}
		});
		
	}
	
	// Use to create an image
	$.fn.image = function(src, f){ 
	   return this.each(function(){ 
	     var i = new Image(); 
	     i.src = src; 
	     i.onload = f; 
	     this.appendChild(i);
	   }); 
	 } 
	
	
	$('input[value=supprimer], a.delete').click(function() {
		return confirm('Voulez-vous vraiment supprimer ?');	
	});	
	
	if ($("#standby input[name=standby]").val() == 0)
	{
		$("#standby").hide();
	}
	
	
	$('select[name=serie_infos]').change(function() {
		$("#teams").empty();
		$("#teams").image("/images/loader.gif");
		var serie_infos = $('select[name=serie_infos]').attr("value");
		$.getJSON("getteams.php?serie_id=" + serie_infos,
		        function(data){
		        

					$("#teams").empty();
					$.each(data['teams'], function(i, data)
					{
						$("#teams").append("<label><input type=\"checkbox\" name=\"team[]\" value=\""+data['id']+"\" />"+data['name']+"</label>");
					});
					
					$("#choice input[name=download_status]").attr("checked", "");
					$("#choice input[name=read_status]").attr("checked", "");
					
					// Checking the chapter availability options
					
		 			if (data['standby'] == 1)
		 			{
		 				$("#standby").show();
		 				$("#standby input[name=standby]").attr("value", "1");
		 			}
		 			else
		 			{
		 				$("#standby").hide();
						$("#standby input[name=standby]").attr("value", "0");
						$("#standby input[name=standby_time]").attr("value", "0");
		 			}

		 			
		 			if (data['read_status'] == 1)
		 			{
		 				$("#choice input[name=read_status]").attr("checked", "checked");
		 			}
					if (data['download_status'] == 1)
					{
						$("#choice input[name=download_status]").attr("checked", "checked");
					}			
		        });		
		        
		        
		 
	})

	
	
	
	
	
	/*
	 * To save uploadify states when form submission failed
	 */
	if ($('input#uploaded_filename').length > 0 && $('#uploaded_filename').attr("value") != "")
	{
		var filename = $('input#uploaded_filename').attr("value");
		$(".uploadfile").hide();
		$(".uploadfile").after("<div class=\"upload_message\"></div>");
		$(".upload_message").html("");
		$(".upload_message").append("<p>L'upload du fichier "+filename+" a reussi !</p>");
		$(".upload_message").image("/upload/tmp/"+filename);
		$(".upload_message").append("<a href=\"\" id=\"cancelUploadChange\"><img src=\"/js/uploadify/cancel.png\" /></a>");
	}
	
	if ($('input#uploaded_filename2').length > 0 && $('#uploaded_filename2').attr("value") != "")
	{
		var filename = $('input#uploaded_filename2').attr("value");
		$(".uploadfile2").hide();
		$(".uploadfile2").after("<div class=\"upload_message2\"></div>");
		$(".upload_message2").html("");
		$(".upload_message2").append("<p>L'upload du fichier "+filename+" a reussi !</p>");
		$(".upload_message2").image("/upload/tmp/"+filename);
		$(".upload_message2").append("<a href=\"\" id=\"cancelUploadChange2\"><img src=\"/js/uploadify/cancel.png\" /></a>");
	}
	
	$('#cancelUploadChange').live("click", function()
			
		{
			$("#uploaded_filename").attr("value","");
			$(".uploadfile").show();
			$(".upload_message").html("Vos changements ont ete annules");
			return false;
		});
		
	$('#cancelUploadChange2').live("click", function()
		
	{
		$("#uploaded_filename2").attr("value","");
		$(".uploadfile2").show();
		$(".upload_message2").html("Vos changements ont ete annules");
		return false;
	});
	
	$('a.ajax').click(function() {
		var link = $('a.ajax').attr("href");
		$.get(link, {ajax:'1'},  function(data)
		{
			alert(data);
		});
		return false;
		
	});
	
	$('img.displaySeries').toggle(
		function(e){
			$(this).attr("src", "images/teams-cacher.png"); 
			var dd = $(this).parent();	
			var list = dd.parent().children(".seriesList");
			$(list).slideDown("slow");
			return false;
		}, 
		function(e){
			$(this).attr("src", "images/teams-afficher.png"); 
			var dd = $(this).parent();	
			var list = dd.parent().children(".seriesList");
			$(list).slideUp("slow");
			return false;
		}
	);
	
	$('div.seriesList').hide();
	
	
	/* ***************************************************************************
	 * Search Auto Complete
	 * ***************************************************************************/
	
	
	if (jQuery.fn.autocomplete)
	{
		monkeyPatchAutocomplete();         
		$("#text").autocomplete({
			source: sdata,
			minLength : 2,
            focus: function (event, ui) {
                $(event.target).val(ui.item.label);
                return false;
            },
            select: function (event, ui) {
                $(event.target).val(ui.item.label);
                window.location = ui.item.value;
                return false;
            }
		});
	}
	
});
