

var scrollerCurrent = 1;
var scrolling = false;
var MyCommunity = {
  
  scroller: function(max){
    if (!scrolling){
      scrolling = true;
      $("#scrollPane").scrollTo($("#scrollPane_" + scrollerCurrent), 500, {onAfter:function(){scrolling = false;}});
      
      if (scrollerCurrent == max - 1)
        scrollerCurrent = 0;
      else
        scrollerCurrent++;
    }
  },

  ping: function() {
    $.post(pingTo, {
        call:"ping"
      },
      function(data){
        //alert(data);
        //alert(data.message); 
      } 
      , "json"
    );       
  },
  
  logout: function(){
    clearTimeout(pingTimer);
  }
}


function commentSubmit(){
  $.post("lib/Profile/profile_ajax.php", { 
      token:$("#tok").val(),
      value:$("#uID").val(),
      message:$("#message").val(),
      where:"c_s"
    },
    function(data){
      var msg_date = data.message_date;
      var message = data.message;
      
      $("#comment_response").append(data.html);
      
      $("#comment_response").slideDown("slow");
      
      var $submitButton = $("#comment_button"); 
      var $message = $("#message");
      $submitButton.attr("disabled", "true");
      $message.attr("disabled", "true");
    } 
    , "json"
  );   
}

function showCommentForm(){
  $form = $("#comment_form");
  if ($form.is(":hidden"))
  {
    //$form.show("slide", {direction: "up"});
    $form.show();
  } else {
    $form.hide();
  }
}

$(document).ready(function()
{
  /**
   *  check cookies
   */
  if ($.cookie('hide_status_form') != null)
  {
    
    $form = $('#write_form');
    $toggle_button = $('#toggle_profile_status_form');
    
    $form.hide();
    $toggle_button.empty();
    $toggle_button.append("Show Form");    
  }
  
  $(".status_message").hover(
    function(){
      jQuery(this).find(".message_drop").show(); 
    },
    
    function(){
      jQuery(this).find(".message_drop").hide(); 
    }    
  );  
  
  
  $(".status_child_message").hover(
    function(){
      jQuery(this).find(".message_child_drop").show(); 
    },
    
    function(){
      jQuery(this).find(".message_child_drop").hide(); 
    }    
  );    
  
  
  $(".status").hover(
    function(){
      jQuery(this).find(".status_drop_button").show(); 
    },
    
    function(){
      jQuery(this).find(".status_drop_button").hide(); 
    }    
  );    
  
 
  
  
  
});

/**
 *  STATUS UPDATE
 */
function dropStatus(id, id_holder, is_viewer_owner)
{
  if (is_viewer_owner == undefined)
    is_viewer_owner = "false";
    
    
  var $status = $("#status_" + id_holder); 
  answer = confirm('Are you sure you want to delete this message?');
  if (answer){  
    $.post("lib/Profile/profile_ajax.php", { 
        value:id,
        is_viewer_owner:is_viewer_owner,
        where:"drop_status"
      },
      function(data){
        $status.slideUp();       
      }
    );   
  }
} 
function toggleProfileStatusForm()
{
  var animate = false;
  $form = $('#write_form');
  $toggle_button = $('#toggle_profile_status_form');
  
  if ($form.is(":hidden")){
    /**
     *  SHOW
     */         
    if (animate){
      $form.slideDown();
    } else {
      $form.show();
    }
    $toggle_button.empty();
    $toggle_button.append("Hide Form");
    
    $.cookie('hide_status_form', null, {path: "/"});
    //alert("Should be Null: " + $.cookie('hide_comments'));
    
  } else {
    /**
     *  HIDE
     */         
    if (animate){
      $form.slideUp();
    } else {
      $form.hide();
    }
    $toggle_button.empty();
    $toggle_button.append("Show Form");
      
    $.cookie('hide_status_form', true, {path: "/"});
    //alert("Should be True: " + $.cookie('hide_comments'));
  }
}
 
function addNewStatus(){
  var $message = $("#message").val();
  var $name = $("#name").val();
  var $button = $("#message_button");
  $button.attr("disabled", "true");
  
  if ($message.length != 0){
    $.post("lib/Profile/profile_ajax.php", { 
        message:$message,
        name:$name,
        where:"add_status"
      },
      function(data)
      {      
        if (data.response){
          $("#new_status_container").prepend(data.html);
          $("#" + data.id_holder).show("blind");
          $button.removeAttr("disabled");
          $("#message").val("");
        } else {
          doFaceBox(data.message);
          $button.removeAttr("disabled");
        }
      } 
      , "json"
    );   
  } else {
    doFaceBox("You must enter a message.");
    $button.removeAttr("disabled");
  }
}

