/* Author : Jongmin Yoon (http://zective.com/code/pi/zPaging/)
 * Licenses : GPL (http://www.opensource.org/licenses/gpl-license.php)
 * Thanks to : Bihon (http://www.bihon.com) | Get object key
 * 
 * Version: 2.0.2
 * 
 * Requires Core: 1.2.6+
 * Requires Plug-in: 2.2+ (Mouse Wheel Extension | http://brandonaaron.net/code/mousewheel/demos)
 */

(function(l) {
    l.zPaging = function(d, e, f, g, h, j) {
        var k = this; k.$el = l(d); k.el = d; k.funcArgsObj = {}; k.$el.data("zPaging", k); k.init = function() {
            k.checkChild();
            k.options = l.extend({}, l.zPaging.defaultOptions, h);
            k.cssClass = l.extend({}, j);
            k.options.movemont = k.toInt(k.options.movemont);
            k.options.perPage = k.toInt(k.options.perPage);
            k.options.selectedPage = k.toInt(k.options.selectedPage);
            k.options.linkWidth = k.toInt(k.options.linkWidth);
            k.options.perPage = 10;
            k.oneSideSize = Math.floor(k.options.perPage / 2);
            if (k.options.perPage % 2 == 0) k.options.perPage++;
            k.total = k.toInt(e); k.movement = k.options.movement;
            k.loop = (k.total >= k.options.perPage) ? k.options.perPage : k.total;
            k.checkURI(); 
            k.setSelectItem();
            var a;
            k.minWidth = (a >= k.loop * k.options.linkWidth) ? a : k.loop * k.options.linkWidth + 70;
            if (k.checkError() == false) return false; k.defaultChildID = k.makeRandomID();
            k.child = l("<div />").attr("id", k.defaultChildID).css({ width: k.loop * k.options.linkWidth + "px" });
            //k.child = k.child + l("<div />").css({ width: "3px" });
            k.$el.append(k.child); k.setLoopCondition();
            k.pagingInfo.init();
            k.$el.css({ width: k.minWidth, overflow: "hidden", marginLeft: (k.options.align == "right" || k.options.align == "center") ? "auto" : "0", marginRight: (k.options.align == "left" || k.options.align == "center") ? "auto" : "0" });
            k.linkCss = { width: k.options.linkWidth, float: "left", textAlign: "center", display: "inline" };
            k.$el.children().each(function() { l(this).css({ marginLeft: "auto", marginRight: "auto", padding: 0 }) });
            k.createAreaAndUpdate(k.selectItem)};
            k.makeRandomID = function() { return "zp_" + Math.floor(Math.random() * 100000000) + 1 };
            k.getDefaultOptVal = function(a) { return l.zPaging.defaultOptions[a] }; k.toInt = function(a) { return parseInt(a, 10) };
            k.linkTitle = function(a) { return k.options.linkTitle.replace(/#no#/ig, a) };
            k.getClassName = function(a) { return (k.selectItem == a) ? k.cssClass.nowPage : k.cssClass.normalPage };
            k.getURI = function(a) { return (typeof k.uri != "function") ? k.link + k.options.key + "=" + a : "javascript: void(0);" };
        k.checkChild = function() {
            var a = k.$el.children().length; if (a > 0) {
                //var b = 0; for (var i = 0; i < a; i++) { if (k.$el.children().eq(i).text().trim().match(new RegExp("\\d{1,}", "g"))) b++ } k.original = { perPage: b };
                k.$el.empty()
            }
        };
        k.setSelectItem = function() {
            if (h != null && h != undefined && h.selectedPage != undefined) { k.selectItem = h.selectedPage } else {
                var a = window.location.href, rst;
                var b = new RegExp("[&*]" + k.options.key + "=(\\d{1,})(&|$)", "i");
                if ((rst = a.match(b))) k.selectItem = k.toInt(rst[1]);
                else k.selectItem = 1
            } k.selectItem = (k.selectItem > k.total) ? k.total : (k.selectItem < 1) ? 1 : k.selectItem
        };
        k.setLoopCondition = function() {
            var a = (!arguments[0]) ? k.selectItem : k.toInt(arguments[0]);
            if (k.total <= k.options.perPage) { k.loopStart = 1; k.loopEnd = k.total } else if (a <= k.oneSideSize) { k.loopStart = 1; k.loopEnd = k.options.perPage } else if (a > k.total - k.oneSideSize) {
                k.loopStart = k.total - k.options.perPage + 1;
                k.loopEnd = k.total
            } else { k.loopStart = a - k.oneSideSize; k.loopEnd = a + k.oneSideSize } k.loopEnd += 1
        };
        k.checkURI = function() {
            k.uri = f;
            if (typeof k.uri == "function") {
                var a = /^function\s+([a-zA-Z0-9_]+)\s*\(/i; a.exec(k.uri);
                var b = new RegExp("^function\\s+" + RegExp.$1 + "\\s*\\((.*)\\)\\s*\\{", "i"); var c = "";
                if (typeof g == "object") { for (var i in g) k.funcArgsObj[i] = g[i] } k.link = ""
            } else {
                var c = "";
                if (typeof g == "object") { for (key in g) { if (g[key] != undefined) c += key + "=" + g[key] + "&" } } else if (typeof g == "string") c = g + "&"; k.querys = c; k.link = k.uri + "?" + k.querys
            }
        };
        k.listItem = function(a) { return l("<a />").css(k.linkCss).attr({ href: k.getURI(a), listNo: a, className: k.getClassName(a), title: k.linkTitle(a) }).html(a).unbind("click").click(function() { k.clickHandler(this) }) };
        k.clickHandler = function(a) {
            k.funcArgsObj.zPagingKey = k.options.key; k.funcArgsObj[k.options.key] = k.toInt(l(a).attr("listNo"));
            if (typeof k.uri == "function") {
                k.selectItem = k.funcArgsObj[k.options.key]; k.uri.call(null, k.funcArgsObj);
                k.createAreaAndUpdate()
            } if (typeof k.options.callBack == "function") k.options.callBack(k.funcArgsObj[k.options.key])
        };
        k.createAreaAndUpdate = function() {
            k.setLoopCondition((arguments[0]) ? arguments[0] : null);
            k.$el.children().eq(0).children().each(function() { l(this).remove() });
            for (var i = k.loopStart; i < k.loopEnd; i++) k.$el.children().eq(0).append(k.listItem(i).fadeIn("fast"));
            k.$el.children().eq(0).unmousewheel().mousewheel(function(a, b) {
                if (k.total > k.options.perPage) {
                    if (b > 0) k.leftPart();
                    else k.rightPart();
                } return false
            })
        };
        k.leftPart = function() {
            for (var i = 0; i < k.movement; i++) {
                if (k.loopStart < 2) return false; else { --k.loopEnd; var a = --k.loopStart } if (!k.$el.children("a[listNo='" + (a) + "']").attr("nodeType")) {
                    k.listItem(a).fadeIn("fast").prependTo("#" + k.defaultChildID);
                    k.$el.children().eq(0).children().each(function() { if (k.toInt(l(this).attr("listNo")) >= k.loopStart + k.options.perPage) l(this).remove() })
                }
            }
        };
        k.rightPart = function() {
            for (var i = 0; i < k.movement; i++) {
                if (k.loopEnd > k.total) return false; else { k.loopStart++; var a = k.loopEnd++ } if (!k.$el.children("a[listNo='" + a + "']").attr("nodeType")) {
                    k.$el.children().eq(0).append(k.listItem(a).fadeIn("fast"));
                    k.$el.children().eq(0).children().each(function() { if (k.toInt(l(this).attr("listNo")) < k.loopEnd - k.options.perPage) l(this).remove() })
                }
            }
        };
        k.checkError = function() {
            if (k.options.perPage <= 0) {
                k.$el.html("<strong>zPaging error</strong> : Check the <strong><ins>perPage</ins></strong> option.").css({ font: "italic normal 9pt 'Verdana'", textAlign: "center" });
                return false
            } if (k.options.perPage < k.movement) {
                k.$el.html("<strong>zPaging error</strong> : Check the <strong><ins>movement</ins></strong> option. (Must be less than '<strong>perPage</strong>')").css({ font: "italic normal 9pt 'Verdana'", textAlign: "center" });
                return false
            } if (typeof k.uri != "function" && h != undefined && h.selectedPage != undefined) {
                k.$el.html("<strong>zPaging error</strong> : Check the <strong><ins>selectedPage</ins></strong> option. (Can only do in the <ins>Method Link</ins> mode)").css({ font: "italic normal 9pt 'Verdana'", textAlign: "center" });
                return false
            } return true
        };
        k.pagingInfo = { init: function() {
            this.makeElements();
            this.applyActions();
            this.addElements();

            }, makeElements: function() {
                k.pageLeft = l("<div />").attr({}).css({ width: k.minWidth, textAlign: "center", float: "left", position: "absolute", top: "0" });
                k.pageFirst = l("<div />").attr({ id: k.pageFirstID, className: k.cssClass.infoPageFirst }).css({ width: 13, textAlign: "center", float: "left", position: "relative" });
                k.pagePrev = l("<div />").attr({ id: k.pagePrevID, className: k.cssClass.infoPagePrev }).css({ width: 13, marginLeft: "6px", textAlign: "center", float: "left", position: "relative" });
                k.pageRight = l("<div />").attr({}).css({ width: k.minWidth, textAlign: "center", float: "right", position: "absolute", top: "0" });
                k.pageNext = l("<div />").attr({ id: k.pageNextID, className: k.cssClass.infoPageNext }).css({ width: 13, textAlign: "center", float: "right", position: "relative" });
                k.pageLast = l("<div />").attr({ id: k.pageLastID, className: k.cssClass.infoPageLast }).css({ width: 13, marginLeft: "6px", textAlign: "center", float: "right", position: "relative" })
            }, applyActions: function() {
                k.pageFirst.html(k.options.goFirstTxt).unbind("click").click(function() { k.createAreaAndUpdate(1) });
                k.pagePrev.html(k.options.goPrevTxt).unbind("click").click(function() { k.leftPart() });
                k.pageNext.html(k.options.goNextTxt).unbind("click").click(function() { k.rightPart() });
                k.pageLast.html(k.options.goLastTxt).unbind("click").click(function() { k.createAreaAndUpdate(k.total) })
            }, addElements: function() {
                k.$el.append(k.pageLeft);
                k.pageLeft.append(k.pageFirst);
                k.pageLeft.append(k.pagePrev);
                k.$el.append(k.pageRight);
                k.pageRight.append(k.pageLast);
                k.pageRight.append(k.pageNext)
            }
        };
        k.init()
    };
    l.zPaging.defaultOptions = {
        align: "center",
        key: "page",
        movement: 1,
        perPage: 10,
        selectedPage: 1,
        linkWidth: 20,
        linkTitle: "Jump to #no# page",
        viewInfo: true,
        infoTxt: "Page <strong>#now#</strong> of #total#",
        goFirstTxt: "<img src=\"http://img.wizwell.co.kr/Design/images/shopping/btn_pre_minus.jpg\" width=\"13\" height=\"13\" class=\"infoPageFirst\">",
        goPrevTxt: "<img src=\"http://img.wizwell.co.kr/Design/images/shopping/btn_pre.jpg\" width=\"13\" height=\"13\" class=\"infoPagePrev\" align=\"absmiddle\">",
        goNowTxt: "NOW",
        goNextTxt: "<img src=\"http://img.wizwell.co.kr/Design/images/shopping/btn_next.jpg\" width=\"13\" height=\"13\" class=\"infoPageNext\" align=\"absmiddle\">",
        goLastTxt: "<img src=\"http://img.wizwell.co.kr/Design/images/shopping/btn_next_plus.jpg\" width=\"13\" height=\"13\" class=\"infoPageLast\" align=\"absmiddle\">"
    };
    l.fn.zPaging = function(a, b, c, d, e) { return this.each(function() { (new l.zPaging(this, a, b, c, d, e)) }) };
    l.fn.getzPaging = function() { this.data("zPaging") }
})(jQuery);
