/*
 * This object takes a URL and parses it looking to extract the page
 * type (page or cover), the page number, and the card number, if any.
 * For example, given this
 *  http://www.dropby.com/AlbumDeLaRevolucionCubana/page01/002.html
 * Results:
 * 	base: "http://www.dropby.com/AlbumDeLaRevolucionCubana";
 * 	pageType: "page"	// the only other type is "cover"
 * 	pageDigs: 2			// number of digits in page name
 * 	pageNo: 1			// as an integer
 * 	pageName: "page01"
 * 	cardDigs: 3			// number of digits in card name
 * 	cardNo: 2			// as an integer
 * 	cardName: "002"
*/
function namesGet(u)
{
	// ../page16
	var p = u.match(/(.*)\/(cover|page)(\d\d)/);
	this.base = p[1];									// ".."
	this.pageType = p[2];								// "page"
	this.pageDigs = 2;
	this.pageNo = eval(p[3]);							// 16
	this.pageName = p[2]+p[3];							// "page16"
	this.cardDigs = 3;

	var f = "";
	// /001.html
	if(f = u.match(/\/(\d\d\d)\.html$/)) {
		this.cardNo = eval((f[1].match(/0*(\d+)/))[1]);  // eliminate leading 0s to avoid it being interpreted as octal
		this.cardName = f[1];							// "001"
	}
	// cover02/frenteEscambray.html
	else if(f = u.match(/\/(\w+)\.html$/)) {
		this.cardNo = null;
		this.cardName = f[1];							// "frenteEscambray"
	}
	else {
		this.cardNo = null;
		this.cardName = null;
	}
/*
alert(u+"\nResults:\n"
+"base: "+this.base+"\n"
+"pageType: "+this.pageType+"\n"
+"pageDigs: "+this.pageDigs+"\n"
+"pageNo: "+this.pageNo+"\n"
+"pageName: "+this.pageName+"\n"
+"cardDigs: "+this.cardDigs+"\n"
+"cardNo:"+this.cardNo+"\n"
+"cardName: "+this.cardName);
*/
}

function leading0s(n, w)
{
	var no = "";
	for(var i=0; i<(w-(""+n).length); i++) {
		no += "0";
	}
	no += n;
	return(no);
}

var _pages = new Array("cover01", "cover02", "page01", "page02", "page03", "page04", "page05", "page06", "page07", "page08", "page09", "page10", "page11", "page12",	"page13", "page14", "page15", "page16", "page17", "page18", "page19", "page20", "page21", "page22", "page23", "page24", "page25", "page26", "page27", "page28", "page29", "page30",	"page31", "page32", "cover03", "cover04");
var _404page = "404.html";

function nextPage(u)
{
	var g = new namesGet(u);
	for(var i=0; i<_pages.length; i++)
		if(g.pageName == _pages[i]) {
			return(g.base+"/"+((i == (_pages.length-1))?_pages[0]:_pages[++i])+"/"+g.cardName+".html");
		}
	return(_404page);
}

function prevPage(u)
{
	var g = new namesGet(u);
	for(var i=0; i<_pages.length; i++)
		if(g.pageName == _pages[i]) {
			return(g.base+"/"+((i == 0)? _pages[_pages.length-1]: _pages[--i])+"/"+g.cardName+".html");
		}
	return(_404page);
}

var _cards = new Array(
	0,
	8, 16, 24, 33, 42, 50, 58, 66, 75, 83,
	92, 100, 108, 117, 125, 133, 141, 149, 157, 165,
	174, 183, 192, 200, 209, 217, 226, 234, 243, 251,
	260, 268
);

var _cover02 = new Array(
	"movimiento26DeJulio",
	"directorioRevolucionario",
	"frenteEscambray"
);

function nextCard(u)
{
	var g = new namesGet(u);
	var c = "";
	var p = g.pageName;
	if(g.cardNo) {
		c = leading0s(g.cardNo+1, g.cardDigs);
		for(var i=1; i<_cards.length; i++)
			if(g.cardNo == _cards[i]) {
				if(i == _cards.length-1) {
					c = _cover02[0];
					p = "cover02";
				}
				else {
					c = leading0s(g.cardNo+1, g.cardDigs);
					p = "page"+leading0s(g.pageNo+1, g.pageDigs);
				}
			}
	}
	else {
		c = g.cardName;
		for(var i=0; i<_cover02.length; i++)
			if(g.cardName == _cover02[i]) {
				if(i == _cover02.length-1) {
					c = leading0s(1, g.cardDigs);
					p = "page"+leading0s(1, g.pageDigs);
				}
				else {
					c = _cover02[i+1];
				}
			}
	}
	top.content.scoutMap.location = g.base+"/"+p+"/"+c+".html";
	top.content.note.location = g.base+"/"+p+"/"+c+"note.html";
}

function prevCard(u)
{
	var g = new namesGet(u);
	var c = "";
	var p = g.pageName;
	if(g.cardNo) {
		c = leading0s(g.cardNo-1, g.cardDigs);
		for(var i=0; i<_cards.length; i++)
			if(g.cardNo == _cards[i]+1) {
				if(i == 0) {
					c = _cover02[_cover02.length-1];
					p = "cover02";
				}
				else {
					c = leading0s(g.cardNo-1, g.cardDigs);
					p = "page"+leading0s(g.pageNo-1, _pageDigs);
				}
			}
	}
	else {
		c = g.cardName;
		for(var i=0; i<_cover02.length; i++)
			if(g.cardName == _cover02[i]) {
				if(i == 0) {
					c = leading0s(_cards[_cards.length-1], g.pageDigs);
					p = "page"+leading0s(_cards.length-1, g.pageDigs);
				}
				else {
					c = _cover02[i-1];
				}
			}
	}
	top.content.scoutMap.location = g.base+"/"+p+"/"+c+".html";
	top.content.note.location = g.base+"/"+p+"/"+c+"note.html";
}

function postalLabel(u)
{
	var g = new namesGet(u);
	if(g.cardNo)
		return("P&aacute;gina "+g.pageNo+"\nPostal #"+g.cardName);
	else
		return("Portada #"+g.pageNo);
}

function pageLabel(u)
{
	var g = new namesGet(u);
	if(g.pageType == "cover") {
		return("Portada");
	}
	else if(g.pageType == "page") {
		return("P&aacute;gina #"+g.pageNo);
	}
}

function mo(s)
{
	self.status = unescape(s);
	return(true);
}

function index(key)
{
	try {
		top.content.scoutMap.location.href = key+".html";
		top.content.note.location.href = key+"note.html";
	}
	catch(e) {
		top.content.location.href = "indexLFcard.html?key="+key; // key=page16/126
	}
}

