var interval = new Array();
var hint;
var delay_hint;

function loadHTML(file, dest, param, add) 
{
  $.ajax(
  {
     url: file,
     type: 'POST',
     data: param,
     beforeSend: function()
     {
       if ( add != "+" && add != "-" && add != "s" && add != 'toggle' )
       {
         $("#"+dest).html("<img src='/images/wait.gif'/>")
                    .slideDown(700);;
       }
     },
     success: function(answ)
     {
       if ( answ != '' )
       {
          if ( add == "+" )
          {
            $("#"+dest).hide()  
                        .append(answ)
                        .slideDown(700);
          }
          else if ( add == '-' )
          {
            $("#"+dest).hide()  
                        .html(answ)
                        .slideDown(0);
          }
          else if ( add == 'toggle' )
          {
            $("#"+dest).html(answ);
          }
          else
          {
            if ( answ != '' )
            {
               $("#"+dest).hide()  
                           .html(answ)
                           .slideDown(700);
            }
            else
            {
               $("#"+dest).slideUp(0)
                          .html(answ)
                          ;
            }
            if ( add == 's' )
            {
               $("#"+dest).slideUp(700);
            }
          }
        }
        else
        {
          $("#"+dest).slideUp(0);
        }
     }
  }
  );
}


function show(id)
{
  var value = document.getElementById(id).style.display;
  
  if ( value == 'none')
  {
     $("#"+id).slideDown(200);
  } 
  else
  {
     hide(id);
  }
}

function pause_show(id, time)
{
  clearTimeout(hint);
  var text = document.getElementById(id).innerHTML;
  var value = document.getElementById(id).style.display;
  if ( value != 'block' && value != '' && text != '' )
  {
      $("#"+id).slideDown(0);
  }
}
function hide( id )
{
   $("#"+id).slideUp(200);
}

function pause_hide( id )
{
   hint = setTimeout('hide("'+id+'")', 100);
}
function auview(id)
{
   loadHTML('/actions.php', "tmp", {action:'author_view', id: id}, '-');
}

function file_view(id)
{
   loadHTML('/actions.php', "tmp", {action:'aforizm_view', id: id}, '-');
}

function getvoice(id, value)
{
   set_passive_stars(value, id, "");
   loadHTML('/actions.php', 'rate'+id, {action:'vote', id: id, value: value}, '+');
}

function getavoice(id, value)
{
   set_passive_stars(value, id, "a");
   loadHTML('/actions.php', "arate"+id, {action:'avote', id: id, value: value}, '+');
}

function get_answer(id)
{
   loadHTML('/actions.php', "result", {action:'answer', id: id}, '');
}
function add_to_funs(id)
{
   loadHTML('/actions.php', "addfuns", {action:'funs', id: id}, '-');
}
function set_stars(kolvo, id, rate, letter)
{
   clearTimeout(interval[id]);
   var text = '';
   for ( var i = 1; i <= 5; i++ )
   {
      if ( i <= kolvo )
      {
         text += '<a href="#" onclick="get' + letter + 'voice(' + id + ', ' + i + '); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\')" ><img src="/images/icons/star1.png" /></a>';
      }
      else
      {
         text += '<a href="#" onclick="get' + letter + 'voice(' + id + ', ' + i + '); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\')" ><img src="/images/icons/star3.png" /></a>';
      }
   }
   $("#" + letter + "rate"+id).html(text);
   interval[id] = setTimeout("return_stars(" + rate + ", " + id + ", '" + letter + "')", 2000);
}
function set_passive_stars(kolvo, id, letter)
{
  clearTimeout(interval[id]);
  document.getElementById(letter + "rate"+id).onmouseout = function fun(){}; 
   var text = '';
   for ( var i = 1; i <= 5; i++ )
   {
      if ( i <= kolvo )
      {
         text += '<img src="/images/icons/star1.png" />';
      }
      else
      {
         text += '<img src="/images/icons/star3.png" />';
      }
   }
   $("#" + letter + "rate"+id).html(text);
}
function return_stars(rate, id, letter)
{
   var text = '';
   for ( var i = 1; i <= 5; i++ )
   {
      if ( i <= rate )
      {
         text += '<a href="#" onclick="get' + letter + 'voice(' + id + ', ' + i + '); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\')" ><img src="/images/icons/star1.png" /></a>';
      }
      else
      {
         if ( (rate - i) > -0.5 )
         {
            text += '<a href="#" onclick="get' + letter + 'voice(' + id + ', ' + i + '); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\')" ><img src="/images/icons/star1.png" /></a>';
         }   
         else if ( (rate - i) > -1 )
         {
            text += '<a href="#" onclick="get' + letter + 'voice(' + id + ', ' + i + '); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\')" ><img src="<img src="/images/icons/star2.png" /></a>';
         }
         else 
         {
            text += '<a href="#" onclick="get' + letter + 'voice(' + id + ', ' + i + '); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\')" ><img src="/images/icons/star3.png" /></a>';
         }
      }
   }
   $("#" + letter + "rate"+id).html(text);
}

