﻿/*
 * Handles moving around the page display
 */
function ms_Navigator() {
    this.pageLeft = 0;
    this.pageRight = 1;

    this.BookPages = []; // contains ALL PAGES for the book
	this.SwapPages = null; // contains all alt pages for each letter

	this.self = this;
	this.hasSwapped = false;

    // events
    this.onNextPage = null;
    this.onPriorPage = null;
    this.onFirstPage = null;
    this.onLastPage = null; 

    // methods
    this.nextPage = function() {
        var totalPages = this.totalPages()-1;
        if (this.pageRight >= totalPages ) {
            this.pageLeft = 0;
            this.pageRight = 1;
        }
        else {

            this.pageLeft += 2;
            this.pageRight += 2;
        }
        if ((this.onNextPage !== null) && (typeof (this.onNextPage) == 'function')) {
            this.onNextPage(this.self);
        }
    };

    this.priorPage = function() {
        var totalPages = this.totalPages();

        if (this.pageLeft <= 1) {
            this.pageLeft = totalPages - 2;
            this.pageRight = totalPages - 1;
        }
        else {
            this.pageLeft -= 2;
            this.pageRight -= 2;
        }
        if ((this.onPriorPage !== null) && (typeof (this.onPriorPage) == 'function')) { this.onPriorPage(this.self); }
    };

    this.firstPage = function() {
        this.pageLeft = 0;
        this.pageRight = 1;
        if ((this.onFirstPage !== null) && (typeof (this.onFirstPage) == 'function')) { this.onFirstPage(this.self); }
    };

    this.lastPage = function() {
        var totalPages = this.totalPages();
        this.pageLeft = totalPages - 2;
        this.pageRight = totalPages - 1;
        if ((this.onLastPage !== null) && (typeof (this.onLastPage) == 'function')) { this.onLastPage(this.self); }
    };

    this.swapPage = function(pageNbr, newPageId) {
        var newPage = this.getSwapPage(newPageId, pageNbr).clone(); // to support multiple of the same page
        newPage.pagenbr = pageNbr;
        
        if (pageNbr == this.leftPageNbr())
            this.BookPages[this.pageLeft] = newPage;
        else
            this.BookPages[this.pageRight] = newPage;
        this.hasSwapped = true;
    };

    this.leftPageNbr = function() { return this.BookPages[this.pageLeft].pagenbr; };
    this.rightPageNbr = function() { return this.BookPages[this.pageRight].pagenbr; };
    this.totalPages = function() { return this.BookPages.length; }
    this.leftPage = function() { return this.BookPages[this.pageLeft]; };
    this.rightPage = function() { return this.BookPages[this.pageRight]; };

    this.getPageByName = function(name) {
        var searchName = name.toLowerCase();
        for (var pg in this.BookPages) {
            if (this.BookPages[pg].name.toLowerCase() == searchName) {
                return this.BookPages[pg].name;
			}
        }
        return null;
    };

    this.getPageByNumber = function(pageNbr) {
        var intPgNbr = parseInt(pageNbr);
        for (var pg in this.BookPages) {
            if (parseInt(this.BookPages[pg].pagenbr) == intPgNbr)
                return this.BookPages[pg];
        }
        return null;
    };

    this.getPageIndex = function(name) {
        var searchName = name.toLowerCase();
        for (var pg in this.BookPages) {
            if (this.BookPages[pg].name.toLowerCase() == searchName) {
                return pg;
			}
        }
        return -1;
    };

    this.getPagesForLetter = function(letter, side) {
        var sideInd = (side.toLowerCase() === 'left') ? 0 : 1;
        var pages = new Array();
        var search = letter.toLowerCase();
        // go through MAP
        for (var pg in this.SwapPages) {
            if (this.SwapPages[pg][0].letter.toLowerCase() == search) {
                for (var pg2 in this.SwapPages[pg]) {
                    if (this.SwapPages[pg][pg2].pagenbr == sideInd) {
                        pages.push(this.SwapPages[pg][pg2]);
                    }
                }
                break;
            }
        }
        return pages;
    };


    this.getPageNbrsForPage = function(pageid) {
		var pageNbrArray = new Array();
		for (var pg in this.BookPages) {
			if (this.BookPages[pg].pageid === pageid) {
				pageNbrArray.push(this.BookPages[pg].pagenbr);
			}
		}
        return pageNbrArray;
	};

	this.getPageIdArray = function() {
	    // remove blank pages at start and finish
	    var result = new Array();
	    for (var pg in this.BookPages) {
	        if (this.BookPages[pg].pageid != -1) {
	            result.push(this.BookPages[pg].pageid);
	        }
	    }
	    return result;
	};

	this.getSwapPage = function(pageId, pageNbr) {
	    var foundPage = null;
	    var sideInd = (pageNbr % 2 == 0) ? "1" : "0";
	    for (var pg in this.SwapPages) {
	        for (var pg2 in this.SwapPages[pg]) {
	            if (this.SwapPages[pg][pg2].pageid == pageId) {
	                foundPage = this.SwapPages[pg][pg2]; // just incase we don't have a left/right match
	                if (this.SwapPages[pg][pg2].pagenbr == sideInd) {
                        return this.SwapPages[pg][pg2];
	                }
	            }
	        }
	    }
	    return foundPage;
	};

    // debugging -- assumes firebug is around :)
    this.debugBookPage = function() {
        for (var pg in this.BookPages) {
            this.debug("[" + pg + "] - Page: " + this.BookPages[pg].toString());
        }
    };

    this.debug = function(msg) {
        if (typeof (console) != 'undefined') {
            console.log(msg);
        }
    };
}

function PageContent(image, name, pagenbr, letter, pageid, autoload, isaltimg) {
    this.name = name;
    this.letter = letter;
    this.pagenbr = pagenbr;
    this.pageid = pageid;
    this.isAltImg = isaltimg;

	if (typeof(image) == "string") {
	    if (autoload) {
	        this.image = new Image();
	        this.image.src = image;
	    }
	    else {
	        this.image = image;
	    }
	}
	else if (typeof(image) == "object")
	{
		this.image = image;
	}

	this.clone = function() {
	    return new PageContent(this.image, this.name, this.pagenbr, this.letter, this.pageid, false, this.isAltImg);
	}

	this.toString = function() {
	    return this.name + ", pageid: " + this.pageid + ", pagenbr: " + this.pagenbr
        + ", image: " + this.image.src + ', isAltImage: ' + this.isAltImg;
	}
}


