function walletJsApiLib()
{
	this.checkEnviroment = function(param) {
		if (typeof $ != "function") {
			alert("jQuery not support or load failed");
			return false;
		}
		if ($.isPlainObject(param)) {
			if ($.isArray(param['plugin'])) {
				for(i = 0 ; i<param['plugin'].length ; i++) {
					if (!$.isFunction($.fn[param['plugin'][i]])) { 
						alert("jQuery plugin load failed!");
						return false;
					}
				}
			} else if (typeof param['plugin'] == 'string') {
				if (!$.isFunction($.fn[param['plugin']]) && !$.isFunction($[param['plugin']])) { 
					alert("jQuery plugin load failed!");
					return false;
				}
			}
		}

		return true;
	};

	this.selfGoToUrl = function (param) {
		if(this.checkEnviroment()) {
			if ($.isPlainObject(param)) {
				if (param["url"]) {
					window.location = param["url"];
				}
			} else if (typeof param === "string") {
				window.location = param;
			}
		} else {
			return false;
		}
	};

	this.parentGoToUrl = function (param) {
		if(this.checkEnviroment()) {
			if ($.isPlainObject(param) && param["parent"]) {
				var win_parent = this.getParent(param["parent"]);
				if (win_parent) {
					if (param["url"]) {
						win_parent.location = param["url"];
					}
				}
			}
		} else {
			return false;
		}
	};

	this.selfShowMsg = function(param) {
		if(this.checkEnviroment()) {
			if ($.isPlainObject(param)) {
				if (param["id"]) {
					$('#'+param["id"]).html(param["message"]);
				}
			}
		} else {
			return false;
		}
	}; 

	this.parentShowMsg = function(param) {
		if(this.checkEnviroment()) {
			if ($.isPlainObject(param) && param["parent"]) {
				var win_parent = this.getParent(param["parent"]);
				if (win_parent) {
					var $ = win_parent.jQuery;
					if (typeof $ != "undefined") {
						$("#"+param["id"]).html(param["message"]);
					}
				}
			}
		} else {
			return false;
		}
	};

	this.selfRefresh = function(param) {
		window.location.reload();
	}

	this.parentRefresh = function(param) {
		if(this.checkEnviroment()) {
			if ($.isPlainObject(param) && param["parent"]) {
				var win_parent = this.getParent(param["parent"]);
				if (win_parent) {
					win_parent.location.reload();
				}
			}
		} else {
			return false;
		}
	}

	this.getParent = function(type) {
		switch (type) {
			case "window":
				if (window.opener) {
					return window.opener;
				}
			break;
			case "iframe":
				if (window.parent != window) {
					return window.parent;
				}
			break;
			case "top":
				if (window.top != window) {
					return window.top;
				}
			break;
		}
		return false;
	};

	this.selfClose = function(param) {
		window.close();
	};

	this.callback = function(param) {
		var obj = this;
		
		if (this.checkEnviroment()) {
			if ($.isPlainObject(param)) {
				$.each(param, function(func_name,func_param){
					if (typeof func_name == "string" && $.isFunction(obj[func_name])) {
						obj[func_name](func_param);
					}
				});
			}
		} else {
			return false;
		}
	};

	this.checkLogin = function(callback) {
		return true;
		var obj = this;
		if (this.checkEnviroment()) {
			if ($.isFunction(callback) && callback) {
				$.ajax({
					'type': "GET",
					'url': "/api/is_login",
					'complete': function(xmlhttp, status) {
						if (status == "success") {
							switch (xmlhttp.responseText) {
								case "not_login":
									callback();
									return true;
								break;
								case "login_first":
									var tpl = window.location.href.match(/tpl=([\w]+)/);
									if (!tpl) {
										tpl = [""];
									}
									obj.selfGoToUrl({"url":"/user/login_first/"+tpl[0]});
								break;
								default:
							}
						}
					}
				});
			}
		} else {
			return false;
		}

		return false;
	}

	this.genParam = function(param) {
		if (this.checkEnviroment({'plugin':'toJSON'})) {
			if ($.isPlainObject(param)) {
				if ($.isPlainObject($.base64) && $.isFunction($.base64.encode)) {
					return $.base64.encode(encodeURIComponent($.toJSON(param)));
				}
			}
		} else {
			return false;
		}
	};

	this.loadUserInfo = function(info,tpl, callback) {
		if (this.checkEnviroment()) {
			if (typeof info == "string") {
				var obj = $('[id='+info+(tpl?'_'+tpl:'')+']');
				if (obj.length) {
					$.get("/user/"+info+(tpl?'/tpl='+tpl:''), function(data,status){
						var str = data.split(",");
						if (str[1] != "not_login") {
							obj.html(data);
							if ($.isFunction(callback)) {
								callback();
							}
						}
					});
				}
			} else if ($.isArray(info)) {
				for (i=0 ; i<info.length ; i++) {
					if (info[i]) {
						this.loadUserInfo(info[i], tpl);
					}
				}
			}
		} else {
			return false;
		}
	}

	
	this.loadComponent = function(param) {
		if (this.checkEnviroment()) {
			if ($.isPlainObject(param) && param['prefix']) {
				if (typeof param['prefix'] == "string") {
					var args = param['prefix'].split("_");
					var t = new Date();
					var tag_name = 'div';

					if (typeof param['tag_name'] == "string" && param['tag_name']) {
						tag_name = param['tag_name'];
					}

					if (args[0] && args[1]) {
						$(tag_name+'[id^='+args[0]+'_'+args[1]+'_]').each(function(){
							var id = $(this).attr('id').split('_')[2];
							if (parseInt(id)) {
								if (typeof param["callback"] == "function") {
									$(this).load("/"+args[0]+"/"+args[1]+"/"+args[1]+"="+id+"&"+t.getTime(), param["callback"]);
								} else {
									$(this).load("/"+args[0]+"/"+args[1]+"/"+args[1]+"="+id+"&"+t.getTime());
								}
							}
						});
						return true;
					}
				}
			}
		} else {
			return false;
		}
		return false;
	}
	
	this.loadPledge = function() {
		var param = {
			"prefix":"pledge_pledgeid",
			"callback":function(){
				$(this).find('a[name=button]').popupWindow({'centerBrowser':1, 'width':662, 'height':362});
			}
		};
		if (!this.loadComponent(param)) {
			alert('Load pledge failed!');
		}
	}
}

