
jQuery.noConflict();
var $j = jQuery;
var _commJs = new Common();
var _groupViewPageParams = new GroupViewParams();
var _params = _groupViewPageParams.params;
var _tips = _groupViewPageParams.tips;
var __webchat_open_flag = 0;
var __requestJoinAndChat = 0;
function init() {
	loadFriendsGroups();
	loadGroupsByCid();
	loadTask(_params.t);
	if (_params.mRole > 0) {
		loadNews();
	}
	if (_params.mRole >= 0) {
//		loadGroupFeed();
		loadGadate();
	}
	if(_params.mpType == 2){
		$j('#noticeDIV').show();
	}
	bindWebChatBtn();
	resetWebChatBtn();
	if(_params.mRole>=0 && _params.wehChatFlag=="1"){
		$j("#web_chat_btn_open").click();
	}
}

function needOpenChat(){
	return _params.wehChatFlag=="1" || __requestJoinAndChat==1;
}
function loadGadate(){
	if(__dageteHtml){
		$j('#dagateId').html(__dageteHtml);
	}
}
function requestActivat(platform) {
	$j.post(_params.requestActivatUrl, {gid:_params.gid, platform:platform}, function (data) {
		if (data.rs == 1) {
			alert(_params.activateResTip);
		}
	}, "json");
}
function loadNews() {
	$j.get(_params.newsUrl, {gid:_params.gid, lang:_params.lang}, function (data) {
		if (data.c > 0) {
			$j("#newsDIV_applyCount").html(data.c);
			$j("#newsDIV").show();
		}
	}, "json");
}
function loadGroupFeed() {
	$j.get(_params.feedUrl, {gid:_params.gid, lang:_params.lang}, function (html) {
		$j("#feedDIV").html(html);
	});
}
function loadFriendsGroups() {
	$j.get(_params.fUrl, {gid:_params.gid}, function (html) {
		$j("#fgsDiv").html(html);
	});
}
function loadGroupsByCid() {
	$j.get(_params.cUrl, {gid:_params.gid, cid:_params.cid}, function (html) {
		$j("#cidgsDiv").html(html);
	});
}
function loadTask(t) {
	var taskType = t;
	
	if(taskType == "j" || taskType == "c"){
		if(_params.mp){//login
			$j.get(_params.tUrl, {gid:_params.gid, t:taskType, mp:_params.mp, lot:_params.mpType, lang:_params.lang}, function (html) {
				$j("#cjqDIV").html(html);
				loadTaskCallBack(taskType);
			});
		}else{
			var gUrl = __webContextPath + "/gid-" + _params.gid + "&t=j";//空间群首页，t=j
			if(needOpenChat()){
				gUrl = __webContextPath + "/gid-" + _params.gid + "&t=j&wehChatFlag=1#web_chat";//空间群首页，t=j
			}
			var encodeUrl = $j.base64Encode(gUrl);
			window.location.href = __webContextPath + "/u/login_show.do?&n=n&jurl=" + encodeUrl;//登录，jurl=encodeUrl
		}
	}else if(taskType == "q"){
		$j.get(_params.cjUrl, {gid:_params.gid, t:taskType, mp:_params.mp, lot:_params.mpType, lang:_params.lang}, function (html) {
			$j("#cjqDIV").html(html);
			loadTaskCallBack(taskType);
		});
	}
	
	if(_params.mRole>=0 && !_params.ceo_mem_task_hide_session){//member
		$j.get(_params.tUrl, {gid:_params.gid, t:"task"}, function (html) {
			$j("#taskDIV").html(html);
			if(t==_params.task_join_group){
				$j("#task_title").html(_tips.addedSucces);
			}else{
				$j("#task_title").html(_tips.groupsSetupWizard);
			}
			$j("#taskDIV_task").show();
			loadTaskCallBack(taskType);
		});
	}
}
function loadTaskCallBack(t) {
	if (t == _params.task_join_group) {
		excJoinGTask(t);
	} else {
		if (t == _params.task_create_group) {
			excCreateGTask(t);
		} else {
			if (t == _params.task_quit_group) {
				excQuitGTask(t);
			}
		}
	}
}
function excQuitGTask() {
	if (_params.mRole < 0) {
		$j("#taskDIV_create").show();
		$j("#task_quitGroup").show();
	} else {
		//loadTask();
	}
}

