/**
/*
 * 	Magic Scroll 0.1.5  - jQuery plugin
 *	written by Gabriele Campi	
 *	http://magicscroll.madeinthecave.com
 *	magicscroll@madeinthecave.com	
 *
 *	Copyright (c) 2009 Gabriele Campi (http://www.madeinthecave.com)
 *	Dual licensed under the MIT and GPL licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
(function($) {
		
	// default configuration properties	
	var defaults = {		
		auto: true,
		area: 10,  // altezza in px che genera scorrimento automatico in alto e in basso
		scroll: 200, // scroll della finestra
		speed: 600,  // speed di scroll della finestra
		play_key:44,  // carattere sulla tastiera che attiva il plugin -> default  ","
		pause_key: 46 // carattere sulla tastiera che disattiva il plugin -> default "."		
	}; 
    
    
    $.fn.magicScroll = function(my_options){

		var options = $.extend({}, defaults, my_options);
		
		var obj = $('html,body'); 	
		var is_active = options.auto;
	
	
		// se � impostata a true l'opzione auto allora attivo subito il plugin
		if(options.auto){
			obj.bind('mousemove',function(e){
				auto_scorrimento(e);		
			});							
		}


		// gestisco la pressione sul tasto control
		var isCtrl = false;
		$(document).keyup(function (e){
			if(e.which == 17){
				isCtrl=false;
			}
		});
		$(document).keydown(function (e){
			if(e.which == 17){
				isCtrl=true;
			}
		});


		// verifico che i tasti di attivazione / disattivazione 
		// se sono uguali
		if(options.play_key==options.pause_key){			
			obj.keypress(function (e){
				if (e.which == options.play_key && isCtrl==true){
					if(is_active){
						obj.unbind('mousemove');
						is_active = false;
					}else{
						obj.bind('mousemove',function(e){
							auto_scorrimento(e);
							is_active = true;
						});
					}			
				}				
			});
		}
		
		// se sono diversi
		if(options.play_key!=options.pause_key){
			// se premo il tasto play attivo l'auto scorrimento'
			obj.keypress(function (e){
				if (e.which == options.play_key && isCtrl==true){
					obj.bind('mousemove',function(e){
						auto_scorrimento(e);
						is_active = true;
					});
				}					
			});

			// se premo il tasto pausa disattivo l'auto scorrimento'
			obj.keypress(function (e){
				if (e.which == options.pause_key && isCtrl==true){
					obj.unbind('mousemove');
					is_active = false;
				}
			});
		}

		// funzione di auto scorrimento
		function auto_scorrimento(e){
			var scrolled = $(window).scrollTop();
			var mouse_pos_y; // y del mouse
			var win_height; // altezza della finestra visibile
			var doc_height; // altezza di tutto il documento

			mouse_pos_y = parseInt(e.pageY);
			win_heigh = parseInt($(window).height());
			win_heigh_limit_down = parseInt(win_heigh - options.area + scrolled);
			win_heigh_limit_top = parseInt(options.area + scrolled);
			doc_heigh = parseInt($(document).height());
			
			// se raggiungo il limite in basso scrollo in gi�
			if(mouse_pos_y > win_heigh_limit_down){
				$('html,body').stop().animate({scrollTop:scrolled+options.scroll}, options.speed);
				$('html,body').unbind('mousemove');
				setTimeout(function(){
					$('html,body').bind('mousemove',function(e){
						auto_scorrimento(e);
						is_active = true;
					});
				},options.speed);
				
			}	
			
			
			// se raggiungo il limite in alto scrollo in su
			if(mouse_pos_y < win_heigh_limit_top){
				$('html,body').stop().animate({scrollTop:scrolled-options.scroll}, options.speed);
				$('html,body').unbind('mousemove');
				setTimeout(function(){
					$('html,body').bind('mousemove',function(e){
						auto_scorrimento(e);
						is_active = true;
					});
				},options.speed);
			}			
		}
	};
})(jQuery);