function set_ok( id )
{
   loadHTML('/actions.php', "status" + id, {action:'set_ok', id: id}, '');
}
function set_save( id )
{
   text = document.getElementById("text"+id).value;
   name = document.getElementById("name"+id).value;
   loadHTML('/actions.php', "status" + id, {action:'set_save', id: id, text: text, name: name}, '');
}
function set_del( id )
{
   loadHTML('/actions.php', "area" + id, {action:'set_del', id: id}, '');
}
function set_checkall()
{
   for ( var i = 0; i <= 100; i++ )
   {
      if ( $("#hide"+i).length  )
      {
         id = $("#hide"+i).val();
         loadHTML('/actions.php', "status" + id, {action:'set_ok', id: id}, '');
      }
   }
}

function search_by_author( id )
{
   loadHTML('/actions.php', "search_result", {action:'search', id: id, word: $("#quotes_word").val()}, '');
}
function add_favorite( id )
{
   loadHTML('/actions.php', "favorite", {action:'add_favorite', id: id}, '-');
}
function del_favorite( id )
{
   loadHTML('/actions.php', "del"+id, {action:'del_favorite', id: id}, '');
}
function del_fans( id )
{
   loadHTML('/actions.php', "delf"+id, {action:'del_fans', id: id}, '');
}
function set_correct( id )
{
   text = document.getElementById("text"+id).value;
   loadHTML('/actions.php', "status" + id, {action:'set_correct', id: id, text: text}, '');
}
function set_correct_del( id )
{
   loadHTML('/actions.php', "area" + id, {action:'set_correct_del', id: id}, '');
}

function set_translation( id )
{
   text = document.getElementById("text"+id).value;
   title = document.getElementById("title"+id).value;
   loadHTML('/actions.php', "status" + id, {action:'set_translation', id: id, text: text, title: title}, '');
}
function set_translation_del( id )
{
   loadHTML('/actions.php', "area" + id, {action:'set_translation_del', id: id}, '');
}


function set_quote_ok( id )
{
   loadHTML('/actions.php', "status" + id, {action:'set_quote_ok', id: id}, '');
}
function set_quote_save( id )
{
   text = document.getElementById("text"+id).value;
   title = document.getElementById("title"+id).value;
   loadHTML('/actions.php', "status" + id, {action:'set_quote_save', id: id, text: text, title: title}, '');
}
function set_quote_del( id )
{
   loadHTML('/actions.php', "area" + id, {action:'set_quote_del', id: id}, '');
}
function set_quote_checkall()
{
   for ( var i = 0; i <= 100; i++ )
   {
      if ( $("#hide"+i).length  )
      {
         id = $("#hide"+i).val();
         loadHTML('/actions.php', "status" + id, {action:'set_quote_ok', id: id}, '');
      }
   }
}

