/*******************************************************************************

FILE: mud_PopContent.js
REQUIRES: prototype.js
AUTHOR: Takashi Okamoto mud(tm) - http://www.mudcorp.com/
VERSION: 2.0 - initial public release
DATE: 01/05/2006

--------------------------------------------------------------------------------

This file is part of MudPopContent.

	MudPopContent is free for anyone to use, but this header MUST be
	included, and may not be modified.

*******************************************************************************/

var MudPopContent = Class.create();

// LIST OF CONSTANTS
MudPopContent.ANIM_TIME = 20; // settimeout interval
MudPopContent.DELAY_TIME = 5000; // time of mouse idle before the nav moves back up
// use MudPopContent.slide* if you don't want to look for mouse idle

// unit coordinates for move up and down scaled to 100
MudPopContent.MOVE_UP = new Array(0, -6, -14, -23, -32, -42, -51, -60, -69, -76, -82, -87, -91, -94, -97, -100);
MudPopContent.MOVE_DOWN = new Array(-99, -92, -84, -75, -66, -56, -47, -38, -30, -23, -17, -11, -7, -4, -1, 0, 2, 4, 5, 5, 4, 3, 2, 1, 0);

MudPopContent.prototype = {
	
	/* ------------- CALLBACKS ------------- */
	/* ------------- EDITABLE -------------- */
	
	// runs right before slide begins
	onSlideStart: function() {
		$(this.id+ "-open").style.visibility = "hidden";
	},
	
	// runs right after slide appears and stops
	onSlideStop: function() {
		
	},
	
	// runs right after slide completes
	onSlideEnd: function() {
		$(this.id+ "-open").style.visibility = "visible";
	},
	
	/* ------------- DON'T EDIT PAST HERE ------------- */
		
	initialize: function(id, showX, showY, hideX, hideY, width, height) {
		this.id = id;
		// position and box size
		this.showX = showX;
		this.showY = showY;
		this.hideX = hideX;
		this.hideY = hideY;
		this.width = width;
		this.height = height;
		this.x = this.hideX;
		this.y = this.hideY;
		// boolean states
		this.hidden = true;
		this.moving = false;
		this.animate = null;
		this.timer = null;
		this.on = false;
		// animation dummy
		this.frame = 0;

		// calculate new move_up and move_down arrays
		this.scale = (this.hideY - this.showY) / 100;
		this.MOVE_UP = new Array();
		this.MOVE_DOWN = new Array();
		for (var i = 0; i < MudPopContent.MOVE_UP.length; i++) {
			this.MOVE_UP[i] = Math.round(MudPopContent.MOVE_UP[i] * this.scale) + this.hideY;
		}
		for (var i = 0; i < MudPopContent.MOVE_DOWN.length; i++) {
			this.MOVE_DOWN[i] = Math.round(MudPopContent.MOVE_DOWN[i] * this.scale) + this.hideY;
		}
	},
	
	slide: function() {
		if (this.hidden) {
			this.y = this.MOVE_UP[this.frame];
			this.frame++;
			this.moveTo(this.y);
			if (this.frame == this.MOVE_UP.length) {
				this.y = this.showY;
				this.slideStop();
			}
			else {
				this.animate = window.setTimeout(this.id + ".slide()", MudPopContent.ANIM_TIME);
			}
		}
		else {
			this.y = this.MOVE_DOWN[this.frame];
			this.frame++;
			this.moveTo(this.y);
			if (this.frame == this.MOVE_DOWN.length) {
				this.y = this.hideY;
				this.slideStop();
			}
			else {
				this.animate = window.setTimeout(this.id + ".slide()", MudPopContent.ANIM_TIME);
			}
		}
	},
	
	slideStart: function() {
		if (!this.moving) {
			this.onSlideStart();
			this.moving = true;
			this.slide();
		}
	},
	
	slideStop: function() {
		this.moveTo(this.y);
		this.moving = false;
		this.frame = 0;
		this.hidden = !this.hidden;
		window.clearTimeout(this.animate);
		// set new font color
		if (!this.hidden) {
			this.onSlideStop();
		}
		else {
			this.onSlideEnd();
		}
	},
	
	moveTo: function(d) {
		$(this.id).style.top = d + "px";
	},
	
	showSlide: function() {
		this.slideStart();
		$(this.id).style.visibility = "visible";
	},
	
	hideSlide: function() {
		if (this.timer) window.clearTimeout(this.timer);
		if (!this.on) {
			this.timer = window.setTimeout(this.id + ".slideStart()", MudPopContent.DELAY_TIME);
		}
	},
	
	overSlide: function() {
		this.on = true;
		if (this.timer) window.clearTimeout(this.timer);
	},
	
	offSlide: function() {
		this.on = false;
		this.hideSlide();
	}
}