function excCreateGTask() {
	if(_params.mpType==1 || _params.mpType==2){
		if (navigator.appName == "Microsoft Internet Explorer" && _params.mp && _params.mpType == 1) {//ie&&登录&&msn帐号
			$j("#taskDIV_create").show();
			$j("#taskDIV_create_step_1").show();
		} else {
			$j("#taskDIV_create").show();
			$j("#taskDIV_create_step_2").show();
		}
	}
}
function ceoMemTaskHideInSession() {
	$j("#taskDIV_task").hide();
	$j.post(_params.tHideInSessionUrl, {gid:_params.gid}, function (html) {
	});
}
function ceoMemTaskHideForever() {
	$j("#taskDIV_task").hide();
	$j.post(_params.tHideForeverUrl, {gid:_params.gid}, function (html) {
	});
}
function joinG_SkipToStep2(gid, mp, lot) {
	_commJs.quickJoinR(gid, mp, lot);
	$j("#taskDIV_create_step_1").hide();
	$j("#taskDIV_create_step_2").show();
}
function joinG_over() {
	//window.location.href = __webContextPath+"/gid-" + _params.gid;
	if(needOpenChat()){
		window.location.href=__webContextPath+"/gid-"+_params.gid+"&wehChatFlag=1#web_chat";
	}else{
		window.location.href=__webContextPath+"/gid-"+_params.gid;
	}
}

function joinG_SuccAndLoadResult() {
	//window.location.href = __webContextPath+"/gid-" + _params.gid;
	if(needOpenChat()){
		window.location.href=__webContextPath+"/gid-"+_params.gid+"&wehChatFlag=1&t=j#web_chat";
	}else{
		window.location.href=__webContextPath+"/gid-"+_params.gid+"&t=j";
	}
}

function joinG_SkipToStep3() {
	$j("#taskDIV_create_step_1").hide();
	$j("#taskDIV_create_step_2").hide();
	$j("#taskDIV_create_step_3").show();
}
function joinG_SkipToFAQ() {
	window.location.href = "http://faq.imgroups.net/";
}
function copy_clip(meintext) {
	var a = _params.coppySuccessTip;
	if (window.clipboardData) {
		window.clipboardData.setData("Text", meintext);
		alert(a + meintext);
	} else {
		window.prompt(_params.coppyUnSuccessTip, meintext);
	}
}
$j().ready(function () {
	setTimeout("init()", 10);
});

function webChatIframOnload(){
	$j('#loadingDiv').hide();
	if (window.attachEvent)
	{
		$j("#web_chat_containt").html(chatCont).slideDown();
	}else{
		$j("#web_chat_containt").html(chatCont).show();
	}
}
function bindWebChatBtn(){
	if(_params.mRole>=0){//group member
		$j("#web_chat_btn_open_desc").html(_tips.chatOpen);
		$j("#web_chat_btn_open").unbind("click").bind("click", function(){
			$j("#loadingDiv").show();
			try{
				var chatCont = '<iframe src="'+_params.webchatUrl + '&t=' + (new Date()).getTime() +'" id = "webchat_iframe_id" name = "webchat_iframe" onload="$j(\'#loadingDiv\').hide();" scroll="no" width="520px" height="475px" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>';
				if (window.attachEvent)
				{
					$j("#web_chat_containt").html(chatCont).slideDown();
				}else{
					$j("#web_chat_containt").html(chatCont).show();
				}
				//showWebChatLayer();
				__webchat_open_flag = 1;
				$j("#web_chat_btn_open").hide();
				$j("#web_chat_contr_DIV").hide();
				resetWebChatBtn();
			}catch(err){
				$j("#loadingDiv").hide();
				$j("#web_chat_btn_open").show();
				$j("#web_chat_contr_DIV").show();
			}
		});
	}else{
		$j("#web_chat_btn_open_desc").html(_tips.chatJoinOpen);
		$j("#web_chat_btn_open").unbind("click").bind("click", function(){
			__requestJoinAndChat = 1;
			loadTask('j');
		});
	}
	
	$j("#web_chat_btn_close").unbind("click").bind("click", function(){
		$j("#loadingDiv").hide();
		$j("#web_chat_containt").slideUp('normal',function(){
			$j("#webchat_iframe_id").attr('src','');
			__webchat_open_flag = 0;
			$j("#web_chat_btn_close").hide();
			$j("#web_chat_contr_DIV").show();
			//closeLayer();
			resetWebChatBtn();
		});
		
		//closeLayer();
		resetWebChatBtn();
	}).hide();
}

function resetWebChatBtn(){
	if(__webchat_open_flag==0){//init
		$j("#web_chat_btn_open").show();
	}else{
		$j("#web_chat_btn_close").hide();
	}
}

function closeChat(){
	//closeLayer();
	$j("#web_chat_btn_close").click();
}

function excJoinGTask(){
	if(_params.mRole>=0){
		excJoinGTaskOld();
	}else{
		if(_params.gStatus==0){//pub-success 
			joinGroupExc_Pub();
		}else if(_params.gStatus==1){//avil
			showVerification();
		}else if(_params.gStatus==3){//ans
			showQuestion();
		}
	}	
}