function set_author_ok( id )
{
   loadHTML('/actions.php', "status" + id, {action:'set_author_ok', id: id}, '');
}
function set_author_save( id )
{
   name = document.getElementById("name"+id).value;
   loadHTML('/actions.php', "status" + id, {action:'set_author_save', id: id, name: name}, '');
}
function set_author_del( id )
{
   loadHTML('/actions.php', "area" + id, {action:'set_author_del', id: id}, '');
}
function set_author_checkall()
{
   for ( var i = 0; i <= 100; i++ )
   {
      if ( $("#hide"+i).length  )
      {
         id = $("#hide"+i).val();
         loadHTML('/actions.php', "status" + id, {action:'set_author_ok', id: id}, '');
      }
   }
}
function get_up( id )
{
   loadHTML('/actions.php', "mean" + id, {action:'get_up', id: id}, '');
}
function get_down( id )
{
   loadHTML('/actions.php', "mean" + id, {action:'get_down', id: id}, '');
}
function get_relay_artist( id )
{
   $("#relay_author").attr("class","");
   name = $("#"+id).val();
   loadHTML('/actions.php', "relay_author", {action:'get_relay_artist', name: name}, '-');
}
function get_relay_title( id, author_id )
{
   $("#relay_title").attr("class","");
   name = $("#"+id).val();
   loadHTML('/actions.php', "relay_title", {action:'get_relay_title', name: name, aid: author_id}, '-');
}
function get_to_edit( url, width, height, id )
{
   $(document).ready(function() {
		$('#crop_div').CropImageCreate(url, width, height, 90, 90, 1);
	});
	$("#img").val(url);
	$("#id").val(id);
	$('#button').show();
}
function showData( obj, url ) 
{
   $("#left").val(obj.left);
   $("#top").val(obj.top);
	$("#width").val(obj.width);
   $("#height").val(obj.height);
   
   //alert($("#left").val()+" "+$("#top").val()+" "+$("#width").val()+" "+$("#height").val()+" "+$("#img").val());
   loadHTML('/actions.php', 'get_result', {action: "crop", left: $("#left").val(), top: $("#top").val(), width: $("#width").val(), height: $("#width").val(), url: $("#img").val(), id: $("#id").val() }, '');
}
function delete_ava( url, id )
{
   loadHTML('/actions.php', 'ava'+id, {action: "delete_ava", url: url, id: id }, '');
}
function set_photo_ok( id )
{
   loadHTML('/actions.php', 'status'+id, {action: "set_photo_ok", id: id }, '');
}
function set_photo_zero( id )
{
   loadHTML('/actions.php', 'status'+id, {action: "set_photo_zero", id: id }, '');
}
function set_photo_unpic( id )
{
   loadHTML('/actions.php', 'status'+id, {action: "set_photo_unpic", id: id }, '');
}
function set_photo_moderu( url, text, pos )
{
   loadHTML('/actions.php', 'error_bad'+pos, {action: "set_photo_moderu", url: url, text: text }, '');
}
function del_url ( url, pos )
{
   loadHTML('/actions.php', 'status'+pos, {action: "del_url", url: url }, '');
}
function del_from_url ( url, pos )
{
   loadHTML('/actions.php', 'status'+pos, {action: "del_from_url", url: url }, '');
}
function get_more_comment( n, id )
{
   $("#com"+n).hide();
   loadHTML('/actions.php', 'add'+n, {action: "get_more_comment", n: n, id: id }, '-');
}
function get_more_text( id, page )
{
   loadHTML('/actions.php', 'text_add'+page, {action: "get_more_text", id: id, page: page }, '-');
}
function send_comment()
{
   text = $('#comment').val(); 
   name = $('#name').val();
   capcha = $('#capcha_com').val();
   id = $('#id').val();
   loadHTML('/actions.php', 'comment_add', {action: "comment_add", text: text, name: name, capcha: capcha, id: id }, '');
}
function checkall( clas, id )
{
   if ($("#"+id+":checked").length)
   {
      $("."+clas+":unchecked").attr("checked","checked");
   }
   else
   {
      $("."+clas+":checked").attr("checked","");
   }
}
function get_word_hint( value )
{
   clearInterval( delay_hint );
   delay_hint = setTimeout('delay_word_hint("'+value+'")', 300);
}
function delay_word_hint( value )
{
   loadHTML('/actions.php', "hint", {action:'get_word_hint', word: value}, '-');
}
function zapros_plus( id )
{
   loadHTML('/actions.php', "add", {action:'zapros_plus', id: id}, '');
}

