function initRequest()
{
	var req = opensocial.newDataRequest();
	var opt_params = { };
	opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = 
		[opensocial.Person.Field.CURRENT_LOCATION];
	opt_params[opensocial.DataRequest.PeopleRequestFields.MAX] = 100;	 

	req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.OWNER, opt_params), "owner");
	req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER, opt_params), "viewer");
	req.add(req.newFetchPeopleRequest(
		opensocial.DataRequest.Group.OWNER_FRIENDS, opt_params),"owner_friends");
	req.send(processInitRequest);
}

function processInitRequest(data)
{
	var owner = data.get("owner").getData();
	
	if ( owner != null )
	{
		owner_id = owner.getId();
		owner_name = owner.getDisplayName();
		owner_thumb = owner.getField(opensocial.Person.Field.THUMBNAIL_URL);
		owner_profile = owner.getField(opensocial.Person.Field.PROFILE_URL);
		owner_friends = data.get("owner_friends").getData(); 
		
		var viewer = data.get("viewer");
		
		if ( viewer != null )
		{
			viewer = viewer.getData();
			viewer_id = viewer.getId();
			hasApp = true;
			
			if ( owner_id == viewer_id )
				isOwner = true;	
		
			uid = getUid(owner_profile);
		
			if ( uid != 0 )
			{
				gadgets.io.makeRequest(requestUrl+"checkuser.php", processCheckUser,
					{'METHOD' : gadgets.io.MethodType.POST,'POST_DATA':"site="+site+"&uid="+uid});		
			}	
		}
		else
		{
			printAccountPage("","");
		}
	}
	//user is not logged in
	else
	{
		document.getElementById("user").innerHTML = "<br/><font size='14'>Please click <a target='_top' href='http://www.myspace.com'>here</a> to login to MySpace to use Restaurants.</font>";
	}
}

function processCheckUser(response) 
{
	if ( response.data != "" )
	{
		username = response.data;
		owner_friends.each(processFriends);
		
		all_friends_uids = friends_uids;
		
		if ( friends_uids != "" )
		{
			//get data to map friends' username and uid
			gadgets.io.makeRequest(requestUrl+"prepfriends.php", processPrepFriends,
				{'METHOD' : gadgets.io.MethodType.POST,'POST_DATA':"site="+site+"&uids="+friends_uids, 'CONTENT_TYPE':gadgets.io.ContentType.DOM});
		}
		else
			processLoadPage();
	}
	else
		printAccountPage("","");
}

function processFriends(person)
{
	if ( site == 'orkut' )
		var uid = getUid(person.getField(opensocial.Person.Field.PROFILE_URL));
	else
		var uid = person.getId();
	if ( uid != owner_id && uid != "198063728" )
		friends_uids += uid + "%2C";
	uid_thumb[uid] = person.getField(opensocial.Person.Field.THUMBNAIL_URL);
	uid_name[uid] = person.getDisplayName();	
}

function processPrepFriends(response)
{
	var xml = response.data;
	if ( xml && xml.getElementsByTagName("head")[0].firstChild != null ) 
	{	
		friends_uids = "";
		var uids = xml.getElementsByTagName("uid");
		var usernames = xml.getElementsByTagName("username");
		for (var i = 0; i < uids.length; i++ )
		{
			username_uid[usernames[i].firstChild.data] = uids[i].firstChild.data;
			friends_uids += uids[i].firstChild.data + "%2C";
		}
	}
	
	processLoadPage();
}

function processLoadPage()
{
	username_uid[username] = uid;
	uid_thumb[uid] = owner_thumb;
	uid_name[uid] = owner_name;
	
	var rest_id = opensocial.getEnvironment().getParams()["rid"];
	var tab = opensocial.getEnvironment().getParams()["tab"];
	if ( rest_id != null && rest_id != 0 )
		printRestaurant(rest_id);
	else if ( tab != null && tab != '')
		loadCanvas(tab);	
	else
		loadCanvas("home");
}

function getUid(url)
{	
	if ( site == "orkut" )
	{
		//get user uid
		var regex = /uid=([^&#]+)/;
		var result = url.match(regex);
		if (result.length == 2) 
		 	return result[1];
		else
			return 0;
	}
	else if ( site == "hi5" )
	{
		return owner_id;
	}
	else if ( site == "myspace" )
	{
		return owner_id;
	}	
	else
		return "";
}

function loadCanvas(page)
{
	if ( username != "" )
	{ 
		if ( page == "home" || page == undefined)
			printHomePage();
		else if ( page == "profile" )
			printProfilePage(username);
		else if ( page == "friends" )
			printFriendsPage();
		else if ( page == "search" )
			printSearchPage("");
		else if ( page == "invite" )
			printInvitePage("");
		else if ( page == "help" )
			printHelpPage("");
		else if ( page == "add" )
			printAddRest();
		else if ( page == "about" )
			printAboutPage("");
		else 
			printHomePage();
	}
	else
		printAccountPage("",""); 
}

function cleanup()
{
	document.getElementById("user").innerHTML = "";
	document.getElementById("leftOne").innerHTML = "";
	document.getElementById("leftTwo").innerHTML = "";
	document.getElementById("rightOne").innerHTML = "";
	document.getElementById("rightTwo").innerHTML = "";
	document.getElementById("rightThree").innerHTML = "";
	document.getElementById("leftOneHead").innerHTML = "";
	document.getElementById("leftTwoHead").innerHTML = "";
	document.getElementById("rightOneHead").innerHTML = "";
	document.getElementById("rightTwoHead").innerHTML = "";
	document.getElementById("rightThreeHead").innerHTML = "";
}

function changeTab(tabName)
{
	document.getElementById("homeTab").className = 'tab';
	document.getElementById("profileTab").className = 'tab';
	document.getElementById("friendsTab").className = 'tab';
	document.getElementById("searchTab").className = 'tab';
	document.getElementById("inviteTab").className = 'tab';
	document.getElementById("helpTab").className = 'tab';
	document.getElementById("aboutTab").className = 'tab';
	document.getElementById(tabName).className='tab selected';
	
	if ( tabName != "searchTab" )
	{
		searchLocation = "";
		searchFood = "";
		searchGid = 0;
		searchCity = "";
		searchIsVillage = "false";
	}
}

function sendActivity(title, body)
{
	var params = {};
	params[opensocial.Activity.Field.TITLE] = title;
	params[opensocial.Activity.Field.BODY] = body;
	if ( site == 'hi5' )
		params[opensocial.Activity.Field.STREAM_FAVICON_URL] = imageUrl + "hi5_restaurants_icon.gif";
	else if ( site == 'myspace' )
		params[opensocial.Activity.Field.STREAM_FAVICON_URL] = imageUrl + "myspace_restaurants_icon.gif";
	var activity = opensocial.newActivity( params );
	opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH);
}

function isDigit(num) 
{
	if ( num.length > 1 )
		return false;
	
	var string="1234567890";
	
	if ( string.indexOf(num) != -1 )
		return true;
	
	return false;
}

function isNumber(num) 
{	
	var string="1234567890";
	
	for ( var i = 0; i < num.length; i ++ )
		if ( string.indexOf(num.charAt(i)) == -1 )
			return false;
	
	return true;
}