function BlimpPortfolio(parentId, bookId) {

    this.eyes = null;    // The parent element
    this.book = null;    // The page wrapper element
    this.pages = null;   // An array containing the portfolio page elements
    this.pageCount = 0;  // Total number of pages
    this.page = 0;       // The page that is viewed
    this.pageStart = 0;  // Where the current page starts
    this.pageHeight = 0; // The height of each page
    this.pageWidth = 0;  // The width of each page
    this.bookHeight = 0; // The height of the book
    this.bookWidth = 0;  // The width of the book
    this.scroller = null // Moo Fx.Scroll object


    this.eyes = $(parentId);
    this.book = $(bookId);

    if (this.book && this.eyes) {
        this.pages = this.book.getChildren();
        this.pageCount = this.pages.length;

        this.pagerDown  = null; // The element that called the last pageDown() method
        this.pagerUp    = null; // The element that called the last pageUp() method
        this.pagerFirst = null; // The element that called the last pageFirst() method
        this.pagerLast  = null; // The element that called the last pageLast() method

        this.pager      = null; // An element that contains page-specific targets to individual pages
        this.pagerChildren = null; // The direct child elements of this.pager

        this.scroller = new Fx.Scroll(this.eyes);
    }
    /*
    this.setPageHeight = function(pHeight) {
        this.pageHeight = pHeight;
    };
    this.getPageHeight = function() {
        return this.pageHeight;
    };
    */

    this.refreshPageControls = function(sender) {
        if (this.pagerUp) {
            this.page == 0 ? this.pagerUp.setOpacity(0) : this.pagerUp.setOpacity(1);
        }
        if (this.pagerFirst) {
            this.page == 0 ? this.pagerFirst.setOpacity(0) : this.pagerFirst.setOpacity(1);
        }
        if (this.pagerDown) {
            ((this.page == this.pageCount-1) || this.pageCount == 0) ? this.pagerDown.setOpacity(0) : this.pagerDown.setOpacity(1);
        }
        if (this.pagerLast) {
            ((this.page == this.pageCount-1) || this.pageCount == 0) ? this.pagerLast.setOpacity(0) : this.pagerLast.setOpacity(1);
        }
    }

    this.pageUp = function(sender) {
        this.pagerUp = $(sender);
        if ((this.pageCount > 0) && (this.page > 0)) {
            this.page--;
            this.pageStart = this.pageStart - this.pageWidth;
            this.scroller.scrollTo(this.pageStart, 0);
        }
        this.refreshPageControls();
    };

    this.pageDown = function(sender) {
        this.pagerDown = $(sender);
        if ((this.pageCount > 0) && (this.page < this.pageCount-1)) {
            this.page++;
            this.pageStart = this.pageStart + this.pageWidth;
            this.scroller.scrollTo(this.pageStart, 0);
        } else {
            this.pageStart = 0;
        }
        this.refreshPageControls();
    };

    this.pageFirst = function(sender) {
        this.pagerFirst = $(sender);
        this.page = 0;
        this.pageStart = 0;
        this.scroller.toLeft();
        this.refreshPageControls();
    };

    this.pageLast = function(sender) {
        this.pagerLast = $(sender);
        if (this.pageCount) {
            this.page = this.pageCount-1;
            this.pageStart = (this.pageWidth * this.pageCount) - this.pageWidth;
            this.scroller.toRight();
        } else {
            this.pageStart = 0;
        }
        this.refreshPageControls();
    };

    this.pageTo = function(pageNo) {
        if (this.pageCount && (pageNo <= this.pageCount) && (pageNo > 0)) {
            this.page = pageNo-1;
            this.pageStart = (this.pageWidth * pageNo) - this.pageWidth;
            this.scroller.scrollTo(this.pageStart, 0);
        }
        this.refreshPageControls();
        return false; // Care for onclick events
    };

    this.pageIndexTo = function(pageIndex) {
        if (this.pageCount && (pageIndex < this.pageCount) && (pageIndex >= 0)) {
            this.page = pageIndex;
            this.pageStart = (this.pageWidth * pageIndex);
            this.scroller.scrollTo(this.pageStart, 0);
        }
        this.refreshPageControls();
    };

}

