/**
 * Ocenture WebFlow.
 *
 * $Id: webflow.js 3604 2009-08-13 17:24:51Z tpuhlick $
 *
 * @author Chris Lewis <clewis@ocenture.com>
 */

/**
 * A FlowPage represents a page of a site, in terms of the URL paths by which
 * it may be accessed.
 */
var FlowPage = Class.create();
FlowPage.prototype = {
	
	paths: [],
	
	initialize: function(paths) {
		this.paths = paths;
	},
	
	/**
	 * Check to see if this page is available by this path name.
	 */
	isAt: function(path) {
		return this.paths.find(function(_path) {
				return _path == path;
			}) != null;
	},
	
	getPath: function() {
		return this.paths[0];
	}
	
}


/**
 * A WebFlow represents a pre-determined sequence of pages.
 */
var WebFlow = Class.create();
WebFlow.prototype = {
	
	pages: [],
	
	path: null,
	
	initialize: function(pages, path) {
		this.pages = pages;
		this.setPath(path);
	},
	
	/**
	 * Set the current URL path of the flow.
	 */
	setPath: function(path) {
		this.path = path;
	},
	
	getPrevious: function(path) {
		
		var index = this.findIndex(path);
		if(index < 1) {
			throw new Error('No previous element in flow (index ' + index + ')!');
		}
		
		return this.pages[index - 1];
	},
	
	getFirstPage: function() {
		return this.pages[0];
	},
	
	/**
	 * Compare the current path against the supplied path, and see if the
	 * current path occurs in the sequence before the supplied one.
	 */
	isBefore: function(path) {
		var current = this.findIndex(this.path);
		if(current < 0) return false;
		
		var requested = this.findIndex(path);
		return current < requested;
	},
	
	/**
	 * Like isBefore, but checks if the path is the one occurring just before
	 * the current path in the flow.
	 */
	isJustBefore: function(path) {
		var current = this.findIndex(this.path);
		if(current < 0) return false;
		
		var requested = this.findIndex(path);
		return current - 1 == requested;
	},
	
	/**
	 * Compare the current path against the supplied path, and see if the
	 * current path occurs in the sequence after the supplied one.
	 */
	isAfter: function(path) {
		var current = this.findIndex(this.path);
		if(current < 0) return false;
		
		var requested = this.findIndex(path);
		return current > requested;
	},
	
	isJustAfter: function(path) {
		var current = this.findIndex(this.path);
		if(current < 0) return false;
		
		var requested = this.findIndex(path);
		return current + 1 == requested;
	},
	
	/**
	 * Checks to see if this flow is at the first page in the sequence.
	 */
	isStarting: function() {
		return this.findIndex(this.path) == 0;
	},
	
	/**
	 * Check if the flow has been completed. This happens only when the last
	 * page in the sequence has been reached.
	 */
	atEnd: function() {
		return this.findIndex(this.path) + 1 == this.pages.length;
	},
	
	/**
	 * Check if a page exists in the flow (by path).
	 */
	inFlow: function(path) {
		return this.findIndex(path) > -1;
	},
	
	/**
	 * Find the sequential index of a page in the flow, given a URL path name.
	 */
	findIndex: function(path) {
		var index = -1;
		for(var i = 0; i < this.pages.length; i++) {
			if(this.pages[i].isAt(path)) {
				index = i;
				break;
			}
		}
		
		return index;
	}
	
};