(
function()
	{
    // eventi unificati
	
    TouchMouseEvent = {
        DOWN: "touchmousedown",
        UP: "touchmouseup",
        MOVE: "touchmousemove"
		};
	
    // listener per gli eventi del mouse
	
    function onMouseEvent(event)
		{
        var type;
        
        switch(event.type)
			{
            case "mousedown": type = TouchMouseEvent.DOWN; break;
            case "mouseup":   type = TouchMouseEvent.UP;   break;
            case "mousemove": type = TouchMouseEvent.MOVE; break;
            default: 
                return;
			}
        
        var touchMouseEvent = normalizeEvent(type, event, event.pageX, event.pageY);
		
        $(event.target).trigger(touchMouseEvent); 
		}
    
	// listener per gli eventi touch
	
    function onTouchEvent(event)
		{
        var type;
        
        switch(event.type)
			{
            case "touchstart": type = TouchMouseEvent.DOWN; break;
            case "touchend":   type = TouchMouseEvent.UP;   break;
            case "touchmove":  type = TouchMouseEvent.MOVE; break;
            default: 
                return;
			}
        
		var touch;
        var touchMouseEvent;
        
        if (type == TouchMouseEvent.UP)
			touch = event.originalEvent.changedTouches[0]; // android ok; ios???
        else
			touch = event.originalEvent.touches[0];
        
		touchMouseEvent = normalizeEvent(type, event, touch.pageX, touch.pageY);
		
        $(event.target).trigger(touchMouseEvent); 
		}
    
    // normalizzazione oggetto evento
	
    function normalizeEvent(type, original, x, y)
		{
        return $.Event(type, {
            pageX: x,
            pageY: y,
            originalEvent: original
			});
		}
    
    // intercetta gli eventi originari
	
    var jQueryDocument = $(document);
	
    if("ontouchstart" in window) // dispositivo touch-screen
		{
        jQueryDocument.on("touchstart", onTouchEvent);
        jQueryDocument.on("touchmove", onTouchEvent);
        jQueryDocument.on("touchend", onTouchEvent);
		}
	else
		{
        jQueryDocument.on("mousedown", onMouseEvent);
        jQueryDocument.on("mouseup", onMouseEvent);
        jQueryDocument.on("mousemove", onMouseEvent);
		}
	}
)();