BlimpPortfolio.prototype.applyStyle = function(autoFit) {
    var pHeight     = 0;
    var pWidth      = 0;
    var totalWidth  = 0;
    var totalHeight = 0;
    var BookWidth   = 0;
    var BookHeight   = 0;

    if (autoFit) {
        // Get page border widths
        var firstPage = $(this.book).getFirst();
        var pageBorderTop    = firstPage.getStyle('borderTopWidth').toInt();
        var pageBorderRight  = firstPage.getStyle('borderRightWidth').toInt();
        var pageBorderBottom = firstPage.getStyle('borderBottomWidth').toInt();
        var pageBorderLeft   = firstPage.getStyle('borderLeftWidth').toInt();
        var pageBordersHorizontal = pageBorderLeft + pageBorderRight;
        var pageBordersVertical = pageBorderTop + pageBorderBottom;
        // Size book to its container
        BookWidth = this.eyes.getStyle('width').toInt();
        BookHeight = this.eyes.getStyle('height').toInt();
        this.book.setStyle('height', BookHeight);
    }
    $$(this.pages).each(
        function(el) {
            // Adjust page styles
            if (autoFit) {
                el.setStyle('width',  BookWidth + pageBordersHorizontal);
                el.setStyle('height', BookHeight + pageBordersVertical);
            }
            el.setStyle('float', 'left');
            if (pHeight == 0) {
                pHeight = el.getStyle('height').toInt() + pageBordersVertical;
            }
            if (pWidth == 0) {
                pWidth = el.getStyle('width').toInt() + pageBordersHorizontal;
            }
            totalHeight = totalHeight + pHeight;
            totalWidth  = totalWidth + pWidth;
        }
    );
    this.pageHeight = pHeight;
    this.pageWidth = pWidth;
    this.bookHeight = totalHeight;
    this.bookWidth = totalWidth;

    // Adjust container styles

    if (autoFit) {
        this.book.setStyle('width', this.bookWidth);
        // Adjust parent container
        this.eyes.setStyle('overflow', 'hidden');
        this.eyes.setStyle('width', pWidth);
        this.eyes.setStyle('height', pHeight);

    }
};

BlimpPortfolio.prototype.setPagers = function(pagerUp, pagerDown, pagerFirst, pagerLast) {
    this.pagerUp    = $(pagerUp);
    this.pagerDown  = $(pagerDown);
    this.pagerFirst = $(pagerFirst);
    this.pagerLast  = $(pagerLast);
    this.refreshPageControls();
}

// Attach paging events to the child elements of the Portfolio-Pager element
BlimpPortfolio.prototype.setPager = function(ePortfolioPager) {
    //this.refreshPageControls();
    if ($(ePortfolioPager)) {
        this.pager = $(ePortfolioPager);
        this.pagerChildren = this.pager.getChildren();
        // Assign paging function calls to each child's onclick event
        var pageNo = 0;
        $$(this.pagerChildren).each(
            //pageNo = pageNo+1;
            function(el) {
                pageNo++;
                var pageIdx = pageNo;
                el.onclick = function() {
                    return portfolio.pageTo(pageIdx);
                };
            }
        );
    }

}

var portfolio = null;

function setupPortfolio(ePortfolio, ePortfolioBook) {
    if ($(ePortfolio) && $(ePortfolioBook)) {
        portfolio = new BlimpPortfolio(ePortfolio, ePortfolioBook);
        portfolio.applyStyle(true);
        //portfolio.setPagers('pagerup','pagerdown','pagerfirst','pagerlast');
    }
}

function setupPortfolioPager(ePortfolioPager) {
    if ($(ePortfolioPager)) {
        if (portfolio) {
            portfolio.setPager(ePortfolioPager);
        }
    }
}

function pagePortfolioNext() {
    var portfolioScroll = new Fx.Scroll($('portfolio'));
    page = page + pageHeight;
    portfolioScroll.scrollTo(0, page);
    return false;
}

function pagePortfolioDown() {
    var portfolioScroll = new Fx.Scroll($('portfolio'));
    page = page + pageHeight;
    portfolioScroll.scrollTo(0, page);
    return false;
}

function pagePortfolioRight() {
    var portfolioScroll = new Fx.Scroll($('portfolio'));
    page = page + pageWidth;
    portfolioScroll.scrollTo(page, 0);
    return false;
}

function pagePortfolioStart() {
    var portfolioScroll = new Fx.Scroll($('portfolio'));
    page = 0;
    portfolioScroll.toLeft();
    return false;
}

function pagePortfolioTop() {
    var portfolioScroll = new Fx.Scroll($('portfolio'));
    page = 0;
    portfolioScroll.toTop();
    return false;
}
