function UserPageManager(){
	this.isChannel = false; //Channel or User
    this.uid = null; //user id
    this.cid = null; //channel id
    this.pid = null; //page id
    this.channelTitle = null; //目前Channel Title
    this.channelDesc = null; //目前Channel desc
    this.pageTitle = null; //目前Page Title
    this.pages = null; //目前user的所有pages
    this.items = null; //目前正在看的items
    this.itemsCache = null; //每個page下的items Cache
    this.viewMode = 1; //顯示模式
}

//設定要顯示的
UserPageManager.prototype.refreshPages = function(isChannel, view_id, default_pid, cbFn){
    var upMgr = this;
    this.isChannel = isChannel;
    this.pid = default_pid;
    this.pageTitle = null;
    this.pages = new Array();
    this.items = new Array();
    this.itemsCache = new Array();
    var refreshDoneFn = function(json) {
        if ('OK' == json.status) {
            upMgr.pages = json.pages;
            if (cbFn) { cbFn(upMgr.pages, default_pid); };
        } else {
            if (cbFn) { cbFn(false); };
        }
    }

    if (this.isChannel) {
        this.cid = view_id;
        $.post('dojob.php', {job:'getpagesByCid', cid:view_id}, function(json) {
            upMgr.uid = json.uid;
            refreshDoneFn(json);
        }, 'json');
    } else {
        this.uid = view_id;
        this.cid = null;
        $.post('dojob.php', {job:'getpages', uid:view_id}, refreshDoneFn, 'json');
    }
};

UserPageManager.prototype.getPageTitle = function(pid){
    pid = (pid) ? pid : this.pid;
    var aPage = this.getOnePage(pid);
    return (aPage) ? aPage.title : '';
};

UserPageManager.prototype.getOnePage = function(pid){
    var thePage = null;
    if (null != pid) {
        for (var i=0; i<this.pages.length; i++) {
            var page = this.pages[i];
            if (pid == page.id) {
                thePage = page;
                break;
            }
        }
    }
    return thePage;
};

UserPageManager.prototype.getPages = function(){
    return this.pages;
};

UserPageManager.prototype.refreshItems = function(pid, cbFn){
    var upMgr = this;
    this.pid = pid; 

    //檢查PID
    if (!pid) {
         if (cbFn) { cbFn(new Array()); };
         return;
    }
    
    //is in cache?
    var items = null;
    var findCache = false;
/*
    for (var i=0; i<upMgr.itemsCache.length; i++) {
        items = upMgr.itemsCache[i];
        if (items.pid == pid) {
            upMgr.items = items.items;
            if (cbFn) { cbFn(upMgr.items); };
            findCache = true;
        }
    }
*/
    if (!findCache) {
        var refreshDoneFn = function(json) {
            if ('OK' == json.status) {
                //upMgr.itemsCache[upMgr.itemsCache.length] = {pid:pid, items:json.items};
                upMgr.items = json.items;
                if (cbFn) { cbFn(upMgr.items); };
            } else {
                if (cbFn) { cbFn(false); };
            }
        }
        $.post('dojob.php', {job:'getbloglist', pid:pid}, refreshDoneFn, 'json');
    }
};

UserPageManager.prototype.getItems = function(pid, cbFn){
    return this.items;
};