/*

   i vse-taki zapishite menya v fan-club Bobuka!!! :)

*/

disKeyTrap = false;
document.onkeydown = CtrlLeftRightHandler;

function CtrlLeftRightHandler(evt)
 {
  if (!evt) evt = window.event;

  if (evt.ctrlKey && !disKeyTrap)
   {
    switch(evt.keyCode)
     {
      case 37:
       if (a = $('previous_page')) location.href = a.href;
       break;
      case 39:
       if (a = $('next_page')) location.href = a.href;
       break;
     }
   }
 }

function ReloadCaptcha(id, url)
 {
  $(id).src = url + '&' + Math.random();

  return false;
 }

function BlockOver(b, cn)
 {
  b.className = 'block block_' + cn + '_hover';
 }

function BlockOut(b, cn)
 {
  b.className = 'block block_' + cn;
 }

function PrettyIn(b)
 {
  b.style.border = '1px solid #404040';
 }

function PrettyOut(b)
 {
  b.style.border = '1px solid #a0a0a0';
 }

function DoKeyDown(event)
 {
  if (event) 
   if((event.ctrlKey) && ((event.keyCode==10)||(event.keyCode==13))) 
    if (f = $('daform'))
     {
      if (f.onsubmit)
       if (!f.onsubmit())
        return true;

      f.submit();
      return false;
     }

  return true;
 }

function FocusTA()
 {
  $('ta').focus();
 }

function CommentReply(where, smth)
 {
  var topc = where.parentNode;
  var replyform = $('replyform');

  replyform.parentNode.removeChild(replyform);
  topc.appendChild(replyform);

  where.style.display = 'none';

  if (prev_to_show)
   prev_to_show.style.display = 'inline';

  prev_to_show = where;

  if (!smth)
   setTimeout('FocusTA();', 50);

  $('replyto').value = where.id;

  return false;
 }

function FocusNewComment()
 {
  Element.scrollTo('new_comment');
 }

function PostCommentReplyUnlock(request)
 {
  if (cp = $('comments_progress'))
   cp.parentNode.removeChild(cp);

  prev_to_show = $('replyformclicker');
  CommentReply($('0'), 1);
  $('ta').value = '';
  $('replyform').style.display = 'block';

  if ($('new_comment'))
   setTimeout('FocusNewComment();', 50);
 }

function PostCommentReply()
 {
  if ($('attachment').value.length > 0)
   return true;

  var data = Form.serialize($('daform'));

  var wtf = document.createElement('SPAN');
  $('replyform').parentNode.insertBefore(wtf, $('replyform'));
  wtf.innerHTML = '<img id="comments_progress" class="progress" src="/i/progress.gif" width="190" height="14">';

  $('replyform').style.display = 'none';

  new Ajax.Updater('comments',
                   '/projects/task.html?taskID=' + taskID + '&projectID=' + projectID + '&digest=' + encodeURIComponent(digest) + '&only_comments=1',
                   {asynchronous: true, 
                    parameters: data,
                    onSuccess: PostCommentReplyUnlock }); 

  return false;
 }

function DeleteComment(wtf, commentID)
 {
  wtf.style.color = '#909090';

  new Ajax.Updater('comments',
                   '/projects/task.html?taskID=' + taskID + '&projectID=' + projectID + '&digest=' + encodeURIComponent(digest) + '&delete_comment=' + commentID,
                   {asynchronous: true }); 

  return false;
 }

function SwitchUpload()
 {
  $('uploadclick').style.display = 'none';
  $('attachment').style.display = 'inline';

  return false;
 }

function Flag(i, kind, id)
 {
  new Ajax.Updater('fl' + i,
                   '/ajax/flag.html?kind=' + kind + '&id=' + id + '&digest=' + encodeURIComponent(digest),
                   {asynchronous: true }); 

  if ($('favour'))
   new Ajax.Updater('favour',
                    '/ajax/favour.html?digest=' + encodeURIComponent(digest),
                    {asynchronous: true });

  return false;
 }

