function objectToString(obj){
	var s="{";
	for(var k in obj) s+="\n\t"+k+":\""+obj[k]+"\"";
	return s+"\n}";
}

function getQuery_all(name){
	var r=document.location.toString().indexOf(name);
	if (r!=-1) return 1; return 0;
}

function escapeReg(regStr){
	if(!arguments.callee.sRE){
	var _2=["/",".","*","+","?","|","(",")","[","]","{","}","\\"];
	arguments.callee.sRE=new RegExp("(\\"+_2.join("|\\")+")","g");
	}
	return regStr.replace(arguments.callee.sRE,"\\$1");
}

var GM_USER = gmu = {
    const_GM_GUEST: "::GM_GUEST", const_GM_SYSTEM: "::GM_SYSTEM",
    //page root
    path_root: "",
    //paths of login user
    path_home: "", path_profile: "", path_friendlist: "", path_editblog: "",
    //the page owner path
    path_owner: "",
    //page owner info
    page_owner: null,
    //funcion deine
    initial: function() {
        var user, poi, vars, lan = this._getLang();
        //page roots
        this.path_root = location.protocol + "//" + location.host + "/";
        //user paths
        this.path_home = this.path_root + "zh-hans/users/[ownerid]";
        this.path_profile = this.path_root + "zh-hans/user/profile/[ownerid]";
        this.path_friendlist = this.path_root + "zh-hans/friendlist/add/[ownerid]/1?destination=[friend_path]";
        this.path_owner = location.pathname.replace(new RegExp("^(.+" + lan + "|\\/)?users"), "users");
        this.path_editblog = this.path_root + "zh-hans/node/[blogid]/edit";
        //
        if (this.isUserLogin()) {
            user = this.getLoginUserInformation();
            poi = this.getPageOwnerInformation();
            //initial vars
            vars = { lan: lan, friend_path: this.path_owner.replace(/^\/zh-hans\//, ""), blogid: this._getBlogID(), ownerid: poi.name == this.const_GM_SYSTEM ? user.uid : poi.uid };
            this.path_home = this._formatPath(this.path_home, vars);
            this.path_profile = this._formatPath(this.path_profile, vars);
            this.path_friendlist = this._formatPath(this.path_friendlist, vars);
            this.path_editblog = this._formatPath(this.path_editblog, vars);
            //check user status
            if (user.uid == poi.uid) {
                //this page belongs to user
                page_owner = user;
                return "this page belongs to current user " + user.name;
            } else {
                //this page belongs to other
                page_owner = poi;
                return "this page belongs to " + poi.name;
            }
        } else {
            //User not login
            page_owner = null;
            return "Please login";
        }
    },
    _getLang: function() {
        var l = $("html").attr("lang");
        return l == "en" ? "" : (l + "/");
    },
    _getBlogID: function() {
        var nid = /.*blogs\/\d+\/(\d+)$/i;
        if (nid.test(location.pathname)) {
            return location.pathname.replace(nid, "$1");
        } else {
            return "null";
        }
    },
    _formatPath: function(path, vals) {
        for (var i in vals) {
            path = path.replace(new RegExp(escapeReg("[" + i + "]")), vals[i]);
        }
        return path;
    },
    /*
    * Get the path by language
    */
    _pageLanPath: function() { return getLang() == "cn" ? "zh-hans/" : ""; },
    /*
    * Check if user logined
    */
    isUserLogin: function() { return $("#welcome #user-login-form").length == 0; },
    /*
    * Collect user informations from special division
    */
    getUserInformation: function(div) {
        var groups = $("#" + div + " .view-content .views-row");
        var fields = ["name", "picture", "uid", "points", "login"];
        var data = [], vars = {};
        var i, len = fields.length;
        //initial data length
        for (i = 0; i < len; i++) data.push([]);
        groups.each(function(i) {
            $(".field-content", this).each(function(j) {
                var as = $("img", this);
                if (as.length > 0) data[j].push(as.attr("src"));
                else data[j].push($(this).text());
            });
        });
        for (i = 0; i < len; i++) vars[fields[i]] = data[i].join("|");
        return vars;
    },
    /*
    * Collect page owner information
    */
    getPageOwnerInformation: function() {
        var u = this.getUserInformation("block-views-user_infor-block_1");
        if (u.name == "") u.name = this.const_GM_SYSTEM;
        return u;
    },
    /*
    * Collect user information
    */
    getLoginUserInformation: function() {
        var u = this.getUserInformation("block-views-user_infor_self-block_1");
        if (u.name == "") u.name = this.const_GM_GUEST;
        return u;
    },
    /*
    * Get login user information.
    * Return the information on right side.
    * If no user logined show register page as default. return a empty data;
    *   If some user logined and the page is owned by the user, return the user information;
    *   if user logined and the page is owned by no one, reutrn the logined user information;
    *   if user logined and the page is owned by others, return the others information;
    */
    getAll: function() {
        var ret = ["", "", "0", "0", "0", "0", "", ""]; //["0 usre picture", "1 user name", "2 has user logined", "3 user points", "4 page owner is the login user",  "5 is logined users blog page"]
        var ui = this.getLoginUserInformation();
        var poi = this.getPageOwnerInformation();
        if (this.isUserLogin()) {
            if (poi.name == this.const_GM_SYSTEM) {//If the page owned by system, then return logined user information
                ret[0] = ui.picture; //user icon
                ret[1] = ui.name; //user name
                ret[2] = "1"; //has user login
                ret[3] = ui.points; //user points
                ret[4] = "1"; //is login user
            } else {//User is login and page owned by other, return the owner information
                if (poi.uid == ui.uid) {//Page owned by current logined user
                    ret[0] = ui.picture; //user icon
                    ret[1] = ui.name; //user name
                    ret[2] = "1"; //has user login
                    ret[3] = ui.points; //user points
                    ret[4] = "1"; //is login user
                    if ((new RegExp("\\/blogs\\/" + ui.uid + "\\/\\d+$", "i")).test(location.pathname)) {//The page is a blog page owned by current user
                        ret[5] = "1";
                    } else {//The page is a profile page
                    }
                } else {
                    ret[0] = poi.picture; //user icon
                    ret[1] = poi.name; //user name
                    ret[2] = "1"; //has user login
                    ret[3] = poi.points; //user points
                    ret[4] = "0"; //is login user
                }
            }
        } else {//Return an empty data for information panel. No user logined show register panel
            if (getQuery_all("/blogs/") == 1 || getQuery_all("/users/") == 1 || getQuery_all("/user/") == 1) {
                ret[0] = poi.picture; //user icon
                ret[1] = poi.name; //user name
                ret[2] = "0"; //has user login
                ret[3] = poi.points; //user points
                ret[4] = "0"; //is login user
            } else {
                ret[0] = "/sites/default/files/swfpictures/personal/11.jpg";
                ret[1] = "";
                ret[2] = "0"; //has user login
                ret[3] = "0";
                ret[4] = "0";
            }
        }

        /*
        * Get facebook status.
        * Moudle added at 2009-11-30 by ziv.yuan
        */
        var self = $(".Status .show_Status #facebook_status_slider"), other = $(".Status .show_Status");
        var msg = '', idx;
        if (self.length > 0) msg = self.html();
        else msg = other.html();
        if (msg.indexOf("</em>") > -1) {
            msg = msg.substr(0, msg.indexOf("</em>")).split("<em>");
        } else {
            msg = msg.substr(0, msg.indexOf("</EM>")).split("<EM>");
        }
        ret[6] = $.trim(msg[0]);
        ret[7] = $.trim(msg[1]);

        return ret;
    },
    getUrl: function() {
        return [this.path_home,
				this.path_friendlist,
				this.path_profile,
				this.path_editblog];
    },
    //Update fackbook status
    updateStatus: function(status) {
        var form = $(".Status .show_Status form");
        if (form.length == 0) return;

        $("#edit-fbss-status", form).attr("value", status);
        form.submit();
    }
}
	//get user info
var getAll=function (){return GM_USER.getAll();},
	//get urls belongs to user
	getUrl=function (){return GM_USER.getUrl();},
	//update user status
	updateStatus=function (msg){GM_USER.updateStatus(msg)};
	//get page language
var getLang=function (){
	var lan=$("html").attr("lang");
	return {"zh-hans":"cn",
			"en":"en"}[lan];
}

$(function (){GM_USER.initial();});
/*********************
 * debug codes start here
 * /
alert(GM_USER.initial());
alert(GM_USER.path_friendlist);
alert(GM_USER.getAll().join("\n")); 
// debug block end */
