/*--------------------------------------------------------------------------
 *  Smooth Scroller Script, version 1.0.1
 *  (c) 2007 Dezinerfolio Inc. <midart@gmail.com>
 *
 *  For details, please check the website : http://dezinerfolio.com/
 *
/*--------------------------------------------------------------------------*/

Scroller = {
	// control the speed of the scroller.
	// dont change it here directly, please use Scroller.speed=50;		
	tx:0,
	speed:10,
	width:450,
	scrollerOBJ:"refer-content-Scroller",
	
	// returns the X position of the div
	gx: function (d) {
		
		obj = document.getElementById(Scroller.scrollerOBJ)
		//Scroller.fx = obj.scrollLeft
		//gx = obj.scrollLeft		
		
		if(d == "sc_back"){
			//Scroller.sx = Scroller.width * -1			
			Scroller.tx -= Scroller.width
		}
		if(d == "sc_next"){
			//Scroller.sx = Scroller.width
			Scroller.tx += Scroller.width
		}
		
		if(Scroller.tx < 0) Scroller.tx = 0;
					
		if(Scroller.tx > (obj.scrollWidth-Scroller.width))  Scroller.tx = (obj.scrollWidth-Scroller.width);	
		return;
	},
	// attach an event for an element
	// (element, type, function)
	add: function(event, body, d) {
	    if (event.addEventListener) return event.addEventListener(body, d,false)
	    if (event.attachEvent) return event.attachEvent('on'+body, d)
	},

	// kill an event of an element
	end: function(e){
		if (window.event) {
			window.event.cancelBubble = true
			window.event.returnValue = false
      		return;
    	}
	    if (e.preventDefault && e.stopPropagation) {
	      e.preventDefault()
	      e.stopPropagation()
	    }
	},
	
	echo: function(s){ 
    	var elDebug = document.getElementById("debug");
    	elDebug.innerHTML = "<div> " + s + "</div>" + elDebug.innerHTML;	
	},
	
	checkarrow: function(s){ 
    	Anext = document.getElementById("sc_next");
		Aback = document.getElementById("sc_back");
		obj = document.getElementById(Scroller.scrollerOBJ)
    	//obj.scrollWidth
		
		//links aus / an
//alert(obj.scrollLeft);
		if(obj.scrollLeft == 0){	
				
			Aback.style.display = 'none'; 
		}else{
			Aback.style.display = 'block'; 	
		}
		
		//rechts aus / an
		if(obj.scrollLeft == (obj.scrollWidth-Scroller.width)){	
				
			Anext.style.display = 'none'; 
		}else{
			Anext.style.display = 'block'; 	
		}
	},	
	// move the scroll bar to the particular div.
	scroll: function(d){
		
		obj = document.getElementById(Scroller.scrollerOBJ)		
		ix = Scroller.tx - obj.scrollLeft
						
		ax =  Math.ceil( ix / Scroller.speed )
		
		if(ax == 0 && Math.abs(ix) > 0)
			ax =  ix / Scroller.speed

		obj.scrollLeft += ax
		
		
		if(ix == 0){
			clearInterval(Scroller.interval);
			Scroller.checkarrow();
		}

	},
	// initializer that adds the renderer to the onload function of the window
	init: function(){
		Scroller.add(window,'load', Scroller.render)
	},

	// this method extracts all the anchors and validates then as # and attaches the events.
	render: function(){
		
		a = document.getElementsByTagName('a');
		Scroller.end(this);
		//window.onscroll
	    for (i=0;i<a.length;i++) {
			l = a[i];
			relAttribute = String(l.getAttribute('rel'));
			if (l.getAttribute('href')) {		
				if (relAttribute.toLowerCase().match('scroller')) {
					Scroller.add(l,'click',Scroller.end);
					Scroller.checkarrow();
					l.onclick = function () { 
									Scroller.end(this);
									clearInterval(Scroller.interval);
						     		Scroller.interval=setInterval('Scroller.scroll('+Scroller.gx(this.name)+')',10);
									}				
					l.href = "#";
				}
			}
		}	
	}
}
// invoke the initializer of the scroller
Scroller.init();


/*------------------------------------------------------------
 *						END OF CODE
/*-----------------------------------------------------------*/