function ProjectCommandGotDelayed()
 {
  if ((pbo = $('projects_button_override')) && (pb = $('projects_button')))
   pb.innerHTML = pbo.innerHTML;
 }

function ProjectCommandGot(wtf)
 {
  setTimeout('ProjectCommandGotDelayed();', 50);
 }

function ProjectMinimizedReload(wtf)
 {
 }

function ProjectCommand(projectID, wtd, refresh_minimized)
 {
  if (refresh_minimized)
   {
    new Ajax.Updater('projects',
                     '/index.html?project_command=' + wtd + '&projectID=' + projectID + '&digest=' + encodeURIComponent(digest),
                     {asynchronous: true,
                      evalScripts: true,
                      onSuccess: function (someshit) {
                       new Ajax.Updater('projects_minimized', '/index.html?minimized=1&digest=' + encodeURIComponent(digest), {asynchronous: true });
                      } });
   }
  else
   {
    new Ajax.Updater('projects',
                     '/index.html?project_command=' + wtd + '&projectID=' + projectID + '&digest=' + encodeURIComponent(digest),
                     {asynchronous: true,
                      evalScripts: true }); 
   }

  return false;
 }

function ProjectOff(projectID)
 {
  Effect.DropOut($('project_' + projectID));

  ProjectCommand(projectID, 'minimize', true);

  return false;
 }

function ProjectOn(projectID)
 {
  Effect.SwitchOff($('project_minimized_' + projectID));

  ProjectCommand(projectID, 'maximize', true);

  return false;
 }

function ProjectUpdateSortOrder()
 {
  new Ajax.Updater('projects_gag',
                   '/index.html?project_command=update_sort_order&digest=' + encodeURIComponent(digest),
                   {asynchronous: true,
                    evalScripts: true,
                    parameters: Sortable.serialize('w'),
                    method: 'post' }); 
 }

function ProjectOption(wtf, option)
 {
  wtf.style.color = '#d0d0d0';

  new Ajax.Updater('projects',
                   '/index.html?project_option=' + option + '&digest=' + encodeURIComponent(digest),
                   {asynchronous: true, onSuccess: ProjectCommandGot, evalScripts: true }); 

  TaskListHideAway();

  return false;
 }

var tasklist_popup_coord = false;
var tasklist_basemargin = false;
var tasklist_hide_timer = false;
var tasklist_hide_timer_id = false;
var tasklist_show_timer = false;
var tasklist_show_timer_id = false;
var tasklist_last_shown = false;

function TaskListHideAway()
 {
  var d = $('tasklistpopup');
  d.style.display = 'none';
 }

function TaskListHide()
 {
//  $('debugger').innerHTML += ' tlh';

  clearTimeout(tasklist_show_timer);
  tasklist_hide_timer = setTimeout('TaskListHideAway();', 150);
  tasklist_hide_timer_id = tasklist_last_shown;
 }

function TaskListPopupLoaded(wtf)
 {
  var d = $('tasklistpopup');
  d.style.display = 'block';
 }

function TaskListPopup(taskID)
 {
  var d = $('tasklistpopup');

  d.style.display = 'none';
  d.style.left = (tasklist_popup_coord[0] - 10) + 'px';
  d.style.top = (tasklist_popup_coord[1]) + 'px';
  d.style.width = (tasklist_basemargin[0] - tasklist_popup_coord[0]) + 'px';

  new Ajax.Updater('tasklistpopup',
                   '/projects/task_popup.html?taskID=' + taskID + '&digest=' + encodeURIComponent(digest),
                   {asynchronous: true, 
                    onSuccess: TaskListPopupLoaded }); 

  tasklist_last_shown = taskID;
 }

