
function initialize_tagstrings()
{
	
	jQuery( '#tagstrings li' ).each( function( i, o ){
		var row = jQuery(o),
			delRowButton = jQuery( '<button class="delRow">-</button>' ),
			addRowButton = jQuery( '<button class="addRow">+</button>' ).appendTo( row );
		
		if( i > 0 )
			delRowButton.appendTo( row );
		
		function addRowHandler( that )
		{
			// clone the tagstrings input
			var cloned = jQuery(that).parent().clone( true ).insertAfter( jQuery(that).parent() );
			jQuery( 'input', cloned ).val( '' ).focus();
			
			// only clone if not existent
			if( !jQuery( 'button.delRow', cloned ).length )
				delRowButton.clone( true ).appendTo( cloned );
		}
		
		function addRowClickHandler( e )
		{
			addRowHandler( this );
			
			e.preventDefault();
			e.stopPropagation();
			return false;	
		}
		
		function addRowKeyUpHandler( e )
		{
			switch( e.which )
			{
				//case 13: // Enter
				case 32: // Space
					addRowHandler( this );
				break;
			}
			
			e.preventDefault();
			e.stopPropagation();
			return false;
		}
		
		function delRowHandler( el )
		{			
			// clone the tagstrings input
			jQuery( el ).parent().remove();
		}
		
		function delRowClickHandler( e )
		{
			delRowHandler( this );
			
			e.preventDefault();
			e.stopPropagation();
			return false;	
		}
		
		function delRowKeyUpHandler( e )
		{
			switch( e.which )
			{
				//case 13: // Enter
				case 32: // Space
					delRowHandler( this );
				break;
			}
			
			e.preventDefault();
			e.stopPropagation();
			return false;
		}
		
		addRowButton.bind( 'click', addRowClickHandler ).bind( 'keyup', addRowKeyUpHandler );
		delRowButton.bind( 'click', delRowClickHandler ).bind( 'keyup', delRowKeyUpHandler );
	} );
	
}


// load when DOM ready
jQuery( initialize_tagstrings );
