function PageTurn(config){
    var instance = this;
    var o = $.extend(true,{},config);
    var $pageWrap = $(o.element);
    var $list = $pageWrap.find('[data-page-role="list"] li');
    var $btnPrev = $pageWrap.find('.btn-page-prev');
    var $btnNext = $pageWrap.find('.btn-page-next');
    var $pageIndex = $pageWrap.find('.page-index');
    var $pageNum = $pageWrap.find('.page-num');
    var $txt = $pageWrap.find('.txt-page');

    this.len = $list.length;
    this.index = 0;
    this.pageNum = Math.ceil(this.len/o.showNum);

    PageTurn = function(){
        return instance;
    }

    var _renderPage = function(){
        $list.addClass("u-hide");
        for(var i=0; i<o.showNum && i<instance.len; i++){
            $list.eq(instance.index * o.showNum + i).removeClass("u-hide");
        } 

        if (instance.index <= 0) {
            $btnPrev.addClass("is-disabled");
        } else {
            $btnPrev.removeClass("is-disabled");
        }

        if (instance.index >= instance.pageNum-1) {
            $btnNext.addClass("is-disabled");
        } else {
            $btnNext.removeClass("is-disabled");
        }
        $pageIndex.text(instance.index+1);
    }

    var _initEvent = function(){
        //上一页
        $pageWrap.on("click",'[data-page-role="nav"] .btn-page-prev',function(){
            var $this = $(this);

            if ($this.hasClass("is-disabled")) {
                return;
            }
        
            --instance.index;
            _renderPage();
            $txt.val("");
        })

        //下一页
        $pageWrap.on("click",'[data-page-role="nav"] .btn-page-next',function(){
            var $this = $(this);

            if ($this.hasClass("is-disabled")) {
                return;
            }

            ++instance.index;
            _renderPage();
            $txt.val("");
        })

        //跳转页
        $pageWrap.on("click",'[data-page-role="nav"] .btn-page-go',function(){
            var reg = /^\d*$/;
            var value = $pageWrap.find('[name="pageIndex"]').val();
            var $this = $(this);

            if (reg && !reg.test(value)) {
                return;
            }

            if ($this.hasClass("is-disabled")) {
                return;
            }

            if(value<=0 || value>instance.pageNum){
                return;
            }

            instance.index = value-1;
            _renderPage();
        })
    }

    var _init = function(){
        _initEvent();
    }

    _init();
}

function Select(config){
    var instance = this;
    var o = $.extend(true,{},config);
    var $select = $(o.element);

    Select = function(){
        return instance;
    }

    var _initEvent = function(){
        $select.on("click","dt",function(){
            var $this = $(this);
            var $parent = $this.parent();

            if ($parent.hasClass("is-open")){
                $parent.removeClass("is-open");
            } else {
                $parent.addClass("is-open");
            }
        });
        $select.on("click","dd",function(){
            var $this = $(this);
            var $parent = $this.parent();
            var sortBy = $this.attr("data-val");

            $parent.removeClass("is-open").find("dt span").text($this.text());

            o.callback && o.callback(sortBy);
        });
        $select.on("mouseleave",function(){
            $(this).removeClass("is-open");
        });
    }

    var _init = function(){
        _initEvent();
    }

    _init();
}

function getParam (name) {
    //先获取#后面的参数
    var str = document.location.hash.substr(2);
    var value = getParam2(name, str);
    if (value == null) {
        str = document.location.search.substr(1);
        value = getParam2(name, str);
    }

    return value;
}

function getParam2 (name, str) {
    //获取参数name的值
    var reg = new RegExp("(^|!|&|\\?)" + name + "=([^&]*)(&|$)");

    //再获取?后面的参数
    r = str.match(reg);
    if (r != null) {
        try {
            return decodeURIComponent(r[2]);
        } catch (e) {
            // console.log(e + "r[2]:" + r[2]);
            return null;
        }
    }
    return null;
}

function setCookie(key,val) {
    document.cookie = key+"="+val;  
}

function getCookie(name) {
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}

function checkPlatform(){
    var platform = navigator.platform;
    var os = 5;

    if (platform.indexOf("Mac") ==0 ) {
        os = 3;
    }

    if (platform.indexOf("Win") ==0 ) {
        os = 4;
    }

    if ((platform.indexOf("X11") ==0) || (platform.indexOf( "Linux") ==0)) {
        os = 5;
    }

    return os;
}

window.commonUtil = {
    PageTurn : PageTurn,
    Select : Select,
    getParam : getParam,
    setCookie : setCookie,
    getCookie : getCookie,
    checkPlatform : checkPlatform
}