// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function show_comments(element) {
    $('movie_comments').hide();
    $('singer_comments').hide();
    $('song_comments').hide();
    $(element).show()
}

function show_favs(element) {
    $('movie_favs').hide();
    $('singer_favs').hide();
    $('song_favs').hide();
    $(element).show()
}

function show_home(element) {
    $('home_movies').hide();
    $('home_singers').hide();
    $('home_songs').hide();
    $(element).show()
}

function show_home_search(element) {
    $('home_movies_search').hide();
    $('home_singers_search').hide();
    $('home_songs_search').hide();
    $(element).show()
}

function show_pop(element) {
    $('viewed_pop').hide();
    //$('emailed_pop').hide();
    $('discussed_pop').hide();
    $(element).show()
}
function show_viewed_pop(element) {
    $('movie_viewed_pop').hide();
    $('singer_viewed_pop').hide();
    $('song_viewed_pop').hide();
    $(element).show()
}

function show_emailed_pop(element) {
    $('movie_emailed_pop').hide();
    $('singer_emailed_pop').hide();
    $('song_emailed_pop').hide();
    $(element).show()
}

function show_discussed_pop(element) {
    $('movie_discussed_pop').hide();
    $('singer_discussed_pop').hide();
    $('song_discussed_pop').hide();
    $(element).show()
}

function move(inputControl)
{
  var left = document.getElementById("left");
  var right = document.getElementById("right");
  var from, to;
  var bAll = false;
  switch (inputControl.value)
  {
  case '<<':
    bAll = true;
    // Fall through
  case '<':
    from = right; to = left;
    break;
  case '>>':
    bAll = true;
    // Fall through
  case '>':
    from = left; to = right;
    break;
  default:
    alert("Check your HTML!");
  }
  for (var i = from.length - 1; i >= 0; i--)
  {
    var o = from.options[i];
    if (bAll || o.selected)
    {
      from.remove(i);
      try
      {
        to.add(o, null);  // Standard method, fails in IE (6&7 at least)
      }
      catch (e)
      {
        to.add(o); // IE only
      }
    }
  }
}

    var viewer, ownerFriends, activities;

    function initAllData() {
      var params = {};
      params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] =
        [opensocial.Person.Field.ID,opensocial.Person.Field.NAME,opensocial.Person.Field.THUMBNAIL_URL,opensocial.Person.Field.PROFILE_URL];

      var paramss = {};
      paramss[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] =
        [opensocial.Person.Field.ID,opensocial.Person.Field.NAME,opensocial.Person.Field.THUMBNAIL_URL,opensocial.Person.Field.PROFILE_URL];
	paramss[opensocial.DataRequest.PeopleRequestFields.MAX] = 100;
	paramss[opensocial.DataRequest.PeopleRequestFields.FILTER] = opensocial.DataRequest.FilterType.HAS_APP;

      var req = opensocial.newDataRequest();
      req.add(req.newFetchPersonRequest('VIEWER', params), 'viewer');
      req.add(req.newFetchPeopleRequest( new opensocial.IdSpec({'userId' : 'OWNER', 'groupId' : 'FRIENDS'}), paramss), 'ownerFriends');
      req.add(req.newFetchActivitiesRequest(new opensocial.IdSpec({'userId' : 'OWNER', 'groupId' : 'FRIENDS'}), params), 'activities');
      req.send(setupData);
    };

    function setupData(data) {
      ownerFriends = data.get('ownerFriends').getData().asArray();

      var html = "";
      activities = data.get('activities').getData();
      if (activities) {
        activities = activities.asArray();
        for (var i = 0; i < activities.length && i < 20; i++) {
          var activity = activities[i];
          var title = activity.getField('title', {escapeType: 'none'});
          var body = activity.getField('body', {escapeType: 'none'});

	   var name="";
          var userId = activity.getField('userId', {escapeType: 'none'});

	   html += "<div class='item'>"
          ownerFriends.each(function(person)
            {
             if(userId == person.getField('id')) {
			html += "<a title='" + person.getField("displayName") + "' href='" + person.getField("profileUrl") + "'>";
          		html += "<img class='memberPhoto' src='" + person.getField("thumbnailUrl")  + "'/>";
          		html += "</a>";
		}
            });


          if (body) {
              body = body.substring(0, 200);
              html += title + " - ";
              html += body + "</div>";
          } else {
              html += title + "</div>";
          }
        };
      } else {
        html = "<div class='item'>Be the first to buzz!</div>";
      }

      document.getElementById('activities').innerHTML = html;
    };