var enabled = true;
var previousPosition = 0;
var position=0;
var currentPicture;
var z = 0;
var timerID = 0;
var currentPictureMove= '';
//PLAY
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;
		options.direction = options.direction || "forward";

		if (options.direction == "backward")
			options.images.reverse();

        return this.each(function(){
			
			var imgArr = [];
			var pic = $(this);
			
			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]);           
			
            timerID=setInterval(function(){
                if (position != 0)
                {
                    //****** could be better *********
                    //keep the position
                    j = currentPictureMove.lastIndexOf('/') +1;
                    k = currentPictureMove.indexOf('.',j);
                    z= parseInt(currentPictureMove.substr(j,k -j));
                    position=0;
                }
                
                if (z >= 61)
                {
                    z = 0;
                }

                z += 1;		                        
                currentPicture = imgArr[z];
               
                pic.attr("src",currentPicture);
                },80);                               
						
	});			
};

//MOUSE MOVE
jQuery.fn.threesixtyMove = function(options){
		options = options || {};
		options.images = options.images || [];
		options.method = options.method || "click"
		options.cycle = options.cycle || 1;
		options.resetMargin = options.resetMargin || 0;
		options.direction = options.direction || "forward";

		if (options.direction == "backward")
			options.images.reverse();

        return this.each(function(){
			
			var imgArr = [];
			var pic = $(this);
						
			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]);
            //attach event move to images  
		    pic.mousemove(function(e) {		        
		        position = Math.floor((e.pageX - pic.offset().left) / (pic.width()/imgArr.length));
		         
		        currentPictureMove = imgArr[position];
		        pic.attr("src",currentPictureMove);
		    });	
								
	});			
};


