// jquery
// hearst session script

var ra_raws = {
	variables : {
		member_id : null,
		cal_age : "?",
		cal_diff : "+/-",
		real_age : "??",
		news : null,
		goals : null,
		isActive : null,
		personalpod_data : null
	},
	
	rpc : {
		getPersonalPodData : function(member_id){
			var newproc = function(json){
				ra_raws.variables.personalpod_data = json.result;
				if (ra_raws.variables.personalpod_data.member_realage != null){
					ra_raws.variables.real_age = ra_raws.variables.personalpod_data.member_realage;
					ra_raws.variables.cal_age = ra_raws.variables.personalpod_data.member_calendarage;
					// get cal_diff string
					var cal_diff = parseFloat(ra_raws.variables.real_age - ra_raws.variables.cal_age).toFixed(1);
					if (cal_diff > 0){
						cal_diff = "+"+cal_diff;
					}
					ra_raws.variables.isActive = true;
					ra_raws.variables.cal_diff = cal_diff;
				} else {
					ra_raws.variables.cal_age = ra_raws.variables.personalpod_data.member_calendarage;
					ra_raws.variables.isActive = false;
				}
				ra_raws.variables.goals = ra_raws.variables.personalpod_data.goals;
				ra_raws.variables.news = ra_raws.variables.personalpod_data.recommendations;
			}
			var errproc = function(json){
			}
			ra_raws.call('{"jsonrpc": "2.0", "method": "getPersonalPodData", "params": ["'+member_id+'"], "id": 1}',newproc,errproc);
		},
		getNews : function(interest_ids,callback,errorCallback){
			// NOTE: interest_ids can be an array
			var newproc,errproc;
			if (!!!callback){
				newproc = function(json){
					ra_raws.variables.news = json.result;
				}
			} else {
				newproc = callback;
			}
			if (!!!errorCallback){
				errproc = function(json){
				}
			} else {
				errproc = errorCallback;
			}
			if (interest_ids == null){
				ra_raws.call('{"jsonrpc": "2.0", "method": "getNews", "params": [], "id": 1}',newproc,errprocc);
			} else {
				ra_raws.call('{"jsonrpc": "2.0", "method": "getNews", "params": ['+interest_ids+'], "id": 1}',newproc,errproc);
			}
		}
	},
	call : function(data,proc,err){
		var success = function(json){
				if (json.hasOwnProperty("error")) {
					console.error("JSON RPI ERROR["+json.error.code+"] "+json.error.message);
					err(json);
				} else if (json.hasOwnProperty("result")) {
					proc(json);
				} else {
					console.error("UNKNOWN RESULT");
				}
		}
		var testobj = {
			"url": "/raws/",
			"dataType": "json",
			"type": "POST",
			"data": data,
			"contentType": "application/json",
			"success": success
		}
		jQuery.ajax(testobj);	
	}
}


