/**
 * dynamicItem
 * require : jQuery, jQuery.ui.sortable
 * 
 * Example
	dynamic_item = new jf_dynamicItem($("body > table > tbody"));
	dynamic_item.setItemSelector("tr");
	dynamic_item.setItemFormName("items");
	dynamic_item.setDelButtonSelector(".item_del_btn");
	dynamic_item.init();
	dynamic_item.setAdd_button($("body > .add_item_btn"));
	dynamic_item.setSortableOptions({dropOnEmpty:false, handle:".sort_handle"});
 */
var jf_dynamicItem = function(base_obj)
{
	/* Attributes */
	this._base_obj = base_obj;
	this._count = 0;
	
	/* Options */
	this._item_selector = "tr";
	this._item_form_name = "items";
	this._del_btn_selector = ".del_btn";
	this._sortableOptions = {dropOnEmpty:true
			, handle:".sort_handle"
			, opacity:0.5
			, placeholder: 'ui-state-highlight'};
	this._sortable = true;

	this._is_empty_to_hide = false;
	this._min_count = 1;
}

jf_dynamicItem.prototype = {

	/**
	 * Initialize
	 */
	init : function()
	{
		if(this._base_obj==null) return;

		if(jQuery.fn.sortable!=null)
		{
			if(this._sortable==true)
			{
				$(this._base_obj).sortable(this._sortableOptions);
			}
		}

		/** delete item's formname **/
		$(this._base_obj).find(":input").removeAttr("name");
		
		/** reset item **/
		this.reset();
	},

	/**
	 * reset all items
	 * 
	 * - delete all items & add default item
	 */
	reset : function()
	{
		if(this._base_obj==null) return;

		/** delete all items **/
		$(this._base_obj).find(this._item_selector).eq(0).hide()
			.nextAll().remove();

		this._count = 0;

		if(this._is_empty_to_hide==true && this._min_count==0)
			$(this._base_obj).hide();
		
		/** add default item **/
		for(var i=0; i<this._min_count; i++)
			this.add();
	},

	
	/**
	 * attach event add item
	 * 
	 * @param mixed add_btn
	 */
	setAdd_button : function(add_btn)
	{
		var this_c = this;
		$(add_btn).click(function(){
				this_c.add();
			});
	},
	
	/**
	 * set Empty to hide
	 * 
	 * @param boolean b
	 */
	setIsEmptyToHide : function(b)
	{
		this._is_empty_to_hide = b;
	},
	/**
	 * set minimum item count
	 * 
	 * @param integer min_count
	 */
	setMinCount : function(min_count)
	{
		this._min_count = min_count;
	},

	/**
	 * set selector : item / del_btn / SortHandle
	 */
	setItemSelector : function(item_selector)
	{
		this._item_selector = item_selector;
	},
	setDelButtonSelector : function(del_btn_selector)
	{
		this._del_btn_selector = del_btn_selector;
	},
	setSortableOptions : function(options)
	{
		this._sortableOptions = options;

		if(jQuery.fn.sortable!=null)
			$(this._base_obj).sortable(this._sortableOptions);
	},
	
	/**
	 * set Sortable
	 * 
	 * @param boolean is_sortable
	 */
	setSortable : function(is_sortable)
	{
		this._sortable = is_sortable;

		if(jQuery.fn.sortable!=null)
		{
			if(this._sortable == true)
			{
				$(this._base_obj).sortable(this._sortableOptions);
				$(this._base_obj).sortable("enable");
			}
			else
			{
				$(this._base_obj).sortable("disable");
			}
		}
	},

	/**
	 * set item_form_name
	 */
	setItemFormName : function(item_form_name)
	{
		this._item_form_name = item_form_name;
	},

	
	/**
	 * add item
	 */
	add : function()
	{
		if(this._base_obj==null) return;
		
		var area_obj = $(this._base_obj);

		/*** show BaseArea ***/
		if(this._is_empty_to_hide==true && this._count==0)
			area_obj.show();

		var this_c = this;
		/*** object clone ***/
		var new_obj = area_obj.find(this._item_selector).eq(0)
			.clone(true)
			.appendTo(area_obj)
			.show()
			/** set Name **/
			.find(":input")
				.each(function(){
						var className = $(this).attr("className");
						var name = this_c._item_form_name;
						if(className)
						{
							name += "["+this_c._count+"]["+className.match(/^[^\[]+/)+"]";
							// 2nd+ array
							if(className.indexOf("[")!=-1)
							{
								var matched = className.match(/(\[[^\[]*\])/g);
								for(m in matched)
									name += matched[m];
							}
						}
						else
						{
							name += "[]";
						}
						$(this).attr("name", name);
					})
				.end()
			/** attach event delete button **/
			.find(this._del_btn_selector).click(function(){
					this_c.del(this);
					})
				.end()
		;
		
		this._count++;
		
		return new_obj;
	},

	/**
	 * item del (call by )
	 */
	del : function(btn_obj)
	{
		/** delete item **/
		$(btn_obj).parents(this._item_selector).eq(0).remove();
		this._count--;

		/*** show BaseArea ***/
		if(this._is_empty_to_hide==true && this._count==0)
			$(this._base_obj).hide();

		/** add default item **/
		if((count=$(this._base_obj).find(this._item_selector).size())<=this._min_count)
		{
			for(var i=count; i<this._min_count; i++)
				this.add();
		}
	}
}


