var is_friend = false;
var albums = null;

function display_profile(data){
  $("#stat_profile").slideUp("slow");
  for (x in data.profile){
    $("#profile-"+x).addClass("profile");
    $("#profile-"+x).html(data.profile[x]);
  }
  $("#anim_profile").show("slide");
}

function queue_messages(data){
  if (data.status == 'NEW'){
    $('#messages').html(data.message);
    $('#messages_container').show("bounce");
  }else if (data.status == 'SYS'){
    for (x in data.messages){
      $('#messages').append('<div><b>System message:</b> '+data.messages[x]+'</div>');
    }
    $('#messages_container').show("bounce");
  }    
}

function authenticate_services(data){
  is_friend = data.status;
  if (is_friend){
    $("#tabs").tabs("option", "disabled", [4]);
    setInterval('Dajaxice.myprofile.check_messages(queue_messages);', 60000)
    Dajaxice.myprofile.check_messages(queue_messages);
  }
}

function display_wall(data){
  $("#tab-wall").html(data.message);
}

function display_interests(data){
  tmp = "<table>";
  for (x in data.interests){
    tmp=tmp+"<tr><th>"+x+":</th><td>"+data.interests[x]+"</td></tr>";
  }
  $("#tab-interests").html(tmp+"</table>");
}

function display_photo(album,title){
  $("#tab-photos").html("<img src=\""+albums[album][title]+"\" onclick=\"display_album('"+album+"')\"/>");
}

function display_album(title){
  tmp = "<a href=\"#\" onclick=\"display_photos();\">Albums</a><ul class=\"polaroids\">";
  for (x in albums[title]){
    tmp=tmp+"<li>";
    tmp=tmp+"<a title=\""+x+"\"><img src=\""+albums[title][x]+"\" width=\"90\" height=\"90\" onclick=\"display_photo('"+title+"','"+x+"')\"/></a>";
    tmp=tmp+"</li>";
  }
  $("#tab-photos").html(tmp+"</ul>");
}

function display_albums(data){
  albums = data.albums;
  display_photos();
}

function display_photos(){
  if (albums == null){
    Dajaxice.myprofile.load_albums(display_albums);
    return;
  }  
  tmp = "<ul class=\"polaroids\">";
  for (x in albums){
    tmp=tmp+"<li>";
    tmp=tmp+"<a title=\""+x+"\"><img src=\"http://iamkevin.ca/album.gif\" onclick=\"display_album('"+x+"')\" alt=\""+x+"\"/></a>";
    tmp=tmp+"</li>";
  }
  $("#tab-photos").html(tmp+"</ul>");
}

function send_message(){
  $('#send_msg').button('disable');
  data = $('#contact_form').serializeObject();
  Dajaxice.myprofile.send_message(Dajax.process, {'form':data});
  return false;
}

$(function(){
  $("#anim_profile").hide();
  $("#tabs").tabs({disabled: [1]});
  $("#tabs").bind('tabsselect', function(event, ui){
    if (ui.index == 1){ Dajaxice.myprofile.load_wall(display_wall); }
    if (ui.index == 2){ display_photos(); }
    if (ui.index == 3){ Dajaxice.myprofile.load_interests(display_interests); }
    if (ui.index == 5){ location.href='http://kveroneau.info'; }
  });
  $('#messages_container').click(function(event){ $('#messages_container').slideUp("slow"); });
  $('#messages_container').hide();
  $('#send_msg').button();
  Dajaxice.myprofile.load_profile(display_profile);
  Dajaxice.myprofile.is_friend(authenticate_services);
});

