var cookie_site_name = 'ski-atlas.ru';
var path_to_cmt_module = '../comments/';

function ShowReplyBox(idobject, idparent, edit) {
  LastOpenedReplyBoxId = idparent;
  div = $("#prl"+idparent);

  if (typeof edit == 'undefined') {
    edit = 0;
  }
  
  var url = edit ? 'edit_box.php?id_post=' + idparent : 'reply_box.php?id_parent=' + idparent;
  url = path_to_cmt_module + url;
  if (idobject >= 0) {
    url += '&id_object=' + idobject;
  }
  url += '&r=' + Math.random();
  div.load(encodeURI(url), function() {
    var cal = $('.period INPUT', div);
    cal
      .keypress(function(e) {
        if (e.which == 8 || e.which == 27) {
          cal.val('').blur();
        }
      })
      .label()
      .datepick({
      rangeSelect: true,
      renderer: $.datepick.themeRollerRenderer,
      monthsToShow: 2
    })
    .next()
    .click(function(e) {
      e.preventDefault();
      cal.datepick('show');
    });
  });  
}

function ShowEditBox(idobject, idparent) {
  ShowReplyBox(idobject, idparent, 1);
  /*
  LastOpenedReplyBoxId = idparent;
  idDiv = "#prl"+idparent;
  $(idDiv).load(encodeURI(path_to_cmt_module+"edit_box.php?id_object="+idobject+"&id_post="+idparent+"&r="+Math.random()));
  */
}

function ReloadChilds(idobject, idparent) {
idDiv = "#childs"+idparent;
//alert(idDiv+" "+idobject);
$(idDiv).load(encodeURI(path_to_cmt_module+"replies_print.php?id_object="+idobject+"&id_parent="+idparent+"&r="+Math.random()));
}

function PostComment(idobject, idparent, idpost) {
  if (typeof idpost == 'undefined') {
    idpost = idparent;
  }
  if (CheckCmtForm(idpost)) {
    
    var post_form = $('#form' + idpost);
    $.post(
      path_to_cmt_module + 'replies_print.php',
      post_form.serialize(),
      function(data) {
        $('#childs' + idparent).html(data);
      }
    );
    $('#prl' + idpost).html('<a onclick="ShowReplyBox(' + idobject + ',' + idpost + ')">Ответить на это</a>');
  }
  return false;
}

function PostEditComment(idobject, idparent, idpost) {
  return PostComment(idobject, idparent, idpost);
  /*
  var post_form = $('#form' + idpost);
  $.post(
    path_to_cmt_module + 'replies_print.php',
    post_form.serialize(),
    function(data){
      $('#childs' + idparent).html(data);                                     
    }
  );
  $('#prl' + idpost).html('<a onclick="ShowReplyBox(' + idobject + ',' + idpost + ')">Ответить на это</a>');
  return false;
  */
}

function CloseReplyForm(idobject, idparent) {
  $('#prl' + idparent).html('<a onclick="ShowReplyBox(' + idobject + ',' + idparent + ')">Ответить на это</a>');
  return false;
}

function ReloadLoginMenu(idobject) {
  $('#cmt_tm').load(encodeURI(path_to_cmt_module+"login_menu.php?id_object="+idobject+"&r="+Math.random()));
}

                    
function LogIn(idobject) {
  var post_form = $('#loginform');
  $.post(
    path_to_cmt_module + 'login.php',
    post_form.serialize() + '&id_object=' + idobject,
    function(data) {
      $('#loginbox').html(data);
    }
  );
  return false;
}

function LogInLite(idobject) {
  var post_form = $('.auth_form');
  $.post(
    path_to_cmt_module + 'login_lite.php',
    post_form.serialize() + '&id_object=' + idobject,
    function(data) {
      if (data.match(/.*OK$/)) {
        $('body').append(data.replace(/OK$/, ''));
        window.location.reload();
      }
      else {
        alert(data);
      }
    }
  );
  return false;
}

function LogOut(idobject)
{
$.cookie('username', null, { expires: 7, path: '/', domain: cookie_site_name, secure: false});
$.cookie('md5pass', null, { expires: 7, path: '/', domain: cookie_site_name, secure: false});
idobject = false;
if (idobject === false) {
  window.location.reload();
}
else {
  ReloadLoginMenu(idobject);
  ReloadChilds(idobject, 0);
}
return false;
}

function ShowSmallLoginBox(cont)
{
  cont = $(cont);
  if (cont.html() == '') {
    cont.load(encodeURI(path_to_cmt_module+"login_box.php?lite=1&r="+Math.random())).show("slow");
  }
  else {
    cont.empty().hide();
  }
  return false;
}

function ShowLoginBox(idobject)
{
if($("#loginbox").html()==""){$("#loginbox").load(encodeURI(path_to_cmt_module+"login_box.php?id_object="+idobject+"&r="+Math.random()));$("#loginbox").show("slow");}
else{$("#loginbox").html("");$("#loginbox").hide("slow");}
}
               
function ShowRegBox(){
  myWin= open(path_to_cmt_module+"reg_box.php", "Registration","width=400,height=400,status=no,toolbar=no,menubar=no, titlebar=no, scrollbars=yes");
  return false;
}              
$(document).ready(function(){
/*
Не удалять. Не знаю почему, но без этой пустой функции мозилла перестает логиниться
*/
});
       
function CheckCmtForm(idpost) {
	var formErrors = false;
	if ($('#cmt_title'+idpost).val().length < 4 && $('#cmt_text'+idpost).val().length< 4) {
		formErrors = "Напишите что-нибудь содержательное!";
  }
	if (formErrors) {
		alert(formErrors);
		return false;
	}
  else {			
		return true;
	}
}



