$(document).ready(function() {
	//initSpotlight();//已经改用html模板处理了。
	
    $("#HowDiningSecretaryWorks").click(function(){
    	$.fn.colorbox({
    		opacity:"0.3",
			speed:0,
			width:"450px",
			inline:true,
			href:"#HowDiningSecretaryWorksTips"
    	});
    });

    $("#MemberRewards").click(function(){
    	$.fn.colorbox({
    		opacity:"0.3",
			speed:0,
			width:"450px",
			inline:true,
			href:"#MemberRewardsTips"
    	});
    });
    
    initBlog();
    
    var online=$("#isHelpOnline").val();
    if(online == "true"){
    	$("#live_help").addClass("liveHelp");
    	$("#live_help").removeClass("liveHelpOffline");
    }else{
    	$("#live_help").removeClass("liveHelp");
    	$("#live_help").addClass("liveHelpOffline");
    }
    $("#clickToFeedback").click(showFeedbackDlg)
    $("#feedbackDlgBtn").click(sendFeedback)
});

function sendFeedback(){
	var emailFrom = $("#emailFrom").val()
	var emailTo = $("#emailTo").val()
	var emailContent = $("#emailContent").val()
	if (emailFrom == "") {
		alertMsg("Please enter a valid email address.")
		return
	}
	if (!isEmail(emailFrom)) {
		alertMsg("The email address you have entered is not valid.")
		return
	}
	function callback(json) {
		if (!json.success) {
			alert("Fail:" + json.resultMsg);
			return;
		}
		alertMsg("Send Success!")
	}
	var title = "EnWeb Guest Feedback "
	var content = emailContent + "<br/> Email From：" + emailFrom 
	var nickName = "Guest"
	if($.trim(login_user_id) != ""){
		title = "EnWeb User :" + login_user_truename + " Feedback"
		nickName = login_user_name
		content += ("<br/> userId = " + login_user_id)
	}
	$.getJSON("/ajax/Rest.sendRestToFriend", {
		from : emailFrom,
		to : emailTo,
		content:content, 
		nickName:nickName,
		title:title
		}, callback);
	// 发送
	$.fn.colorbox.close();
}

function showFeedbackDlg(){
	if($.trim(login_user_id) == ""){
		$("#emailFrom").val("")	
	}else{
		$("#emailFrom").val(login_user_email)
	}
	$("#emailContent").val("")
	$("#emailTo").val("shau-ru@57575777.com")   
	$.fn.colorbox({
		opacity:"0.3",
		speed:0,
		width:"400px",
		inline:true,
		href:"#FeedbackDlg"
	});
}

function initBlog(){
	function callback(json){
		if(!json.success){
			if(json.resultMsg != undefined){
				alert("Fail[IX001]:"+ json.resultMsg);
			}
			return;
		}
		var recent=json.gsonMap.recentPosts;

		$("#recentPosts").html("");
		$("#recentPosts").append($(recent));
		//只显示前三个
		$("#recentPosts").children("li").each(function(i){
			var  a=$(this).children("a");
			var text=a.text();
			if(text.length > 22){
				text=text.substring(0,22)+"...";
			}
			a.text(text);
			if(i >= 3){
				$(this).hide();
			}
		});
	}
	$.get("/ajax/HomePage.getRecentPosts",{},callback);
}

//登录后初始化界面。
function callBackLogin() {
	if($.trim(login_user_id) == ""){
		$("#welcom_not").show();
		$("#welcom_logined").hide();
		return;
	}else{
		$("#welcom_not").hide();
		$("#welcom_logined").show();
	}
	
	function cb(json){
		if(!json.success){
			alert("Fail[IX003]:"+json.resultMsg);
			return;
		}
		$("#welcomeGuest").html("Welcome, "+login_user_name+"!");
		//$("#welcomeGuest").html("Welcome, Guest!")
		
		var url="/html/UserCenter/mainpage?userId="+login_user_id

		var ul=$("#userInfo");

		var span=null;
		
		span=$("<span></span>").text("Reservations ("+json.gsonMap.upcomingReserv+")");
		ul.append($("<li></li>").append(span));

		span=$("<span></span>").text("Points ("+json.gsonMap.point+")").attr({href:url});
		ul.append($("<li></li>").append(span));
		
		span=$("<span></span>").text("Favorites ("+json.gsonMap.favorite+")").attr({href:url});
		ul.append($("<li></li>").append(span));
		
		span=$("<span></span>").text("Reviews ("+json.gsonMap.review+")").attr({href:url});
		ul.append($("<li></li>").append(span));
		
		var a=$("<a></a>").text("Go to my User Center ").attr({href:url});
		ul.append($("<li></li>").append(a));
		

	};
	$.getJSON("/ajax/UserCenter.getHomePageInfo",{userId:login_user_id},cb);
}


