/*
	domNews 2.0
	released: 2006-10-03

	What's new?

    - Object oriented programmation
    - Multiple istance allowed
    - Vertically and horizontally scrolling

*/

//class domNews {
//}

function domNews (id, direction, speed, jsObjName, contatore,height,nome_div) {
	/* ID of the scroller */
	this.id = ( id==undefined || id=='' || id==0 ? nome_div+'_'+contatore: id )
	/* name of the javascript object, only for IE */
	this.jsObjName = ( jsObjName==undefined || jsObjName=='' || jsObjName==0 ? 'dn' : jsObjName )

	/* Direction and Speed of the scroller */
	this.direction = ( direction==undefined || direction=='' || direction==0 ? 'stop' : direction )
	this.speed = 3 

	/* Scrolling variables */
	this.startPos
	this.endPos
	this.scrollPos

	/* intervalID */
	this.interval

	/* the DOM element */
	var domNewsElement = document.getElementById(this.id)
	if ( !domNewsElement ) return

	/* change the CSS properties of the scroller... */
	if ( domNewsElement.style.position!='relative' || domNewsElement.style.position!='absolute' )
		domNewsElement.style.position = 'relative'
	//domNewsElement.style.overflow = 'hidden'
	/* ...and its container */
	//domNewsElement.parentNode.style.overflow = 'hidden'
	domNewsElement.parentNode.style.overflow = 'auto'

	/* initial positions by direction */
	switch ( this.direction ) {
		case 'left':
		case 'right':
			this.startPos = domNewsElement.parentNode.offsetWidth
			this.scrollPos = 0	// se lo scroller deve partire dalla posizione attuale
			this.endPos = -1 * domNewsElement.offsetWidth
		break
		case 'down':
		case 'up':
			this.startPos = domNewsElement.parentNode.offsetHeight
			this.scrollPos = 0	// se lo scroller deve partire dalla posizione attuale
				//this.endPos = -1 * domNewsElement.offsetHeight
			//	alert(id+" ___ "+ domNewsElement.offsetHeight);
				if( (id!='box_lastminute_1') && (id!='box_descrizioni_0') ){
					this.endPos = -1 * height;
				}else{
					this.endPos = -1 * domNewsElement.offsetHeight;
				}
//				alert(id+" ___ "+ this.endPos);
				domNewsElement.parentNode.style.height='100%' //perchè con internet explorer non va
			break
	}
}


/* Start the scrolling */
domNews.prototype.start = function () {
	if ( this.interval == undefined ){
		if ( navigator.appName == 'Microsoft Internet Explorer' ){
			this.interval = setInterval( this.jsObjName + '.scrolling()', this.speed)
			// ok x tutti (ie e firefox), però richiede la conoscenza dell'oggetto creato con "new domNews()"
		}else{
			this.interval = setInterval(this.scrolling, this.speed, this)
			// ok x firefox, raggiungo l'oggetto orginario passandolo come paramentro
		}
	}
	return
}



/* Stop the scrolling */
domNews.prototype.stop = function () {
	if ( this.interval != undefined )
		this.interval = clearInterval(this.interval)
	return
}



/* Change the scrolling direction/speed */
domNews.prototype.update = function (direction, speed) {
	this.stop()
	this.direction = ( direction==undefined || direction=='' || direction==0 ? 'stop' : direction )
	this.speed = 3 
	this.start()
}



/* Scrolling function */
domNews.prototype.scrolling = function (jsNewsElement) {
	/* jsNewsElement is defined if the browser isn't IE */
	if ( jsNewsElement == undefined )
		if ( this.direction == undefined )
			return
		else
			jsNewsElement = this

	var domNewsElement = document.getElementById(jsNewsElement.id)

	switch ( jsNewsElement.direction ) {

		case 'stop':
			jsNewsElement.stop()
			break

		case 'left':
			domNewsElement.style.left = jsNewsElement.scrollPos + 'px'
//			if ( jsNewsElement.scrollPos == jsNewsElement.endPos ) jsNewsElement.scrollPos = jsNewsElement.startPos
			if (jsNewsElement.endPos>=(jsNewsElement.scrollPos-domNewsElement.parentNode.offsetWidth)){
				jsNewsElement.stop();
				jsNewsElement.scrollPos++
			}
			jsNewsElement.scrollPos--
			break

		case 'right':
			domNewsElement.style.left = jsNewsElement.scrollPos + 'px'
//			if ( jsNewsElement.scrollPos == jsNewsElement.startPos ) jsNewsElement.scrollPos = jsNewsElement.endPos
			if ( jsNewsElement.scrollPos == 0 ){
				jsNewsElement.stop();
				jsNewsElement.scrollPos--
			}
			jsNewsElement.scrollPos++
			break

		case 'down':
			domNewsElement.style.top = jsNewsElement.scrollPos + 'px'
//			if ( jsNewsElement.scrollPos == jsNewsElement.startPos ) jsNewsElement.scrollPos = jsNewsElement.endPos
			if ( jsNewsElement.scrollPos == 0 ){
				jsNewsElement.stop();
				jsNewsElement.scrollPos--
			}
			jsNewsElement.scrollPos++
			break

		case 'up':
//		default:
			if( (jsNewsElement.id!='box_lastminute_1') && (jsNewsElement.id!='box_descrizioni_0') && (domNewsElement.offsetHeight!=700) ){
				jsNewsElement.endPos= -1 * domNewsElement.offsetHeight
			}
			domNewsElement.style.top = jsNewsElement.scrollPos + 'px'
			//alert(jsNewsElement.endPos+">=("+jsNewsElement.scrollPos+"-"+domNewsElement.parentNode.offsetHeight+")");
			//if ( jsNewsElement.scrollPos == jsNewsElement.endPos ) jsNewsElement.scrollPos = jsNewsElement.startPos
			if (jsNewsElement.endPos>=(jsNewsElement.scrollPos-domNewsElement.parentNode.offsetHeight)){
				jsNewsElement.stop();
				jsNewsElement.scrollPos++
			}

			jsNewsElement.scrollPos--
			break
	}
}