var ra_personalpod = {
	set_active : function(){
		var displayemail = "";
		if (mag_user.email.length > 23){
		 displayemail = mag_user.email.substr(0,23)+"...";
		} else {
		 displayemail = mag_user.email +"!";
		}
		$("#rr_personal_pod_hdr").css({"height":"133px"});
		$("[pod_field='first_name']").html("Hi, "+displayemail);
		$(".rr_personal_plan_header_l").hide();
		$(".rr_personal_plan_header_2").hide();
		$(".rr_personal_plan_whatsmyrealage").show();
		$("[pod_field='my_plan'] > .rr_pp_std_cont_active").show();
		$("[pod_field='my_plan'] > .rr_pp_std_cont_inactive").hide();
		$("[pod_field='my_plan']").show();
		$("[pod_field='my_recipebox']").show();
		$("#rr_personal_pod").show();
	},
	set_inactive : function(){
		var displayemail = "";
		if (mag_user.email.length > 23){
		 displayemail = mag_user.email.substr(0,23)+"...";
		} else {
		 displayemail = mag_user.email +"!";
		}		
		$("#rr_personal_pod_hdr").css({"height":"133px"});
		$("[pod_field='first_name']").html("Hi, "+displayemail);
		$(".rr_personal_plan_header_l").show();
		$(".rr_personal_plan_header_2").hide();
		$(".rr_personal_plan_whatsmyrealage").show();
		$("[pod_field='my_plan'] > .rr_pp_std_cont_inactive").show();
		$("[pod_field='my_plan'] > .rr_pp_std_cont_active").hide();
		$("[pod_field='my_plan']").show();
		$("[pod_field='my_recipebox']").show();
		$("#rr_personal_pod").show();
	},
	set_notsignedin : function(){
		$("[pod_field='first_name']").html("<a class=\"pp_link_arrow\" href=\"http://www.realage.com/reg/regvar/st1.aspx?mod=LONGFORM\">Take the RealAge Test</a>");
		$(".rr_personal_plan_header_l").hide();
		$(".rr_personal_plan_header_2").show();
		$(".rr_personal_plan_whatsmyrealage").hide();
		$("[pod_field='my_plan']").hide();
		$("[pod_field='my_goals']").hide();
		$("[pod_field='my_recipebox']").hide();
		$("#rr_personal_pod_hdr").css({"height":"38px"});
		$("#rr_personal_pod").show();
	},
	listener : {
		email : null,
		isActive : null,
		personalPodData : null,
		hasGoals : false
	},
	init : function(){
		ra_raws.rpc.getPersonalPodData(mag_user.realage_member_id);
		ra_personalpod.set_inactive();
		ra_personalpod.listener.isActive = setInterval(function(){
			if(ra_raws.variables.isActive != null){
				$("[pod_field='cal_age']").html(ra_raws.variables.cal_age);
				if (ra_raws.variables.isActive == false){
					ra_personalpod.set_inactive();
				} else {
					ra_personalpod.set_active();
				}
				window.clearInterval(ra_personalpod.listener.isActive);
			}
		},500);
		ra_personalpod.listener.personalPodData = setInterval(function(){
			if(!!ra_raws.variables.personalpod_data){
				window.clearInterval(ra_personalpod.listener.personalPodData);
				$("[pod_field='cal_age']").html(ra_raws.variables.cal_age);
				$("[pod_field='cal_diff']").html(ra_raws.variables.cal_diff);
				$("[pod_field='cal_realage']").html(ra_raws.variables.real_age);
				$(".rr_personal_plan_whatsmyrealage").show();
				var newsArray = {};
				newsArray[1] = "Walk Your Way Fit";
				newsArray[2] = "Get Younger Skin in 9 Days";
				newsArray[3] = "Lose a Few Pounds";
				newsArray[4] = "Lose a Lot -- Even 100+ Pounds";
				newsArray[5] = "Lose 10 to 50 Pounds";
				newsArray[6] = "Feel Younger in 14 Days";
				newsArray[7] = "Stop Smoking -- For Real";
				newsArray[8] = "Flatten Your Belly with Dr. Oz's Trainer";
				newsArray[9] = "Grow Younger in 60 Days";
				var myhtml = "";
				for (var i in ra_raws.variables.goals) {
					myhtml += "<li><a href=\"http://www.realage.com/my-goals/"+ra_raws.variables.goals[i].goal_name+"\">"+newsArray[ra_raws.variables.goals[i].goal_id]+"</a></li>";
				}
				$("[pod_field='goal_list']").html(myhtml);
				myhtml = "";
				var goalslength = (ra_raws.variables.news.length > 3) ? 2 : ra_raws.variables.news.length;
				for (var i = 0; i < goalslength; i++) {
					myhtml += "<li><a href=\"http://www.realage.com/ralong/planner/Diet.aspx#RID"+ra_raws.variables.news[i].id+"\">"+ra_raws.variables.news[i].title+"</a></li>";
				}
				$("[pod_field='my_plan_ul']").html(myhtml);
				if (ra_raws.variables.goals.length > 0){
					$("[pod_field='my_goals'] > .rr_pp_std_cont_active").show();
					$("[pod_field='my_goals'] > .rr_pp_std_cont_inactive").hide()
					$("[pod_field='my_goals']").show();
				} else {
					$("[pod_field='my_goals'] > .rr_pp_std_cont_inactive").show();
					$("[pod_field='my_goals'] > .rr_pp_std_cont_active").hide()
					$("[pod_field='my_goals']").show();
				}
			}
		},500);
	}
}

ra_newsfromtheweb = {
	list : new Array(),
	drawNews : function(){
		$h.console.log("RAWS: calling getNews("+ra_newsfromtheweb.list+")");
		ra_raws.rpc.getNews(ra_newsfromtheweb.list,ra_newsfromtheweb.newsCallback);
	},
	newsCallback : function(json){
		$h.console.log("RAWS: getNews() returned: "+json.result);
		var myhtml = "";
		for (i in json.result){
			myhtml += "<li><a href=\""+json.result[i].url+"\" target=\"_blank\">"+json.result[i].title+"</a></li>";
		}
		$("[news_field='ul']").html(myhtml);
	}
}
