// this file holds any custom jquery functions we may need to include

//delete confirmation simply requires an ok to continue to link location on any links with class 'confirm_delete'
$(function() {
   $('.confirm_delete').click(function() {
      return confirm("Delete this item?");
   });
});

//I moved this here and converted to jquery because the old function was not working
function create_link() {
   var the_title = $('#link_title').val();
   var the_link = $('#link_page').val();
   var the_target = '';
   if ($('#link_target').is(":checked")) {
      the_target = ' target="_BLANK" ';
   }
   $('#link_display').val('<a href="' + the_link + '" ' + the_target + 'title="' + the_title + '">' + the_title + '</a>');
   $('#link_copy').show();
}



//these next functions are for the datatable jquery, it would not let me pu js inline in the datatable
// so each action link is a function

// toggle comment form in the hidden details view
function toggleCommentForm( num ) {
   $('#comment_form_' + num).toggle();
}

// toggle comment form in the hidden details view
function toggleComments( num ) {
   $('#comments_' + num).toggle();
}

function submitCommentForm( num ) {
   var comments = $('#comment_form_' + num + ' textarea').val();
   var sel = $('#comment_form_' + num + ' .notice');
   sel.html('<img src="/img/loading.gif">');
   if(  sel.is(':hidden')){
      sel.toggle();
   }
   $.post("/client/ajax_manage_watchlist_comments.php", { mls_num : num, comments: comments }, function(data){
      sel.html(data);
   });

   return false;
}

function changeRank ( mls_num,pid ) {
   var rank = $("#rank_select_" + mls_num).val();
   var cnf = confirm("Change rank to " + rank);
   if (cnf) {
      var sel = $('#change_rank_notice_' + mls_num);
      sel.html('<img src="/img/loading.gif">');
      if(sel.is(':hidden')) {
         sel.toggle();
      }
      $.post("/client/ajax_manage_watchlist_comments.php", { mls_num:mls_num,rank:rank,pid:pid }, function(data){
         sel.html(data);
      });
   }

   return false;
}


function removeComment ( rid ) {
   var cnf = confirm("Delete this item?");
   if (cnf) {
      var sel = $('#remove_comment_notice_' + rid);
      sel.html('<img src="/img/loading.gif">');
      if(  sel.is(':hidden')){
         sel.toggle();
      }
      $.post("/client/ajax_manage_watchlist_comments.php", { rid : rid }, function(data){
         sel.html(data);
         $('#comment_' + rid).remove()
      });
   }

   return false;
}




// this formats the details table row of the datatable
var oTable;
var oTable1;
// we do two of these functions because I could not get it to work with one, a better js guru could I am sure
function fnFormatDetails1 (nTr) {
   //aData is an array of the watchlist tables data items we can access them all here aData[10] is a hidden column that contains the html we pass here
   var aData = oTable1.fnGetData( nTr );
   var sOut = '<table style="padding:5px;" width="100%">';
   sOut += '<tr><td>'+aData[10]+'</td></tr>';
   sOut += '</table>';
   return sOut;
}
function fnFormatDetails (nTr) {
   var aData = oTable.fnGetData( nTr );
   var sOut = '<table style="padding:5px;" width="100%">';
   sOut += '<tr><td>'+aData[10]+'</td></tr>';
   sOut += '</table>';
   return sOut;
}


