function Browser() {
	var ua, s, i;

	this.isIE4	= false;
	this.isIE5	= false;
	this.isIE	= false;
	this.isNS4	= false;
	this.isNS6	= false;
	this.isNS	= false;
	this.isW3C	= false;
	this.version	= null;

	ua = navigator.userAgent;

	this.isW3C=(document.getElementById)?true:false;
	this.isNS4=(document.layers)?true:false;
	this.isIE4=(document.all && !this.isW3C)?true:false;
	this.isIE5=(document.all && !this.isW3C)?true:false;
	this.isNS6=(this.isW3C && navigator.appName.indexOf("Netscape")>=0)?true:false;

	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = 6.1;
		return;
	}
	
}

var browser = new Browser();
var clientWidth = 0;
var clientHeight = 0;
var ScrollingLeft = 0;
var ScrollingRight = 0;

function getELWidth(el) {
	if(browser.isNS4)return (el.document.width)? el.document.width : el.clip.right-el.clip.left;
	else if(browser.isIE4||browser.isIE5)return (el.style.width)? el.style.width:el.clientWidth;
	else if(browser.isW3C)return (el.style.width)?parseInt(el.style.width):parseInt(el.offsetWidth);
	else return -1;
}

function GetClientSize() {
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    clientWidth = window.innerWidth;
    clientHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    clientWidth = document.documentElement.clientWidth;
    clientHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    clientWidth = document.body.clientWidth;
    clientHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + clientWidth );
  //window.alert( 'Height = ' + clientHeight );
}

function ScrollLeft() {
	var scr = document.getElementById('Scroller');
	var x = parseInt(scr.style.left);

	x = x + 50;
	if( x > 0 )
		x = 0;
	scr.style.left = x+'px';
}

function ScrollRight() {
	var scr = document.getElementById('Scroller');
	var x = parseInt(scr.style.left);
	var scc = document.getElementById('ScrollContainer');
	var sw = getELWidth(scr);
	var cw = getELWidth(scc);

	x = x - 50;
	//if( sw - x <= cw )
	if( -x + cw > sw ) 
		x = -(sw - cw);
	scr.style.left = x+'px';
}

function StartScrollLeft() {
	var scr = document.getElementById('Scroller');
	var x = parseInt(scr.style.left);
	if( ScrollingLeft == 0) {
		ScrollingLeft = setInterval( 'ScrollLeft()', 100 );
	}
}

function StopScrollLeft() {
	clearInterval(ScrollingLeft);
	ScrollingLeft = 0;
}

function StartScrollRight() {
	var scr = document.getElementById('Scroller');
	var x = parseInt(scr.style.left);
	if( ScrollingRight == 0) {
		ScrollingRight = setInterval( 'ScrollRight()', 100 );
	}
}

function StopScrollRight() {
	clearInterval(ScrollingRight);
	ScrollingRight = 0;
}

function CheckMousePos(event) {
	var x, y;
	var clientW, clientH;
	
	GetClientSize();

	if (browser.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft
			+ document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
			+ document.body.scrollTop;
	}
	if (browser.isNS) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}

	if( x <= 50 ) {
		StartScrollLeft();
	} else {
		StopScrollLeft();
	}
	if( x >= clientWidth-50) {
		StartScrollRight();
	} else {
		StopScrollRight();
	}
	
	if (browser.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS) {
		event.preventDefault();
	}
}

function CheckMousePosDocument(event) {
	StopScrollLeft();
	StopScrollRight();
	if (browser.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS) {
		event.preventDefault();
	}
}

function StartCaptureMouse(event) {
	var clientWidthLast = 0;
	var clientHeightLast = 0;
	
	window.onresize = function(	) {
		// In caso di ridimensionamento della finestra del browser aggiusto la posizione orizzontale dell'immagine
		// per evitare che rimanga uno spazio vuoto a destra.
		var scr = document.getElementById('Scroller');
		var x = parseInt(scr.style.left);
		if( x < 0 ) {
			GetClientSize();
			if( this.clientWidthLast != clientWidth ||
				this.clientHeightLast != clientHeight ) {
				this.clientWidthLast = clientWidth;
				this.clientHeightLast = clientHeight;
				if( clientWidth-x > parseInt(scr.style.width) ) {
					x += (clientWidth-x) - parseInt(scr.style.width); 
					scr.style.left = x+'px';
				}
			}
		}
	}

	var scr = document.getElementById('Scroller');
	
	if (browser.isIE) {
		document.attachEvent("onmousemove", CheckMousePos);
		//document.attachEvent("onmousemove", CheckMousePosDocument);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS) {
		document.addEventListener("mousemove", CheckMousePos, true);
		//document.addEventListener("mousemove", CheckMousePosDocument, true);
		event.preventDefault();
	}
}

