<?xml version="1.0" encoding="utf-8"?>
<Module>
<ModulePrefs title="Diet and Nutrition Tip of The Day" 
title_url="http://www.nutritionpedia.com/" 
author="NutritionSystems" 
author_email="tips@nutritionpedia.com" 
author_location="Atlanta, GA (USA)" 
author_affiliation="Company" 
directory_title="Diet &amp; Nutrition Tip of the Day (Test)" 
description="Receive a daily tip on health, wellness or nutrition" 
thumbnail="http://www.nutritionpedia.com/gadget/logo_thumb.gif" 
screenshot="http://www.nutritionpedia.com/gadget/screenshot_tipOfDay.gif" 
scrolling="true" 
height="180" 
width="315">
<Require feature="opensocial-0.7"/>
<Require feature="views" />  
</ModulePrefs>
<Content type="html"><![CDATA[
<script type="text/javascript" src="http://www.nutritionpedia.com/gadget/dietTipOfDay.js"></script>
<script type="text/javascript">
var ratings_max = 5;
var post_rating = 0;
var ratings_text_wait = "This tip has already been rated by You.";
var votes;
var rating;
var rating_score=3;
var insert_half=4;
var user;
var change;
var rating_on="http://www.nutritionpedia.com/gadget/opensocial/img/rating_on.gif";
var rating_half="http://www.nutritionpedia.com/gadget/opensocial/img/rating_half.gif";
var rating_over="http://www.nutritionpedia.com/gadget/opensocial/img/rating_over.gif";
var rating_off="http://www.nutritionpedia.com/gadget/opensocial/img/rating_off.gif";

function requestMe() {
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonRequest('OWNER'), 'owner');
  req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
  req.send(handleRequestMe);
};

function handleRequestMe(data) {
	if(!data.hadError()){
	    var viewer = data.get("viewer").getData();
	    var owner=data.get("owner").getData();	
		var id_owner=owner.getId();
		var id_viewer=viewer.getId();
		if(id_viewer && (id_viewer!=null)){
			user=id_viewer;
			change=true;
			//if(user==id_owner) {
			//	var show_tip = document.getElementById('show_tip');
			//	show_tip.style.display="block";
			//}
			//else {
			//	var show_tip = document.getElementById('show_tip');
			//	show_tip.style.display="none";
			//}
		}
		
	}
	else {
		change=false;
		//var show_tip = document.getElementById('show_tip');
		//show_tip.style.display="none";
	}
}

function show_comments(id){
	var action='get_comments';
	gadgets.io.makeRequest('http://www.nutritionpedia.com/gadget/opensocial/diet_tips_rate.php',
    	function (response) {
    					var comments=response.data;
    					if(comments!=0){
							document.getElementById('user_comments').innerHTML=comments;
						}						
				 	},
		 {METHOD:"POST",CONTENT_TYPE:'html',POST_DATA:'tipID='+id+'&action='+action}); 
}

function get_rating(id) {
var tipID=id;
gadgets.io.makeRequest('http://www.nutritionpedia.com/gadget/opensocial/diet_tips_rate.php',
     function (response) {
         	var votes_rate=response.data.split("/");
			votes=votes_rate[0];
			rating=votes_rate[1];
			if(rating!=0){
			    x=parseInt(rating);
			    y=rating-x;
			    if(!y){
				insert_half=0;
			    }
			    for(i = 1; i<=ratings_max; i++) {
				if(i<=x) {
					document.images['rating_'+i].src =rating_on;
					rating_score=i;
				} else if(y) {
					document.images['rating_'+i].src =rating_half;
					y=0;
					insert_half=i;
				} else {
					document.images['rating_'+i].src =rating_off;
				}
			    }
			}
     },
     {METHOD:"POST",CONTENT_TYPE:'html',POST_DATA:'tipID='+id});   
}

function current_rating(rating) {
		post_rating = rating;
		for(i=1; i<=rating; i++) {
			document.images['rating_' + i].src =rating_over;
		}
}
 
 function ratings_off() {
		for(i = 1; i<=ratings_max; i++) {
			if(i<=rating_score) {
				document.images['rating_'+i].src =rating_on;
			} else if(i == insert_half) {
				document.images['rating_'+i].src = rating_half;
			} else {
				document.images['rating_'+i].src =rating_off;
			}
		}
}

function rate_post(){
if(change){
	gadgets.io.makeRequest('http://www.nutritionpedia.com/gadget/opensocial/diet_tips_rate.php',
	     function (response) {
	       		var votes_rate=response.data.split("/");
				votes=votes_rate[0];
				rating=votes_rate[1];
				if(votes>0) {
					x=parseInt(rating);
					y=rating-x;
					if(!y){
						insert_half=0;
					}
					for(i = 1; i<=ratings_max; i++) {
						if(i<=x) {
							document.images['rating_'+i].src =rating_on;
							rating_score=i;
						} else if(y) {
							document.images['rating_'+i].src =rating_half;
							y=0;
							insert_half=i;
						} else {
							document.images['rating_'+i].src =rating_off;
						}
					}
				} else {
					alert(ratings_text_wait); 	
				}
	     },
	     {METHOD:"POST",CONTENT_TYPE:'html',POST_DATA:'tipID='+tipsID[number]+'&rating='+post_rating+'&user='+user});
}
else{
	alert("You are an anonymous viewer!");
}
}

</script>
<table border="0" width="100%">
	<script type="text/javascript">
	requestMe();
	</script>
	<tr>
		<td colspan="2">
			<table width="100%">
				<tr> 
					<td align="left" nowrap>
						<img id="rating_1" src="http://www.nutritionpedia.com/gadget/opensocial/img/rating_on.gif" alt="1 Star"  onmouseover="current_rating(1);" onmouseout="ratings_off();" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />
						<img id="rating_2" src="http://www.nutritionpedia.com/gadget/opensocial/img/rating_on.gif" alt="2 Stars" onmouseover="current_rating(2);" onmouseout="ratings_off();" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />
						<img id="rating_3" src="http://www.nutritionpedia.com/gadget/opensocial/img/rating_on.gif" alt="3 Stars" onmouseover="current_rating(3);" onmouseout="ratings_off();" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />
						<img id="rating_4" src="http://www.nutritionpedia.com/gadget/opensocial/img/rating_half.gif" alt="4 Stars" onmouseover="current_rating(4);" onmouseout="ratings_off();" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />
						<img id="rating_5" src="http://www.nutritionpedia.com/gadget/opensocial/img/rating_off.gif" alt="5 Stars" onmouseover="current_rating(5);" onmouseout="ratings_off();" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" /> 
						<font size='-1'>  Member Rating</font>
					</td>
					<td align="right"><font size='-1'>Rate This Tip</font></td>
					<td align="right"><b><font size='-1' id="num"></font></b></td>
				</tr>
			</table>
		</td>
		<td rowspan="4">&nbsp;</td>
		<td rowspan="4" valign="top" nowrap>
				<div id="message"> </div>
		</td>
	</tr>
	<tr>
		<td aling="center" valign="top">
			<a href="http://www.nutritionpedia.com" title="NutritionPedia" target="_blank">
			<img src="http://www.nutritionpedia.com/gadget/logo.gif" border="0"></a>
		</td>
		<td align='justify'>
			<font size='-1'><script>genTip(); get_rating(tipsID[number]);</script></font>
		</td>
	</tr>
	<tr>
		<td colspan="2" align="center">
			<div align="center" id="my_ads" display='none'>
				<script type="text/javascript">
					var views = gadgets.views.getSupportedViews();  
				    var currentview = gadgets.views.getCurrentView();  
					if (currentview.getName()=='canvas'){
						document.getElementById('my_ads').style.display='block';
					}
					else{
						document.getElementById('my_ads').style.display='none'; 
					}		
				</script>
				
				<script type="text/javascript">
				<!--
				google_ad_client = "pub-9651387157154354";
				/* open-1 */
				google_ad_slot = "6750497771";
				google_ad_width = 234;
				google_ad_height = 60;
				//-->
				</script>
				
				<script type="text/javascript"
				src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
				</script>
				
			</div>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<div id="user_comments"></div>
		</td>
	</tr>
</table>
<script>
show_comments(tipsID[number]);

var params = {};
params[opensocial.DataRequest.PeopleRequestFields.FILTER] = opensocial.DataRequest.FilterType.HAS_APP;
params[opensocial.DataRequest.PeopleRequestFields.SORT_ORDER] = opensocial.DataRequest.SortOrder.NAME;
params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.PROFILE_URL];

