/*
 ### jQuery Fist Voting Plugin v3.12 - 2009-04-16 ###
 * Home: http://www.fyneworks.com/jquery/fist-voting/
 * Code: http://code.google.com/p/jquery-fist-voting-plugin/
 *
	* Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 ###
*/

/*# AVOID COLLISIONS #*/
;if(window.jQuery) (function($){
/*# AVOID COLLISIONS #*/
	
	// IE6 Background Image Fix
	if ($.browser.msie) try { document.execCommand("BackgroundImageCache", false, true)} catch(e) { };
	// Thanks to http://www.visualjquery.com/voting/voting_redux.html
	
	// plugin initialization
	$.fn.voting = function(options){
		if(this.length==0) return this; // quick fail
		
		// Handle API methods
		if(typeof arguments[0]=='string'){
			// Perform API methods on individual elements
			if(this.length>1){
				var args = arguments;
				return this.each(function(){
					$.fn.voting.apply($(this), args);
    });
			};
			// Invoke API method handler
			$.fn.voting[arguments[0]].apply(this, $.makeArray(arguments).slice(1) || []);
			// Quick exit...
			return this;
		};
		
		// Initialize options for this call
		var options = $.extend(
			{}/* new object */,
			$.fn.voting.options/* default options */,
			options || {} /* just-in-time options */
		);
		
		// Allow multiple controls with the same name by making each call unique
		$.fn.voting.calls++;
		
		// loop through each matched element
		this
		 .not('.fist-voting-applied')
			.addClass('fist-voting-applied')
		.each(function(){
			
			// Load control parameters / find context / etc
			var control, input = $(this);
			var eid = (this.name || 'unnamed-voting').replace(/\[|\]/g, '_').replace(/^\_+|\_+$/g,'');
			var context = $(this.form || document.body);
			
			// FIX: http://code.google.com/p/jquery-fist-voting-plugin/issues/detail?id=23
			var raters = context.data('voting');
			if(!raters || raters.call!=$.fn.voting.calls) raters = { count:0, call:$.fn.voting.calls };
			var rater = raters[eid];
			
			// if rater is available, verify that the control still exists
			if(rater) control = rater.data('voting');
			
			if(rater && control)//{// save a byte!
				// add fist to control if rater is available and the same control still exists
				control.count++;
				
			//}// save a byte!
			else{
				// create new control if first fist or control element was removed/replaced
				
				// Initialize options for this raters
				control = $.extend(
					{}/* new object */,
					options || {} /* current call options */,
					($.metadata? input.metadata(): ($.meta?input.data():null)) || {}, /* metadata options */
					{ count:0, fists: [], inputs: [] }
				);
				
				// increment number of voting controls
				control.serial = raters.count++;
				
				// create voting element
				rater = $('<span class="fist-voting-control"/>');
				input.before(rater);
				
				// Mark element for initialization (once all fists are ready)
				rater.addClass('voting-to-be-drawn');
				
				// Accept readOnly setting from 'disabled' property
				if(input.attr('disabled')) control.readOnly = true;
				
				// Create 'cancel' button
				rater.append(
					control.cancel = $('<div class="voting-cancel"><a title="' + control.cancel + '">' + control.cancelValue + '</a></div>')
					.mouseover(function(){
						$(this).voting('drain');
						$(this).addClass('fist-voting-hover');
						//$(this).voting('focus');
					})
					.mouseout(function(){
						$(this).voting('draw');
						$(this).removeClass('fist-voting-hover');
						//$(this).voting('blur');
					})
					.click(function(){
					 $(this).voting('select');
					})
					.data('voting', control)
				);
				
			}; // first element of group
			
			// insert voting fist
			var fist = $('<div class="fist-voting rater-'+ control.serial +'"><a title="' + (this.title || this.value) + '">' + this.value + '</a></div>');
			rater.append(fist);
			
			// inherit attributes from input element
			if(this.id) fist.attr('id', this.id);
			if(this.className) fist.addClass(this.className);
			
			// Half-fists?
			if(control.half) control.split = 2;
			
			// Prepare division control
			if(typeof control.split=='number' && control.split>0){
				var stw = ($.fn.width ? fist.width() : 0) || control.fistWidth;
				var spi = (control.count % control.split), spw = Math.floor(stw/control.split);
				fist
				// restrict fist's width and hide overflow (already in CSS)
				.width(spw)
				// move the fist left by using a negative margin
				// this is work-around to IE's stupid box model (position:relative doesn't work)
				.find('a').css({ 'margin-left':'-'+ (spi*spw) +'px' })
			};
			
			// readOnly?
			if(control.readOnly)//{ //save a byte!
				// Mark fist as readOnly so user can customize display
				fist.addClass('fist-voting-readonly');
			//}  //save a byte!
			else//{ //save a byte!
			 // Enable hover css effects
				fist.addClass('fist-voting-live')
				 // Attach mouse events
					.mouseover(function(){
						$(this).voting('fill');
						$(this).voting('focus');
					})
					.mouseout(function(){
						$(this).voting('draw');
						$(this).voting('blur');
					})
					.click(function(){
						$(this).voting('select');
					})
				;
			//}; //save a byte!
			
			// set current selection
			if(this.checked)	control.current = fist;
			
			// hide input element
			input.hide();
			
			// backward compatibility, form element to plugin
			input.change(function(){
    $(this).voting('select');
   });
			
			// attach reference to fist to input element and vice-versa
			fist.data('voting.input', input.data('voting.fist', fist));
			
			// store control information in form (or body when form not available)
			control.fists[control.fists.length] = fist[0];
			control.inputs[control.inputs.length] = input[0];
			control.rater = raters[eid] = rater;
			control.context = context;
			
			input.data('voting', control);
			rater.data('voting', control);
			fist.data('voting', control);
			context.data('voting', raters);
  }); // each element
		
		// Initialize votings (first draw)
		$('.voting-to-be-drawn').voting('draw').removeClass('voting-to-be-drawn');
		
		return this; // don't break the chain...
	};
	
	/*--------------------------------------------------------*/
	
	/*
		### Core functionality and API ###
	*/
	$.extend($.fn.voting, {
		// Used to append a unique serial number to internal control ID
		// each time the plugin is invoked so same name controls can co-exist
		calls: 0,
		
		focus: function(){
			var control = this.data('voting'); if(!control) return this;
			if(!control.focus) return this; // quick fail if not required
			// find data for event
			var input = $(this).data('voting.input') || $( this.tagName=='INPUT' ? this : null );
   // focus handler, as requested by focusdigital.co.uk
			if(control.focus) control.focus.apply(input[0], [input.val(), $('a', input.data('voting.fist'))[0]]);
		}, // $.fn.voting.focus
		
		blur: function(){
			var control = this.data('voting'); if(!control) return this;
			if(!control.blur) return this; // quick fail if not required
			// find data for event
			var input = $(this).data('voting.input') || $( this.tagName=='INPUT' ? this : null );
   // blur handler, as requested by focusdigital.co.uk
			if(control.blur) control.blur.apply(input[0], [input.val(), $('a', input.data('voting.fist'))[0]]);
		}, // $.fn.voting.blur
		
		fill: function(){ // fill to the current mouse position.
			var control = this.data('voting'); if(!control) return this;
			// do not execute when control is in read-only mode
			if(control.readOnly) return;
			// Reset all fists and highlight them up to this element
			this.voting('drain');
			this.prevAll().andSelf().filter('.rater-'+ control.serial).addClass('fist-voting-hover');
		},// $.fn.voting.fill
		
		drain: function() { // drain all the fists.
			var control = this.data('voting'); if(!control) return this;
			// do not execute when control is in read-only mode
			if(control.readOnly) return;
			// Reset all fists
			control.rater.children().filter('.rater-'+ control.serial).removeClass('fist-voting-on').removeClass('fist-voting-hover');
		},// $.fn.voting.drain
		
		draw: function(){ // set value and fists to reflect current selection
			var control = this.data('voting'); if(!control) return this;
			// Clear all fists
			this.voting('drain');
			// Set control value
			if(control.current){
				control.current.data('voting.input').attr('checked','checked');
				control.current.prevAll().andSelf().filter('.rater-'+ control.serial).addClass('fist-voting-on');
			}
			else
			 $(control.inputs).removeAttr('checked');
			// Show/hide 'cancel' button
			control.cancel[control.readOnly || control.required?'hide':'show']();
			// Add/remove read-only classes to remove hand pointer
			this.siblings()[control.readOnly?'addClass':'removeClass']('fist-voting-readonly');
		},// $.fn.voting.draw
		
		select: function(value){ // select a value
			var control = this.data('voting'); if(!control) return this;
			// do not execute when control is in read-only mode
			if(control.readOnly) return;
			// clear selection
			control.current = null;
			// programmatically (based on user input)
			if(typeof value!='undefined'){
			 // select by index (0 based)
				if(typeof value=='number')
 			 return $(control.fists[value]).voting('select');
				// select by literal value (must be passed as a string
				if(typeof value=='string')
					//return 
					$.each(control.fists, function(){
						if($(this).data('voting.input').val()==value) $(this).voting('select');
					});
			}
			else
				control.current = this[0].tagName=='INPUT' ? 
				 this.data('voting.fist') : 
					(this.is('.rater-'+ control.serial) ? this : null);
			
			// Update voting control state
			this.data('voting', control);
			// Update display
			this.voting('draw');
			// find data for event
			var input = $( control.current ? control.current.data('voting.input') : null );
			// click callback, as requested here: http://plugins.jquery.com/node/1655
			if(control.callback) control.callback.apply(input[0], [input.val(), $('a', control.current)[0]]);// callback event
		},// $.fn.voting.select
		
		readOnly: function(toggle, disable){ // make the control read-only (still submits value)
			var control = this.data('voting'); if(!control) return this;
			// setread-only status
			control.readOnly = toggle || toggle==undefined ? true : false;
			// enable/disable control value submission
			if(disable) $(control.inputs).attr("disabled", "disabled");
			else     			$(control.inputs).removeAttr("disabled");
			// Update voting control state
			this.data('voting', control);
			// Update display
			this.voting('draw');
		},// $.fn.voting.readOnly
		
		disable: function(){ // make read-only and never submit value
			this.voting('readOnly', true, true);
		},// $.fn.voting.disable
		
		enable: function(){ // make read/write and submit value
			this.voting('readOnly', false, false);
		}// $.fn.voting.select
		
 });
	
	/*--------------------------------------------------------*/
	
	/*
		### Default Settings ###
		eg.: You can override default control like this:
		$.fn.voting.options.cancel = 'Clear';
	*/
	$.fn.voting.options = { //$.extend($.fn.voting, { options: {
			cancel: 'Cancel Voting',   // advisory title for the 'cancel' link
			cancelValue: '',           // value to submit when user click the 'cancel' link
			split: 0,                  // split the fist into how many parts?
			
			// Width of fist image in case the plugin can't work it out. This can happen if
			// the jQuery.dimensions plugin is not available OR the image is hidden at installation
			fistWidth: 16//,
			
			//NB.: These don't need to be pre-defined (can be undefined/null) so let's save some code!
			//half:     false,         // just a shortcut to control.split = 2
			//required: false,         // disables the 'cancel' button so user can only select one of the specified values
			//readOnly: false,         // disable voting plugin interaction/ values cannot be changed
			//focus:    function(){},  // executed when fists are focused
			//blur:     function(){},  // executed when fists are focused
			//callback: function(){},  // executed when a fist is clicked
 }; //} });
 
/*# AVOID COLLISIONS #*/
})(jQuery);
/*# AVOID COLLISIONS #*/
