var interval = new Array();
function loadHTML(file, dest, param, add) 
{
  $.ajax(
  {
     url: file,
     type: 'POST',
     data: param,
     beforeSend: function()
     {
       if ( add != "+" && add != "-" && add != "s" && add != 'noslide' )
       {
         $("#"+dest).html("<img src='/images/wait.gif'/>")
                    .slideDown(700);;
       }
     },
     success: function(answ)
     {
       if ( add == "+" )
       {
         $("#"+dest).append(answ)
                     .slideDown(700);
       }
       else if ( add == "noslide" )
       {
         $("#"+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() 
{
   get_gallery();
}
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)
{
   loadHTML('/actions.php', name+id, {action:'save',name: name, id: id, text: $("#edit"+id).val()}, ''); 
}
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)
{
   loadHTML('/actions.php', "rate"+id, {action:'vote', id: id}, '');
}
function getilvoce(id, znak)
{
   loadHTML('/actions.php', "rate"+id, {action:'voteil', id: id, znak: znak}, '');
}
function getrvoce(id)
{
   loadHTML('/actions.php', "rate"+id, {action:'rvote', id: id}, '');
}
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)
{
   clearTimeout(interval[id]);
   var text = '';
   for ( var i = 1; i <= 5; i++ )
   {
      if ( i <= kolvo )
      {
         text += '<a href="#" onclick="getkonkursvoice(' + id + ', ' + i + '); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\')" ><img src="/images/star.gif" border="0"/></a>';
      }
      else
      {
         text += '<a href="#" onclick="getkonkursvoice(' + id + ', ' + i + '); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\')" ><img src="/images/empty_star.gif" border="0"/></a>';
      }
   }
   $("#konkursrate").html(text);
   interval[id] = setTimeout("return_stars(" + rate + ", " + id + ", '" + letter + "')", 2000);
}
function set_passive_stars(kolvo, id, letter)
{
  clearTimeout(interval[id]);
  document.getElementById("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"/>';
      }
   }
   $("#konkursrate").html(text);
}
function return_stars(rate, id, letter)
{
   var text = '';
   for ( var i = 1; i <= 5; i++ )
   {
      if ( i <= rate )
      {
         text += '<a href="#" onclick="getkonkursvoice(' + 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="getkonkursvoice(' + 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="getkonkursvoice(' + 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="getkonkursvoice(' + id + ', ' + i + '); return false;" onmousemove="set_stars(' + i + ', ' + id + ', ' + rate + ', \'' + letter + '\')" ><img src="/images/empty_star.gif" border="0"/></a>';
         }
      }
   }
   $("#konkursrate").html(text);
}
function getkonkursvoice(id, value)
{
   set_passive_stars(value, id, "");
   loadHTML('/actions.php', 'konkursrate', {action:'konkursvote', id: id, value: value}, '+');
}