(function($) {

jQuery.fn.MuoviImg = function (speed){ 

var sto = this;				// box (<div>) that contains the image 
var imm = $("img", sto);			// the image in the box 


// Permette col mouse di muovere l'immagine all'interno del box contenitore, dove naturalmente l'immagine è più grande del box stesso. 
function moveImg(){
$(sto).mousemove(function(event) {
var hy = $(this).offset();		
var topBox  = hy.top;			
var leftBox = hy.left;			
var hImg = $(imm).height();		
var lImg = $(imm).width();		
var hBox = $(this).height();		
var lBox = $(this).width();		
var rapp_H = hImg / hBox;		
var rapp_L = lImg / lBox;		
var sT = event.pageY - topBox; 
var sL = event.pageX - leftBox; 
var scrLeft = sL * rapp_L;
var scrTop = sT * rapp_H;
$(this).scrollLeft(scrLeft);		
$(this).scrollTop(scrTop);		
}); // mousemove
}; // "moveImg"  

// Funzione che fa scorrere l'immagine all'interno di un contenitore(box) facendo un percorso a doppia zeta
function doppiaZeta(){
var lImg = $(imm).width();		
var hImg = $(imm).height();		
var lBox = $(sto).width();		
var hBox = $(sto).height();		
var diffL = lImg - lBox;		
var diffL2 = diffL / 2;		
var diffH = hImg - hBox;		
var diffH2 = diffH / 2;		
$(sto)
.animate({'scrollLeft':diffL}, speed)					
.animate({'scrollTop': diffH2, 'scrollLeft':0}, speed)		
.animate({'scrollTop': diffH2, 'scrollLeft':diffL}, speed)	
.animate({ 'scrollTop':diffH, 'scrollLeft':0 }, speed)		
.animate ({'scrollTop':diffH, 'scrollLeft': diffL}, speed)	
.delay(1000)	
.animate({ 'scrollTop':0, 'scrollLeft':0}, speed, doppiaZeta);	
} // "doppiaZeta" 

// Funzione di controllo che richiama la funzione "moveImg" al 'mouseenter' e richiama "doppiaZeta" al 'mouseleave' 
function inter(){
$(sto).bind({
mouseenter: function() {
$(this).stop(true);
moveImg();					
},
mouseleave: function(){
doppiaZeta();				
}
}); // bind 
}; // "inter" 

function aperturaPage(){
$(window).load(function(){		// Chrome e Safari  
doppiaZeta();
}); // load
}; // "aperturaPage" 


// Richiamo Metodi
aperturaPage();
inter();

}; // chiude plugin 

})(jQuery)



