// JavaScript Document
(function($) {
 	
	$.fn.multilineinput = function(settings) {
		var config = {eventBinding: 'bind',
					  selector:$(this).selector};
		
		if (settings) $.extend(config, settings);
	 	
		/* INIT */
		this
			.find('input[type=checkbox]')
			.each(function(index,elem){
				$(this)
					.after('<input type="hidden" name="'+$(this).attr('name')+'" value="'+($(this).is(':checked')?'1':'0')+'">')
					.removeAttr('name');
			});
		
		this.find("input[type=checkbox]").live('click',function(){
			if($(this).is(':checked')){
				$(this).next().attr('value','1');
			}else{
				$(this).next().attr('value','0');
			}
		});		
		
		this.parent().find(config.selector+":last").each(function(e) {
			try{
				
				/**
				 * COPY OF ORIGINAL
				 */
				 
				var original = $(this).clone(false);
				original.find(".js_multiline_delete").remove();
				
				/**
				 *	UNIQUE SELECT ELEMENTS
				 */
				
				disableUniqueSelecteds($(this).parent(),config.selector);
				
				/**
				 * INITIALIZATION
				 */
				
				$(this)
					.parents()
					.delegate(config.selector+":last input,"+config.selector+":last select, "+config.selector+":last textarea",
							  'change',
							  {original:original,selector:config.selector},
							  automultiline_input_change);
					
				$(this)// UNIQUE SELECT OPTION SUPPORT
					.parents()
					.delegate(config.selector+" select",
							  'change',
							  {original:original,selector:config.selector},
							  automultiline_any_select_change);
					
				$(".js_multiline_delete")
					.live("click",function(e){
						var topParent = $(this).closest(config.selector).parent();
						$(this).closest(config.selector).remove();																			  
						topParent.find(config.selector+":nth-child(odd)").css("background-color", "#eee");
						topParent.find(config.selector+":nth-child(even)").css("background-color", "transparent");
						disableUniqueSelecteds(topParent,config.selector);
					});	
								   
			}catch(e){
				if(window.console) {
					console.debug(e);
				} else {
					alert(e);
				}
			}
			
		});
	 			
		return this;
	 
   };
   
	function automultiline_input_change(e){
		
		$(this).closest(e.data.selector).after(e.data.original.clone());
		$(this).closest(e.data.selector).parent().find(e.data.selector+":nth-child(odd)").css("background-color", "#eee");
		$(this).closest(e.data.selector).parent().find(e.data.selector+":nth-child(even)").css("background-color", "transparent");
		$(this).closest(e.data.selector).append("<input class='js_multiline_delete' type='button' value='l&ouml;schen' />");
		$(this).closest(e.data.selector).next().find('input[type=checkbox]')
			.each(function(index,elem){
				if($(this).is(':checked')){
					$(this).next().attr('value','true');
				}else{
					$(this).next().attr('value','false');
				}
			});
			
	}
	
	function automultiline_any_select_change(e){
		disableUniqueSelecteds($(this).closest(e.data.selector).parent(),e.data.selector);
	}
	
	function disableUniqueSelecteds(parent,selector){
		
		parent.
		children(selector)
		.find('option.unique')
		.removeAttr("disabled");
		
		parent.
		children(selector)
		.find('option.unique:selected')
		.each(function(index,elem){
			parent
			.children(selector)
			.find("select[name="+$(this).parent().attr('name')+"]")
			.find("option.unique:not(:selected)[value="+$(this).val()+"]")
			.attr("disabled","disabled");
		});
		
	}
 
})(jQuery);

