books = [
    {
    id:1, 
    title: "Down Right Feel Right1",
    subtitle: "- Outercourse For Her",
    description: "asdfasdfsadfsadfasdfasdfasdfas\r\ndfasdfasdfasdfasdf",
    testimony: "Thanks. You\'re great...incredibly supportive and professional! \r\n-name",
    testimonyRef: "- Reference Name",
    price: 12
    },
    {
    id:2, 
    title: "Down Right Feel Right2",
    subtitle: "- Outercourse For Her",
    description: "asdfasdfsadfsadfasdfasdfasdfas\r\ndfasdfasdfasdfasdf",
    testimony: "Thanks. You\'re great...incredibly supportive and professional! \r\n-name",
    testimonyRef: "- Reference Name",
    price: 12
    },
    {
    id:3, 
    title: "Down Right Feel Right3",
    subtitle: "- Outercourse For Her",
    description: "asdfasdfsadfsadfasdfasdfasdfas\r\ndfasdfasdfasdfasdf",
    testimony: "Thanks. You\'re great...incredibly supportive and professional! \r\n-name",
    testimonyRef: "- Reference Name",
    price: 12
    },
    {
    id:4, 
    title: "Down Right Feel Right4",
    subtitle: "- Outercourse For Her",
    description: "asdfasdfsadfsadfasdfasdfasdfas\r\ndfasdfasdfasdfasdf",
    testimony: "Thanks. You\'re great...incredibly supportive and professional! \r\n-name",
    testimonyRef: "- Reference Name",
    price: 12
},
    {
        id: 5,
        title: "Down Right Feel Right5",
        subtitle: "- Outercourse For Her",
        description: "asdfasdfsadfsadfasdfasdfasdfas\r\ndfasdfasdfasdfasdf",
        testimony: "Thanks. You\'re great...incredibly supportive and professional! \r\n-name",
        testimonyRef: "- Reference Name",
        price: 12
    },
    {
    id:6, 
    title: "Down Right Feel Right6",
    subtitle: "- Outercourse For Her",
    description: "asdfasdfsadfsadfasdfasdfasdfas\r\ndfasdfasdfasdfasdf",
    testimony: "Thanks. You\'re great...incredibly supportive and professional! \r\n-name",
    testimonyRef: "- Reference Name",
    price: 12
    },
    {
    id:7, 
    title: "Down Right Feel Right7",
    subtitle: "- Outercourse For Her",
    description: "asdfasdfsadfsadfasdfasdfasdfas\r\ndfasdfasdfasdfasdf",
    testimony: "Thanks. You\'re great...incredibly supportive and professional! \r\n-name",
    testimonyRef: "- Reference Name",
    price: 12
    }
];
    // truncate text

//truncate text     
(function($) {

    BookOrders = function(t, p) {
        //can user $.extend here
        var self = this;
        $.extend(self, {
            addBook: function(book) {

                bookImg = document.createElement("img");
                $(bookImg).attr("src", "files/"+book.image).click(function(e) { p.click(e, book) });
                imgDiv = document.createElement("div");
                $(imgDiv).addClass("book-content-image").append(bookImg);


                titleP = document.createElement("p");
                $(titleP).addClass("book-content-title").html(book.title).click(function(e) { p.click(e, book); });
                subtitleP = document.createElement("p");
                $(subtitleP).addClass("book-content-subtitle").html(book.subtitle).click(function(e) { p.click(e, book); });
                descriptionP = document.createElement("p");
                more = $("<span style=\"padding-left:3px;cursor:pointer;font-weight:bold;font-size: 12px;font-style:italic;color:#DE0083;\">more...</span>").click(function(e) { p.click(e, book); });
                $(descriptionP).addClass("book-content-description").html(self.truncateText(book.description, 78, 4)).append(more);
                orderImg = document.createElement("img");
                $(orderImg).attr("src", "images/order.gif").click(function(e) { p.order(e, book); });
                orderImgP = document.createElement("p");
                $(orderImgP).addClass("book-content-button-p").append(orderImg);

                textDiv = document.createElement("div");
                $(textDiv).addClass("book-content-text");
                $(textDiv).append(titleP).append(subtitleP).append(descriptionP).append(orderImgP);

                bDiv = document.createElement("div");
                $(bDiv).addClass("book-content").append(imgDiv).append(textDiv);

                testimonyDiv = document.createElement("div");
                $(testimonyDiv).addClass("book-testimony").html(self.truncateText(book.testimony, 94, 2) + " ...").click(function(e) { p.click(e, book); });

                testimonyBottomDiv = document.createElement("p");
                $(testimonyBottomDiv).addClass("book-testimony-ref").html(book.testimonyRef);

                frame = document.createElement("div");
                $(frame).addClass("book-content-frame").append(bDiv).append(testimonyDiv).append(testimonyBottomDiv);
                $(t).append(frame);
            },
            addPaging: function(total, page) {
                pageNum = Math.ceil(total / p.itemPerPage);
                pDiv = $('<div class="book-paging" align="center"></div>');
                nDiv = document.createElement("div");
                $(nDiv).width(20 * pageNum);
                for (i = 1; i <= pageNum; i++) {
                    bDiv = document.createElement("div");
                    classBookOrders = this;
                    $(bDiv).text(i).css("float", "left");
                    if (page == i)
                        $(bDiv).css("cursor", "text").css("text-decoration", "none").css("font-weight", "bold");
                    else {
                        bDiv.page = i;
                        $(bDiv).click(
                        function() {
                            classBookOrders.render(this.page);
                        }
                    );
                    }
                    $(nDiv).append(bDiv);
                }
                pDiv.append(nDiv);
                $(t).append(pDiv);
            },
            render: function(page) {
                books = null;
                total = 0;
                classBookOrders = this;
                $.post(p.url,
                { "page": page,
                    "itemPerPage": p.itemPerPage
                },
                function(r) {
                    books = r.books;
                    total = r.total;
                    for (i = 0; i < books.length; i++) {
                        classBookOrders.addBook(books[i]);
                    }
                    classBookOrders.addPaging(total, page);
                },
                "json"
                );
                $(t).empty();

            },
            truncateText: function(s, charPerLine, lineLimit) {
            var nl = s.replace("<p>", "").replace("</p>", "<br/>").split("<br/>");
            if (nl.length == 0) nl = [s];
            var tl = 0;
            var r = "";
            var isLastLineFull = false;
            for (var i = 0; i < nl.length; i++) {
                cl = Math.ceil(nl[i].length / charPerLine);
                tl += cl;
                if (tl > lineLimit) {
                    sf = (cl - (tl - lineLimit)) * charPerLine;
                    if (sf == 0) break;
                    r += (nl[i].substring(0, sf) + "<br />");
                    isLastLineFull = true;
                    break;
                }
                r += (nl[i] + "<br />");
            }
            return r.substr(0, r.length - (isLastLineFull ? 13 : 6));
        }
        });
    };


    $.fn.ebookorder = function(settings) {
        var config = {
            itemPerPage: 3,
            url: false,
            currentPage: 1,
            click: function() { },
            order: function() { }
        };

        if (settings) settings = $.extend(config, settings);

        this.each(function() {
            bookOrder = new BookOrders(this, settings);
            bookOrder.render(1);

        });

        return this;
    };

})(jQuery);