//Mathieu dumais-savard 2008
// http://www.mathieusavard.info/
// version 0.2
jQuery.fn.threesixty = function(options){
		options = options || {};
		options.images = options.images || [];
		options.method = options.method || "click"
		options.cycle = options.cycle || 1;

    return this.each(function(){
			var imgArr = [];
			var pic = $(this);
		//ask browser to load all images.. I know this could be beter but is just a POC
				$.each(options.images, function(index, record) { var o =$("<img>").attr("src",record);		$("body").append(o); o.hide(); });

			for (var x=1; x<=options.cycle; x++)
				for (var y=0; y<options.images.length; y++)
					imgArr.push(options.images[y]);

			//add the first slice again to complete the loop
			imgArr.push(options.images[0]);
			

			
			if (options.method == "mousemove")
				pic.mousemove(function(e) {
					pic.attr("src",imgArr[Math.floor((e.pageX - pic.offset().left) / (pic.width()/imgArr.length))]);
				});


			if (options.method == "click")
			{			
					var follower;
					if (!$.browser.msie)
					{
						follower = $("<div>").css({"z-index":0, "width":"15px", "height":"15px", "position":"absolute", "top": pic.offset().top, "left":pic.offset().left});
						disableSelection(follower[0]);
					}
					disableSelection(pic[0]);
					var imgSrc, enabled;
					$("body").append(follower);
					pic.mousemove(function(e) {
						if (follower)
							follower.css({"top": e.pageY, "left": e.pageX});
						if (enabled==true)
							pic.attr("src",imgArr[Math.floor((e.pageX - pic.offset().left) / (pic.width()/imgArr.length))]);
					})
					pic.add((follower)?follower:null).mouseup(function() {
							enabled=false; 
					}).mousedown(function() { enabled=true; });
					return;
			}
	});			

//more than inspired of Bret Taylor's work : http://ajaxcookbook.org/disable-text-selection/
						function disableSelection(element) {
						    element.onselectstart = function() {
						        return false;
						    };
						    element.unselectable = "on";
						    element.style.MozUserSelect = "none";
						    element.style.cursor = "default";
						}


};