function set_zapros_ok( id )
{
   loadHTML('/actions.php', "status" + id, {action:'set_zapros_ok', id: id}, '');
}
function set_zapros_save( id )
{
   text = document.getElementById("text"+id).value;
   name = document.getElementById("name"+id).value;
   topic = $("#topic"+id+" option:selected").val();
   type = $("#type"+id+" option:selected").val();
   loadHTML('/actions.php', "status" + id, {action:'set_zapros_save', id: id, text: text, name: name, topic: topic, type: type}, '');
}
function set_zapros_del( id )
{
   loadHTML('/actions.php', "area" + id, {action:'set_zapros_del', id: id}, '');
}
function set_zapros_checkall()
{
   for ( var i = 0; i <= 100; i++ )
   {
      if ( $("#hide"+i).length  )
      {
         id = $("#hide"+i).val();
         loadHTML('/actions.php', "status" + id, {action:'set_zapros_ok', id: id}, '');
      }
   }
}

function set_otvet_ok( id )
{
   loadHTML('/actions.php', "status" + id, {action:'set_otvet_ok', id: id}, '');
}
function set_otvet_save( id )
{
   var oEditor = FCKeditorAPI.GetInstance('text'+id) ;
   text = oEditor.GetHTML();
   
   name = document.getElementById("name"+id).value;
   descr = $("#descr"+id+"").val();
   
   loadHTML('/actions.php', "status" + id, {action:'set_otvet_save', id: id, text: text, name: name, descr: descr}, '');
}
function set_otvet_del( id )
{
   loadHTML('/actions.php', "area" + id, {action:'set_otvet_del', id: id}, '');
}
function set_otvet_checkall()
{
   for ( var i = 0; i <= 100; i++ )
   {
      if ( $("#hide"+i).length  )
      {
         id = $("#hide"+i).val();
         loadHTML('/actions.php', "status" + id, {action:'set_otvet_ok', id: id}, '');
      }
   }
}

function del_mat_checkall()
{
   for ( var i = 0; i <= 100; i++ )
   {
      if ( $("#hide"+i).length  )
      {
         id = $("#hide"+i).val();
         loadHTML('/actions.php', "area" + id, {action:'set_del', id: id}, '');
      }
   }
}
function set_mat_checkall()
{
   for ( var i = 0; i <= 100; i++ )
   {
      if ( $("#hide"+i).length  )
      {
         id = $("#hide"+i).val();
         loadHTML('/actions.php', "status" + id, {action:'set_mat_ok', id: id}, '');
      }
   }
}
function set_mat_ok( id )
{
   loadHTML('/actions.php', "status" + id, {action:'set_mat_ok', id: id}, '');
}
function add_censor_word( )
{
   var val = $('#words').val();
   loadHTML('/actions.php', "add", {action:'add_censor_word', word: val}, '-');
}
function get_censor_word()
{
   loadHTML('/actions.php', "listwords", {action:'get_censor_word'}, '-');
}
function del_censor_word( id )
{
   loadHTML('/actions.php', "a"+id, {action:'del_censor_word', id: id}, '-');
}

function del_zmat_checkall()
{
   for ( var i = 0; i <= 100; i++ )
   {
      if ( $("#hide"+i).length  )
      {
         id = $("#hide"+i).val();
         loadHTML('/actions.php', "area" + id, {action:'set_zapros_del', id: id}, '');
      }
   }
}
function set_zmat_checkall()
{
   for ( var i = 0; i <= 100; i++ )
   {
      if ( $("#hide"+i).length  )
      {
         id = $("#hide"+i).val();
         loadHTML('/actions.php', "status" + id, {action:'set_zmat_ok', id: id}, '');
      }
   }
}
function set_zmat_ok( id )
{
   loadHTML('/actions.php', "status" + id, {action:'set_zmat_ok', id: id}, '');
}
function add_file()
{
   $('#files').append('<input type="file" name="file[]"/><br/>');
}
function get_sub( value, name, clas, onclick )
{
   document.write('<input type="submit" value="'+value+'" name="'+name+'" class="'+clas+'" onclick="'+onclick+'"/>');
}