function toggleStatusDefault(){
  $toggle_button = $("#toggle_default_page");
  $.post("lib/Profile/profile_ajax.php", { 
      where:"default_page"
    },
    function(data)
    {      
      //toggle_default_page
      if (data == 0)
      {
        $toggle_button.empty();
        $toggle_button.append("Make this my default page");       
      } else {
        $toggle_button.empty();
        $toggle_button.append("Do not make this my default page"); 
      }      
    } 
    //, "json"
  );   
}
/**
 *  endOF STATUS UPDATE
 *  ------------------- 
 */


function editFormShow(id, obj){
  //doFaceBox(id);
  var $replaceDom = $("#container_" + obj);
  var $formDom = $("#form_" + obj);
  $replaceDom.hide(); 
  $formDom.show();
}

function editFormHide(obj){
  var $replaceDom = $("#container_" + obj);
  var $formDom = $("#form_" + obj);
  $replaceDom.show();
  $formDom.hide();
}

/**
 *  bio
 */ 
function editBioSubmit()
{
  var obj = "bio";
  var $replaceDom = $("#replace_" + obj);
  var $profileBioDom = $("#profile_" + obj);
  var $inputVal = $("#bio_textarea").val();
  $.post("lib/Profile/profile_ajax.php", { 
      bio:$inputVal,
      where:"e_b"
    },
    function(data){
      //doFaceBox(data.sql1 + "<br>" + data.sql2);
      editFormHide("bio");
      $replaceDom.empty();
      $replaceDom.append(data.bio);
      $profileBioDom.effect("highlight", 500);
      
    } 
    , "json"
  );   
}

/**
 *  general
 */ 
function editGeneralSubmit()
{
  var obj = "general";
  var $replaceDom = $("#replace_" + obj);
  var $profileBioDom = $("#profile_" + obj);
  var $inputVal = $("#"+obj+"_textarea").val();
  $.post("lib/Profile/profile_ajax.php", { 
      gen:$inputVal,
      where:"e_g"
    },
    function(data){
      //doFaceBox(data.new_value);
      editFormHide(obj);
      $replaceDom.empty();
      $replaceDom.append(data.new_value);
      $profileBioDom.effect("highlight", 500);
    } 
    , "json"
  );   
}

/**
 *  general
 */ 
function editGeneral(obj, field)
{
  var obj = obj;
  var $replaceDom = $("#replace_" + obj);
  var $profileBioDom = $("#columnA_" + obj);
  var $inputVal = $("#"+obj+"_textarea").val();
  $.post("lib/Profile/profile_ajax.php", { 
      value:$inputVal,
      fi:field,
      where:"e_g"
    },
    function(data){
      //doFaceBox(data.new_value);
      editFormHide(obj);
      $replaceDom.empty();
      
      new_val = data.new_value;
      new_val = new_val.replace(/\n/g, "<br />");
      $replaceDom.append(new_val);
      $profileBioDom.effect("highlight");
    } 
    , "json"
  );   
}

function editDob()
{
  var obj = "dob";
  var $replaceDom = $("#replace_" + obj);
  var $profileBioDom = $("#columnA_" + obj);
  var $month = $("#dob_month").val();
  var $day = $("#dob_day").val();
  var $year = $("#dob_year").val();
  
  
  $.post("lib/Profile/profile_ajax.php", { 
      month:$month,
      day:$day,
      year:$year,
      where:"e_dob"
    },
    function(data){
      //doFaceBox(data);
      editFormHide(obj);
      $replaceDom.empty();
      $replaceDom.append(data);
      $profileBioDom.effect("highlight");
    } 
    //, "json"
  );   
}

function dropHomie(id_holder, user_id)
{
  $('#' + id_holder).addClass('ui-state-error ui-corner-all');
  answer = confirm('Are you sure you want to drop this friend?');
  
  if (answer){
    //var $replaceDom = $("#replace_" + obj);
    //var $profileBioDom = $("#profile_" + obj);
    //var $inputVal = $("#"+obj+"_textarea").val();
    $.post("lib/Profile/profile_ajax.php", { 
        value:user_id,
        where:"d_u"
      },
      function(data){
        $('#' + id_holder).hide();
        /*
        doFaceBox(data.new_value);
        editFormHide(obj);
        $replaceDom.empty();
        $replaceDom.append(data.new_value);
        $profileBioDom.effect("highlight", 500);
        */
      } 
      //, "json"
    );  
  } else {
    $('#' + id_holder).removeClass('ui-state-error ui-corner-all');
  }
}


function addFriend(target_id, email_notification){

  var inputDelayer=delayTimer(1);
  inputDelayer(function(){
      doFaceBox(
        "<div style='text-align: center; '><img src='resources/template/css/images/loading/horizontal_blue.gif'/></div>"
      );
  });

  $.post("lib/Profile/profile_ajax.php", {
      target_id:target_id,
      where:"f_r",
      email_notification:email_notification
    },
    function(data){
        // close the loading box if opened and clear the timer
        inputDelayer();
        jQuery.facebox.close();    
            
      if(data == 'logged_in'){
        doFaceBox(b_loggedIn);		
      } else if (data == 'request_sent'){
        doFaceBox(b_profileAddFriendSent);
      } else if (data == 'already_friends'){
        doFaceBox(b_profileAddFriendTrack);
      } else if (data == 'already_sent_request'){
        doFaceBox(b_profileAddFriendSentTrack);
      } else if (data = 'user_made_request'){
        doFaceBox('This user has already sent you a request.  Go to your homepage to accept.');
      } else if (data == 'error'){
        doFaceBox(b_error);
      }
      
    }
  );     
}

