/* ---------------------------------------------------------
   jLo Element List Item
   Author: Tim Kinnane www.owlandfox.com
   Pattern by: Tim Kinnane www.owlandfox.com
   Version: 1.0 2 Nov 2010
   Description: An item in a element list, corresponding
   to a layout layer control set.
--------------------------------------------------------- */
(function($){
	// Initialise - applied to the element that plugin is run on
	$.fn.jloElementListItem = function(options) {
		var opts = $.extend(true, {}, $.fn.jloElementListItem.defaults, options); // combine passed in args with defaults
		var count = $(this).length; // how many elements being used
		var collection = $(this); // store elements collection for getting index of current for suffix
		return this.each(function() { // apply to all jquery elements in selector and return self for chaining
			var o = $.extend({}, opts, $(this).data()); // Extend passed options with any preexisting data
			var myself = $(this); // for easy access to self
			o.before.call(this); // do anything needed prior to init
			myself.children().remove();
			myself.empty(); // clear anything in the element first, just in case
			myself.addClass(o.className).css(o.css); // add class and css from options 
			var idsuffix = (count > 1) ? "-"+collection.index(this) : ""; // use numbered suffix for id'ing multiple elements
			//if(myself.attr('id') == "") myself.attr("id", o.id+idsuffix); // if no id set, use id from defaults
			myself.data(o); // store all current options in element data
			if (o.addLabel) myself.prepend($('<label>'+o.title+'</label>')); // add a label to element if required
			
			/* CUSTOM FUNCTIONALITY HERE ---------------------------------------------------------------> */
			
			myindex = myself.parent().children().index(myself);
			if(myself.attr('id') == "") myself.attr("id", o.id+"-"+myindex); // if no id set, use default + index of item in list
			
			removeButton = $('<button />').addClass('list-remove');
			selectedSymbol = $('<span />').addClass('list-selected');
			itemp = $('<p />').text(o.title);
			myself.append(removeButton);
			myself.append(itemp);
			myself.append(selectedSymbol);			
			removeButton.click(function() {
				myself.data('removed').call(myself);
				myself.remove();
			});
			myself.click(function() {
				myself.siblings().removeClass('selected');
				myself.addClass('selected');
			});
			
			/* END CUSTOM FUNCTIONALITY <---------------------------------------------------------------- */
			
			myself.bind('click', function() { $(this).data('clicked').call(this) }); // call custom function for after its clicked
			if(o.chainClick){$(this).bind('click',function(e,bc){bc?bc.push(this):bc=[this];$($(this).data('chainTo')).each(function(){if($(bc).index(this)<0)$(this).triggerHandler('click',[bc])})})}; // my super function to tie click events together
			if (o.focus) myself.focus(); // focus on self
			o.after.call(this);
		});
	};
	// Update the list item label when an element is updated
	$.fn.jloElementListItemUpdate = function(newlabel) {
		var myself = $(this);
		myself.children('P').text(newlabel);
		myself.data('updated').call(this); // Call custom function for doing additional stuff
	};
	// Set some defaults
	$.fn.jloElementListItem.defaults = {
		/* CUSTOM PROPERTIES AND METHODS */
		
		/* PATTERN DEFAULTS... */
		id: 'layer-item',
		className: 'eli-layer',
		title: 'Element List Layer Item',
		
		addLabel: false,
		css: {}, // default style object
		chainTo: [], // array of elements that this plugin is attached to - allows chaining events from one to the next
		chainClick: true, // will fire click event on chained elements when it is clicked
		before: function() {}, // function to call before initialising elements
		after: function() {}, // function to call when initialised
		clicked: function() {}, // function to call after click event processed
		updated: function() {}, // function to call when something is updated
		error: function() {}, // function to call when an error occurs
		removed: function() {}, // function to call if plugin element removed
		debug: false, // do or don't do debuging type stuff
		focus: false, // grab focus on self after init
		additional: function() {} // rename to whatever custom additional function you need
	};
})(jQuery); // ALL DONE
