 var __function_do_name = null;
 var __function_do_params = null;

/* rewrite alert function */
 var winAlert=window.alert;
 window.alert=function(msg,left_position,top_position){
	$("panel_button").innerHTML = '<a class="blockLink" href="javascript:__cancel()" id="__confirm"><b>确定</b></a>';
	$("__panel__").style.width = '400px';
	var Panel;
	$("panel_content").innerHTML = msg;
	if(!left_position){
		left_position = 0;
	}
	if(!top_position){
		top_position = 0;
	}
	Panel = new panel('__panel__', {
		title:       "美丽部落提示信息",
		left_position:       left_position,
		top_position:       top_position
	});
	$("__confirm").focus();
}

/* rewrite confirm function */
var winConfirm=window.confirm;
window.confirm = function(msg,ok_function,ok_param,avatar_html,cc){
	var Panel;
	$("panel_content").innerHTML = msg;
	$("__panel__").style.width = '400px';
	Panel = new panel('__panel__', {
		title:       "美丽部落提示信息",
		left_position:0,
		top_position:0	
	});
	__function_do_name = ok_function;
	__function_do_params = ok_param;
	var s = '<a class="blockLink" href="javascript:__confirm('+ok_function+',__function_do_params)" id="__confirm"><b>确定</b></a>';
	if(avatar_html){
		$('panel_icon').innerHTML = avatar_html;
	}
	if(!cc) {
		s += ' <a class="cancelLink" href="javascript:__cancel()"><b>取消</b></a>';
	}
	$("panel_button").innerHTML =  s;
	$("__confirm").focus();

}

function __confirm(f,p){
	$("panel").remove();
	if(p.length > 0){
		/* at now support max 6 params */
		if(p.length == 1){
			f(p[0]);
		}else if(p.length == 2){
			f(p[0],p[1]);
		}else if(p.length == 3){
			f(p[0],p[1],p[2]);
		}else if(p.length == 4){
			f(p[0],p[1],p[2],p[3]);
		}else if(p.length == 5){
			f(p[0],p[1],p[2],p[3],p[4]);
		}else if(p.length == 6){
			f(p[0],p[1],p[2],p[3],p[4],p[5]);
		}
	} else {
		f();
	}
}

function __cancel(id){
	if(id){
		$(id).remove();
	}else{
		$("panel").remove();
	}
}

function generatePanel(width,height,title,content,parent_id,left,top,action_button){
	//if($(parent_id).innerHTML == ''){
		var str = '<div style="top:'+top+'px;left:'+left+'px;width: '+width+'px;" id="panel"><div><div class="pt"><b class="ptl"></b><b class="ptr"></b><div class="pc"></div></div><table width="100%" cellspacing="0" cellpadding="0"><tbody><tr><td class="pcl"></td><td class="contenttd"><div class="header"><b id="panel_close" title="Close" class="r icon close" onclick="__close(\''+parent_id+'\')"></b><b></b><h2>'+title+'</h2></div><div style="overflow-y:auto;overflow-x:hidden;height:'+height+'px; " class="content" id="__content">'+content+'</div>';
		if(action_button){
			str += '<div class="c control">'+action_button+'</div>';
		}
		str += '</td><td class="pcr"></td></tr></tbody></table><div class="pb"><b class="pbl"></b><b class="pbr"></b><div class="pc"/></div></div></div>';
		$(parent_id).innerHTML = str;
	//}
	$(parent_id).show();
}


function __close(id){
	$(id).hide();
}

function __resetFriendAPI(parent_id){
	$(parent_id).innerHTML = '';
	$(parent_id).hide();
}

function __generateFriendAPI(parent_id,inputWith,divTop,divHeight,divRight){
	if(!inputWith){
		inputWith = 420;
	}
	input2With = inputWith-65;
	str = '<div class="inputf" style="width:500px;">';
	str += '<div class="l"><div style="width: 500px" class="it_s"><div class="r"><img src="/images/select_down.gif" onclick="__chooseFriend()" id="__select_down"></div><div class="it1"><div onclick="fs_superOnclick()" style="cursor: text; height: 25px;" class="fsg21" id="superinput"><div style="width: 50px;" id="activediv" class="fsg_id"><input class="mm" type="text" maxlength="50" autocomplete="off" style="width:'+input2With+'px" class="fsg_it" value="" name="active" id="active" onfocus="fs_inputOnfocus(this);" onblur="fs_inputOnblur(this);" onkeyup="fs_inputOnkeyup(event)" onkeydown="return fs_inputOnkeydown(event)"/><div style="display: none; width: 210px;" id="suggest" class="fsg_nl"/><div style="background: rgb(238, 238, 238) none repeat scroll 0% 0%; padding-left: 7px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(102, 102, 102); width: 220px; display: none;" class="fsg_nl" id="emptysuggest">请输入好友的姓名</div></div></div><div class="c"/></div></div></div>';
	if(!divTop && divTop!=0){
		divTop = -202;
	}
	if(!divHeight){
		divHeight = 195;
	}
	div2Height = divHeight-65;
        if(!divRight){
            divRight=43;
        }
	str += '<div id="fsg_nr" style="position:absolute;right:'+divRight+'px;top:'+divTop+'px;height:'+divHeight+'px;width:340px;display:none;z-index:10000"><table width="100%" cellspacing=0><tr height=30 style="background:#F2F6FB"><td>请选择发送对象</td><td width="60" align="right"></td></tr></table><div id="fsg_nr_in" style="width:100%;overflow-y: scroll;overflow-x: hidden;height:'+div2Height+'px"></div><table width="100%" cellspacing=0><tr height=30><td width="15"><input type="checkbox" name="all" id="all_choose" onclick="__choose_all(this)" /></td><td width="85" style="padding-top:5px"><span>全选</span></td><td><input type="button" value="确定" class="gb1-12" onclick="__add_recipients()"></td></tr></table></div>';
	str += '</div>';
	//str += '<input type="hidden" name="uids">';
	$(parent_id).innerHTML = str;
	$(parent_id).show();
}

