/*$(document).ready(function () {
   $("#second_navigation li").cornerz({radius:15,
                                       //borderWidth: 1,
                                       corners: "tl tr"});

   $("#art_bar_container").cornerz({radius:12});
   $("#block_last_publish").cornerz({radius:12});
   $("#block_clouds").cornerz({radius:12});
   $("#user_karma").cornerz({radius:12});

}); */
function select_vuz(self){
   var data = $.metadata.get(self);
   $(".lv_"+(data.level+1)).hide();
   $(".lv_"+(data.level+2)).hide();

   if($(self).val()>0){
      $(".lv_"+(data.level+1)).show().load('/ajax/get/vuz/'+$(self).val());
   }

   $("input[name='ad9']").val($(self).val());
}

function setcookie (name, value, the_time) {
        var expDate = new Date();
        expDate.setTime (expDate.getTime() + (the_time * 1000));
        document.cookie = name + "=" + escape (value) + "; expires=" + expDate.toGMTString ();
}


function add_art_rating (the_id, the_value){

        $.post(
                   "/include/ajax/articles/add_art_rating.php",
                { "id": the_id, "value": the_value },
                function(ret) { $("#rating_" + the_id).html(ret); }
        );

}

function add_com_rating (the_id, the_value){

        $.post(
                   "/include/ajax/comments/comments_rating.php",
                { "id": the_id, "value": the_value },
                function(ret) { $("#comm_rating_" + the_id).html(ret); }
        );

}

function add_answer (the_id, module, idi){
        var the_form = "<div id='add_"+the_id+"' class='add_proc'></div><form name='answer' id='answer_form_" + the_id + "' action='' onsubmit=\"update_comments('" + module + "', " + the_id +", " + idi + "); return false;\" method=post><input type=hidden name=parent value='" + the_id + "'><textarea name=com_body id='textarea_" + the_id + "' rows=3 style='width:90%'></textarea>"+
                 "<br><input type=button value='скрыть' onclick='close_answer(" + the_id + ")'> <input type=submit value='додати'></form>";
        var the_element = document.getElementById('answer_' + the_id);
        if (the_element.style.display == "none") {
                //the_element.style.display = "";
                the_element.innerHTML = the_form;
                $('#answer_'+the_id).slideDown('slow');
                document.answer.com_body.focus();
        }
        else {
                //the_element.style.display = "none";
                $('#answer_'+the_id).slideUp('slow');
                the_element.innerHTML = "";
        }
}

function img_upload(){
         var fancy = $("#fancy_div");
         $(fancy).find("#comment_div a").removeClass("curr");
         $(fancy).find("a#img_edit").addClass("curr");
         $(fancy).find("#comment_write").show();
         $(fancy).find("#comment_err").hide();
         $(fancy).find("#comment_view").hide();
         return false;

}

function img_params(){
         var fancy = $("#fancy_div");
         $(fancy).find("#comment_div a").removeClass("curr");
         $(fancy).find("a#img_view").addClass("curr");
         $(fancy).find("#comment_write").hide();
         $(fancy).find("#comment_err").hide();
         $(fancy).find("#comment_view").show();
         return false;
}

function img_insert(){
         var fancy = $("#fancy_div");
         $(fancy).find("#comment_div a").removeClass("curr");
         $(fancy).find("a#img_send").addClass("curr");
         $(fancy).find("#comment_write").hide();
         $(fancy).find("#comment_view").hide();
         $(fancy).find("#comment_err").html("<img src='/js/loading.gif' />").show();

         var align = $(fancy).find("#comment_view select").val();
         var title = $(fancy).find("#comment_view textarea").val();
         $.post('/ajax/insert/img/', {align: align, title: title},  function(ret) { insert_tag(ret, ''); $.fn.fancybox.close();} );


         return false;
}

function comm_view(){
         var fancy = $("#fancy_div");
         $(fancy).find("#comment_div a").removeClass("curr");
         $(fancy).find("a#comm_view").addClass("curr");
         $(fancy).find("#comment_write").hide();
         $(fancy).find("#comment_view").html($(fancy).find("#comment_write textarea").val()).show();
         return false;

}

function comm_edit(){
         var fancy = $("#fancy_div");
         $(fancy).find("#comment_div a").removeClass("curr");
         $(fancy).find("a#comm_edit").addClass("curr");
         $(fancy).find("#comment_write").show();
         $(fancy).find("#comment_view").hide();
         return false;
}