function showVerification(){
	showLayer('joinGroup_verify');
}

function showVerificationSucc(){
	showLayer('joinGroup_verify_succ');
}

function showQuestion(){
	showLayer('joinGroup_question');
}

function joinGroupExc_Pub(){
	$j.post(_params.joinGroupExcUrl, {gid:_params.gid}, function (jsonObject) {
		if(jsonObject){
			if(jsonObject.rs==1){
				joinG_SuccAndLoadResult();
			}else{
				alert(_tips.sysBusy);
			}
		}
	},"json");
}

function joinGroupExc_Question(){
	var ans = $j("#joinGroup_success_answer").val();
	if(ans){
		$j("#joinGroup_question_btn").removeClass().addClass("btn_gray").unbind("click");
		$j.post(_params.joinGroupExcUrl, {gid:_params.gid, answer:ans}, function (jsonObject) {
			if(jsonObject){
				if(jsonObject.rs==1){
					joinG_SuccAndLoadResult();
				}else{
					$j("#joinGroup_question_btn").removeClass().addClass("btn").unbind("click").bind(
						"click",joinGroupExc_Question
					);
					if(jsonObject.rs==-1){
						$j("#joinGroup_answer_tip").show();
						$j("#joinGroup_success_answer").unbind("focus").bind("focus", function(){
							$j("#joinGroup_answer_tip").hide();
						});
					}else{
						alert(_tips.sysBusy);
					}
				}
			}
		},"json");
	}else{
		$j("#joinGroup_answer_tip").show();
		$j("#joinGroup_success_answer").unbind("focus").bind("focus", function(){
			$j("#joinGroup_answer_tip").hide();
		});
	}
}

function joinGroupExc_verify(){
	var msg = $j("#joinGroup_verify_msg").val();
	if(msg && _commJs.strLength(msg)<250){
		$j.post(_params.joinGroupExcUrl, {gid:_params.gid, applySmg:msg}, function (jsonObject) {
			if(jsonObject){
				if(jsonObject.rs==1){
					showVerificationSucc();
				}
			}
		},"json");
	}else{
		$j("#joinGroup_verify_tip").show();
		$j("#joinGroup_verify_msg").unbind("focus").bind("focus", function(){
			$j("#joinGroup_verify_tip").hide();
		});
	}
	
}

function showLayerOld(divId){
	var div = $j('#'+divId);
	$j.blockUI({
		//theme:     true, 
		//draggable: true,
		//title:    'drag me', 
		message: div,
		css: {
			top:  ($j(window).height() - div.height()) /2 + 'px', 
		    left: ($j(window).width() - div.width()) /2 + 'px', 
		    width: div.width() + 'px',
		    cursor: 'default',
		   
			border: 'none', 
			padding: '15px', 
			backgroundColor: '#000', 
			'-webkit-border-radius': '10px', 
			'-moz-border-radius': '10px', 
			//opacity: .9, 
			color: '#fff' 
		}
	}); 
}

function showLayer(divId){
	var div = $j('#'+divId);
	$j.blockUI({
		//theme:     true, 
		//draggable: true,
		//title:    'drag me', 
		message: div,
		css: {
			top:  ($j(window).height() - div.height()) /2 + 'px', 
		    left: ($j(window).width() - div.width()) /2 + 'px', 
		    width: div.width() + 'px',
		    cursor: 'default',
		   
			border: 'none', 
			//padding: '15px', 
			//backgroundColor: '#000', 
			'-webkit-border-radius': '10px', 
			'-moz-border-radius': '10px', 
			//opacity: .9, 
			color: '#333' 
		}
	}); 
}

function showWebChatLayer(){
	var div = '<iframe src="'+_params.webchatUrl+'" id = "webchat_iframe_id" name = "webchat_iframe" onload="$j(\'#loadingDiv\').hide();" width="520" height="473" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>';

	$j.blockUI({
		//theme:     true, 
		//draggable: true,
		//title:    'drag me', 
		message: div,
		css: {
			top:  ($j(window).height() - 473) /2 + 'px', 
		    left: ($j(window).width() - 520) /2 + 'px', 
		    width: 520 + 'px',
		    cursor: 'default',
		   
			border: 'none', 
			//padding: '15px', 
			//backgroundColor: '#000', 
			'-webkit-border-radius': '10px', 
			'-moz-border-radius': '10px', 
			//opacity: .9, 
			color: '#333' 
		}
	}); 
}
function closeLayer(){
	$j.unblockUI();
}

$j().ready(function () {
	$j("#join_group_loading").ajaxStart(function () {
		$j(this).show();
	});
	$j("#join_group_loading").ajaxStop(function () {
		$j(this).hide();
	});
});