function __chooseFriend(){
	var s = $('__select_down').src.lastIndexOf('/');
	var p = $('__select_down').src.substr(s+1,$('__select_down').src.length);
	if(p == 'select_down.gif'){
		$('__select_down').src = '/images/select_up.gif';
	}else{
		$('__select_down').src = '/images/select_down.gif';
		$('fsg_nr').hide();
		return;
	}
	var url = "/friends/myFriends";
	var myAjax = new Ajax.Request(url, 
		{
			method: "post", 
			onLoading: function(){$('fsg_nr_in').innerHTML='读取好友中';$('fsg_nr').show()},
			onComplete: function (req) {__show_friend(eval(req.responseText));}
		});
}

function __show_friend(friends){
		fs_allfrienddata = friends;
		var result = '<table width=100% cellspacing=0>';
		//result += '<tr height=30 style="background:#F2F6FB"><td colspan=3>请选择发送对象</td></tr>';
		if(friends.length == 0){
			result += '<tr height="25"><td colspan=3>你还没有好友哦。</td></tr>';
		} else {
			for(var i=0;i<friends.length;i++){
				var colspan='';
				if(i%3 == 0)
					result += '<tr height="25">';
				if(i == friends.length-1){
					var col = friends.length%3;
					if(col > 0){
						colspan = ' colspan='+(4-col);
					}
				}
				result += '<td style="border-bottom:1px solid #e5e5e5"'+colspan+'><input class="all_friends" id="checkbox'+i+'" onclick="fs_countcheck(this);" type="checkbox"><span>'+friends[i]['username']+fs_logo20(friends[i])+'</span></td>';
				if(i%3 == 2)
					result += '</tr>';
			}
		}
		result += '</table>';
		
		$('fsg_nr_in').innerHTML = result;
		$('fsg_nr').show();
}

function __choose_all(a){
	$$(".all_friends").each(function(e){
		if($("all_choose").checked)
			$(e).checked = true;
		else
			$(e).checked = false;
	})
}

function __add_recipients(){
	fs_data = new Array();
	fs_data2 = new Array();
	fs_selectFriend();
	__submit_recipients();
	$('all_choose').checked = false;
	$('__select_down').src = '/images/select_down.gif';
}

function __submit_recipients(){
	var uidsobj = {};
	for(var i=0; i<fs_data.length; i++){	
		if(fs_data[i].type == "static"){
			uidsobj[fs_data[i].uid] = 1;
		}
	} 
	var uids = "";
	for(var uid in uidsobj){
		uids += uids==""?uid:","+uid;
	}
	$('uids').value = uids;
}

function __changeShareApplication(id,application_id){
	__share_resource_id = id;
	__application_id = application_id;
}

var __share_resource_id = 0;
var __application_id = 0;

function __share(id,application_id,is_share){
	new Insertion.Top('container', '<div id="share_div" style="position:absolute;display:none;z-index:99"><input name="__share_resource_id" id="__share_resource_id" value="'+id+'"><input id="__application_id" value="'+application_id+'"></div>');
	__share_resource_id = id;
	__application_id = application_id;
	__formatShareDescription(id,application_id,is_share);
}


