var mfx = window.mfx || {};
mfx.version = "0.1";
mfx.gears 	= {};
mfx.widget 	= {};
mfx.page	={};

mfx.form = {};
mfx.form.freeze = function(obj){
	obj.disabled = 'disabled';
	return obj;
}

mfx.form.unFreeze = function(obj){
	obj.disabled = '';
	return obj;
}

mfx.page.logout = function(){
	
	var jsonRequest = new Request.JSON({url: "/user/logout.php", onSuccess: function(r)
	{
		top.location.replace('/');
	}}).get({time:Math.random()});
}

/*
user info
*/
mfx.user	= (function(){
	var uid	='';
	return {
		setUid:function(u){
			uid=u;
		},
		getUid:function()
		{
			return uid;
		}
	/*return ends*/}
})();

mfx.app 	= {};
mfx.app.getalluser = function(f,parent){
	var jsonRequest = new Request.JSON({url: "/friend/get_all.php?random="+(new  Date).getTime(), onSuccess: function(jso)
	{
		f.call((parent || this),jso);
	}}).get({});
}

mfx.inv = {};
/**
  * 获取邀请链接
  * @param f 回调函数
  * @param parent 回调函数所属的对象
  * @param canvas_name 应用的url名称，如吸血鬼为vampie，平台邀请此参数传空
  * @param invite_flag 邀请类型，1为普通用户邀请, 2为虚拟用户邀请
  * @param baby_id 如果invite_flag=2，则需要此参数，虚拟用户id
  * @return 回调参数中的回调函数,传给参数为json对象，结构如下
  *     result.result.link 链接地址
  */
mfx.inv.getlink = function(f,parent, canvas_name, invite_flag,  baby_id){
    var jsonRequest = new Request.JSON({url: "/invite/link.php", onSuccess: function(jso)
	{
		f.call((parent || this),jso);
	}}).post({'canvas_name':canvas_name,'invite_flag':invite_flag, 'baby_id':baby_id});
}

mfx.inv.displaylink = function(result) {
      var url = result.result.link;
      var html = '<div id="linkedhtml">'+
	'<div class="linkedhtml">'+
		'<div> 复制此邀请链接地址，用QQ、MSN或其他方式发送给你的朋友；打开此网址即可加入...</div>'+
		'<div> <input class="regurl" id="regurl"  type="text" onclick="mfx.inv.copylink($(this).get(\'value\'),this);this.focus();this.select();" readonly="" /></div>'+
		'<div  class="cantseeit">网址复制成功！您可以利用快捷方式Ctrl+V键粘贴到QQ或MSN里。</div>'+
        '<center><a href="#" class="dtclose" />关闭窗口</a></center></div></div>';
      var displayer = new dialog({
					width:'550',
					height:'200',
					backDisable:true
				});
	displayer.create();	
	displayer.setDialogContent(html);
	
    $("regurl").set('value', url);
}

mfx.inv.copylink = function(str, e) {
    if (window.clipboardData) {
        window.clipboardData.setData("Text", str);
    } else {
        if (!$('flash_copy')) {
            var div = new Element("div").set('id', 'flash_copy');
            div.inject($(document.body));
        }
        $('flash_copy').set('html','');
        var flash = "<embed src='/swf/clipboard.swf' FlashVars='clipboard=" + escape(str) + "' width='0' height='0' type='application/x-shockwave-flash'></embed>";
         $('flash_copy').set('html',flash);
    }
    $(e).getParent().getNext().removeClass('cantseeit');
    return true;
}

mfx.feed = {};
/*
发送feed

@param opt.app_id 	app_id
@param opt.content 		feed正文,请求,正文中的{_user_id_}替换成user_id_arr中的每一个用户id
@param opt.completed 	回调函数
@param opt.completed return result
								0	成功
								-1	失败
*/
mfx.feed.send 	= function(opt){
        var appId		= opt.app_id || '';
	var content 	= opt.content || '';
	var completed	= opt.completed || function(){};
	
	var jsonRequest = new Request.JSON({url: "/feed/send.php", onSuccess: function(jso)
	{
		completed.call(this,jso);
	}}).post({'content':content, 'app_id':appId});	
};

mfx.box = {};
mfx.box.send = function(opt){
        var appId		= opt.app_id || '';
	var content 	= opt.content || '';
	var completed	= opt.completed || function(){};
	
	var jsonRequest = new Request.JSON({url: "/app/create_box.php", onSuccess: function(jso)
	{
		completed.call(this,jso);
	}}).post({'content':content, 'app_id':appId});	
};

mfx.request ={};
/*
发送请求

@param opt.user_id_arr 	请求接受者的id数组
@param opt.app_id		app的id
@param opt.content	请求,正文中的{_user_id_}替换成user_id_arr中的每一个用户id
@param opt.completed	回调函数
@param opt.completed return result
									0	成功
									-1	失败
*/
mfx.request.send	= function(opt){
	var content 	= opt.content || '';
	var appId		= opt.app_id || '';
	var userId		= opt.user_id_arr || '';
	var completed	= opt.completed || function(){};
	var jsonRequest = new Request.JSON({url: "/request/send_batch.php", onSuccess: function(jso)
	{
		completed.call(this,jso);
	}}).post({'user_id_arr':'['+userId.toString()+']','content':content,'app_id':appId});
	
}


mfx.online = function(func){
	var showReqCountr	= false;
	var oldCount		= 0;
	var obj				= $('messagecount');
	var doctitle		= document.title;
	var blinknumber		= 0;
	var blinkid			= 0;

	var msgBlink = function()
		{
			document.title = blinknumber % 2 ? '[　　　] - ' + doctitle : '[新消息] - ' + doctitle;
			blinknumber++;
		};

	var emptyMsg 		= function()
		{
			clearInterval(blinkid);
			obj.getElement('.mid').innerHTML	= '';
			obj.title		='';
			obj.style.display='none';
			document.title	= doctitle;
		};
	var newMsg			= function(cot)
		{
			obj.getElement('.mid').innerHTML	= cot+'';
			obj.style.display='inline';
			obj.title		= '有'+cot+'条未读新消息'
			blinkid			= setInterval(msgBlink, 5000);
		};
	
	var renewReqCount 	= function(count)
	{

		if(showReqCountr===false && count===0)
			return true;
		if(showReqCountr===true && count===0)
		{
			emptyMsg();
			showReqCountr = false;
		}
		else if(count>0)
		{
			if(count!=oldCount)
			{
				oldCount = count;
				newMsg(count);
				showReqCountr = true;
			}
		}
		
	};

	var jsonRequest =function(){ new Request.JSON({url: "/user/online.php?random="+Math.random(),onSuccess: function(jso){
		var req = parseInt(jso.result.request_count) + parseInt(jso.result.wall_count) + parseInt(jso.result.message_count)+ parseInt(jso.result.scrip_send_count);
		if(!isNaN(req) )renewReqCount(req);
	        if(mfx.online.cbFunc)mfx.online.cbFunc.call(this,jso.result);
	}}).get({expire:30})};
	jsonRequest();
	setInterval(jsonRequest,20000);
}

if(typeof gb !='undefined')gb.bootLoader.done();
