/* ---------------------------------------------------------
   jlo Font Select
   Author: Tim Kinnane www.owlandfox.com
   Pattern by: Tim Kinnane www.owlandfox.com
   Version: 1.0 2 Nov 2010
   Description: Provides a select list of fonts for
   changing a chained element's font style properties
--------------------------------------------------------- */
(function($){
	$.fn.jloFontSelect = function(options) {
		var opts = $.extend(true, {}, $.fn.jloFontSelect.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 ---------------------------------------------------------------> */
			
			// add font options to select list
			var selectList = $('<select />').addClass('font-select');
			$.each(o.fonts, function(i, item) {
				var thisOption = $('<option />');
				thisOption.attr("value", item);
				if (o.selected == item) thisOption.attr("selected", "selected"); // set initial value
				thisOption.text(item.split(',')[0]);
				selectList.append(thisOption);
			});
			myself.append(selectList);
			
			selectList.change(function() {
				var selectedfont = $(this).val();
				var chain = myself.data('chainTo');
				chain.each(function() {
					$(this).css({ "font-family": selectedfont });
				});
				myself.data('selected', selectedfont);
				myself.data('updated').call(this, selectedfont);
			});
			
			/* END CUSTOM FUNCTIONALITY <---------------------------------------------------------------- */
			myself.bind('click', function() { myself.data('clicked').call(this) }); // call custom function for after its clicked
			if(o.chainClick){myself.bind('click',function(e,bc){bc?bc.push(this):bc=[this];$(myself.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);
		});
	}
	// Manually trigger selection of a specific font
	$.fn.jloFontSelectSet = function(val) {
		var selectval = (val) ? val : $(this).data('startfont'); // use passed or default font value
		$(this).find("option[value='"+selectval+"']").trigger('click');
		return this;
	};
	// Set some defaults
	$.fn.jloFontSelect.defaults = {
		/* CUSTOM PROPERTIES AND METHODS */
		
		fonts: [
			'Arial,Arial,Helvetica,sans-serif',
			'Arial Black,Arial Black,Gadget,sans-serif',
			'Comic Sans MS,Comic Sans MS,cursive',
			'Courier New,Courier New,Courier,monospace',
			'Georgia,Georgia,serif',
			'Impact,Charcoal,sans-serif',
			'Lucida Console,Monaco,monospace',
			'Lucida Sans Unicode,Lucida Grande,sans-serif',
			'Palatino Linotype,Book Antiqua,Palatino,serif',
			'Tahoma,Geneva,sans-serif',
			'Times New Roman,Times,serif',
			'Trebuchet MS,Helvetica,sans-serif',
			'Verdana,Geneva,sans-serif'
			],
		startfont: 'Arial,Arial,Helvetica,sans-serif',
		selected: {},
		
		/* PATTERN DEFAULTS... */
		id: 'font-control',
		className: 'font-controls',
		title: 'Select Font',

		addLabel: true,
		css: {}, // default style object
		chainTo: [], // array of elements that this plugin is attached to - allows chaining events from one to the next
		chainClick: false, // 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
	}
})(jQuery); // ALL DONE