function set_dcma_ok( id )
{
   loadHTML('/actions.php', "status" + id, {action:'set_dcma_ok', id: id}, '');
}
function del_dcma_checkall()
{
   for ( var i = 0; i <= 100; i++ )
   {
      if ( $("#hide"+i).length  )
      {
         id = $("#hide"+i).val();
         loadHTML('/actions.php', "area" + id, {action:'set_dcma_del', id: id}, '');
      }
   }
}
function set_dcma_del( id )
{
   loadHTML('/actions.php', "area" + id, {action:'set_dcma_del', id: id}, '');
}
function set_dcma_hide( id )
{
   loadHTML('/actions.php', "area" + id, {action:'set_dcma_hide', id: id}, '');
}
function get_ajax_page()
{
   loadHTML('/actions.php', 'all', {action: "get_ajax_dic" }, 'toggle');
}

function set_ref_ok( id )
{
   loadHTML('/actions.php', "status" + id, {action:'set_ref_ok', id: id}, '');
}
function set_ref_save( id )
{
   var oEditor = FCKeditorAPI.GetInstance('text'+id) ;
   text = oEditor.GetHTML();
   
   name = document.getElementById("name"+id).value;
   descr = $("#descr"+id+"").val();
   
   loadHTML('/actions.php', "status" + id, {action:'set_ref_save', id: id, text: text, name: name, descr: descr}, '');
}
function set_ref_del( id )
{
   loadHTML('/actions.php', "area" + id, {action:'set_ref_del', id: id}, '');
}
function set_ref_checkall()
{
   for ( var i = 0; i <= 100; i++ )
   {
      if ( $("#hide"+i).length  )
      {
         id = $("#hide"+i).val();
         loadHTML('/actions.php', "status" + id, {action:'set_ref_ok', id: id}, '');
      }
   }
}
function add_link( id )
{
   var code = '';
   lnk = prompt('Вставте ссылку');
   if ( lnk )
   {
      descr = prompt('Напишите название ссылки');
      if ( descr )
      {
         code = '[url=' + lnk + ']' + descr + '[/url]'
      }
      else
      {
         code = '[url]' + lnk + '[/url]'
      }
      insertText( eval('document.' + id), code)
   }
   else
   {
      return false;
   }
}
function storeCaret(element)  
{  
  if (document.selection && document.selection.createRange)
  {  
      element.caretPos=document.selection.createRange().duplicate();
  }   
}  
 
function insertText(element,text)  
{  
     if (element && element.caretPos)
     {  
        element.caretPos.text=text;
     }  
     else if (element && element.selectionStart+1 && element.selectionEnd+1)
     {  
        element.value=element.value.substring(0,element.selectionStart)+text+element.value.substring(element.selectionEnd,element.value.length);
     }  
     else if (element)
     {  
        element.value+=text;
     }
}
function get_rnd_question( )
{
   loadHTML('/actions.php', "add_answ_block", {action:'get_rnd_question'}, '');
}
function not_auth_add()
{
   id = $('#id').val();
   name = $('#name').val();
   answer = $('#answer').val();
   capcha = $('#capcha').val();
   loadHTML('/actions.php', "return", {action:'return_answer_not_auth', id: id, name: name, answer: answer, capcha: capcha}, '');
}
function auth_add()
{
   id = $('#id').val();
   answer = $('#answer').val();
   loadHTML('/actions.php', "return", {action:'return_answer_auth', id: id, answer: answer}, '');
}
function check_capcha( id )
{
	 capcha = $('#capchaname').val();
   loadHTML('/actions.php', "capcha", {action:'check_capcha', id: id, capcha: capcha }, '');
}
function del_download( id )
{
   loadHTML('/actions.php', "del_status", {action:'del_download', id: id }, '');
}
