﻿/**
 * 远程调用API库
 * @author 老雷<leizongmin@gmail.com>
 * $Id: api.js 9 2011-05-05 07:00:55Z leizongmin $
 */

window.lfxAPI = {

	baseUrl : 'http://www.frontwinner.com/sharepic/api/',

	//获得模块的基本地址
	moduleURL : function( _m ){
		if( 'undefined' == typeof(_m) )	return false;
		switch(_m){
			case 'comment_list':	return lfxAPI.baseUrl + 'comment.php?act=list';			//获取评论
			case 'comment_post':	return lfxAPI.baseUrl + 'comment.php?act=post';			//发布评论
			case 'mark_like':		return lfxAPI.baseUrl + 'comment.php?act=mark_like';			//标记为喜欢
			default: return false;
		}
	},
	
	//生成调用API的URL	_m=模块名称，	_p=参数,	_f=回调函数
	requestURL : function( _m, _p, _f ){
		if( 'undefined' == typeof(_m) )	return false;
		var url;
		if( !(url = lfxAPI.moduleURL(_m)) )	return false;
		if( 'undefined' != typeof(_f) ){
			url += '&_callback=' + _f;
		}
		if( 'undefined' != typeof(_p) ){
			for( i in _p ){
				url += '&' + i + '=' + (_p[i] + '').replace(/#/g, '%23');		//替换#号为%5c
			}
		}
		return url;
	},

	//调用API
	request : function( _m, _p, _f ){
		var url = lfxAPI.requestURL(_m,_p,_f);
		if(!url)	return false;
		//console.debug(url);
		var s = document.createElement('script');
		s.src = url;
		document.body.appendChild(s);
		return true;
	}
}