function __formatShareDescription(id,application_id,is_share){
	var url = '/shares/getShareDescription';
	var pars = 'id='+id+'&application_id='+application_id+'&t='+Math.random();
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) {
		if(req.responseText == 'shared'){
			alert('你已经分享过该内容，不能重复分享。');
		}else{
			data = req.responseText.evalJSON(true);
			var str = '<dl><dd><span id="__toOthers" onclick="javascript:__shareOthers()" style="cursor:pointer;background:#f5f5f5;padding:3px;">分享此'+data.application_name+'</span> <span id="__toMe" onclick="javascript:__shareMe()" style="cursor:pointer;padding:3px;">私藏给自己</span><p>';
			if(data.album_id && is_share != 'true'){
				str += '<input type="radio" name="__share_application" checked onclick="__changeShareApplication('+id+','+application_id+')">照片：'+data.subject+'<br /><input type="radio" name="__share_application" onclick="__changeShareApplication('+data.album_id+','+data.album_application_id+')">整个照片专辑：'+data.album_name;
				x = 150;
				div_height = 220;
			}else{
				str += data.subject;
				x = 130;
				div_height = 200;
			}
			
			str += '</p><h4  id ="tell_friend_label" class="margin-T" style="padding-bottom:10px">同时通知给：</h4><div id="tell_friend">e</div><div id="__share_comment"><h4 class="margin-T c">添加评论：</h4><textarea class="margin-T" style="width:98%;height:50px" name="comment" id="comment"></textarea></div></dd></dl><input type="hidden" name="uids" id="uids">';
			var button = '<span id="__share_button"><a href="javascript:__share_do('+id+','+application_id+')" class="blockLink"><b>确定</b></a></span>';
			var div_width = 550;
			var left = ($('warp').getWidth() - div_width + 20)/2;
			var top = document.documentElement.scrollTop + (document.documentElement.clientHeight - div_height - 100)/2 - 100;
			generatePanel(div_width,div_height,"分享",str,"share_div",left,top,button);
			__generateFriendAPI('tell_friend',480,x,140);
		}
	} });
}

function __share_do(id,application_id){
	id = __share_resource_id;
	application_id = __application_id;
	var url = '/shares/shareDo';
	var comment = $('comment').value;
	if(comment != ''){
		comment = '评论：<br  />'+comment;
	}
	var pars = 'shareTo='+__shareTo+'&id='+id+'&application_id='+application_id+'&uids='+$('uids').value+'&comment='+comment+'&t='+Math.random();
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) {
		if(__shareTo == 'Me'){
			action = '私藏';
		}else{
			action = '分享';
		}
		if(req.responseText == 'shared'){
			$("__share_comment").innerHTML = '<div style="text-align:center;margin-top:50px">你已经分享过该内容，不能重复分享。</div>';
		} else if(req.responseText == 'failed'){
			$("__share_comment").innerHTML = '<div style="text-align:center;margin-top:50px">'+action+'失败</div>';
		}else{
			$("__share_comment").innerHTML = '<div style="text-align:center;margin-top:50px"><br />'+action+'成功</div>';
		}
	} });
	$("__share_button").innerHTML = '<a href="javascript:__cancel(\'share_div\')" class="blockLink"><b>确定</b></a>';
}

var __shareTo = 'Others';
function __shareOthers(){
	__shareTo = 'Others';
	$('__toMe').setStyle({'background':'#ffffff'});
	$('__toOthers').setStyle({'background':'#f5f5f5'});
	$('tell_friend_label').show();
	$('tell_friend').show();
}

function __shareMe(){
	__shareTo = 'Me';
	$('__toOthers').setStyle({'background':'#ffffff'});
	$('__toMe').setStyle({'background':'#f5f5f5'});
	$('tell_friend_label').hide();
	$('tell_friend').hide();
	fs_allfrienddata = new Array();
	fs_data = new Array();
	fs_data2 = new Array();
	fs_selectFriend();
	$('uids').value = '';
}


function __report(id,application_id){
	new Insertion.Top('container', '<div id="report_div" style="position:absolute;display:none;z-index:99"><input name="__report_resource_id" id="__report_resource_id" value="'+id+'"><input id="__application_id" value="'+application_id+'"></div>');

	var str = '<dl><dd>举报此内容 (请填写举报的原因)<div id="__report_comment"><br /><textarea class="margin-T" style="width:98%;height:60px" name="comment" id="comment"></textarea></div></dd></dl>';
	var button = '<span id="__report_button"><a id="__report_confirm_button" href="javascript:__report_do('+id+','+application_id+')" class="blockLink"><b>确定</b></a></span>';
	var div_width = 550;
	var div_height = 100;
	var left = ($('warp').getWidth() - div_width + 20)/2;
	var top = document.documentElement.scrollTop + (document.documentElement.clientHeight - div_height - 100)/2 - 100;
	generatePanel(div_width,div_height,"不良内容举报",str,"report_div",left,top,button);

}

function __report_do(id,application_id){
	var url = '/reports/report';
	var comment = $('comment').value;
	var pars = 'id='+id+'&application_id='+application_id+'&comment='+comment+'&t='+Math.random();
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onLoading:function (){
			$('__report_button').innerHTML = 'loading...';
		},onComplete: function (req) {
		if(req.responseText == 'failed'){
			$("__report_comment").innerHTML = '<div style="text-align:center;margin-top:50px">举报失败</div>';
		}else{
			$("__report_comment").innerHTML = '<div style="text-align:center;margin-top:50px"><br />举报成功</div>';
		}
		$("__report_button").innerHTML = '<a href="javascript:__cancel(\'report_div\')" class="blockLink"><b>确定</b></a>';
	} });
	
}