var interval = new Array();
var wisy = 'wysiwyg';
var time_for_search;
var superstar = 0;
var supertype = 0;
var superid = 0;
var fck_start;
var editor;
var bookedit;

function loadHTML(file, dest, param, add) 
{
  $.ajax(
  {
     url: file,
     type: 'POST',
     data: param,
     beforeSend: function()
     {
       if ( add != "+" && add != "-" && add != "s" && add != 'noslide' && add != 'noadd')
       {
         $("#"+dest).html("<img src='/images/wait.gif' align='absmidlle'/>")
                    .slideDown(700);;
       }
     },
     success: function(answ)
     {
       if ( add == "+" )
       {
         $("#"+dest).append(answ)
                     .slideDown(700);
       }
       else if ( add == "noadd" )
       {
         if ( answ != '' )
         {
            $("#"+dest).html(answ);
         }
       }
       else if ( add == "noslide" || add == "noslide2" )
       {
         $("#"+dest).html(answ);
       }
       else
       {
         if ( answ != '' )
         {
            $("#"+dest).hide()  
                        .html(answ)
                        .slideDown(700);
         }
         else
         {
            $("#"+dest).slideUp(700)
                       .html(answ)
                       ;
         }
         if ( add == 's' )
         {
            $("#"+dest).slideUp(700);
         }
       }
     }
  }
  );
}
window.onload = function starter() 
{
   setInterval("set_online()", 300000);
   setInterval("last_comment()", 30000);
   get_gallery();
   if ( miniopera != 5 && $('#wysiwyg').length )
   {
        fckstart( wisy );
   }
}
function last_comment( )
{
   loadHTML('/actions.php', "lastcomment", {action:'last_comment', time: starttime}, 'noadd');
}
function set_online()
{
   loadHTML('/actions.php', "tmp", {action:''}, '-');
}
function fckstart( param )
{
   eval('var ' + param + ' = CKEDITOR.replace("' + param + '")');
}
function prefly()
{
   if ( $("#word").val() == 'полетели' )
   {
      fly();
   }
}
function fly()
{
   
   $("div,table,ul,li,a").each(function(n,element)
   {
      if ( n < 100)
      {
         $(element).attr("style", "position:relative; display: block;");
      }
      
   }
   );
   setInterval("hausmove()", 1000);
}
function hausmove()
{
   $("div,table,ul,li,a").each(function(n,element)
      {
         if ( n < 100)
         {
            action = Math.floor(Math.random()*2);
            znak = '+'; 
            if ( action == 1 )
            {
               znak = '-';
            }
            
            action = Math.floor(Math.random()*2);
   
            if ( action == 1 )
            {
               $(element).animate({top: znak+"=50px"}, 1000);
            }
            else
            {
               $(element).animate({left: znak+"=50px"}, 1000);
            }
         }
         
      });
}
function show(id)
{
  var value = document.getElementById(id).innerHTML;
  if ( value != "" && value != "ok" )
  {
   $("#"+id).slideDown(200);
  }
}
var photomove = 0;
function showpic(id, x, y)
{
   if ( !photomove )
   {
   $("#"+id).animate({ width: x, height: y }, 150);
   $("#"+id).attr("class", "hide photo");
   }
   photomove = 1;
}
function hidepic(id, x, y)
{
   $("#"+id).animate({ width: 30, height: 30 }, 150);
   $("#"+id).attr("class", "miniphoto");
   setTimeout('photomove = 0;', 50);
}

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