var req = opensocial.newDataRequest();
req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.OWNER_FRIENDS, params), 'ownerFriends');
req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.OWNER),"owner");
req.send(fetchPeopleHandler);

function fetchPeopleHandler(resp) {
  if (!resp.hadError()) {
     var owner = resp.get('owner').getData();
     var ownerFriends = resp.get('ownerFriends').getData(); 
     if (ownerFriends.getTotalSize()!=0){
	  	 html = '<table><tr><td colspan="2" align="center" nowrap><font size="-1">Friends Using Diet Tips:</font></td></tr>'; 
	  	 var i=1;
	     ownerFriends.each( 
		     function(person) {
		     	if(i==1){
		     		html +='<tr>';
		     	}
			    var thumb = person.getField(opensocial.Person.Field.THUMBNAIL_URL);
			    var profile = person.getField(opensocial.Person.Field.PROFILE_URL);
		      	html += '<td valign="middle" align="center" nowrap><a href="' + profile + '" target="_top"><img border="0" src="' + thumb + '"/></a></br></br>';
		      	html +='<a href="' + profile + '" target="_top"><font size="-1">' + person.getDisplayName() + '</font></a></td>';
		      
				if(i==2){
					html +='</tr>';
					i=1;
				} else {
					i++;	
				}     
		      }
	     );
	     if (ownerFriends.getTotalSize()%2){
	     	html +='<td>&nbsp;</td></tr>';
	     }
	     document.getElementById("message").innerHTML = html+"</table>";
     }
  }
};
</script>
]]></Content>
</Module>