function RowIn(b, pip)
 {
  b._className = b.className;
  b.className = b.className + ' hover';
//  b.style.background = '#f7f7f7';

  if (pip)
   {
    if (e = $('tc' + pip))
     e.style.display = 'inline';

//    if (e = $('base' + pip))
//     {
//      if (!tasklist_basemargin) 
//       tasklist_basemargin = Position.cumulativeOffset($('basemargin'));

//      tasklist_popup_coord = Position.cumulativeOffset(e);

//      clearTimeout(tasklist_show_timer);

//      tasklist_show_timer = setTimeout('TaskListPopup(' + pip + ');', 700);
//      tasklist_show_timer_id = pip;
//     }
   }
 }
 
function RowOut(b, pip)
 {
  b.className = b._className;
//  b.style.background = '#ffffff';

  if (pip)
   {
    if (e = $('tc' + pip))
     e.style.display = 'none';

//    if (tasklist_hide_timer_id == pip)
//     clearTimeout(tasklist_hide_timer);

//    if (tasklist_show_timer_id == pip)
//     clearTimeout(tasklist_show_timer);

//    $('debugger').innerHTML += ' rout';
   }
 }

function RowClick(tid)
 {
  if (a = $('task' + tid))
   a.click();
 }

// zoomer

var zoom_ninja = new Image;
zoom_ninja.src = '/i/ninja.gif';

function TaskZoomLoaded(wtf)
 {
  return false;
 }

function TaskZoom(taskID)
 {
  var img = $('');

  if ((tm = $('taskmore' + taskID)).innerHTML == '')
   {
    tm.innerHTML = '<br><img src="/i/ninja.gif" width="15" height="15" style="margin-top: 4px;">';

    $('zoom' + taskID).src = '/i/zoom_off.gif';

    new Ajax.Updater('taskmore' + taskID,
                     '/projects/task_popup.html?taskID=' + taskID + '&digest=' + encodeURIComponent(digest),
                     {asynchronous: true, 
                      onSuccess: TaskZoomLoaded }); 
   }
  else
   {
    tm.innerHTML = '';

    $('zoom' + taskID).src = '/i/zoom_on.gif';
   }

  return false;
 }

// stolen from http://www.gotdotnet.ru/Forums/Web/460064.aspx
// ...and it works :) yea

function addHandler(node, evt, func) 
 {
  if (node)
   if (node.addEventListener) 
    {
     node.addEventListener(evt, func, false); 
    } 
   else 
    {
     node.attachEvent('on' + evt, func);     
    }
 }
 
function isParent(child, parent) 
 {
  if (!child || !parent) 
   return false;

  while (true) 
   {
    if (child == parent) 
     return true;

    if (child.parentElement) 
     {
      child = child.parentElement;
     } 
    else if (child.parentNode) 
     {
      child = child.parentNode;
     } 
    else 
     {
      return false;
     }
   }
 }

function LoadTimelineFor(a, projectID)
 {
  a.style.display = 'none';

  new Ajax.Updater('timeline_holder',
                   '/timeline/?ajax=1&mode=all&no_timeline_header=1&no_project_title=1&filter=project%3A' + projectID + '&digest=' + encodeURIComponent(digest),
                   {asynchronous: true }); 

  return false;
 }

function Ajaxify(container, a)
 {
  a.style.color = '#909090';

  new Ajax.Updater(container,
                   a.href + '&ajax=1&digest=' + encodeURIComponent(digest),
                   {asynchronous: true });

  return false;
 }

function AjaxifyForm(container, f)
 {
  new Ajax.Updater(container,
                   f.action + '?' + Form.serialize($(f)) + '&ajax=1&digest=' + encodeURIComponent(digest),
                   {asynchronous: true });

  return false;
 }

addHandler(window, 'load', function() 
 {
  var node = document.getElementById('tasklistpopup');

  addHandler(node, 'mouseout', function(e) 
   {
    var target = e.relatedTarget || e.toElement;

    if (!isParent(target, node)) 
     {
      TaskListHide();
     }
   });
 });
