// JavaScript Document
var direction2 = "left";
var timer = "";
var timer2 = "";
var timer3 = "";
var timer4 = "";
var workArea;
var workBox;
var workText;
var imageTextArea;
var imageNumberTextArea;
var currentImage = 1;
var doText;
var imageThisTotal;
var scrollingAlready=0;

function scrollDiv(id,scrollAmount,direction,toScroll,text){
	if (imageThisTotal>1) {
		if (scrollingAlready<1) {
			scrollingAlready=1;
			doText=text;
			if (currentImage >= imageThisTotal) {
	   			direction = "right"  
      		}
      		if (currentImage <= 1) {
	   			direction = "left"  
      		}
			clearAllTimers ()
			timer = setInterval(function() {
  				if (scrollAmount >= toScroll) {
    				scrollAmount = scrollAmount - toScroll;
  				}
  				else {
    				toScroll=scrollAmount;
    				scrollAmount=0;
  				}
  				if (direction == "left") {
  					direction2 = "left"
  					$(id).scrollLeft+= toScroll;
  				}
  				else {
  					direction2 = "right"
  					$(id).scrollLeft-= toScroll;
  				}
  				if (scrollAmount === 0) {
					scrollingAlready=0;
					if (timer!="") {
     					clearInterval(timer);
     					timer="";
    				}
					if (direction == "left") {
       					currentImage=currentImage+1;
      				}
      				else {
       					currentImage=currentImage-1;
    				}
	  				if (text ==1 ) {
	  					theTextNumber=parseInt(currentImage);
      					$(imageTextArea).innerHTML=y[theTextNumber];
      					$(imageNumberTextArea).innerHTML=currentImage + " of " + imageThisTotal;
	  				}
					timer2 = setInterval(function() { scrollDiv2(workArea,863,direction2,20,doText)},timerTime);
				}
  			}, 4);
		}
	}
}

function scrollDiv2(id,scrollAmount,direction,toScroll,text){
  if (imageThisTotal>1) {
	doText=text;
	if (currentImage >= imageThisTotal) {
		clearAllTimers ()
		scrollAmountTotal=imageThisTotal*863;
		scrollAmountTotal2=imageThisTotal*18;
		scrollAmountTotal2=234;
	   	scrollDiv3(workArea,scrollAmountTotal,'right',scrollAmountTotal2,doText);
		currentImage=1;
    }
	else {
    if (currentImage <= 1) {
		direction = "left"  
    }
 	timer3 = setInterval(function() {
  		if (scrollAmount >= toScroll) {
    		scrollAmount = scrollAmount - toScroll;
  		}
  		else {
    		toScroll=scrollAmount;
    		scrollAmount=0;
  		}
  		if (direction == "left") {
  			direction2 = "left"
  			$(id).scrollLeft+= toScroll;
  		}
  		else {
  			direction2 = "right"
  			$(id).scrollLeft-= toScroll;
  		}
  		if (scrollAmount === 0) {
			if (timer3!="") {
    			clearInterval(timer3);
       			timer3="";
      		}
			if (direction == "left") {
       		currentImage=currentImage+1;
    		}
    		else {
    			currentImage=currentImage-1;
    		}
			if (text ==1 ) {
				theTextNumber=parseInt(currentImage);
    			$(imageTextArea).innerHTML=y[theTextNumber];
    			$(imageNumberTextArea).innerHTML=currentImage + " of " + imageThisTotal;
			}
		}
  	}, 4);
	}
  }
}

function scrollDiv3(id,scrollAmount,direction,toScroll,text){
			clearAllTimers ()
			timer = setInterval(function() {
  				if (scrollAmount >= toScroll) {
    				scrollAmount = scrollAmount - toScroll;
  				}
  				else {
    				toScroll=scrollAmount;
    				scrollAmount=0;
  				}
  				if (direction == "left") {
  					direction2 = "left"
  					$(id).scrollLeft+= toScroll;
  				}
  				else {
  					direction2 = "right"
  					$(id).scrollLeft-= toScroll;
  				}
  				if (scrollAmount === 0) {
					clearAllTimers ();
					currentImage=1;
	  				if (text ==1 ) {
	  					theTextNumber=parseInt(currentImage);
      					$(imageTextArea).innerHTML=y[theTextNumber];
      					$(imageNumberTextArea).innerHTML=currentImage + " of " + imageThisTotal;
	  				}
					timer2 = setInterval(function() { scrollDiv2(workArea,863,'left',20,doText)},timerTime);
				}
  			}, 4);
}

function doOnLoad(id,text,page,s) {
  if (s == null) s = 1;
  doText=text;
  workArea='workCaseContainer';
  workBox='workCaseBox';
  workText='workText'+id;
  imageTextArea="imageText";
  imageNumberTextArea="imageNumberText";
  $(id).scrollLeft = 0;
  if (page=='services') {
	  if (s==1) $('service1').style.backgroundPosition="-185px 0";
	if (s==2) $('service2').style.backgroundPosition="-326px 0";
	if (s==3) $('service3').style.backgroundPosition="-174px 0";
	if (s==4) $('service4').style.backgroundPosition="-227px 0";
	if (s==5) $('service5').style.backgroundPosition="-206px 0";
	if (s==6) $('service6').style.backgroundPosition="-53px 0";
  }
  if (imageThisTotal>1) {
  timer4 = setInterval(function() {scrollDiv2(workArea,863,direction2,20,doText)},timerTime);
  }
}

function clearAllTimers () {
    if (timer!="") {
    clearInterval(timer);
    timer="";
    }
    if (timer2!="") {
    clearInterval(timer2);
    timer2="";
    }
    if (timer3!="") {
    clearInterval(timer3);
    timer3="";
    }
	if (timer4!="") {
    clearInterval(timer4);
    timer4="";
    }	
}


function toStart(id) {
$(id).scrollLeft = 0;
}