//Mathieu dumais-savard 2008
// http://www.mathieusavard.info/
// version 0.3.0 (nobody likes 1.0, it would be too nice to pretend that there is no bug :P

var loadedimages=0;

jQuery.fn.threesixty = function(options){
		options = options || {};
		options.images = options.images || [];
		options.method = options.method || "click"
		options.cycle = options.cycle || 1;
		options.resetMargin = options.resetMargin || 0;

    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);o.bind("load", function()	{	
																															  
																											
																											loadedimages++;
																											
																											if (loadedimages==options.images.length)
																											{
																												
																											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]);
			pic.attr("src",imgArr[0]);	
			$("div#loadmessage").remove();
																												}
																															  
																															  });
																																		   
	$("body").append(o);o.hide();
	
																																		
														
																																		
																																		});

			
			


				if (loadedimages==options.images.length)
		
		{
				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]);
			pic.attr("src",imgArr[0]);	
			$("div#loadmessage").remove();
		}


			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});
  					$("body").append(follower);
						disableSelection(follower[0]);
					}
					disableSelection(pic[0]);
					var imgSrc, enabled;
					pic.mousemove(function(e) {
										   
										   if (loadedimages==options.images.length){
													if (e.pageX<=pic.offset().left+options.resetMargin || e.pageX > pic.offset().left + pic.width()-options.resetMargin || e.pageY<=pic.offset().top+options.resetMargin || e.pageY>=pic.offset().top+pic.height()-options.resetMargin)
													{	enabled=false;
														return false;
													}
													if (follower)
														follower.css({"top": e.pageY-7, "left": e.pageX-7});
													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; });
			}
	});			

//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";
						}


};


