// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var show_details_init = false;
var show_comment_form_init = false;
var show_edit_answer_init = false;
var edit_answer_id = 0;
var edit_comment_id = 0;

function scrollToY(element) 
{
  var pos = element.cumulativeOffset();
  window.scrollTo(0, pos[1]);
  //document.getElementById('id').scrollTop = document.getElementById('id').offsetTop;
  //document.body.scrollTop = element.offsetTop;
}

function swap_elements(orig, to_swap)
  {
  var orig_pn = orig.parentNode;
  var next_sibling = orig.nextSibling;
  var to_swap_pn = to_swap.parentNode;
  if (! to_swap_pn)
    return false;

  to_swap_pn.replaceChild(orig, to_swap);
  orig_pn.insertBefore(to_swap, next_sibling);
  Element.show(to_swap);
  return true;
  }

function show_details(answer_id)
{
  $('answer_detail_form_body').value = '';
  if (!show_details_init)
    {
    $('answer_id').value = '';
    show_details_init = true;
    }
  else if($('answer_id').value != '')
    {
    hide_details()
    }

  swap_elements($('links_' + answer_id), $('answer_detail_form'))
  $('answer_id').value = answer_id;
  $('answer_detail_form_tag').onsubmit = function()
  {
    $('answer_detail_form_button').disabled=true; 
    $('answer_detail_form_button').value="Подождите..."; 
    /* if ($('answer_comments_div_' + answer_id))
    {
      $('answer_detail_form_last_comment').value = $('last_comment_id_' + answer_id).value
      new Ajax.Updater('answer_new_comment_div_'+answer_id, '/questions/add_answer_detail/', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this), onComplete:function(request){clearDetailsForm(answer_id)}});
    }
    else
    {
    */
      //clear links div until duplicated by reload
      el = $('links_' + answer_id);
      el.parentNode.removeChild(el);
      show_details_init = false;
      
      new Ajax.Updater('answer_div_'+answer_id, '/questions/add_answer_detail_with_reload/', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this), onComplete:function(request){onAddDetailsWithReload(answer_id)}});
    //}
    
    return false;
  };
}

function onAddDetailsWithReload(answer_id)
{
  scrollToY($('answer_last_comment_' + answer_id));
}

function clearDetailsForm(answer_id)
{
  hide_details();
  $('answer_detail_form_button').disabled = false;
  $('answer_detail_form_button').value = 'Добавить';
  $('answer_detail_form_body').value = '';
  $('show_answer_details_link_' + answer_id).value = $('answer_show_comments_' + answer_id).value
  $('answer_last_comment_' + answer_id).scrollTo(); 
}

function hide_details()
{
  swap_elements($('answer_detail_form'), $('links_'+$('answer_id').value))
  $('answer_id').value = '';
}

function show_edit_answer_form(answer_id, urlBody, urlSource)
{
  if (edit_answer_id != 0)
  {
    hide_edit_answer_form()
  }

  $('edit_answer_form_button').disabled = false; 
  $('edit_answer_form_button').value = "Сохранить"; 
    
  $('edit_answer_body').value = "Загружается...";
  $('edit_answer_body').disabled = true;
  $('edit_answer_source').value = "Загружается...";
  $('edit_answer_source').disabled = true;
  swap_elements($('answer_fields_' + answer_id), $('edit_answer_form'));
  $('links_' + answer_id).hide();
  edit_answer_id = answer_id;
  
  //Get answer and source values
  new Ajax.Request(urlBody,
  {
    method:'get',
    onSuccess: function(transport){
      $('edit_answer_body').value = transport.responseText;
      $('edit_answer_body').disabled = false
    },
    onFailure: function(){ alert('Can not get answer body...') }
  });

  new Ajax.Request(urlSource,
  {
    method:'get',
    onSuccess: function(transport){
      $('edit_answer_source').value = transport.responseText;
      $('edit_answer_source').disabled = false;
    },
    onFailure: function(){ alert('Can not get answer source...') }
  });
  
  $('edit_answer_form_tag').onsubmit = function()
  {
    $('edit_answer_form_button').disabled=true; 
    $('edit_answer_form_button').value="Подождите..."; 
    
    hide_edit_answer_form()
    
    new Ajax.Updater('answer_fields_'+answer_id, 
      '/questions/update_answer/' + answer_id, 
      {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)});
    
    return false;
  };
}