$(document).ready(function(){
   //watchlist.php uses this for sorting the table data
   $('#watched_properties_table').dataTable( {
      "sScrollX": "700px",
      //"sScrollXInner": "140%",
      "bScrollCollapse": true,
      "bPaginate": false,
      "bFilter": false,
      "bInfo": false,
      "bStateSave": true, //save table state into cookie
      "iCookieDuration": 31536000, // 1 year in seconds
      //"bAutoWidth": false,
      //"bLengthChange": false,

      "aaSorting": [[1,'asc'], [4,'asc']],

      "aoColumns": [
         { "bSearchable": false, "bSortable" : false  },
         { "bSearchable": false },
         null,
         { "bSearchable": false },
         null,
         { "bSearchable": false, "bVisible": false },
         { "bSearchable": false, "bVisible": false },
         { "bSearchable": false, "bVisible": false },
         { "bSearchable": false, "bVisible": false },
         { "bSearchable": false, "bVisible": false },
         { "bSearchable": false, "bVisible": false }
      ]
   } );

   //setup a table for the details box we hide/show the table in the click function below
   oTable = $('#watched_properties_table').dataTable( {
      "bProcessing": true,
      "bRetrieve" : true,
      "bServerSide": true
   } );

   // set click finction for edit/close image
   $('#watched_properties_table .details').click( function () {
      var nTr = this.parentNode.parentNode;
      if ( this.src.match('cancel_icon') ) {
         /* This row is already open - close it */
         this.src = "../img/edit_icon.gif";
         oTable.fnClose( nTr );
      } else {
         /* Open this row */
         this.src = "../img/cancel_icon.gif";
         oTable.fnOpen( nTr, fnFormatDetails(nTr), 'details' );
      }
   } );

   //setup a table for the details box we hide/show the table on page load via php _GET or _POST value
   oTable1 = $('#watched_properties_table').dataTable( {
      "bProcessing": true,
      "bRetrieve" : true,
      "bServerSide": true
   } );

   // opens a row from php $_GET or _POST based on mls num
   $('#watched_properties_table tbody tr').each( function() {
      var nTr = this;
      var td = $('td', this);
      var mls = $(td[2]).text();
      if (mls == post_mls) {
         oTable1.fnOpen( nTr, fnFormatDetails1(nTr), 'details' );
      }
   });
   //set click function for admin_manage_watchlist_comments.php comment form
   $('.add_reply_btn').click( function() {
      $(this).next().toggle();
      return false;
   });


   // ajax update db with show/hide status
   $('#save_search_icon_link').click( function(){
      $('#save_search_message').html('<img src="./img/loading.gif">');
      $.post("./client/ajax_save_search.php", { name: $('#save_search_title').val() }, function(data){
         $('#save_search_message').html(data);
         $('#save_search_title_form').toggle();
      });
   });

   // ajax add watched property to db
   $('.add_to_watchlist').click( function(){
      var sel = $(this).next();

      sel.html('<img src="./img/loading.gif">');
      sel.toggle();
      $.post("./client/ajax_save_watch_item.php", { mls_num : $(this).attr("Mls_Num") }, function(data){
         sel.html(data);
      });
      return false;
   });

   // ajax add comment and watched property to db
   $('.add_comment_watchlist').click( function(){
      var sel = $(this).next();

      sel.html('<img src="./img/loading.gif">');
      sel.toggle();
      $.post("/client/ajax_manage_watchlist_comments.php", { mls_num : $(this).attr("Mls_Num"), comments : $('#comments_textarea').val() }, function(data){
         sel.html(data);
      });
      $.post("./client/ajax_save_watch_item.php", { mls_num : $(this).attr("Mls_Num") }, function(data){
         sel.append(data);
      });
      return false;
   });

   $("#edit_task_cat").change(function() {
      $('#task_cat_msg').hide();
      var tid = $("#edit_task_cat").attr('tid');
      var cat = $("#edit_task_cat").val();
      $.post("/client/ajax_update_task_category.php", { tid : tid, cat : cat }, function(data){
         $('#task_cat_msg').html(data).toggle();
         $('#info_category_' + tid).html(cat);
      });
      return false;
   });

   //links/selectors with class of toggle_link will either look for eid attribute and find matching id to toggle
   // or they will find the next class "toggle_content" to toggle (toggle = show/hide)
   $('.toggle_link').addClass('pointer');
   $('.toggle_link').click( function() {
      eid = $(this).attr('eid');
      if (eid) {
         $('#' + eid).toggle("fast");
      } else {
         $(this).next('.toggle_content').toggle("fast");
      }
      return false;
   });

   $('.hide_this').css('font-size', -1);
   $('.hide_this').click( function(){
      $(this).parent(".toggle_content").hide("slow");
      return false;
   });


});

