/*
	File: pageflipperobject.js
	
	$Id: pageflipperobject.js,v 1.4 2003-05-16 00:47:05+02 chriz Exp chriz $
*/

var csComponent_PageFlipperObject = false;
if (typeof(csComponent_DOMfuncs) == "undefined") {
	alert("The script [pageflipperobject.js] needs the 'DOMfuncs' object,\nfound in [domfuncs.js].");
}

// Constants:
var PF_FIRST		= "---PF_FIRST---";
var PF_FORWARD		= "---PF_FWD---";
var PF_BACK			= "---PF_RWD---";
var PF_LAST			= "---PF_LAST---";
var PF_NONE_SHOWING = "---NO_PAGE_SHOWING---";

function csPageFlipperObject() {

// Properties
	this.currentPage = PF_NONE_SHOWING;
	this.pages 		= [];
	this.stopAtEnd	= false;
	this.findID		= "";
	
// Simple Methods
	this.first		= function() { this.gotoPage(PF_FIRST) };
	this.back		= function() { this.gotoPage(PF_BACK) };
	this.forward	= function() { this.gotoPage(PF_FORWARD) };
	this.last		= function() { this.gotoPage(PF_LAST) };

// Events
	this.onFlipStart = new Function("return true;");
	this.onFlipEnd = new Function();
}

// Advanced Methods
csPageFlipperObject.prototype.gotoPage = function(vGoto) {
	if (this.pages.length <= 0)
		return pageFlipperError("No pages defined.");
	var pTarget = "";	// ID of layer to go to when finished
	
	if (typeof(vGoto) == "string") {
		switch (vGoto) {
			case PF_FIRST :
				pTarget = this.pages[0];
			break;
			case PF_FORWARD :
				if (this.currentPage != PF_NONE_SHOWING) {
					for (var p=0; p<this.pages.length; ++p) {
						if (this.pages[p] == this.currentPage) {
							pTarget = (p < this.pages.length-1 ? this.pages[++p] : (this.stopAtEnd ? this.currentPage : this.pages[0]));
							break;
						} else {
							continue;
						}
					}
				} else {
					pTarget = this.pages[0];
				}
			break;
			case PF_BACK :
				if (this.currentPage != PF_NONE_SHOWING) {
					for (var p=0; p<this.pages.length; ++p) {
						if (this.pages[p] == this.currentPage) {
							pTarget = (p > 0 ? this.pages[--p] : (this.stopAtEnd ? this.currentPage : this.pages[this.pages.length-1]));
							break;
						} else {
							continue;
						}
					}
				} else {
					pTarget = this.pages[0];
				}
			break;
			case PF_LAST :
				pTarget = this.pages[this.pages.length-1];
			break;
			default:
				pTarget = csSafeCheck(vGoto); // Must be the layer's ID, since it's a string
			break;
		}
	} else {
		if (!isNaN(vGoto)) {
			pTarget = (vGoto > 0 && vGoto <= this.pages.length)? this.pages[vGoto-1] : "";
		}
	}
	if (pTarget) {
		// Fire event - if it returns false, skip defaultAction
		if (this.onFlipStart(pTarget)) {
			this._defaultFlip(pTarget);
		}
		
		this.currentPage = pTarget;	// Make this the current page
	} else {
		return pageFlipperError("I can't go to page '" + vGoto + "'.");
	}
	
	// Fire event
	this.onFlipEnd();
}

csPageFlipperObject.prototype.addPage = function(vID) {
	if (csSafeCheck(vID)) {
		if (this.pages.join("%%").indexOf(vID) == -1) {
			this.pages[this.pages.length] = vID;
		}
	} else {
		return pageFlipperError("I couldn't add '" + vID + "'.");
	}
};

csPageFlipperObject.prototype.findPages = function(sIdentifier/* Optional: */, bAppend) {
	var arrDivs = "";
	this.findID = "";		// Reset current identifier
	
	blnAppend = bAppend || false;
	if (document.getElementsByTagName) {
		arrDivs = document.getElementsByTagName("div");
	} else if (document.all) {
		arrDivs = new Array();
		for (var o=0; o<document.all.length; ++o) {
			if (document.all[o].tagName.toLowerCase() == "div") {
				arrDivs[arrDivs.length] = document.all[o];
			}
		}
	}
	
	if (arrDivs) {
		if (!blnAppend) {
			this.pages = [];
			this.findID = sIdentifier;
		}
		for (var d=0; d<arrDivs.length; ++d) {
			if (arrDivs[d].id.indexOf(sIdentifier) >= 0)
				this.addPage(arrDivs[d].id);
		}
	}
}


// Hidden member - the default flip function
csPageFlipperObject.prototype._defaultFlip = function(sTarget) {
	if (this.currentPage != PF_NONE_SHOWING) {
		DOMfuncs.hideLayer(this.currentPage);
	}
	DOMfuncs.showLayer(sTarget);
}

// Utilities

// Check if 'strID' refers to an actual layer...
function csSafeCheck(strID) {
	return (DOMfuncs.getDOMelement(strID)? strID : "");
}
	
function pageFlipperError(sMsg) {
	alert("PageFlipper said:\n\t" + sMsg);
	return;
}

csComponent_PageFlipperObject = true;