/*********** label                     ******************************/
/*********** Version 1.0               ******************************/
/*********** Author Sebastien JUZAC    ******************************/
/*********** Conversion attribut for des label a la bonne valeur ****/


jQuery.fn.label = function(o) {
	
	// Parametre par defaut
	var settings = jQuery.extend({}, o);
	
	checkLabels();
	
	function checkLabels () {
		$('label').each(function() {
			var id_input_prev = $(this).prev().find("input").attr("id");
			var id_input_next = $(this).next().find("input").attr("id");
			if (id_input_prev != '' || id_input_prev != undefined) {
				$(this).attr("for", id_input_prev);
			} else if (id_input_next != '' || id_input_next != undefined) {
				$(this).attr("for", id_input_next);
			} else {
				id_input_prev = $(this).prevAll().find('input:first').attr("id");
				id_input_next = $(this).nextAll().find('input:first').attr("id");
				if (id_input_prev != '' || id_input_prev != undefined) {
					$(this).attr("for", id_input_prev);
				} else if (id_input_next != '' || id_input_next != undefined) {
					$(this).attr("for", id_input_next);
				}
			}
		});
		
		$('input').each(function() {
			var classInput = $(this).attr("class");
			var idInput = $(this).attr("id");
			var tabClasses = new Array();
			tabClasses = classInput.split(" ");
			for(var i=0; i<tabClasses.length;i++){
				 if(tabClasses[i].indexOf("label_")>-1){
						$('label.'+tabClasses[i]).each(function() {
							$(this).attr("for", idInput);	  
						});
				 }
			}			
		});
		
	}
};

$(document).ready(function() {
	$(document).label();
});
