/*
 * jQuery Spinner plugin for NIGZ
 *
 * Fier Concept & Design B.V. Utrecht
 * Auteur: G. Stevens
 *
 * version: 0.10 -  9 february 2010
 * 
*/

$.fn.spinner = function(options) {	
    var defaults = {
        spinDuration : 1000
    };    
	var settings = $.extend({}, defaults, options);	  	
    var spinner = jQuery(this); //Save  the jQuery object for later use.
  	
  	return this.each(function() {	
  		// Start with random word
  		newWord();  		
		$(this).find("#btn-spin").click(function() {
			spinner.find("li").hide();
			// Start animation (replace bg by animated gif)
			var img = spinner.find("#animated-image");
			var oldSrc = img.attr('src');
			var newSrc = "inc/kidshome/images/codewoord_bg_anim_animated.gif";
			img.attr('src', newSrc);						
						
			// Stop animation after timeout and show new word
			$(this).oneTime(settings['spinDuration'], function() {
				img.attr('src', oldSrc);
	   			newWord();
	  		});						
			// Cancel link action
			return false;
		});
	
	});
	
	function newWord() {
		var words = spinner.find("li");
		words.hide();		
		// Pick random index between 0 and #words - 
		var index = Math.floor(Math.random() * (words.length ));
		var word = words.eq(index);
		// Hide all words but this one;
		words.eq(index).show();		
	}	 
};
