$(document).ready(function() {
	if ($("#userId").size() == 0) {
		setUserLoginStatus();
	} else {
		var userId = $("#userId").val();
		setUserStatus(userId);
	}	
});


function setUserLoginStatus() {
    SessionAccessAjaxBean.getUserData(function(data) {
    	var userData = eval("(" + data + ")");
    	
    	_setUserStatus(userData);
    	
    });
}

function setUserStatus(userId) {
	SessionAccessAjaxBean.getUserDataByUserId(userId, function(data) {
		var userData = eval("(" + data + ")");
		
		_setUserStatus(userData);
	});
}

function _setUserStatus(userData) {
	var logined = userData.logined;
	var myhomeUrl = userData.myhomeUrl;
	var userHomeUrl = userData.userHomeUrl;
	var message = userData.message;
	var existsNewMessage = userData.existsNewMessage;
//	var newMessageSize = userData.newMessageSize;
	
	if (logined) {
		$(".logined").attr("style", "display:inline;");
		$(".unlogined").attr("style", "display:none;");
		$(".myhomeUrl").attr("href", myhomeUrl);
		$(".userHomeUrl").attr("href", userHomeUrl);
		$(".userId").text(userData.userId);
		$(".userName").text(userData.userName);
		
		if (existsNewMessage) {
			$(".msgStatus").attr("style", "display:inline;");
		} else {
			$(".msgStatus").attr("style", "display:none;");
		}
		$(".newMessageSize").text(userData.newMessageSize);
	} else {
		$(".logined").attr("style", "display:none;");
		$(".unlogined").attr("style", "display:inline;");
	}
	
	if (userData.memberSelf) {
   		$("#"+memberSelfId).attr("style", "display:inline;");
  		$("."+memberSelfId).attr("style", "display:inline;");
	}
}

function isLogined() {
	var ret = false;
	SessionAccessAjaxBean.isLogined(function(logined) {
		ret = logined;
	});
	return ret;
}

function loginAndDoAjax(callback) {
	if (isLogined()) {
		callback(null);
	} else {
		alert("not logined");
		// show login box
		
		if(callback)
			tb_setcallback(callback);
		else
			tb_setcallback(function(msg){
				location.reload();
			});
		tb_show('','http://localhost:8080/shopping/login/?width=600&modal=true',false);	//TODO
		alert("after login");
	}
}

function loginAndDo(callback) {
	SessionAccessAjaxBean.isLogined(function(logined) {
		try {
			if (logined) {
				callback(null);
			} else {
				if(typeof contextPath == "undefined") {
					contextPath = "";
				}
				if (typeof loginUrl === "undefined") {
					loginUrl = contextPath + "/login/";
				}
				location.href = loginUrl;	//init this var before call me
				//location.reload();	//GOTO LOGIN PAGE
			}
		} catch (e) {
			alert(e.message);
		}
	});
	
//	if (isLogined()) {
//		callback(null);
//	} else {
//	}
}

////////////////////////////////////////
//follow user
var followedId = "followed";
var unFollowedId = "notFollowed";
var memberSelfId = "memberSelf";
function followStatus(userId) {
	//isFollowed
	UserActionBean.followStatus(userId, function(status) {
    	//var ok = eval("(" + data + ")");
    	
    	if (status == 1) {
    		$("#"+followedId).attr("style", "display:inline;");
    		$("#"+unFollowedId).attr("style", "display:none;");
    	} else if (status == 0) {
    		$("#"+followedId).attr("style", "display:none;");
    		$("#"+unFollowedId).attr("style", "display:inline;");
    	} else {	//member himself
    		$("#"+followedId).attr("style", "display:none;");
    		$("#"+unFollowedId).attr("style", "display:none;");
    		$("#"+memberSelfId).attr("style", "display:inline;");
    		$("."+memberSelfId).attr("style", "display:inline;");
    	}
    });
}

//follow a user
// <div id=followedId>when followed: <a href="#" onclick="unfollow('xxxx');">Cancel</a></div>
// <div id=unFollowedId>when not followed: <a href="#" onclick="follow('xxxx');">Follow</a></div>
function follow(userId) {
	loginAndDo(function(msg){
		//call this if logined or login success
		UserActionBean.follow(userId, function(ok) {
	    	//var ok = eval("(" + data + ")");
	    	   	
	    	if (ok) {
	    		$("#"+followedId).attr("style", "display:inline;");
    			$("#"+unFollowedId).attr("style", "display:none;");
	    	} else {
	    		$("#"+followedId).attr("style", "display:none;");
    			$("#"+unFollowedId).attr("style", "display:inline;");
	    	}
	    });
    });
}

//unfollow a user
// <div id=followedId>when followed: <a href="#" onclick="unfollow('xxxx');">Cancel</a></div>
// <div id=unFollowedId>when not followed: <a href="#" onclick="follow('xxxx');">Follow</a></div>
function unfollow(userId) {
	loginAndDo(function(msg){
		//call this if logined or login success
		UserActionBean.unfollow(userId, function(ok) {
	    	//var ok = eval("(" + data + ")");
	    	
	    	if (ok) {
	    		$("#"+followedId).attr("style", "display:none;");
    			$("#"+unFollowedId).attr("style", "display:inline;");
	    	} else {
	    		$("#"+followedId).attr("style", "display:inline;");
    			$("#"+unFollowedId).attr("style", "display:none;");
	    	}
		});
	});
}


