/* ---------------------------------------------------------
   jlo Scaler
   Author: Tim Kinnane www.owlandfox.com
   Pattern by: Tim Kinnane www.owlandfox.com
   Version: 1.0 2 Nov 2010
   Description: Makes a slider to scale an attached 
   elements size proportionaly
--------------------------------------------------------- */
(function($){
	// Initialise - applied to the element that plugin is run on
	$.fn.jloScaler = function(options) {
		var opts = $.extend(true, {}, $.fn.jloScaler.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 ---------------------------------------------------------------> */
			
			// create scale controls
			var slider = $('<div />').addClass("scale-slider");
			myself.append(slider);
			
			slidefunction = function(event, ui) {
				$(this).next(".slider-val").text(ui.value);
				
				var chain = myself.data('chainTo');
				$(chain).each(function() {
					// if this is your first time, remember what its like
					var el = $(this);
					if (!el.data('scale')) {
						el.data('original_w', el.width());
						el.data('original_h', el.height());
					}
					var originalW = el.data('original_w');
					var originalH = el.data('original_h');
					var newW = originalW * (ui.value / 100);
					var newH = originalH * (ui.value / 100);
					el.width(newW);
					el.height(newH);
					el.data('scale', ui.value);
				});
				myself.data('updated').call(myself, ui.value);
			}
			
			slider.slider({
				value: o.startscale,
				step: o.increments,
				min: o.min,
				max: o.max,
				slide: slidefunction,
				change: slidefunction
			}).css({
				width: o.width
			}).after(function() {
				return "<div class='slider-val left'>"+ slider.slider("value") + "</div>";
			});
			
			/* 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);
		});
	}
	// Manualy set scale
	$.fn.jloScalerSet = function(scale) {
		if (!scale) scale = $(this).data('startscale');
		$(this).children('scale-slider').slider('value', scale);
	};
	// Set some defaults
	$.fn.jloScaler.defaults = {
		/* CUSTOM PROPERTIES AND METHODS */
		
		width: 120,
		min: 20,
		max: 180,
		increments: 5,
		startscale: 100,
		
		/* PATTERN DEFAULTS... */
		id: 'scale-control',
		className: 'scale-control',
		title: 'Scale',

		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
		additional: function() {} // rename to whatever custom additional function you need
	}
})(jQuery); // ALL DONE
