/*************************************************************************
  This code is from Dynamic Web Coding at www.dyn-web.com
  Copyright 2001-4 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

/* dw_glidescroll.js  version date: June 2004 
   glide onclick scrolling for dw_scrollObj (in dw_scrollObj.js)  */

dw_scrollObj.slideDur = 500; // duration of glide

// intermediary functions needed to prevent errors before page loaded 
dw_scrollObj.scrollBy = function(wnId, x, y, dur) {
  if ( dw_scrollObjs[wnId] ) dw_scrollObjs[wnId].glideBy(x, y, dur);
}

dw_scrollObj.scrollTo = function(wnId, x, y, dur) {
  if ( dw_scrollObjs[wnId] ) dw_scrollObjs[wnId].glideTo(x, y, dur);
}

// Resources for time-based slide algorithm: 
//  DHTML chaser tutorial at DHTML Lab - www.webreference.com/dhtml	
//  and cbe_slide.js from	www.cross-browser.com by Mike Foster
dw_scrollObj.prototype.glideBy = function(dx, dy, dur) {
  if ( !document.getElementById || this.sliding ) return;
  this.slideDur = dur || dw_scrollObj.slideDur;
  this.destX = this.destY = this.distX = this.distY = 0;
  this.lyr = document.getElementById(this.lyrId);
  this.startX = this.x; this.startY = this.y;
  if (dy < 0) this.distY = (this.startY + dy >= -this.maxY)? dy: -(this.startY  + this.maxY);
  else if (dy > 0) this.distY = (this.startY + dy <= 0)? dy: -this.startY;
  if (dx < 0) this.distX = (this.startX + dx >= -this.maxX)? dx: -(this.startX + this.maxX);
  else if (dx > 0) this.distX = (this.startX + dx <= 0)? dx: -this.startX;
  this.destX = this.startX + this.distX; this.destY = this.startY + this.distY;
  this.slideTo(this.destX, this.destY);
}

dw_scrollObj.prototype.glideTo = function(destX, destY, dur) {
  if ( !document.getElementById || this.sliding) return;
  this.slideDur = dur || dw_scrollObj.slideDur;
  this.lyr = document.getElementById(this.lyrId); 
  this.startX = this.x; this.startY = this.y;
  this.destX = -Math.max( Math.min(destX, this.maxX), 0);
  this.destY = -Math.max( Math.min(destY, this.maxY), 0);
  this.distY = this.destY - this.startY;
  this.distX =  this.destX - this.startX;
  this.slideTo(this.destX, this.destY);
}

dw_scrollObj.prototype.slideTo = function(destX, destY) {
  this.per = Math.PI/(2 * this.slideDur); this.sliding = true;
	this.slideStart = (new Date()).getTime();
	this.aniTimer = setInterval(this.animString + ".doSlide()",10);
  this.on_slide_start(this.startX, this.startY);
}

dw_scrollObj.prototype.doSlide = function() {
	var elapsed = (new Date()).getTime() - this.slideStart;
	if (elapsed < this.slideDur) {
		var x = this.startX + this.distX * Math.sin(this.per*elapsed);
		var y = this.startY + this.distY * Math.sin(this.per*elapsed);
    this.shiftTo(this.lyr, x, y); this.on_slide(x, y);
	} else {	// if time's up
    clearInterval(this.aniTimer); this.sliding = false;
		this.shiftTo(this.lyr, this.destX, this.destY);
    this.lyr = null; this.on_slide_end(this.destX, this.destY);
	}
}