function hide_edit_answer_form()
{
  $('links_' + edit_answer_id).show();
  swap_elements($('edit_answer_form'), $('answer_fields_' + edit_answer_id))
  edit_answer_id = 0;
}

function createCommentMenu(comment, url_destroy, url_get_comment, can_delete, editable){
  items = new Array();
  next_item = 1;
  if (can_delete) items[next_item++] = new MenuItem('Удалить','if(confirm(\'Вы уверены что хотите удалить комментарий?\')) {CMenuClick(\'' + url_destroy + '\');}');
  if (editable) items[next_item++] = new MenuItem('Редактировать','show_edit_comment_form('+ comment + ', \'' + url_get_comment +'\');', '');
  comment_mm = new Array();
  comment_mm[1] = new Menu(items);

  comment_menuParam = new Array();
  comment_menuParam[0]=1;
  comment_menuParam[1]=comment_mm;
  
  return comment_menuParam;
}

function show_edit_comment_form(comment_id, urlComment)
{
  if (edit_comment_id != 0)
  {
    hide_edit_comment_form()
  }

  $('edit_comment_form_button').disabled = false; 
  $('edit_comment_form_button').value = "Сохранить"; 
    
  $('edit_comment_body').value = "Загружается...";
  $('edit_comment_body').disabled = true;
  swap_elements($('comment_fields_' + comment_id), $('edit_comment_form'));
  $('comment_links_' + comment_id).hide();
  edit_comment_id = comment_id;
  
  //Get answer and source values
  new Ajax.Request(urlComment,
  {
    method:'get',
    onSuccess: function(transport){
      $('edit_comment_body').value = transport.responseText;
      $('edit_comment_body').disabled = false
    },
    onFailure: function(){ alert('Can not get comment body...') }
  });

  $('edit_comment_form_tag').onsubmit = function()
  {
    $('edit_comment_form_button').disabled=true; 
    $('edit_comment_form_button').value="Подождите..."; 
    
    hide_edit_comment_form()
    
    new Ajax.Updater('comment_fields_' + comment_id, 
      'set_comment/' + comment_id, 
      {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)});
    
    return false;
  };
}

function hide_edit_comment_form()
{
  $('comment_links_' + edit_comment_id).show();
  swap_elements($('edit_comment_form'), $('comment_fields_' + edit_comment_id))
  edit_comment_id = 0;
}

function show_comment_form(comment_id)
{
  $('comment_form_body').value = '';
  if (!show_comment_form_init)
    {
    $('comment_id').value = '';
    show_comment_form_init = true;
    }
  else if($('comment_id').value != '')
    {
    hide_comment_form();
    }

  swap_elements($('comment_links_' + comment_id), $('comment_form'));
  $('comment_id').value = comment_id;
  
  $('comment_form_tag').onsubmit = function()
  {
    $('comment_form_button').disabled=true; 
    $('comment_form_button').value="Подождите..."; 

    //clear links div until duplicated by reload
    el = $('comment_links_' + comment_id)
    el.parentNode.removeChild(el);
    show_comment_form_init = false;

    new Ajax.Updater('comment_div_' + comment_id, 'add_comment_with_reload', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this), onComplete:function(request){onAddCommentWithReload(comment_id)}});

    return false;
  };
}

function onAddCommentWithReload(comment_id)
{
  //TODO doesn't work in IE
  //scrollToY($('comment_last_comment_' + comment_id));
}

function hide_comment_form()
{
  swap_elements($('comment_form'), $('comment_links_'+$('comment_id').value));
  $('comment_id').value = '';
}

function checkLimit(obj, maxL) {
    if (obj.value.length > maxL){
      return false;
    }
    return true;
  }


function updateCounter(obj, counterName, maxL) { 
  objCnt = $(counterName);
  objVal=obj.value;
  if (objVal.length > maxL) 
  {  
    objVal=objVal.substring(0, maxL);
    obj.value = objVal
  }
  objCnt.innerHTML = maxL - objVal.length;

  return true;
}