//randomImage by Tristan Margot
//spam@tristanmargot.com

(function($){
 $.fn.randomimage = function(options) {

  var defaults = {
   images: [],
   imagepath:'images/',
   rootrelative:true
  };
  var options = $.extend(defaults, options); 
  return this.each(function() {
	obj = $(this);

	if(typeof options.images[0] == "undefined"){
		alert("No images have been specified");
		return;
	}	

	//change back slashes to forward slashes
	options.imagepath = options.imagepath.replace(/\\/g, "/");	
	var imagepathLastChar = options.imagepath.charAt(options.imagepath.length-1);
	//append slash if not existing
	options.imagepath = (imagepathLastChar != "/")?options.imagepath+'/':options.imagepath;
	//add root relative slash setting true
	options.imagepath = (options.rootrelative = true)?'/'+options.imagepath:options.imagepath;
	
	var randomNumber=Math.floor(Math.random()*options.images.length);	
	obj.append('<img src="'+options.imagepath+options.images[randomNumber]+'" />');
	
  });
 };
})(jQuery);
