/**
 * Table row cloner for centrik
 * @link http://customr.net
 * @copyright Copyright (c) 2009 James Ellis http://customr.net
 * @license Licensed under the MIT license
 * @since 0.1 (17.02.2009)
 * @depends jquery >= 1.3.1
 * @note one row must be present in the table. It must have a name attribute suffix of [0] e.g record[Name][0]. The id attribute must have a suffix of _0 e.g record_Name_0 (handled automatically in centrik)
 */

(function($) {
	/**
	 * centrik_row_cloner()
	 */
	$.fn.centrik_row_cloner = function(settings) {
		//return an instance of the validate handler
		return new $.centrik_row_clone(this, settings);
	};
	
	$.centrik_row_clone = function(row, data, settings) {
		this.row = row;
		this.rowgroup = $(row).parent().get(0).rows;
		this.template = this.rowgroup[0];
		this.settings = $.extend({}, this.settings, settings);
		this.init();
	};
		
	$.extend($.centrik_row_clone, {
		prototype : {
			init : function() {},
			settings : {},
			reindex : {},
			add : function() {
				try {
					var t = this;
					//clone from the template
					var tgt = $(this.template).clone(true);
					//insert after the action row
					tgt.insertAfter(this.row);
					//current number of rows, post insertion
					//update the id of each element in the row for validation
					$(this.template).find(':input').each(
						//traverse each input in the template row
						//update the input id, validation message and any label pointing to the input
						function() {
							var id = $(this).attr('id');
							var n = id + '_' + t.rows;
							//grab validation information from the source element
							$(tgt).find(':input#'+ id).data('centrik_validationdata',$(this).data('centrik_validationdata'));
							//update the id
							$(tgt).find(':input#'+ id).attr('id', n);
							//remove the value
							$(tgt).find(':input#'+ n).attr('value', '');
							//validation message
							$(tgt).find('div#vmessage_' + id).attr('id', 'vmessage_' + n);
							//label referencing element, if any
							$(tgt).find("label[for='" + id + "']").attr('for', n);
							//and bind a validation routine to it
							
						}
					);
					
					/**
					$(tgt).find(':input').each(
						function() {
							console.log($(this).attr('id'));
							console.log($(this).data('centrik_validationdata'));
						}
					);
					**/
					
					//add elements to the validation queue
				} catch (e) {
					console.log(e);
				}
				
			},
			//todo: cannot remove the 1st cloned row as well
			del : function() {
				if(this.rowgroup.length > 1) {
					$(this.row).remove();
				}
			}
		}
	});
})(jQuery);

//the client to instantiate based on a class on some spans
jQuery.centrik_row_cloner_client = {
	handle :  function() {
		this.additem();
		this.delitem();
	},
	additem : function() {
		var t=this;
		$('span.ck-row-add').each(function() {
				$(this).click(
					function() {
						//clone the row
						var result = $(this.parentNode.parentNode).centrik_row_cloner().add();
					}
				)
			}
		);
	},
	delitem : function() {
		var t=this;
		$('span.ck-row-del').each(function() {
				$(this).click(
					function() {
						//clone the row
						$(this.parentNode.parentNode).centrik_row_cloner().del();
					}
				)
			}
		);
	}
}

$(document).ready(
	function() {
		jQuery.centrik_row_cloner_client.handle();
	}
);