
function show_wall(which) {

  // not already displaying?
	if (!$(which).visible()) {

    // hide every wall
	  $$('.wall-holder').each(function(wall){
	      wall.hide();
	  });
	  
	  //also hide notice
	  $('wall_notice').hide();
	  
	  //see if wall we want to show has any children, if not show the notice instead
    if($$('#'+which+' .wall_posts .wall-post').length == 0)
    {
      Effect.Appear('wall_notice', {duration:0.2});
    }
    else
    {
      // show the one they want to see
      Effect.Appear(which, {duration:0.2});
    }
    
    //change styling of wall links to show selected one
    $$('#post_head a').each(function(elm){
      elm.removeClassName('on');
    });
    
    $$('#post_head a.'+which).each(function(elm){
      elm.addClassName('on');
    });
	}

	return false;
}


/*
  posting to the wall
*/
function post_form_onValidationFailed(frm)
{
  display_notification('Your wall post is empty, you must enter a message before you can post on the wall.');
}

function post_form_onSuccess(t) {
  var result = t.responseJSON;

  // update the page with the new post
  if (result.success) {
    // add the post to the wall
    wall_add_post(result.post.FEED_DETAIL[0]);
    // clear the text from the input box
    $('wall_post_body').value = '';
  }
  //We have failed display message
  else
  {
    switch(result.error_code)
    {
      case 'no_rights':
        display_notification('You do not have permission to do this. You need to be a friend of the wall owner in order to post on their wall.');
        break;
      default:
        display_notification('There was an error when trying to post on the wall.');
        break;
    }
  }
}
function post_form_onFailure(e) {

}

function wall_add_post(post) {
  var actor = can_act_as[post.initiator_id];
  
  // build the new wall post
  var new_post = $('wall-dummy').cloneNode(true);
  new_post.id = 'wall-post-' + post.action_id.toString();
  new_post.select('.wall-body')[0].innerHTML = '"' + post.wp_body + '"';

  // update the new post with the actor details
  new_post.select('.image img')[0].src = actor.tiny_thumb;
  new_post.select('.member_link')[0].href = actor.url;
  new_post.select('.member_link')[0].innerHTML = actor.name;

  // include the event handlers
  if (new_post.select('.remove-link').length > 0) {
    var link = new_post.select('.remove-link')[0];
    link.href = link.href + post.id.toString();
    // hack for ie6 weirdness
    if (link.href.substring(0, 11) == 'about:blank') {
      link.href = link.href.substring(11);
    }
    wall_add_link_events(link, new_post);
  }          

  // add it to the page
  $('my_wall').down('.wall_posts').insert({top:new_post});
  new_post.show();
  
  // reset the wall colouring
  wall_shuffle('my_wall');
}




/*
    deleting from the wall
*/

Event.observe(document, 'dom:loaded', function(){
  // attach the mouse over and outs to highlight the delete buttons
  $$('.wall-post').each(function(post){
    if (post.down('.remove-link')) {
      var link = post.down('.remove-link');
      wall_add_link_events(link, post)
    }
  });
});

function wall_add_link_events(link, post) {
  Event.observe(post, 'mouseover', function(){ wall_show_remove(link); });
  Event.observe(post, 'mouseout', function(){ wall_hide_remove(link); });
  Event.observe(link, 'click', function(e){ wall_kill_post(e, link, post); });
}

function wall_show_remove(link) {
  link.show();
}

function wall_hide_remove(link) {
  link.hide();
}

function wall_kill_post(e, link, post) {
  // try to stop the normal linking (though allow it to fall through without javascript)
  Event.stop(e);
  // use ajax instead
  var aj_link = link.href + '.json';
  var the_post = post;
  new Ajax.Request(aj_link, {asynchronous:true, onSuccess:function(t){ kill_post_onSuccess(t, the_post); }, onFailure:kill_post_onFailure});
}
function kill_post_onSuccess(t, post) {
  // get rid of the note
  Effect.toggle(post, 'appear', { duration:0.3, afterFinishInternal:function(){
      // get rid of it from the dom
      post.remove();
      // reset the alternating colours
      wall_shuffle('my_wall');
    }
  }
  );
}
function kill_post_onFailure() {
  // do we care?
}

function wall_shuffle(wall) {
  // add the alternating colours to the post listing 
  var pos = 0;
  $$('#' + wall + ' .wall-post').each(function(post){
    post.removeClassName('row' + ((pos % 2) + 1).toString());
    post.addClassName('row' + ((pos % 2) + 2).toString());
    pos++;
  });
  
  if (pos > 0) {
    $('wall_notice').hide();
  } else {
    $('wall_notice').show();
  }
}

