/* =======================================================
* Scroll
* 11/04/2007
* 
* http://www.isys.ch/
* ======================================================= */

simpleScroll.prototype.scrollUp = function() { this.startScroll(90) }
simpleScroll.prototype.scrollDown = function() { this.startScroll(270) }

simpleScroll.prototype.startScroll = function(deg, speed) {
	if (this.aniTimer) window.clearTimeout(this.aniTimer)
	this.overrideScrollAngle(deg)
	//this.speed = speed ? speed : this.origSpeed
	this.speed = 200
	this.lastTime = (new Date()).getTime() - this.y.minRes
	this.aniTimer = window.setTimeout(this.gRef + ".scroll()", this.y.minRes)
}

simpleScroll.prototype.overrideScrollAngle = function(deg) {
		deg = deg % 360
		if (deg % 90 == 0) {
			var cos = deg == 0  ? 1 : deg == 180 ? -1 : 0
			var sin = deg == 90 ? -1 : deg == 270 ? 1 : 0
		} else {
			var angle = deg * Math.PI / 180
			var cos   = Math.cos(angle)
			var sin   = Math.sin(angle)
			sin = -sin
		}

		this.fy = sin / (Math.abs(cos) + Math.abs(sin))
		this.stopV = deg == 0 || deg == 180 ? this.scrollPos : deg < 180 ? 0 : this.scrollH
}


simpleScroll.prototype.endScroll = function() {
	window.clearTimeout(this.aniTimer)
	this.aniTimer = 0;
	this.speed = this.origSpeed

}

simpleScroll.prototype.overrideScrollSpeed = function(speed) {
	this.speed = speed
}

simpleScroll.prototype.scrollTo = function(stopV, aniLen) {
	if (stopV != this.scrollPos) {
		if (this.aniTimer) window.clearTimeout(this.aniTimer)
			this.lastTime = (new Date()).getTime()
		var dy = Math.abs(stopV - this.scrollPos)
		this.fy = (stopV - this.scrollTop) / dy
		this.stopV = stopV
		this.speed = dy / aniLen * 1000
		window.setTimeout(this.gRef + ".scroll()", this.y.minRes)
	}
}

simpleScroll.prototype.jumpTo = function(ny) { 
	ny = Math.min(Math.max(ny, 0), this.scrollH)
	this.scrollPos = ny
	if (this.y.ns4)
		this.content.moveTo(0,-ny)
	else {
		this.content.style.top = -ny + "px"
	}
}

simpleScroll.minRes = 10
simpleScroll.ie = document.all ? 1 : 0
simpleScroll.ns4 = document.layers ? 1 : 0
simpleScroll.dom = document.getElementById ? 1 : 0
simpleScroll.mac = navigator.platform == "MacPPC"
simpleScroll.mo5 = document.getElementById && !document.all ? 1 : 0

simpleScroll.prototype.scroll = function() {
	//this.reloadHeight()
	this.aniTimer = window.setTimeout(this.gRef + ".scroll()", this.y.minRes)
	var nt = (new Date()).getTime()
	var d = Math.round((nt - this.lastTime) / 1000 * this.speed)
	if (d > 0)
	{
		var ny = d * this.fy + this.scrollPos
			if(ny>=this.scrollH){
			alert(this.stopV+' '+ny+' '+this.fy)
		}
		var yOut = (ny >= this.scrollPos && ny+this.docH >= this.stopV) || (ny <= this.scrollPos && ny <= this.stopV)
		if (nt - this.lastTime != 0 &&
		(this.fy == 0 ||
		(this.fy != 0 && yOut)))
		{
			//this.jumpTo(this.stopV)
			this.endScroll()
		}
		else {
			this.jumpTo(ny)
			this.lastTime = nt
		}
	}
}


simpleScroll.prototype.isScrollNeeded = function() {

	if(this.docH>=this.scrollH) {/*alert(this.docH+' '+this.scrollH);*/ return false;}
	else return true;
}


function simpleScroll(id, speed)
{
	var y = this.y = simpleScroll
	if (y.ie || y.ns4 || y.dom) {
		this.id = id
		if (speed)
			this.origSpeed = speed
		else
			this.origSpeed = 10
		this.aniTimer = false
		this.resized= false
		this.lastTime = 0

		this.gRef = "simpleScroll_"+id
		eval(this.gRef+"=this")

		this.load=load;
		
		this.load();
	}
	
 
  
}

function load() {
	var d, lyrId1, lyrId2
		d = document
		lyrId1 = this.id + "Container"
		lyrId2 = this.id + "Content"
		this.container = this.y.dom ? d.getElementById(lyrId1) : this.y.ie ? d.all[lyrId1] : d.layers[lyrId1]
		this.content = obj2 = this.y.ns4 ? this.container.layers[lyrId2] : this.y.ie ? d.all[lyrId2] : d.getElementById(lyrId2)
		this.docH = this.y.ns4 ? this.container.document.height : this.y.ie ? this.container.clientHeight : this.container.offsetHeight
		this.scrollH = this.content.scrollHeight
				this.scrollPos =0
		this.jumpTo(this.scrollPos)
			//alert(this.docH+' '+this.scrollH)
}



simpleScroll.prototype.resize = function() {
	this.load();
}

