/** 
 * de scrollerclass version 0.7
 * december 2006 [JV]
 * laat de scrolldiv naar links of naar rechts bewegen na het klikken op een van de knopjes
 */
hscroller = {
	/**hang het scroll event aan de knoppen */
	initbuttons: function () {
		var i, b;
		for (i=0;i<arguments.length;i++) {
			b = document.getElementById(arguments[i]);
			if (!b)
				continue;
			b.onmouseover = hscroller.scroll;
			b.onmouseout = hscroller.stopscroll;
		}
	},
	
	/** geef aan welke container er gescrolled moet worden */
	setscrollcontainer:function () {
		var i, el = document.getElementById(arguments[0]);
		if (!el) return false;
		hscroller._s = el;
//		hscroller.initOutercontainer ();
		hscroller._getMax();
	},
	_getMax: function ()  {
		hscroller._smax = 0;
		for (i=0;i<hscroller._s.childNodes.length;i++) 
			hscroller._smax+=hscroller._s.childNodes[i].offsetWidth;
	},
	setscrollwidth:function () {
		hscroller.scrollwidth = parseInt(arguments[0]);
	},
	setscrollspeed:function () {
		hscroller.scrollstep = parseInt(arguments[0]);
		hscroller.originalscrollstep = hscroller.scrollstep;
	},
	
	/*de eigenlijke scrollfunctie */
	scroll:function (e,pm) {
		if (typeof top.AdminMode == 'function' && top.AdminMode ())
			return false;
		if (hscroller.timer === false) {
			
			hscroller._getMax();
			
			hscroller.inposition = false;
			
			e = e || window.event;
		
			if (!hscroller._s)
				return false;

			var clickelement =e? hscroller.clickelement = hscroller.getTarget(e): hscroller.clickelement;
			if (clickelement.id == '' || clickelement.id == null) {
				var tmpElement = nodes.parentNodeBy (clickelement, 'tagName=DIV');
				var clickelementId = clickelement.id ? clickelement.id : tmpElement.id;
			} else {
				var clickelementId = clickelement.id;
			}
			
			if(hscroller._s.style.marginLeft=='')
				hscroller._s.style.marginLeft = 0;
		
			hscroller._a = hscroller._s.style.marginLeft;
			
			if (pm)
				hscroller.plusmin = pm;
			else
				hscroller.plusmin = /left/i.test(clickelementId)?'min':'plus';

			if (/fast/i.test(clickelementId)) {
				if (hscroller.scrollstep == hscroller.originalscrollstep) {
					hscroller.scrollstep = hscroller.scrollstep*3;
				}
			} else {
				hscroller.scrollstep = hscroller.originalscrollstep;
			}
		
			hscroller.timer = window.setTimeout (hscroller._scroll, 1);
		}
	},
	
	_scroll: function () {
		var sMarginleft = parseInt(hscroller._s.style.marginLeft);

		if (hscroller.plusmin=='plus') {
			if (sMarginleft<0) {
				if (Math.abs(sMarginleft) >= ( hscroller._smax -  (hscroller.scrollwidth*2)) ) {
					return (hscroller.scounter = 0);
				}
			}
		} else {
			if (sMarginleft >= parseInt(hscroller._s.parentNode.offsetWidth) -  (hscroller.scrollwidth*2)) {
				return (hscroller.scounter = 0);
			}
		}
		

		hscroller.scounter = hscroller.scounter + hscroller.scrollstep;
		hscroller._a = hscroller._s.style.marginLeft = parseInt(hscroller._a) - ( hscroller.plusmin=='plus'?parseInt(hscroller.scrollstep):(parseInt(hscroller.scrollstep) -(parseInt(hscroller.scrollstep)*2)));
						
		hscroller.timer = window.setTimeout (hscroller._scroll, 1);
	},

	_debug: function(arg) {

		var debug = document.getElementById('debuggerxyz');
		if (!debug){
			debug = document.createElement('DIV');
			debug.id = 'debuggerxyz';
			document.body.appendChild(debug);
			debug.style.position = 'absolute';
			debug.style.width = '600px';
			debug.style.left = '200px';
			debug.style.top = '40px';
			debug.style.height = '900px';
			debug.style.overflow = 'auto';
			debug.style.border = '1px red solid';
			debug.style.backgroundColor = 'white';
			debug.style.zIndex = '100';
		};
		debug.innerHTML  = debug.innerHTML + arg + "<br>";
	},

	stopscroll: function() {
		window.clearTimeout(hscroller.timer);
		window.clearTimeout(hscroller.timer2);
		hscroller.timer = false;
		hscroller.timer2 = false;
	},

	gotoImage: function (nr) {
		if (hscroller.inposition == nr)
			return;
			
		hscroller._getMax();
		hscroller.stopscroll();
		if (hscroller._s.childNodes[nr])  {
		hscroller.oops = false;
			hscroller.nieuw = hscroller._s.childNodes[nr].offsetLeft;
			hscroller.huidig = hscroller._s.style.marginLeft==''?0:parseInt(hscroller._s.style.marginLeft);
			hscroller.delta = Math.abs(hscroller.huidig) - hscroller.nieuw;

			if (hscroller.delta!=0) {
				hscroller.goleft = hscroller.delta < 0;
				if (hscroller.huidig > hscroller.nieuw) {
					hscroller.goleft = true;
					hscroller.oops = true;
				}
				hscroller.timer2 = window.setTimeout( hscroller._goto,10);
			}
		}
		hscroller.inposition = nr;
	},
	_goto : function() {
		var currLeft = hscroller._s.style.marginLeft==''?0:parseInt(hscroller._s.style.marginLeft);
		
		hscroller._s.style.marginLeft = hscroller.goleft? currLeft - 10:currLeft +10;

		if ( hscroller.goleft) {
//			hscroller._debug(Math.abs(currLeft) + " :: " +(hscroller.nieuw-10));
			
			if (hscroller.oops)
				var tmp = currLeft -(currLeft*2);
			else
				var tmp = Math.abs(currLeft);
				
			if (tmp <= (hscroller.nieuw-10))
				hscroller.timer2 = window.setTimeout( hscroller._goto,10);
		} else {

			if (Math.abs(currLeft) > Math.abs((hscroller.nieuw+10)))
				hscroller.timer2 = window.setTimeout( hscroller._goto,10);
		}

	},
	
	scounter:0,
	getTarget:function (e) {return !e? false: (e.srcElement? e.srcElement: e.target);},
	scrollwidth:50,
	scrollstep:4,
	originalscrollstep:4,
	orgmillisec:0,
	_smax: 0,
	_s:false,
	timer:false
};