////////////////////////////////////////
//clip file
var clipedId = "cliped";
var unClipedId = "notCliped";
function clipStatus(filesId) {
	UserActionBean.isCliped(filesId, function(ok) {
		//var ok = eval("(" + data + ")");
		if (ok) {
			$("#"+clipedId).attr("style", "display:inline;");
	    	$("#"+unClipedId).attr("style", "display:none;");
		} else {
			$("#"+clipedId).attr("style", "display:none;");
	    	$("#"+unClipedId).attr("style", "display:inline;");
		}
	});

}

//clip a user
// <div id=clipedId>when cliped: <a href="#" onclick="unclip('xxxx');">Cancel</a></div>
// <div id=unClipedId>when not cliped: <a href="#" onclick="clip('xxxx');">Clip</a></div>
function clip(filesId, projectName) {
	loginAndDo(function(msg){
		UserActionBean.clip(filesId, projectName, function(ok) {
	    	//var ok = eval("(" + data + ")");
	    	
	    	if (ok) {
	    		$("#"+clipedId).attr("style", "display:inline;");
    			$("#"+unClipedId).attr("style", "display:none;");
	    	} else {
	    		$("#"+clipedId).attr("style", "display:none;");
    			$("#"+unClipedId).attr("style", "display:inline;");
	    	}
	    });
	});
}

//unclip a user
// <div id=clipedId>when cliped: <a href="#" onclick="unclip('xxxx');">Cancel</a></div>
// <div id=unClipedId>when not cliped: <a href="#" onclick="clip('xxxx');">Clip</a></div>
function unclip(filesId) {
	loginAndDo(function(msg){
		UserActionBean.unclip(filesId, function(ok) {
	    	//var ok = eval("(" + data + ")");
	    	
	    	if (ok) {
	    		$("#"+clipedId).attr("style", "display:none;");
    			$("#"+unClipedId).attr("style", "display:inline;");
	    	} else {
	    		$("#"+clipedId).attr("style", "display:inline;");
    			$("#"+unClipedId).attr("style", "display:none;");
	    	}
	    });
	});
}

var addMemberActionType = "actionType";
var addMemberActionBtnId = "actionMe";
var addMemberActionFormId = "actionMeForm";
var addMemberActionContentId = "actionMeContent";
function showAddMemberAction() {
	loginAndDo(function(msg){
		$("#"+addMemberActionBtnId).attr("href", "#TB_inline?width=320&height=220&inlineId=" + addMemberActionFormId);
		$("#"+addMemberActionBtnId).click();	//TODO
//		$("#"+addMemberActionFormId).attr("style", "display:inline;");
		//call this if logined or login success
		//UserActionBean.addMemberAction(userId, function(ok) {
	    	//var ok = eval("(" + data + ")");
	    //});
    });
}

function addMemberAction() {
	var actionType = $("#" + addMemberActionType).val();
	var content = $("#" + addMemberActionContentId).val();
	UserActionBean.addMemberAction(actionType, content, function(ok) {	//TODO
    	if (ok) {
			alert("Added Ok!");
			tb_remove();
			location.href = location.href + "?refresh";	//TODO
    	} else {
    		alert("Added failed.");
    	}
    });
}

var sendmsgBtnId = "sendmsg";
var sendmsgFormId = "sendmsgForm";
var sendmsgContentId = "sendmsgContent";
var sendmsgReceiverId = "sendmsgReceiver";
function showSendmsg(userId) {
	loginAndDo(function(msg){
		$("#"+sendmsgReceiverId).val(userId);
		$("#"+sendmsgBtnId).attr("href", "#TB_inline?width=320&height=220&inlineId=" + sendmsgFormId);
		$("#"+sendmsgBtnId).click();	//TODO
    });
}

function sendmsg() {
	var content = $("#" + sendmsgContentId).val();
	var userId =  $("#" + sendmsgReceiverId).val();
	UserActionBean.sendmsg(userId, content, function(ok) {	//TODO
    	if (ok) {
			alert("Send Message Ok!");
			tb_remove();
    	} else {
    		alert("Send Message failed.");
    	}
    });
}

function deleteFilesComment(filesCommentId) {
	loginAndDo(function(msg){
		UserActionBean.deleteFilesComment(filesCommentId, function(ok) {	//TODO
	    	if (ok) {
				alert("Delete Comment Ok!");
				tb_remove();
				location.href = location.href + "?refresh";	//TODO
	    	} else {
	    		alert("Delete Comment failed.");
	    	}
	    });
    });
}

function queryProjectUserStatus(projectId) {
	//"0":not logined. "1":not a member. "2":project manager. "3":project member
	SessionAccessAjaxBean.getProjectUserStatus(projectId, function(status) {
		if ("0" == status || "1" == status) {//not logined or not a member
			$(".notMember").attr("style", "display:inline;");
			$(".member").attr("style", "display:none;");
		} else {
			$(".member").attr("style", "display:inline;");
			$(".notMember").attr("style", "display:none;");
		}
	});
}