function del(name, id)
{
   loadHTML('/actions.php', name+id, {action:'del',name: name, id: id}, ''); 
}
function edit(name, id)
{
   loadHTML('/actions.php', name+id, {action:'edit',name: name, id: id}, ''); 
}
function save(name, id)
{
   eval( 'var oEditor = CKEDITOR.instances.' + 'edit'+id);

   loadHTML('/actions.php', name+id, {action:'save',name: name, id: id, text: oEditor.getData()}, ''); 
}
function saveraskaz(name, id)
{
   loadHTML('/actions.php', name+id, {action:'save',name: name, id: id, text: $("#edit"+id).val(), tema: $("#tema"+id+" option:selected").val()}, ''); 
}
function showid()
{
   $(".mustshow").show(1000);
   setTimeout( "mustsel()", 1010);
}
function mustsel()
{
   $(".mustselect").select();
}
function validate(textform)
{
    
    var text = $("#"+textform).val();
    if ( text.length > maxlength )
    {
       text = text.substring(0, maxlength);
       $("#"+textform).val(text);
    }
    document.getElementById('count').innerHTML = "Осталось символов: " + (maxlength - text.length);
}
function getvoce(id, znak)
{
   loadHTML('/actions.php', "rate"+id, {action:'vote', id: id, znak: znak}, '');
}
function getilvoce(id, znak)
{
   loadHTML('/actions.php', "rate"+id, {action:'voteil', id: id, znak: znak}, '');
}
function getrvoce(id, znak)
{
   loadHTML('/actions.php', "rate"+id, {action:'rvote', id: id, znak: znak}, '');
}
function get_widget(id)
{
   loadHTML('/actions.php', "widget", {action:'getwidget', id: id}, '');
}

function get_gallery( n )
{
   loadHTML('/actions.php', "gallery", {action:'gallery', id: n}, '');
}
function move_gall( direction )
{
   if ( direction == '-1')
   {
      $("#animatronik").animate({ "left": "+=100px" }, 300);
   }
   else
   {
      $("#animatronik").animate({ "left": "-=100px" }, 300);
   }
   setTimeout( "down_gallery("+direction+")", 250);
}
function down_gallery(direction)
{
   loadHTML('/actions.php', "gallery", {action:'move_gallery', dir: direction}, "noslide");
}
function get_opros_voce(aid, pid)
{
   loadHTML('/actions.php', "opros", {action:'get_opros_voce', aid: aid, pid:pid}, '');
}