function update_comments(the_module, the_id, idi){

       idi = comment.cid;
       the_id = comment.pid;

       var body = $("#fancy_div #textarea_0").val();

       if(body!=''){

           $.fn.fancybox.close();

           $.post(
                      "/include/ajax/comments/update_comments.php",
                   {
                           "id": idi,
                           "parent": the_id,
                           "module": the_module,
                           "com_body": body
                   },
                   function(ret) {
                        $("#comments_div").html(ret);
                        $("#textarea_0").attr("value", "");
                        $('.add_process').html('');
                        $("a.answer_comment").mouseup(function(){comment = $(this).metadata();}).fancybox({"zoomSpeedIn": 0, "zoomSpeedOut": 0, "overlayShow": true});
                    }
           );

           $("#comments_div").html('<img src="/js/loading.gif">');
       }
       else{
         var fancy = $("#fancy_div");
         $(fancy).find("#comment_div a").removeClass("curr");
         $(fancy).find("a#comm_send").addClass("curr");
         $(fancy).find("#comment_write").hide();
         $(fancy).find("#comment_view").html('Я не можу надіслати порожній коментар!').show();
       }

}

function reload_captcha () {
        var rndval = new Date().getTime();
        document.getElementById('captcha_image').innerHTML = "<img src='/image.php?mod=bot&rnd=" + rndval + "' align='absmiddle' />";
}

function add_friend (the_id){

        $.post(
                   "/include/ajax/users/add_friend.php",
                { id: the_id },
                function(ret) { $("#friends_list_false").html(ret); }
        );
}

function change_karma (the_id, the_value){

        $.post(
                   "/include/ajax/users/karma.php",
                { "user_id": the_id, "value": the_value },
                function(ret) { $("#karma_value").html(ret); }
        );
}

function vote_blog_karma (the_value, the_id){

        setcookie ("blog_" + the_id, "1", 36000);
        $.post(
                   "/include/ajax/articles/karma.php",
                { "blog_name": the_id, "value": the_value },
                function(ret) { $("#karma_value").html(ret); }
        );

}

function blog (the_id, type){

        var the_type = "exit";

        if (type == "join") the_type = "join";

        $.post(
                   "/include/ajax/articles/blog.php",
                { "blog_id": the_id, "type": the_type },
                function(ret) { $("#blog_options").html(ret); }
        );
}

function close_answer (the_id) {
        $('#answer_'+the_id).slideUp('slow');
}

function check_name(what, layer){

        var requestFile = "/include/ajax/users/check_reg.php";

        if (what == 'name') {
                $.get(
                        requestFile,
                        { "mod": what, "username": document.regist.name.value },
                        function(ret) { $("#"+layer).html(ret); }
                );
        }
        if (what == 'login') {
                $.get(
                        requestFile,
                        { "mod": what, "userlogin": document.regist.login.value },
                        function(ret) { $("#"+layer).html(ret); }
                );

        }
        if (what == 'kod') {
                $.get(
                        requestFile,
                        { "mod": what, "kod": document.regist.kod.value },
                        function(ret) { $("#"+layer).html(ret); }
                );

        }
        if (what == 'email') {
                $.get(
                        requestFile,
                        { "mod": what, "usermail": document.regist.email.value },
                        function(ret) { $("#"+layer).html(ret); }
                );

        }

}


//-----------------------------------------------------------------------------------

var mouseY;
var textarea_height;
var undo='';
var quote=false;

//undo
function text_undo(){
  if (undo) document.getElementById("htmlarea").value=undo;
  document.getElementById("undoimg").src="/images/editor/undo_grey.gif";
}
function text_save_undo(){
  undo=document.getElementById("htmlarea").value;
  document.getElementById("undoimg").src="/images/editor/undo.gif";
}
function text_clear_undo(){
  undo='';
  document.getElementById("undoimg").src="/images/editor/undo_grey.gif";
}
//

//cursor
function getCursor(input)
{
  var result = { start: 0, end: 0 };
  if (input.setSelectionRange)
  {
    result.start= input.selectionStart;
    result.end = input.selectionEnd;
  }
  else if (!document.selection) { return 0; }
  else if (document.selection && document.selection.createRange)
  {

    var range = document.selection.createRange();
    var stored_range = range.duplicate();
    stored_range.moveToElementText(input);
    stored_range.setEndPoint('EndToEnd', range);
    result.start = stored_range.text.length - range.text.length;
    result.end = result.start + range.text.length; //}


  }
  return result;
}
function setCursor(txtarea, start, end){

  if(txtarea.createTextRange) {
        var range = txtarea.createTextRange();
        range.move("character", start);
        range.select();
    } else if(txtarea.selectionStart) {
        txtarea.setSelectionRange(start, end);
    }

}

