/***********************************************************************
 *  Filename: mod25_scrollbar.js                                       *
 *  Date Created: 12MAY06                                              *
 *  Date Last Modified: 16AUG06                                        *
 *  Package: mod25                                                     *
 *  Type: Class definition                                             *
 *  Copyright © 2006 Mod25 Solutions, LLC. All Rights Reserved         *
 *  Notes: This class can be used to create a scrollbar for things     *
 *	like windows and textboxes.                                        *
 **********************************************************************/


/***********************************************************************
 * Requirements:                                                       *
 *      mod25_misc                                                     *
 *      mod25_events                                                   *
 *      mod25_dragDrop                                                 *
 **********************************************************************/

// *** NOTE *** Trouble shooting
//	dragbar middle won't stretch to fit size: make sure there is no height or width set for the dragbar
//
//	extra pixel height in dragbar or track: check that font size is set to 1px

// Array of all the scrollbars.
mod25_scrollbar.objs = new Array();

// Contstructor for a new scrollbar
// Arguments: container - String ID of the element containing the scrollbar parts; track - String ID of the track element;
//	bar - String ID of the bar element; axis - Either 'v' or 'h', default is 'v'
function mod25_scrollbar(container, track, bar, axis) {
	// Get the container element
	this.container = document.getElementById(container);
	
	// Get the track element
	this.track = document.getElementById(track);
	
	// Get the bar element
	this.bar = document.getElementById(bar);
	
	//alert('this.bar = ' + this.bar);
	
	// Get the axis
	this.axis = axis || 'v';
	
	// Get the initial offset of the bar
	this.offX = parseInt(getElementStyle(this.bar, 'left', 'left'));
	this.offY = parseInt(getElementStyle(this.bar, 'top', 'top'));
	
	// Get the boundries for the bar
	mod25_scrollbar.getBoundaries(this);
	
	// Add this scrollbar to the array of scrollbars
	mod25_scrollbar.objs.push(this);
	var tmpId = mod25_scrollbar.objs.length - 1;
	
	// Save the scrollId to the bar and track
	this.bar.scrollId = tmpId;
	this.track.scrollId = tmpId;

	// Add dragging capability
	mod25_dragDrop.addDrag(this.bar);
	
	// Setup event handlers for the drag events
	mod25_events.addEvent(this.bar, 'on_drag', mod25_scrollbar.on_drag, false);
	mod25_events.addEvent(this.bar, 'on_drop', mod25_scrollbar.on_drop, false);

};

// Get the boundaries the bar can travel between
// Arguments: scrollbar - mod25_scrollbar object to get boundaries for
mod25_scrollbar.getBoundaries = function(scrollbar) {

	if (scrollbar.axis == 'v') {
		// Calculate how far down the bar can travel
		scrollbar.bar.maxY = scrollbar.track.offsetHeight - scrollbar.bar.offsetHeight - scrollbar.offY;
		
		// Set the minimum to the original position
		scrollbar.bar.minY = scrollbar.offY;
		
		// Set the horizontal travel to 0
		scrollbar.bar.minX = scrollbar.offX;
		scrollbar.bar.maxX = scrollbar.offX;
	} else {
		// Calculate how far the bar can travel
		scrollbar.bar.maxX = scrollbar.track.offsetWidth - scrollbar.bar.offsetWidth - scrollbar.offX;
		
		// Set the minimum to the original position
		scrollbar.bar.minX = scrollbar.offX;
		
		// Set the vertical travel to 0
		scrollbar.bar.minY = scrollbar.offY;
		scrollbar.bar.maxY = scrollbar.offY;
	}
	
};

// Handle the drag event
mod25_scrollbar.on_drag = function(obj, x, y) {
	// Get the scrollbar
	var scrollbar = mod25_scrollbar.objs[obj.scrollId];
	
	// Calculate the percent the bar has been dragged
	if (scrollbar.axis == 'v') {
		var dragPct = y / scrollbar.bar.maxY;
	} else {
		var dragPct = x / scrollbar.bar.maxX;
	}
	
	// Try and call the on_scroll event
	if (scrollbar._on_scroll)
		scrollbar._on_scroll(dragPct);

};

// Handle the drop event
mod25_scrollbar.on_drop = function() {};

// Set the scrollbars to a specific percent
// Arguments: pct - Percent of scroll for scrollbar;
mod25_scrollbar.prototype.setScroll = function(pct) {
	// Check that the percent is not greater than 100
	if (pct > 1 || pct < 0)
		return false;

	// Calculate the new position of the bar
	if (this.axis == 'v') {
		// Calculate the new position
		var tmpPos = parseInt(pct * this.bar.maxY);
		
		// Ensure the new position is not less than the minimum position
		tmpPos = (tmpPos < this.bar.minY) ? this.bar.minY : tmpPos;
		
		// Set the new position
		this.bar.style.top = tmpPos + "px";
	} else {
		// Calculate the new position
		var tmpPos = parseInt(pct * this.bar.maxX);
		
		// Ensure the new position is not less than the minimum position
		tmpPos = (tmpPos < this.bar.minX) ? this.bar.minX : tmpPos;
		
		// Set the new position
		this.bar.style.left = tmpPos + "px";
	}
	
	// Try and call the on_setScroll event
	if (this._on_setScroll)
		this._on_setScroll(pct);
		
};
