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")
       {
         $("#"+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 ( 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).innerHTML;
  if ( value != "" && value != "ok" )
  {
   $("#"+id).slideDown(200);
  }
}

function hide( id )
{
   $("#"+id).slideUp(200);
}

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 author_view(id)
{
   loadHTML('/actions.php', "tmp", {action:'author_view', id: id}, '-');
}

function aforizm_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/star.gif" border="0"/></a>';
      }
      else
      {
         text += '<a href="#" onclick="get' + letter + 'voice(' + id + ', ' + i + '); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\')" ><img src="/images/empty_star.gif" border="0"/></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/star.gif" border="0"/>';
      }
      else
      {
         text += '<img src="/images/empty_star.gif" border="0"/>';
      }
   }
   $("#" + 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/star.gif" border="0"/></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/star.gif" border="0"/></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="/images/poluempty_star.gif" border="0"/></a>';
         }
         else 
         {
            text += '<a href="#" onclick="get' + letter + 'voice(' + id + ', ' + i + '); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\')" ><img src="/images/empty_star.gif" border="0"/></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_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;
   accord = $("#accord"+id).attr('checked');
   tab = $("#tab"+id).attr('checked');
   loadHTML('/actions.php', "status" + id, {action:'set_quote_save', id: id, text: text, title: title, accord: accord, tab: tab}, '');
}
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 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 author_search( )
{
   loadHTML('/actions.php', "author_result", {action:'author_search', word: $("#author").val()}, '');
}
function title_search( )
{
   loadHTML('/actions.php', "title_result", {action:'title_search', word: $("#title").val(), author: $("#author").val()}, '');
}
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 showForm() {
    oDiv = document.getElementById('theFormDiv');
    oDiv.style.display='block';
    return false;
}

function hideForm() {
    oDiv = document.getElementById('theFormDiv');
    oDiv.style.display='none';
    return false;
}
function addtoduble( id )
{
   loadHTML('/actions.php', 'doublelist', {action: "add_to_double", id: id }, '');
}
function deltoduble( id )
{
   loadHTML('/actions.php', 'doublelist', {action: "del_to_double", id: id }, '');
}
function get_more_comment( n, id )
{
   $("#com"+n).hide();
   loadHTML('/actions.php', 'add'+n, {action: "get_more_comment", n: n, id: id }, '-');
}
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 get_word_hint_xml( value )
{
   loadHTML('/actions.php', "hint", {action:'get_word_hint_xml', word: value}, '-');
}
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 get_word_hint( value )
{
   loadHTML('/actions.php', "hint", {action:'get_word_hint', word: value}, '-');
}
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 get_sub( value, name, clas, onclick )
{
   document.write('<input type="submit" value="'+value+'" name="'+name+'" class="'+clas+'" onclick="'+onclick+'"/>');
}
function show_relay ( val )
{
   
   block = document.getElementById('viewrelay').style.display;
   
   if ( block == '' || block == 'block' )
   {
      $('#viewrelay').hide();
      loadHTML('/actions.php', "viewrelay", {action:'show_relay', value: 0}, '');
   }
   else
   {
      $('#viewrelay').show();
      loadHTML('/actions.php', "viewrelay", {action:'show_relay', value: 1}, '');
   }
}
