/**
 *                  HTML
 * <SCRIPT type="text/javascript" src="js/common.js"></SCRIPT>

 * <div id="news_content"></div>  //content
 * <div id="news_page"></div>    //pages

 *                 JavaScript ( your.js)
 * 1. parseXML(responseText)
 *     document.getElementById("news_content").innerHTML = responseText;
 *
 * 2. getXML(page)  -- if you don't want to getXML,
 *     loadXML("gnd?page=" + page);
 *
 * */


//ShowPage = function(name, div_page, div_content, totalPage, currentPage, url_param, page_limit, imgPath) {
ShowPage = function(name, div_page, div_content, totalPage, currentPage, url_param, page_limit, imgPath, ajax) {
    this.name = name;
    this.div_page = div_page;
    this.div_content = div_content;
    this.currentPage = currentPage;
    this.url_param = encodeURI(url_param); //encoding
    this.totalPage = totalPage;
    this.page_limit = page_limit;
    this.imgPath = imgPath;//"../";
    this.ajax = ajax; // using ajax or not

    // init
    this.init();

    //alert("function(" + name + "," + div_page + "," + div_content + "," + totalPage + "," + currentPage + "," + page_limit + "," + imgPath + "," + ajax + ")");
}


ShowPage.prototype.init = function() {
	
	var showpage_div = "<div id=\"" + this.name + "_previous_page\" style=\"float:left; padding-right: 10px;\"></div>"+
					"<div id=\"" + this.name + "_show_page\" style=\"float:left;\"></div>"+
					"<div id=\"" + this.name + "_next_page\" style=\"float:left; padding-left: 10px;\"></div>";
    document.getElementById(this.div_page).innerHTML = showpage_div;
    this.previousPage();
    this.showPages();
    if (this.ajax)
        this.reflashPage();
    this.nextPage();
}


/*             更新全部
 * (包括上一頁、頁數、下一頁的按鈕)
 */
ShowPage.prototype.reflashPage = function(type) {
    if (type == "previous") {
        this.currentPage -= 1;
    } else if (type == "next") {
        this.currentPage += 1;
    } else if (type == "first") {
        this.currentPage = 1;
    } else if (type == "last") {
        this.currentPage = this.totalPage;
    } else if (typeof(type) == "number") {
        this.currentPage = type;
    }
    if (this.ajax) {
        this.previousPage();
        this.nextPage();
        this.showPages();
        getXML(this.currentPage, this.name);
    } else {
        location.replace(this.name + ".php?cp=" + this.currentPage +"&"+ this.url_param);
    }
}

/*
 *  顯示 第一頁 與 上一頁 的按鈕
 */
ShowPage.prototype.previousPage = function() {
    if (this.currentPage > 1) {
        if (document.getElementById(this.name + "_previous_page") != "undefined") {
            document.getElementById(this.name + "_previous_page").innerHTML =
            "<a onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('"+this.name+"_pre','','" + this.imgPath + "icon-left.jpg',1);\"" +
            " onmousedown=\"" + this.name + ".reflashPage('previous')\">" +
            "<img style='cursor:pointer;' align='absmiddle' src=\"" + this.imgPath + "icon-left_2.jpg\" name=\""+this.name+"_pre\" id=\""+this.name+"_pre\" border=\"0\">" +
            "</a>";
        } else {
            // undefine previous_page div
        }
    } else {
        // has no previous page
        if (document.getElementById(this.name + "_previous_page") != "undefined") {
            document.getElementById(this.name + "_previous_page").innerHTML = "";
        }
    }
}

/*
 *  顯示 下一頁 與 最後一頁 的按鈕
 */
ShowPage.prototype.nextPage = function() {
    if (this.currentPage < this.totalPage) {
        if (document.getElementById(this.name + "_next_page") != "undefined") {
            document.getElementById(this.name + "_next_page").innerHTML =
            "<a onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('"+this.name+"_next','','" + this.imgPath + "icon-right.jpg',1)\"" +
            " onmousedown=\"" + this.name + ".reflashPage('next')\">" +
            "<img style='cursor:pointer;' align='absmiddle' src=\"" + this.imgPath + "icon-right_2.jpg\" name=\""+this.name+"_next\" id=\""+this.name+"_next\" border=\"0\">" +
			"</a>";
        } else {
            // undefine next_page div
        }
    } else {
        // has no next page
        if (document.getElementById(this.name + "_next_page") != "undefined") {
            document.getElementById(this.name + "_next_page").innerHTML = "";
        }
    }
}

/*
 *  show pages   ex: 1, 2, 3
 */
ShowPage.prototype.showPages = function() {
    var page_sum = this.totalPage;
    // remove pages
    document.getElementById(this.name + "_show_page").innerHTML = "";
    var text = "";
    var page_start;
    var page_end;
    if (this.currentPage <= Math.ceil(this.page_limit / 2)) {
        page_start = 1;
        page_end = this.page_limit;
    }
    else if (this.currentPage >= (page_sum - Math.ceil(this.page_limit / 2))) {
        page_start = page_sum - this.page_limit + 1;
        page_end = page_sum;
    }
    else {
        page_start = this.currentPage - Math.ceil(this.page_limit / 2) + 1;
        page_end = parseInt(this.currentPage) + Math.ceil(this.page_limit / 2) - 1;
    }
    if (page_sum < this.page_limit) {
        page_start = 1;
        page_end = page_sum;
    }
    if (page_end > 1) {
        for (var i = page_start; i <= page_end; i++) {
            // current page (no link)
            if (i == this.currentPage) {
                text += '<span style="padding-left:5px; padding-right:5px;"><strong>'+i+'</strong></span>';
                //text += '<font style=\"color:#CCC;\">'+i+'</font>';
				//text += '<span style=\"font-size:12px; color:#336600\">'+i+'</span>';
            } else { // add link
                if (this.ajax) {
                    text += '<a href="javascript:' + this.name + '.reflashPage(' + i + ');">' + i + '</a>';
                } else {
                    text += '<span style="padding-left:5px; padding-right:5px;"><a href="javascript:location.replace(\'' + this.name + ".php?cp="+i+'&' + this.url_param + '\');">' + i + '</a></span>';
                }
            }
            //text += "&nbsp;|&nbsp;";
        }
    }
    //show all data
    //text += '<a href="&showall=1">All</a>';
    
    // 顯示頁數
	document.getElementById(this.name + "_show_page").innerHTML = text;
}