function fr_response(id, id_holder, tAction){  
  answer = true;
  if (answer)
  {
    $.post
    (
      "lib/Profile/profile_ajax.php",
      {
        value:id,
        action:tAction,
        where:"fr_r"
      },
      
      function(data)
      {      
        if(data){
          if (!$.browser.msie){
            $("#"+id_holder).slideUp("slow");
          } else {
            $("#"+id_holder).slideUp("slow");
          }
        } else {
          doFaceBox('we\'re sorry, but you might be logged out or signed in onto a different account.  Please try again later.');        
        }
        
      }
    );
  } else {
    //alert('no');
  }
}

function setProfileImage(id){
  
  $.post("lib/Profile/profile_ajax.php", {
      value:id,
      where:"si_p"
    },
    function(data){
      doFaceBox("This image is now set to your default.");
      //$('#' + id_holder).hide();
      //alert(data);
      /*
      doFaceBox(data.new_value);
      editFormHide(obj);
      $replaceDom.empty();
      $replaceDom.append(data.new_value);
      $profileBioDom.effect("highlight", 500);
      */
    } 
    //, "json"
  );  
}

function dropImage(id){
  answer = confirm('Are you sure you want to delete this image?');
  
  if (answer){
    $.post("lib/Profile/profile_ajax.php", {
        value:id,
        where:"d_i"
      },
      function(data){
        //$('#' + id_holder).hide();
        //doFaceBox(data);
        $("#image_holder").hide("drop", {direction: "down"});
        /*
        doFaceBox(data.new_value);
        editFormHide(obj);
        $replaceDom.empty();
        $replaceDom.append(data.new_value);
        $profileBioDom.effect("highlight", 500);
        */
      } 
      //, "json"
    );  
  }
}

/**
 *  dropSubmission
 */ 
function dropSubmission(id, id_holder)
{ 
   var $submission_holder = $("#submission_holder_" + id_holder);
   
   $submission_holder.addClass('jHighlight');
  answer = confirm('Are you sure you want to delete this shared thought?');
  
  if (answer){  
    $.post("lib/Profile/profile_ajax.php", { 
        value:id,
        where:"d_s"
      },
      function(data){
        $submission_holder.hide("blind");       
      } 
    );   
  } else {
    $submission_holder.removeClass('jHighlight');
  }
}

/**
 *  drop favorite
 */ 
function dropFavorite(id, id_holder)
{ 
  var $holder = $("#"+id_holder);
  $holder.addClass("jHighlight");
  
  $.post("lib/ajax/Bumpley_Ajax.php", { 
      call:"drop_favorite",
      id:id
    },
    function(data){
      $holder.effect("blind");
    }
  );   
}


/**
 *  dropSubmission
 */ 
function dropMessage(safe_id, key, id_holder, profile_owner)
{
  var $submission_holder = $("#message_holder_id_" + id_holder);

  answer = confirm('Are you sure you want to delete this message?');
  if (answer){  
    $.post("lib/Profile/profile_ajax.php", { 
        value:safe_id,
        value_key:key,
        where:"d_m",
        option:profile_owner
      },
      function(data){
        $submission_holder.hide("blind");
      } 
      //, "json"
    );   
  }
}


function dropOverview(safe_id, key, id_holder)
{
  var debug = false;
  var $submission_holder = $("#overview_holder_" + id_holder);
  //answer = confirm('Are you sure you want to delete this message?');
  if (!debug){
    $.post("lib/Profile/profile_ajax.php", {
        value:safe_id,
        value_key:key,
        where:"d_o"
      },
      function(data){
        //$submission_holder.slideUp("slow");
        //doFaceBox(data);
        $submission_holder.hide("blind");
      } 
      //, "json"
    );   
  } else {
    $submission_holder.hide("highlight");
  }
}

function dropAllOverview(){
  $answer = confirm("Are you sure you want to delete all notifications?");
  
  if ($answer){
    $.post("lib/ajax/Bumpley_Ajax.php", {
        call:"drop_all_overview"
      },
      function(data){
        //$submission_holder.hide("blind");
        $(".overview_container").slideUp();
      } 
      //, "json"
    );  
  }
}

function showImageForm()
{
  var $form = $("#image_form");
  if ($form.is(":hidden"))
  {
    $form.show("slide", {direction: "up"});
  } else {
    $form.hide("slide", {direction: "up"});
  }
}


function mediaNav()
{
  var w = document.media_form.media_list.selectedIndex;
  var url_add = document.media_form.media_list.options[w].value;
  window.location.href = url_add;
}