//menus
$(".menu-box-bottom").live('click', function() {
  id = this.id;
  if ($(".menu-box-bottom-text").html() == 'Hide') {
    $("#"+id+"_dropdown").slideUp('slow');
    $(".menu-box-bottom-text").html('More actions');
  } else {
    $("#"+id+"_dropdown").slideDown('slow');
    $(".menu-box-bottom-text").html('Hide');
  }  
})

$(".find_choice_options").live('click', function(){
  h = $(this).html();
  $("#find_choice_link").html(h);
  $("#find_choice_results").hide();
  $("#find_choice_results").attr('search_type', this.id);
  $(".find_choice_options").removeClass('choice_active');
  $(this).addClass('choice_active');
})

//manage the people search function
$("#search_input").live('keyup', function() {
  if ($("#search_input").val() == '') {
    $("#search_results").hide();
  } else {
    $.post('/code/ajax/search.php', {
         'search_type' : $("#find_choice_results").attr('search_type'),
         'data': $("#search_input").val(),
         'archived' : $("#include_archived").attr('checked'),
         'active' : $("#include_active").attr('checked')
       }, function(e) {         
           $("#search_results").html ( e.html );
           $("#search_results").show();
       }, 'json');
  }     
})
$(".person").live('click', function() {
  id = $(this).attr('id');  
  $(".person").removeClass('highlighttext');
  $(this).addClass('highlighttext');
  $(this).html($(this).html()+' Loading...');
  $.post('/code/ajax/people_set_active.php', {'id':id},
         function(e) {
           if (e.ok = 1) {
             $("#people_search_results").html ( 'Loading....' );
             location.href="/people.php";
           } else alert('Error!');
         });
})
$(".assetnm").live('click', function() {
  var id = $(this).attr('gid');
  document.location.href='/assets.php?assetid='+id;  
})


$("#search_options").live('click', function() {
  $("#search_options_results").toggle();
})

// Same as above but for quickfind
$("#people_search_input_qf").live('focus', function() {
  if ( $("#people_search_input_qf").val() == 'Type first or lastname') $("#people_search_input_qf").val('');
  $("#people_search_input_qf").addClass('people_search_input_data');
})

$("#people_search_input_qf").live('keyup', function() {
  if ($("#people_search_input_qf").val() == '') {
    $("#people_search_results_qf").hide();
  } else {
    $.post('/code/ajax/people_search.php', {
         'data': $("#people_search_input_qf").val(),
         'archived' : 'false',
         'deceased' : 'false',
         'suffix' : 'qf'
       }, function(e) {         
           $("#people_search_results_qf").html ( e.html );
           $("#people_search_results_qf").show();
       }, 'json');
  }     
})

$(".personqf").live('click', function() {
  id = $(this).attr('id');  
  sname = $("#"+id+" .personsn").html();
  fname = $("#"+id+" .personnames").html();
  local_people_clicked(id,sname,fname);  //resides in the javascript for the form this feature resides on
})

$("#find_choice_link").live('click', function() {
  $("#find_choice_results").toggle();
})


//make sure this is loaded last!
$("#content").live('click', function() { 
  $("#people_search_results_qf").hide(); 
  $("#people_search_results").hide();
})

$(".markasdone").live('click', function() {
  aid = $(this).attr('aid');
  h = $("#doneactivity").html();
  h = h.replace('$', aid);
  h = h.replace('$', aid);
  h = h.replace('$', aid);
  $("#show_activitydone_"+aid).append( h );
  $("#show_activitydone_"+aid).fadeIn();
  if ($(this).attr('gid') == 0) { //dont have the option to show to group as only a user can do this
    $("#show_activitydone_"+aid+" #groupvisible").remove();    
  }  
})
$(".activitydonecancel").live('click', function() {
  aid = $(this).attr('aid');
  $("#show_activitydone_"+aid).fadeOut( "slow", function() {
    $("#show_activitydone_"+aid).children().remove() 
    });
})
$(".activitydonesave").live('click', function() {
  aid = $(this).attr('aid');
  fields = $("#show_activitydone_"+aid+" form").serializeArray();
  $.post('/code/ajax/save_action_done.php', {'fields':fields}, function() {
      $("#hometodoitem_"+aid).fadeOut('slow', function() {
        $("#hometodoitem_"+aid).children().remove()  
      });
    }, 'json')  
})
  
function load_action_list(concerning, mth) {
  $("#actionlist").load('/code/ajax/show_todo_list.php', {
    'months' : mth, 'concerning' : concerning
  });
}

function keepalive(idelement, startmsg, donemsg, start) {
  s = 'keepalive("'+idelement+'", "'+startmsg+'", "'+donemsg+'",false);';
  if (start) {
    var v = setTimeout(s, 300000);    
  } else {
    $(idelement).html(startmsg);
    $.post('/code/ajax/keepalive.php', {}, function(e) {
      if ((e.ok == 1) && (idelement != '')) $(idelement).html(donemsg);
      var v = setTimeout(s, 300000);
    }, 'json')
  }  
}
