//=====================================================================================================================
//
//	Title: 		 Eminence Scroller ver1.0
//
//	Description: My scroller
//
//	Author:		 Jonathan Awotwi
//
//
//=====================================================================================================================

var outerDiv, innerDiv, totalItems, totalWidth, currentDiv, stopPosition, startPosition
var moveOffset=15;
var xlocation;
var itemWidth=452
var initialised=false
var speed=1;

//Browser sniffer code.
function verifyCompatibleBrowser(){ 
    this.ver=navigator.appVersion 
    this.dom=document.getElementById?1:0 
    this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0; 
    this.ie4=(document.all && !this.dom)?1:0; 
    this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
    this.appName = navigator.appName 
    this.ns4=(document.layers && !this.dom)?1:0; 
    this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5) 
    return this 
} 
bw=new verifyCompatibleBrowser() 


//Construct a scroller object.
function ConstructScroller(innerDiv, outerDiv){

	//outerDiv=(!outerDiv) ? '':'document.'+outerDiv+'.' 
    this.el=  bw.dom?document.getElementById(innerDiv):bw.ie4?document.all[innerDiv]:bw.ns4?eval(outerDiv+'document.'+innerDiv):0; 
    this.css= bw.dom?document.getElementById(innerDiv).style:bw.ie4?document.all[innerDiv].style:bw.ns4?eval(outerDiv+'document.'+innerDiv):0;
    this.nestTop= bw.dom?document.getElementById(outerDiv).offsetTop:bw.ie4?document.all[outerDiv].offsetTop:bw.ns4?eval(outerDiv+'document.'):0;    
   
    this.scrollHeight=bw.ns4?this.css.document.height:this.el.offsetHeight 
    this.clipHeight=bw.ns4?this.css.clip.height:this.el.offsetHeight 
    this.scrollWidth=bw.ns4?this.css.document.width:this.el.offsetWidth 
    this.clipWidth=bw.ns4?this.css.clip.width:this.el.offsetWidth 
    this.leftScroll=ScrollLeft;
    this.rightScroll=ScrollRight;
	this.upScroll=ScrollUp;	
	this.Top= bw.ns4?this.css.document.top:this.el.offsetTop
	
	this.moveAll=MoveContent;
	this.x; 
	this.y; 
    this.obj = innerDiv + "Object" 
    eval(this.obj + "=this") 
    this.stop=CeaseScroll;
    this.Print=PrintAll;
    return this 
}

function PrintAll()
{
  var output = "this.el = " + this.el + 
  "\nthis.css = " + this.css + 
  "\nthis.nestTop = " + this.nestTop + 
  "\nthis.scrollHeight = " + this.scrollHeight + 
  "\nthis.clipHeight = " + this.clipHeight + 
  "\nthis.scrollWidth = " + this.scrollWidth + 
  "\nthis.clipWidth = " + this.clipWidth + 
  "\nthis.Top = " + this.Top + 
  "\nthis.x = " + this.x + 
  "\nthis.y = " + this.y + 
  "\nthis.obj = " + this.obj
  
  alert(output)

}

//Scroll Up function
function ScrollUp(move)
{
//alert("this.el.offsetTop + this.nestTop: " + (this.el.offsetTop+this.nestTop) + "\nthis.nestTop: " + this.nestTop + "\nthis.clipHeight: " +  this.clipHeight + "\n(this.nestTop - this.clipHeight): " + (this.nestTop - this.clipHeight))
	if((this.el.offsetTop + this.nestTop) <(this.nestTop - this.clipHeight) - restartLapse ){
	initialised = false
	InitialiseScroller(0, startPosition, move) 
    }else{
    	this.moveAll(0,this.y+move) 
    	if (loop) timer = setTimeout(this.obj+".upScroll("+move+")",speed) 
    } 

}

