/***********************************************************************
 *  Filename: mod25_slide.js                                           *
 *  Date Created: 11AUG06                                              *
 *  Date Last Modified: 11AUG06                                        *
 *  Package: mod25                                                     *
 *  Type: Class definition                                             *
 *  Copyright © 2006 Mod25 Solutions, LLC. All Rights Reserved         *
 *  Notes: This class contains functions for creating a slide.         *
 **********************************************************************/


/***********************************************************************
 * Requirements:                                                       *
 **********************************************************************/

// Create a new slide
function mod25_slide() {

	// Initialize the input array
	this.input = new Array();
	
	// Return the object
	return this;
}

// Background
mod25_slide.prototype.background;

// Controls
mod25_slide.prototype.controls;

// Input components
mod25_slide.prototype.input;

// Show the slide
mod25_slide.prototype.show = function() {
	// Call event handler
	if (this._on_show)
		this._on_show();
	
	// Show background
	this.background.style.display = "block";
	
	// Show controls
	this.controls.style.display = "block";
	
	return true;
};

// Hide the slide
mod25_slide.prototype.hide = function() {
	// Call event handler
	if (this._on_hide)
		this._on_hide();
	
	// Hide the background
	this.background.style.display = "none";
	
	// Hide the controls
	this.controls.style.display = "none";
	
	return true;
};

// Add a background for the slide
// Arguments: id - String ID of background
mod25_slide.prototype.setBackground = function(id) {
	// Get the background container
	this.background = document.getElementById(id);
	
	// Check if the container was found
	if (!this.background)
		return false;
	
	// Ensure the background is hidden
	this.background.style.display = "none";
	
	return true;
};

// Add the controls to the slide
// Arguments: id - String ID of the controls
mod25_slide.prototype.setControls = function(id) {
	// Get the controls
	this.controls = document.getElementById(id);
	
	// Check if the container was found
	if (!this.controls)
		return false;
	
	// Ensure the controls are hidden
	this.controls.style.display = "none";
	
	// Find all the input controls for this slide
	var inputs = this.controls.getElementsByTagName('wizardinput');
	var a;
	for (a = 0; a < inputs.length; a++) {
		// Check if the tag has children
		if (inputs[a].childNodes.length > 0) {
			// Check if there is more than one child node meaning that there are
			// text nodes inserted for the whitespace
			if (inputs[a].childNodes.length > 1) {
				// Assume the second node is the input element
				this.input[this.input.length] = inputs[a].childNodes[1];
			} else {
				// Assume the only node is the input element
				this.input[this.input.length] = inputs[a].childNodes[0];
			}
		}
	}
	
	// Get the the navigation controls
	var controls = this.controls.getElementsByTagName('div');
	for (div in controls) {
		// Check if controls were returned
		if (!controls[div] || !controls[div].hasAttribute)
			break;
		
		// Check if we got a navigation control
		if (controls[div].hasAttribute('wizard') == true) {
			// Switch the attribute
			switch (controls[div].getAttribute('wizard')) {
				case "back":
					// Add an event handler
					mod25_events.addEvent(controls[div], 'click', function() { this.slide.parent.prevSlide(); }, false);
					break;
				case "next":
					// Add an event handler
					mod25_events.addEvent(controls[div], 'click', function() { this.slide.parent.nextSlide(); }, false);
					break;
				case "cancel":
					// Add an event handler
					mod25_events.addEvent(controls[div], 'click', function() { this.slide.parent.stopWizard(); }, false);
					break;
				case "finish":
					// Add an event handler
					mod25_events.addEvent(controls[div], 'click', function() { this.slide.parent.submitForm(false); }, false);
					break;
				default:
					continue;
			}
			
			// Add the wizard to this control
			controls[div].slide = this;
		}
	}
	
	return true;
};

// Get the offsets of the controls from the background
// startDrag handler
mod25_slide.prototype.beginDrag = function(obj, x, y) {
	// Get the offset of the controls from the background
	this.controlOffX = getElementStyle(this.controls, 'left', 'left') - getElementStyle(this.background, 'left', 'left');
	this.controlOffY = getElementStyle(this.controls, 'top', 'top') - getElementStyle(this.background, 'top', 'top');
	
	return true;
};

// Position the controls with the background
mod25_slide.prototype.doDrag = function(obj, x, y) {
	// Position the controls
	this.controls.style.left = (x + this.controlOffX) + 'px';
	this.controls.style.top = (y + this.controlOffY) + 'px';
	
	return true;
};
