wmenu_Objs = {};
wmenu_Obj.speed = 100; // default speed for mouseover scrolling

function wmenu_Obj(wnId, lyrId, cntId) {
  this.id = wnId; wmenu_Objs[this.id] = this;
  this.animString = "wmenu_Objs." + this.id;
  this.load(lyrId, cntId);
}

wmenu_Obj.loadLayer = function(wnId, id, cntId) {
  if ( wmenu_Objs[wnId] ) wmenu_Objs[wnId].load(id, cntId);
}

wmenu_Obj.prototype.load = function(lyrId, cntId) {
  if (!document.getElementById) return;
  var wndo, lyr;
  if (this.lyrId) {
    lyr = $(this.lyrId);
    lyr.style.visibility = "hidden";
  }
  lyr = $(lyrId);
  wndo = $(this.id);
  lyr.style.top = this.y = 0; lyr.style.left = this.x = 0;
  this.maxY = (lyr.offsetHeight - wndo.offsetHeight > 0)? lyr.offsetHeight - wndo.offsetHeight: 0;
  this.wd = cntId? $(cntId).offsetWidth: lyr.offsetWidth;
  this.maxX = (this.wd - wndo.offsetWidth > 0)? this.wd - wndo.offsetWidth: 0;
  this.lyrId = lyrId; // hold id of currently visible layer
  lyr.style.visibility = "visible";
  this.on_load(); this.ready = true;
}

wmenu_Obj.prototype.on_load = function() { }  

wmenu_Obj.prototype.shiftTo = function(lyr, x, y) {
  lyr.style.left = (this.x = x) + "px";
  lyr.style.top = (this.y = y) + "px";
}

wmenu_Obj.slideDur = 950; // duration of glide

// intermediary functions needed to prevent errors before page loaded 
wmenu_Obj.scrollBy = function(wnId, x, y, dur) {
  if ( wmenu_Objs[wnId] ) wmenu_Objs[wnId].glideBy(x, y, dur);
  if(x<0 ){
	$('prev').className = "";
  }
}

wmenu_Obj.scrollTo = function(wnId, x, y, dur) {
  if ( wmenu_Objs[wnId] ) wmenu_Objs[wnId].glideTo(x, y, dur);
}

wmenu_Obj.prototype.glideBy = function(dx, dy, dur) {
  if ( !document.getElementById || this.sliding ) return;
  this.slideDur = dur || wmenu_Obj.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;
  
  var thumbcolumns = getElementsByClassName($('worksmenu'), "div", "thumbcolumn");
  var currentpos = (this.destX / -$('worksmenumask').offsetWidth) + 1;

  $('status').innerHTML = currentpos + " of " + thumbcolumns.length;
  
  
  //alert ( "startX: " + this.startX + ", maxX:" + this.maxX + ", distX:" + this.distX + ", destX:" + this.destX + ", listwidth:" + document.getElementById('worksmenu').offsetWidth + ", windwidth:" + document.getElementById('worksmenumask').offsetWidth );
  
  if ( this.destX >= 0 ) {
  	document.getElementById('prev').className = "inactive";
  } 
 
  if ( -this.destX + $('worksmenumask').offsetWidth >= $('worksmenu').offsetWidth ) {
  	$('next').className = "inactive";
  } else {
  	$('next').className = "";
  }
  this.slideTo(this.destX, this.destY);
}

wmenu_Obj.prototype.glideTo = function(destX, destY, dur) {
  if ( !document.getElementById || this.sliding) return;
  this.slideDur = dur || wmenu_Obj.slideDur;
  this.lyr = $(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);
}

wmenu_Obj.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);
}

wmenu_Obj.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);
	}
}

wmenu_Obj.prototype.on_slide_start = function() {}
wmenu_Obj.prototype.on_slide = function() {}
wmenu_Obj.prototype.on_slide_end = function() {}

function worksmenu() {
	var wndo = new wmenu_Obj('worksmenumask', 'worksmenu', null);

	$('prev').onclick = function() {
		wmenu_Obj.scrollBy('worksmenumask',340,0);
		this.blur();
		return false;
	}

	$('next').onclick = function() {
		wmenu_Obj.scrollBy('worksmenumask',-340,0);
		this.blur();
		return false;
	}
}
