/*
* BCDC.US - no repeat image randomize jQuery plugin
* Examples and documentation at: http://bcdc.us
* Version: 1.0 ( Dec-11-2009 )
* Copyright (c) 2009 Bruno Correia
* Requires: jQuery v1.3+
* Demo at http://bcdc.us/jquery.plugins/jquery.randomize/demo.html
*
* Dependency: Cookie plugin by Klaus Hartl (stilbuero.de)
*/

(function($) {
  $.fn.randomize = function(customOptions) {
      var options = $.extend({},$.fn.randomize.defaultOptions, customOptions);
        
	        var n = Math.floor(Math.random() * options.imagesTotal) + 1; // generate rand number
	        if ( $.cookie("random") == null ) { // if no cookie
	             $.cookie("random", n, { expires: options.cookieExpires }); // create cookie if null
	        } else {
	        var n = $.cookie("random"); // read cookie value
	          function _rand() {
	            var i = Math.floor(Math.random() * options.imagesTotal) + 1; // generate rand number
	              if ( n == i ) { _rand(); } // recursive
	              else { n = i; $.cookie("random", n, { expires: options.cookieExpires }); } // update cookie value
	          } _rand();
	        }

			$(this).hide();
      $(this).attr({
			src: "./" + options.imagesPath + "/" + n + ".jpg"
      });
			$(this).fadeIn(options.fadeinSpeed);
  };
		
    $.fn.randomize.defaultOptions = {
      imagesPath: 'images/random',
			imagesTotal: 5, // total number of images
			cookieExpires: 7, // in days
			fadeinSpeed: 1200 // speed in milliseconds
    };
  
})(jQuery);
