/**
 * Implémente le gap entre les specs HTML et les navigateurs en rendant utilisable l'attribut accept des champs de type file
 * Lorsqu'un fichier est sélectionné : son extension est envoyée au serveur pour en déduire son type du coté serveur
 * Si le type correspond à un de ceux renseignés dans le champ accept : le fichier est sélectionné normalement
 * Si le type ne correspond pas : l'input est vidé et un message d'erreur du BO est levé.
 * Les content type avec des jokers sont gérés : image/* accepte tous les types images, *\/* accepte tout
 *
 * L'url par défaut appelée pour récupérer les contents types peut être surchargée en renseignant la variable "wedia.wxm.bov3.params.urls.get_content_type" avec le chemin relatif à cette dernière
 * Cette url recoit un ou plusieurs paramètres ext et envoit un objet json du type {"ext1": "content_type1", "ext2":"content_type2"}
 *
 */

jQuery(document).ready(function($) {

	var hCheck = !($.browser.msie || $.browser.opera);

	function pixelValue(val, font_size) {
		var match = val.match(/(\d+)(\w*)/);
		var pixels = Number(match[1]);
		if( match.length == 3 && match[2] == "em") pixels = pixels * font_size;
		return pixels;
	}

	function resize() {
		var $this = $(this);
		var min = $this.css("min-height");
		var max = $this.css("max-height");
		if( !min && !max ) return; //On oublie dans ce cas là
		var font_size = Number($this.css("font-size").match(/\d+/)[0]);
		if( !min || min == "none" ) min = (5 * font_size);
		else min = pixelValue(min, font_size);
		if( max && max == "none") max = undefined;
		else max = pixelValue(max, font_size);
		var height;
		if( this.scrollHeight > this.clientHeight ) {
			window.console && console.log("++++");

			if( max ) height = Math.min(max, this.scrollHeight + font_size );
			else height = this.scrollHeight + font_size;
			var old_height = $this.data("old-height");
			if( !old_height || old_height != height ) {
				$this.data("old-height", height);
				$this.css("height", height);
				if( height == max && ($this.css("overflow-y") == "hidden") ) $this.css("overflow-y", "auto");
			}
		} else {
			var bck_height = $(this).css("height");
			if( hCheck ) $(this).css("height", 0);
			if( min ) height = Math.max(min, this.scrollHeight + font_size );
			else height = this.scrollHeight + font_size;
			var old_height = $this.data("old-height");
			if( !old_height || old_height != height ) {
				$this.data("old-height", height);
				$this.css("height", height);
				if( height == max && ($this.css("overflow-y") == "hidden") ) $this.css("overflow-y", "auto");
			} else if( hCheck ) {
				$this.css("height", bck_height);
			}
		}
		return true;
	}
	$("textarea").each(resize);

	$("body").delegate("textarea:visible", {
		"keyup": resize
	});

});