function ScrollLeft()
{
   currentDiv = parseInt(document.getElementById('txtCurrentDiv').value) 
   if (currentDiv != 1)	
   {
   //check if contentDiv is initialised, if not initialise it
   if (!initialised)
   {
	contentDiv=new ConstructScroller('divSlider', 'divContainer')
	totalItems = parseInt(document.getElementById('txtTotalItems').value)
	
	totalWidth=totalItems*itemWidth
		
		//set the stop position for the current scroll
		if (currentDiv==totalItems)
		{
		  stopPosition=0-(totalWidth-(itemWidth*2))
		  startPosition=0-(totalWidth-itemWidth)
		}
		else
		{
		  startPosition=0-(itemWidth*(currentDiv-1))
		  stopPosition=startPosition + itemWidth
		}
	 initialised=true;
   }
	//contentDiv.Print();    //Debug
	
   
	
	 if(contentDiv.x)
	 {	 
		
		//alert("StartPosition:" + startPosition + "\nStopPosition: " + stopPosition)
		xlocation=contentDiv.x
		if ((xlocation+moveOffset) < stopPosition)
		{
		  contentDiv.moveAll(xlocation + moveOffset,0);
		  timer=setTimeout(contentDiv.obj + ".leftScroll(" + 1 +")",speed); 
		}
		else
		{
		  contentDiv.moveAll(stopPosition,0);
		  document.getElementById('txtCurrentDiv').value = currentDiv - 1
		  document.getElementById('txtCurrentPage').innerHTML = currentDiv - 1
		  initialised=false;		 
		}
		 
	 }
	 else
	 {
	    
		contentDiv.moveAll(startPosition,0);
	    timer=setTimeout(contentDiv.obj + ".leftScroll(" + 1 + ")",speed); 
	 }
  }
}

function ScrollRight(val)
{
  currentDiv = parseInt(document.getElementById('txtCurrentDiv').value)
  if (currentDiv != totalItems)
  {	
   if (!initialised)
   {
		contentDiv=new ConstructScroller('divSlider', 'divContainer')
		totalItems = parseInt(document.getElementById('txtTotalItems').value)
		
		totalWidth=totalItems*itemWidth
		
		//set the stop position for the current scroll
		if (currentDiv==1)
		{
		  stopPosition=0-itemWidth
		  startPosition=-1
		}
		else
		{
		  stopPosition = 0-(currentDiv*itemWidth)	
		  startPosition=stopPosition+itemWidth	  
		}
		
		//alert(stopPosition)		//Debug
		initialised=true;
   }
  
   //contentDiv.Print();
  
 
   if(contentDiv.x)
   {
		//alert("contentDiv.x:" + contentDiv.x)
        xlocation=contentDiv.x
		
		if ((xlocation-moveOffset) > stopPosition )
		{
		    //alert("contentDiv.x (in if statement):" + contentDiv.x + "\nMove offset:" + moveOffset)
   
		   contentDiv.moveAll(xlocation - moveOffset,0);
		   timer=setTimeout(contentDiv.obj + ".rightScroll(" + -1 +")",speed); 
		}
		else
		{
		   contentDiv.moveAll(stopPosition,0);
		   document.getElementById('txtCurrentDiv').value = currentDiv + 1
		   document.getElementById('txtCurrentPage').innerHTML = currentDiv + 1
		   initialised=false;
		}
   }
   else
   {
		contentDiv.moveAll(startPosition,0);
	    timer=setTimeout(contentDiv.obj + ".rightScroll(" + startPosition + ")",speed); 
	}
  }
}

//Scroll functions call MoveContent to do the actual moving.
function MoveContent(x,y)
{
    //alert('x=' + x + '\ny=' + y)
    this.x=x;
    this.y=y;
    this.css.marginLeft=this.x +"px"; 
    this.css.top=this.y +"px"; 
    //alert('this.css.left=' + this.css.left + '\nthis.css.top=' + this.css.top)
}

//CeaseScroll is usually called on Mouseover
function CeaseScroll()
{
    loop=false 
    if(timer) clearTimeout(timer)
}


function InitialiseScroller()
{ 
    contentDiv=new ConstructScroller('divContainer', 'divSlider') 
    initialised=true;       
    //alert(moveValue)
} 


