/*jquery.dynamiclist.js
*
* Copyright 2009 Padilla Technology Solutions. All rights reserved.
*
*/

(function($){
	//create class to use this plug-in as OO
	var DynamicList = function(list, options){
		list = $(list);
		var _self = this;
		var items = $(' > li', list);
		_self.count = items.length;
		
		//set up the options first in the settings then use opts to set up any meta tags if included
		var settings = $.extend({}, $.fn.dynamicList.defaults, options);
		var opts = $.meta ? $.extend({}, settings, _self.data()) : settings;
		
		var vert = function(){
			//console.log('orientation var is set to vertical');
		}
		var hor = function(){
			$('li',list).each(function(){$(this).css({'display':'inline', 'margin': '10px'})});
			//console.log('orientation var is set to horizontal');
		}
		
		function setupItemsForOrientation(orientation){
			if(orientation == 'vertical')
				vert();
			else if(orientation == 'horizontal')	
				hor();
		}
		/*@function addItem
		*	adds 'item' to the end of the current list element
		* 
		* @params item: the item to add to the list
		* 		item should be an html <li> element and can hold any valid html elements. If item is not an <li> 
		*   	element the results are undefined.
		*
		* @return void
		*/
		_self.addItem = function(item){
			list.append(item);
			items = $(' > li', list);
			_self.count++;
			setupItemsForOrientation(opts.orientation);
		}
		
		/*@function removeItem
		*	removes 'item' from the current list element
		* 
		* @params item: the item to remove from the list
		* 		item should be an html <li> element. If item is not an <li> element the results are undefined.
		*
		* @return void
		*/
		_self.removeItem = function(item){
			$(item).remove();
			items = $(' > li', list);
			_self.count--;
		}
		
		/*@function getValues
		*	 sets up a string containing the values for each item in the current list element
		* 
		* @params takes no parameters
		* 		
		* @return a string containing the values for each of the items elements of each item in the list delimited by
		* specified delimiter and sub-delimiter
		*/
		_self.getValues = function(){
			var rtn = "";
			items.each(function(){
				if(rtn != "")
					rtn += opts.delimeter;
				
				try{
					var value = $("input:first", this).val();
					if(value)
						rtn += 	value;
					else	
						rtn += 	$(this).text()
				}catch(e){
					//alert(e)
				}
			});
			
			return rtn;
		}
		
		/*@function clear
		*	clear all the elments from the list
		* 
		* @params none
		*
		* @return void
		*/
		_self.clear = function(){
			return list.empty();
		}
			
			
		/*@function empty
		*	
		* 
		* @params none
		*
		* @return boolean
		*/
		_self.empty = function(){
			return _self.count <= 0;
		}	
		//set up some of the css for the list
		list.css({'list-style':'none'});
		
		
		setupItemsForOrientation(opts.orientation);
	}
	//create the actual jquery plugin
	$.fn.dynamicList = function(options){
		
		return this.each(function(){
				var _self = $(this);
				// build element specific options
				
				
				//if element is already an instance of this plug-in, return 
				if(_self.data('dynamicList')) return;
				
				var dl = new DynamicList(this, options);
				
				/*********************************************************
				THIS IS WHAT YOU NEED TO USE TO GET HOLD OF THE OBJECT!!!!
								var myvar = $('myaccessor').data('dynamicList')
				**********************************************************/
				//store plug-in object in this element's data ;;; 
				_self.data('dynamicList', dl);
		});//end this.each
	};

	$.fn.dynamicList.defaults = {orientation : 'vertical', delimeter : ':', subDelimeter : ','};

	//create class to use this plug-in as OO
	var DynamicListsManager = function(manager, options){
		manager = $(manager);
		var _self = this;
		var lists = new Array();//$(' > ul', manager).dynamicList({orientation:'horizontal'}).data('dynamicList')
		
		//set up the options first in the settings then use opts to set up any meta tags if included
		var settings = $.extend({}, $.fn.dynamicListsManager.defaults, options);
		var opts = $.meta ? $.extend({}, settings, _self.data()) : settings;
		
			$(' > ul', manager).each(function(){
				lists.push($(this).dynamicList({orientation:opts.orientation}).data('dynamicList'));
			});
		
		/*@function count
		*	
		*	@return int the number of elements in the lists array
		*/
		_self.count = function(){
			return lists.length;
		}
		
		_self.addItem = function(item){
			if(_self.count() == 0 || lists[_self.count()-1].count >= opts.listMaxElements){
				var list = $(document.createElement('ul'));	
				_self.addList(list);		
			}
			lists[_self.count()-1].addItem(item);
		}
		/*@function addList
		*	adds 'list' to the end of the lists array
		* 
		* @params list: the list to add to the lists array
		* 		list should be a dynamicList object
		*
		* @return void
		*/
		_self.addList = function(list){
			//append to the end of the manager to show in the view
			manager.append(list);
			//add the list to the lists array
			list = $(list).dynamicList({orientation : opts.orientation}).data('dynamicList');
			lists.push(list);
			
		}
		
		/*@function removeList
		*	removes'list' from the lists array
		* 
		* @params list: the list to remove from the lists array
		*
		* @return void
		*/
		_self.removeItem = function(list){
			$(list).remove();
		}
		
		/*@function getValues
		*	 sets up a string containing the values for each list in the lists array
		* 
		* @params takes no parameters
		* 		
		* @return a string containing the values for each of the lists the lists array delimited by
		* specified delimiter and sub-delimiter
		*/
		_self.getValues = function(){
			var rtn = "";
			$(lists).each(function(){
				if(rtn != "")
					rtn += opts.delimeter;
				rtn += this.getValues();
			})
			
			return rtn;
		}
		
		_self.empty = function(){
			manager.empty();
			lists = new Array();
		}
	}
	//create the actual jquery plugin
	$.fn.dynamicListsManager = function(options){
		
		return this.each(function(){
				var _self = $(this);
				//if element is already an instance of this plug-in, return 
				if(_self.data('dynamicListsManager')) return;
				
				var dl = new DynamicListsManager(this, options);
				
				/*********************************************************
				THIS IS WHAT YOU NEED TO USE TO GET HOLD OF THE OBJECT!!!!
								var myvar = $('myaccessor').data('dynamicListsManager')
				**********************************************************/
				//store plug-in object in this element's data ;;; 
				_self.data('dynamicListsManager', dl);
		});//end this.each
	};

	$.fn.dynamicListsManager.defaults = {listMaxElements : 5, delimeter : ':', orientation : 'vertical'};
})(jQuery);
