/**
* Random image presenter
* 
* Usage: 
*
*   Minimum options:
*
*	jQuery(div).randoMaker({ images: [imgLink1, imgLink2, imgLink3, etc ] });
*
*   All possible options:
*
*	jQuery(div).randoMaker({
*		images: [imglink1, imglink2, imglink3, etc ],
*		links: [img1Link, img2Link, img3Link, etc ],
*		width: "250px",
*		height: "250px",
*		linkTarget: "_blank",
*		title: "A random image"
*
* @copyright Network Solutions, LLC 2011
* @author  Mitch Viner
* @created 01-10-2011
* @version 0.1.beta
*/

(function($) {

	var settings = {
			"images" : [],
			"links" : [],
			"width" : "auto",
			"height" : "auto",
			"linkTarget" : "_self",
			"title" : ""
		};

	var methods = {
		init: function( options ) {
			return this.each(function() {
				if( options ) {
					$.extend( settings, options );
				}
				if(settings.images.length>0) {
					var $this = $(this);
					var img = $('<img />');
					var rand = Math.floor(Math.random()*settings.images.length);
					
					img.attr('src', settings.images[rand])
						.attr('title', settings.title)
						.addClass('randoMakerImg')
						.css({ width: settings.width, height: settings.height })
						.appendTo($this);

					if(settings.links.length>0) {
						if(settings.links.length==settings.images.length) {
							if(settings.links[rand]!='') {
								var anchor = $("<a />")
									.attr('href', settings.links[rand])
									.attr('target', settings.linkTarget);
								img.wrap(anchor);
							}
						} else {
							$.error(' The number of links supplied does not match the number of images. Please see documentation. ');
						}
					}

				} else {
					$.error(' No images supplied to jQuery.randoMaker. Please see documentation. ');
				}
			});
		},

		update: function(forceTo) {
			return this.each(function() {
				var img = $(this).find('img.randoMakerImg').eq(0);
			
				img.attr("src", settings.images[forceTo]);
			});
		}
		
	};


	$.fn.randoMaker = function( method) {

		if( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call(arguments, 1 ));
		} else if ( typeof method === 'object' || !method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' + method + ' does not exist on jQuery.randoMaker' );
		}

	};


})(jQuery);