function insert_tag(starttag,endtag){
text_save_undo();

var txtarea = document.getElementById("htmlarea");
txtarea.focus();
var scrtop = txtarea.scrollTop;
var cursorPos=getCursor(txtarea);

if (cursorPos.start==cursorPos.end)
{
var nuCursorPos=cursorPos.start+starttag.length;
txtarea.value = txtarea.value.substring(0,cursorPos.start)+starttag+endtag+txtarea.value.substr(cursorPos.start );
setCursor(txtarea,nuCursorPos,nuCursorPos);
}
else{
var txt_pre=txtarea.value.substring (0,cursorPos.start);
var txt_sel=txtarea.value.substring(cursorPos.start,cursorPos.end);
var txt_aft=txtarea.value.substring(cursorPos.end);
txtarea.value = txt_pre+starttag+txt_sel+endtag+txt_aft;
var nuCursorPos=String(txt_pre+starttag+txt_sel+endtag).length;
setCursor(txtarea,nuCursorPos,nuCursorPos);
}
if (scrtop) txtarea.scrollTop=scrtop;
}

function insert_text_quote(){
  var txtarea = document.getElementById("htmlarea");
  var cur = getCursor(txtarea);
  if (cur.start!=cur.end){
    insert_tag("&laquo;", "&raquo;");
  }
  else
  {
    document.getElementById("quoteimg").src=quote?"/images/editor/raquo.gif":"/images/editor/laquo.gif";
    insert_tag("", quote?"&laquo;":"&raquo;");
    quote=!quote;
  }
}
function insert_text_mdash(){
  insert_tag("", "&mdash;");
}
function insert_text_blockquote(){
  insert_tag("<blockquote>", "</blockquote>");
}
function insert_text_b(){
  insert_tag("<strong>", "</strong>");
}
function insert_text_img(){

  document.getElementById("picuploadform").style.display="block";
 // document.getElementById("screen").style.display="block";
}
function insert_text_img_close(){
  document.getElementById("picuploadform").style.display="none";
 // document.getElementById("screen").style.display="none";
}
function insert_text_i(){
  insert_tag("<em>", "</em>");
}
function insert_text_user(){
  var txtarea = document.getElementById("htmlarea");
  var cur = getCursor(txtarea);

  var username=window.prompt("Кто?", '');
  if (username){
    if (cur.start!=cur.end){
       insert_tag('<hh user="'+username+'">', '</hh>');
    }
    else
    {
      insert_tag('', '<hh user="'+username+'" />');
    }
  }
}
function insert_text_u(){
  insert_tag("<u>", "</u>");
}
function insert_text_s(){
  insert_tag("<s>", "</s>");
}
function insert_text_link(){
  var txtarea = document.getElementById("htmlarea");
  var cur = getCursor(txtarea);

  if (cur.start!=cur.end){
    var hreff=window.prompt("Введіть URL лінку", '');
    var regexx = new RegExp('([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)');


    if (hreff && hreff.match(regexx)) {
        insert_tag('<a href="'+hreff+'">', "</a>");
    }
    else alert("Это не URL");
  }
  else{
    alert("Спочатку виділіть текст, який ви бажаєте зробити посиланням");
  }
}
function resize_mousemove(evt){
  if (!evt) evt=this.event;
  if (evt.pageY)
    var newheight=evt.pageY-mouseY;
  else
    var newheight=evt.clientY-mouseY;
  if (newheight>=50){
    document.getElementById("htmlarea").style.height=newheight+"px";
    textarea_height=newheight;
  }
}
function resize_start(evt){
  if (!evt) evt=this.event;
  if (!textarea_height) textarea_height=100;
  if (evt.pageY)
    mouseY=evt.pageY-textarea_height;
  else
    mouseY=evt.clientY-textarea_height;
  if (document.addEventListener){ //GECKO
    document.addEventListener('mousemove',resize_mousemove,false);
  }
  else{//IE
    document.attachEvent('onmousemove',resize_mousemove);
  }
}

function resize_stop(){
  if (document.addEventListener){ //GECKO
    document.removeEventListener('mousemove',resize_mousemove,false);
  }
  else {//IE
    document.detachEvent('onmousemove',resize_mousemove);
  }
}

if (document.addEventListener){ //GECKO
  document.addEventListener('mouseup',resize_stop,false);
}
else {//IE
  document.attachEvent('onmouseup',resize_stop);
}