

var modified_timestamp;
var _base_url = location.href.split(/[\?\#]/)[0];
if (_base_url.slice(-10)=='index_html')
  _base_url = _base_url.slice(0,-10);
if (_base_url.charAt(_base_url.length-1)=='/')
  _base_url = _base_url.substring(0, _base_url.length-1);

$(function() {
   $.get(_base_url+'/getModifyTimestamp', {}, function(resp) {
      if (resp) modified_timestamp = resp;
   });
});

function checkRefresh() {
   if (!modified_timestamp) return false;
   $.get(_base_url+'/getModifyTimestamp',{}, function(resp) {
      if (resp && modified_timestamp && parseInt(resp) != parseInt(modified_timestamp)) {
         modified_timestamp = resp;
         $('#threads').load(_base_url+'/ShowIssueThreads');
         $('#issuedata').load(_base_url+'/ShowIssueData');
         if (document.title.indexOf("(automatically refreshed) ")==-1)
           document.title = "(automatically refreshed) " + document.title;
         refreshinterval = orig_refreshinterval;
      }
   });
}

function clearAutoRefreshTitle() {
  document.title = document.title.replace(/\(automatically refreshed\) /,'');
}


  
function clairvoyant_followups() {
   $.get(_base_url+'/getRecentOtherDraftThreadAuthor?nochache='+(Math.random()+"").substr(2, 5),
         {'only_fromname':1}, function(resp) {
      try {
      if (resp) {
         $('#recent-draftthread-author').text(resp).show();
         setTimeout(function() {
            $('#recent-draftthread-author').fadeOut(500);
         }, 30*1000);
      }
      } catch(ex) {alert(ex);}
   });
}
var cf_timer;
start_clairvoyant_followups = function() {
   clairvoyant_followups();
   cf_timer=window.setTimeout("start_clairvoyant_followups()", 4*1000);
}


function autosave() {
   $.post(_base_url+'/AutoSaveDraftThread', 
          $(document.form_followup).fastSerialize(),
          function(resp) {
	    if (resp)
               $('input[name="draft_followup_id"]').val(resp);
          });
}

var as_timer;
var orig_autosaveinterval=6;
var autosaveinterval=6;
startautosave = function() {
  autosave(); 
  as_timer=window.setTimeout("startautosave()", autosaveinterval*1000);
};
stopautosave=function() {
  if (as_timer) window.clearTimeout(as_timer);
  
};
  
var r_timer;
var refreshinterval,orig_refreshinterval;
refreshinterval=orig_refreshinterval=3;
function startautorefresh() {
  checkRefresh();
  r_timer=window.setTimeout(startautorefresh, refreshinterval*1000);
  refreshinterval+=refreshinterval*0.3;
}

$(function() {
   startautorefresh();
   start_clairvoyant_followups();
});



function af(dest, e, ignoreword) {
  var val=dest.value;
  if (val.indexOf(e)==-1) {
    val = val.replace(ignoreword, "");
    if ($.trim(val).charAt($.trim(val).length-1)!=',') val = val+", ";
    val = val+e+", "
  } else {
    if (val.indexOf(e+", ")!=-1) {
      val = val.replace(e+", ", "");
    }
  }
  dest.value = val;  
}
function softsubmit(action) {
  if (document.form_followup) {
    document.form_followup.action.value=action;
    document.form_followup.submit();
  }
}