dw_scrollObj.prototype.on_slide_start = function() {}
dw_scrollObj.prototype.on_slide = function() {}
dw_scrollObj.prototype.on_slide_end = function() {}


	function initScrollLayer() {
	  // hide loading gif
	  var load_lyr = document.getElementById? document.getElementById("loading"): document.all? document.all["loading"]: null;
	  if (load_lyr) load_lyr.style.visibility = "hidden";
	  
	  // arguments: id of layer containing scrolling layers (clipped layer), id of layer to scroll, 
	  // if horizontal scrolling, id of element containing scrolling content (table?)
	  var wndo = new dw_scrollObj('wn', 'lyr1', 'imgTbl');
	  
	  // Read instructions if your scrolling layers are inside tables 

	}


	function scroll(direction){
		var i_current = 1;
		var i_next = 0
		if (document.getElementById('sq_img1').className == 'bt_on'){ i_current = 1 }
		if (document.getElementById('sq_img2').className == 'bt_on'){ i_current = 2 }
		if (document.getElementById('sq_img3').className == 'bt_on'){ i_current = 3 }
		if (document.getElementById('sq_img4').className == 'bt_on'){ i_current = 4 }
		if (document.getElementById('sq_img5').className == 'bt_on'){ i_current = 5 }
		if (document.getElementById('sq_img6').className == 'bt_on'){ i_current = 6 }
		if (document.getElementById('sq_img7').className == 'bt_on'){ i_current = 7 }
		if (document.getElementById('sq_img8').className == 'bt_on'){ i_current = 8 }
		if (document.getElementById('sq_img9').className == 'bt_on'){ i_current = 9 }
		
		// we now know which is currently selected. find which is the next to be selected
		if (!isNaN(direction)){
			i_next = direction;
			direction = 'either';
		}
		if (direction == 'left'){
			if (i_current == 1) { i_next = 9 } else { i_next = i_current - 1 }
		}
		if (direction == 'right'){
			if (i_current == 9) { i_next = 1 } else { i_next = i_current + 1 }
		}


		
		// loop until we find an image that is visible
		do{
			if (document.getElementById('sq_img'+i_next).style.visibility != 'hidden') {
				switch (i_next){
					case 1: { dw_scrollObj.scrollTo('wn',0,0) }
					case 2: { dw_scrollObj.scrollTo('wn',393,0) }
					case 3: { dw_scrollObj.scrollTo('wn',786,0) }
					case 4: { dw_scrollObj.scrollTo('wn',0,354) }
					case 5: { dw_scrollObj.scrollTo('wn',393,354) }
					case 6: { dw_scrollObj.scrollTo('wn',786,354) }
					case 7: { dw_scrollObj.scrollTo('wn',0,708) }
					case 8: { dw_scrollObj.scrollTo('wn',393,708) }
					case 9: { dw_scrollObj.scrollTo('wn',786,708) }
				}
				toggle_on(document.getElementById('sq_img'+i_next));
				return;
			} 
			else { 
				if (direction == 'left'){
					i_next-=1;
				}
				else {
					i_next+=1;
				}
			}

			// If equals 6 then start at beginning again
				if (direction == 'left'){
					if (i_next == 1){
						i_next = 9
					}
				}
				else {
					if (i_next > 8){
						i_next = 1
					}
				}

		}
		while (i_next>0);

	}

	// toggle on off square indicator image
	function toggle_on(obj){
		if (obj.src="images/img_square_off.gif"){
			document.getElementById('sq_img1').src="images/img_square_off.gif";document.getElementById('sq_img1').className='bt_off';
			document.getElementById('sq_img2').src="images/img_square_off.gif";document.getElementById('sq_img2').className='bt_off';
			document.getElementById('sq_img3').src="images/img_square_off.gif";document.getElementById('sq_img3').className='bt_off';
			document.getElementById('sq_img4').src="images/img_square_off.gif";document.getElementById('sq_img4').className='bt_off';
			document.getElementById('sq_img5').src="images/img_square_off.gif";document.getElementById('sq_img5').className='bt_off';
			document.getElementById('sq_img6').src="images/img_square_off.gif";document.getElementById('sq_img6').className='bt_off';
			document.getElementById('sq_img7').src="images/img_square_off.gif";document.getElementById('sq_img7').className='bt_off';
			document.getElementById('sq_img8').src="images/img_square_off.gif";document.getElementById('sq_img8').className='bt_off';
			document.getElementById('sq_img9').src="images/img_square_off.gif";document.getElementById('sq_img9').className='bt_off';
			obj.src="images/img_square_on.gif";
			obj.className='bt_on';
		}
		else {
			obj.src="images/img_square_off.gif";
			obj.className='bt_off';
		}
	}

	// toggle on off square indicator image
	function toggle_on_new(obj){
		if (obj.className == 'bt_off'){
			for (i==1; i==9 ; i++ ){
				document.getElementById('sq_img'+i).className='bt_off';
			}
			obj.className = 'bt_on';
		}
		else {
			obj.className = 'bt_off';
		}
	}