function set_stars(kolvo, id, rate, letter, type)
{
   clearTimeout(interval[id]);
   var text = '';
   for ( var i = 1; i <= 5; i++ )
   {
      if ( i <= kolvo )
      {
         text += '<a href="#" onclick="get'+letter+'konkursvoice(' + id + ', ' + i + ', '+type+'); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\', '+type+')" ><img src="/images/star.gif" border="0"/></a>';
      }
      else
      {
         text += '<a href="#" onclick="get'+letter+'konkursvoice(' + id + ', ' + i + ', '+type+'); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\', '+type+')" ><img src="/images/empty_star.gif" border="0"/></a>';
      }
   }
   $("#"+letter+"konkursrate").html(text);
   interval[id] = setTimeout("return_stars(" + rate + ", " + id + ", '" + letter + "', "+type+")", 2000);
}
function set_passive_stars(kolvo, id, letter)
{
  clearTimeout(interval[id]);
  document.getElementById(letter+"konkursrate").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+"konkursrate").html(text);
}
function return_stars(rate, id, letter, type)
{
   var text = '';
   for ( var i = 1; i <= 5; i++ )
   {
      if ( i <= rate )
      {
         text += '<a href="#" onclick="get'+letter+'konkursvoice(' + id + ', ' + i + ', '+type+'); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\','+type+')" ><img src="/images/star.gif" border="0"/></a>';
      }
      else
      {
         if ( (rate - i) > -0.5 )
         {
            text += '<a href="#" onclick="get'+letter+'konkursvoice(' + id + ', ' + i + ', '+type+'); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\','+type+')" ><img src="/images/star.gif" border="0"/></a>';
         }   
         else if ( (rate - i) > -1 )
         {
            text += '<a href="#" onclick="get'+letter+'konkursvoice(' + id + ', ' + i + ', '+type+'); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\','+type+')" ><img src="/images/poluempty_star.gif" border="0"/></a>';
         }
         else 
         {
            text += '<a href="#" onclick="get'+letter+'konkursvoice(' + id + ', ' + i + ', '+type+'); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\','+type+')" ><img src="/images/empty_star.gif" border="0"/></a>';
         }
      }
   }
   $("#"+letter+"konkursrate").html(text);
}
function getkonkursvoice(id, value, type)
{
   set_passive_stars(value, id, "");
   superstar = value;
   supertype = type;
   superid = id;
   $('#comment_box').show();
}
function getgkonkursvoice(id, value, type)
{
   set_passive_stars(value, id, "g");
   superstar = value;
   supertype = type;
   superid = id;
   $('#comment_box').show();
}
function send_stars()
{
   text = $('#comment_because').val();
   if ( text.length < 10 )
   {
      alert('Слишком короткая аргументация.');
   }
   else
   {
      loadHTML('/actions.php', 'comment_box', {action:'konkursvote', id: superid, value: superstar, type: supertype, text: text}, '');
   }
}
function send_gstars()
{
   text = $('#comment_because').val();
   if ( text.length < 10 )
   {
      alert('Слишком короткая аргументация.');
   }
   else
   {
      loadHTML('/actions.php', 'comment_box', {action:'gkonkursvote', id: superid, value: superstar, type: supertype, text: text}, '');
   }
}
function get_but()
{
   document.write('<input type="submit" value="Сохранить" name="reg_in_new"/>');
}
function get_forum_but()
{
   document.write('<input type="submit" name="new_raskaz" value="Добавить"/>');
}
function get_relay_users( uid, type, word )
{
   loadHTML('/actions.php', 'res_'+type, {action:'relay_user', uid: uid, type: type, word: word}, 'noslide');
}
function get_send_but( user_id )
{
   if ( user_id == 34 )
   {
      document.write('<input type="submit" value="Сохранить" name="newraskaz" onclick="if (!confirm(\'Вы хотите опубликоваться под Гостем?\')){ return false; }"/>');
   }
   else
   {
      document.write('<input type="submit" value="Сохранить" name="newraskaz"/>');
   }
}
function get_rifma( page )
{
   word = $('#rifma').val();
   first = $('#start').val();
   loadHTML('/actions.php', 'onemore'+page, {action:'get_rifma', word: word, page: page, first: first}, '');
}
function comm_sign( id, sign )
{
   loadHTML('/actions.php', 'sign'+id, {action:'comm_sign', id: id, sign: sign}, '');
}
function wall_sign( id, sign )
{
   loadHTML('/actions.php', 'wall'+id, {action:'wall_sign', id: id, sign: sign}, '');
}
function view( id )
{
   loadHTML('/actions.php', 'tmp', {action:'view', id: id }, '-');
}
function set_admirer( id )
{
   loadHTML('/actions.php', 'res_adm', {action:'set_admirer', id: id }, '-');
}
function del_admirer( id )
{
   loadHTML('/actions.php', 'res_adm', {action:'del_admirer', id: id }, '-');
}
function reset_voce ( id )
{
   loadHTML('/actions.php', 'reset'+id, {action:'reset_voce', id: id }, '-');
}
function get_opr ( word )
{
   loadHTML('/actions.php', 'opred', {action: 'get_word_text', word: word}, '');
}
function quote( id )
{
   showid('mustshow');
   $("#wysiwyg").val('<div class="quote"><strong>'+$("#user_q"+id).html()+'</strong><br/>'+$("#quote"+id).html()+'</div><br/><br/>');
   
   var oEditor = CKEDITOR.instances.wysiwyg;
   oEditor.setData('<div class="quote"><strong>'+$("#user_q"+id).html()+'</strong><br/>'+$("#quote"+id).html()+'</div><br/><br/>&#160;');
}
function rfollow ( id )
{
   loadHTML('/actions.php', 'folus', {action: 'rfollow', id: id}, '');
}
function rnfollow ( id )
{
   loadHTML('/actions.php', 'folus', {action: 'rnfollow', id: id}, '');
}
function ffollow ( id )
{
   loadHTML('/actions.php', 'folus', {action: 'ffollow', id: id}, '');
}
function fnfollow ( id )
{
   loadHTML('/actions.php', 'folus', {action: 'fnfollow', id: id}, '');
}
function set_public( id )
{
   value = $("#public"+id+":checked").length; 
   loadHTML('/actions.php', 'tmp', {action: 'set_public', id: id, view: value}, '');
}
function deldis ( id )
{
   loadHTML('/actions.php', 'dis'+id, {action: 'deldis', id: id}, '');
}
function set_search_filter ( id )
{
   value = $("#ch"+id+":checked").length;
   loadHTML('/actions.php', 'ok'+id, {action: 'set_search_filter', id: id, value: value}, 'noslide2');
}
function set_checkall( id )
{
   $("."+id).each(function()
   {
      if ( $("#"+$(this).attr("id")+":checked").length )
      {
         $(this).attr('checked', '');
         set_search_filter($("#"+$(this).attr("id")).val());
      }
      else
      {
         $(this).attr('checked', "checked");
         set_search_filter($("#"+$(this).attr("id")).val());
      }
   });
}
function get_users_forwhite()
{
   clearTimeout(time_for_search);
   time_for_search = setTimeout("user_search()", 1000);
}
function user_search()
{
   val = $('#filtr').val();
   loadHTML('/actions.php', 'all', {action: 'get_users_forwhite', value: val}, 'noslide2');
}
function get_users_forblack()
{
   clearTimeout(time_for_search);
   time_for_search = setTimeout("user_search2()", 1000);
}
function user_search2()
{
   val = $('#filtr2').val();
   loadHTML('/actions.php', 'black', {action: 'get_users_forblack', value: val}, 'noslide2');
}
function inblack( id )
{
   $('#a'+id).html('');
   loadHTML('/actions.php', 'black', {action: 'inblack', id: id}, 'noslide2');
}
function inwhite( id )
{
   $('#a'+id).html('');
   loadHTML('/actions.php', 'all', {action: 'inwhite', id: id}, 'noslide2');
}
function des_sign( id, sign )
{
   loadHTML('/actions.php', 'sign'+id, {action:'des_sign', id: id, sign: sign}, '');
}
function get_notebook_text ( id )
{
   loadHTML('/actions.php', 'work_area', {action:'get_notebook_text', id: id}, '');
}
function del_notebook( id )
{
   $('#a'+id).html('');
   loadHTML('/actions.php', 'work_area', {action:'del_notebook', id: id}, '');
}
function save_notebook ( id, rand )
{
   eval ( 'var oEditor = CKEDITOR.instances.wysiwyg' + rand);
   text = oEditor.getData();
   name = $('#name'+rand).val();
   loadHTML('/actions.php', 'status', {action:'save_notebook', id: id, name: name, text: text}, 'noslide2');
   return false;
}
function start_autosave ( id, rand )
{
   setInterval('save_notebook ( ' + id + ', ' + rand + ' )', 60000);
}
function opublick( id, rand )
{
   save_notebook ( id, rand );
   tema = $("#tema option:selected").val();
   konkurs = $("#konk:checked").length;
   loadHTML('/actions.php', 'ostatus', {action:'opublick', id: id, tema: tema, konkurs: konkurs}, 'noslide2');
}
function get_media()
{
   var __rt= Math.round(Math.random() * 100000);
   document.write('<scr'+'ipt language="javascript" type="text/javascript" src="http://id64132.luxup.ru/show/133649/?div=lx_133649&rt='+__rt+'&r='+escape(document.referrer)+'"><'+'/scr'+'ipt>');
}
function get_comm_but( user_id )
{
   if ( user_id == 34 )
   {
      document.write('<input type="submit" value="Добавить" name="new_com" onclick="if (!confirm(\'Вы хотите опубликоваться под Гостем?\')){ return false; }"/>');
   }
   else
   {
      document.write('<input type="submit" value="Добавить" name="new_com"/>');
   }
}
function get_wall_but( user_id )
{
   if ( user_id == 34 )
   {
      document.write('<input type="submit" value="Написать" name="new_wall" onclick="if (!confirm(\'Вы хотите опубликоваться под Гостем?\')){ return false; }"/>');
   }
   else
   {
      document.write('<input type="submit" value="Написать" name="new_wall"/>');
   }
}
function get_newvar_but( user_id )
{
   if ( user_id == 34 )
   {
      document.write('<input type="submit" value="Добавить" name="new_var" onclick="if (!confirm(\'Вы хотите опубликоваться под Гостем?\')){ return false; }"/>');
   }
   else
   {
      document.write('<input type="submit" value="Добавить" name="new_var"/>');
   }
}
function show_view ( id, color )
{
   $('#read'+id).animate({color: color}, 2000);
   loadHTML('/actions.php', 'tmp', {action:'show_view', id: id}, 'noslide');
}
