var request;
var dest;
var add = '';
var tmp;
var tmp_name;
var moveline;
var moveline_time = 2000;
var prevshow = '';
var photomove = 0;
var ax = ay = 0;
var ox = oy = 0;

window.onload = function starter() 
{
   if ( $("#gallery").length )
   {
      width = (getClientWidth()-625)+'px';
      $("#gallery").animate({ width: width}, 0);
      
      get_gallery();
      start_motion();
   }
}
function start_motion()
{
   photomove = 0;
   moveline = setInterval("move_gall(1)", moveline_time);
}
function stop_motion()
{
   clearInterval(moveline);
}
function loadHTML(file, dest, param, add) 
{
  $.ajax(
  {
     url: file,
     type: 'POST',
     data: param,
     enctype: 'multipart/form-data',
     beforeSend: function()
     {
       if ( add != "+" && add != "-" && add != "s" && add != 'noslide' && add != '+end' && add != '+start' )
       {
         $("#"+dest).html("<img src='/images/wait.gif'/>")
                    .slideDown(700);
       }
     },
     success: function(answ)
     {
       if ( add == "+start" )
       {
         $("#"+dest).html(answ + $("#"+dest).html());
       }
       else if ( add == "+end" )
       {
         $("#"+dest).html($("#"+dest).html() + answ );
       }
       else if ( add == "+" )
       {
         $("#"+dest).hide()  
                     .append(answ)
                     .slideDown(700);
       }
       else if ( add == "*" )
       {
         $("#"+dest).hide()  
                     .html(answ)
                     .slideDown(1);
       }
       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);
         }
       }
     }
  }
  );
  return 1;
}
function show(id)
{
  var value = document.getElementById(id).innerHTML;
  if ( value != "" && value != "ok" )
  {
   $("#"+id).slideDown(200);
  }
}
function hide( id )
{
   $("#"+id).slideUp(200);
}

function showsub(id)
{
   $("#"+id).show(300);
   if ( tmp_name == id )
   {
      clearTimeout(tmp);
   }
}
function hidesub(id)
{
   tmp_name = id;
   tmp = setTimeout('$("#'+id+'").hide(300)', 100);
}

function get_voce( id, pid )
{
   loadHTML('/getvoce.php', 'oprosnik', {aid: id, pid: pid }, '');
}
function checkall(id, clas)
{
   
   if ( $("#"+id).val() == 1 )
   {
      $("#"+id).val('0');
      uncheckall(clas);
   }
   else
   {
      $("#"+id).val('1');
      $("."+clas).attr("checked","checked");
   }
}
function uncheckall(clas)
{
   $("."+clas).attr("checked","");
}
function get_gallery( n )
{
   loadHTML('/actions.php', "gallery", {action:'gallery', id: n}, '');
}
function move_gall( direction )
{
   down_gallery(direction);
}

function movediv( direction, id )
{
   if ( direction > 0)
   {
      $('#ani' + id + '').animate({ "width": "0px" }, moveline_time);
   }
   else
   {
      $('#ani' + (id-10) + '').animate({ "width": "210px" }, moveline_time);
   }
   setTimeout("$('#ani" + id + "').remove()", moveline_time);
}
function down_gallery(direction)
{
   if ( direction > 0)
   {
      loadHTML('/actions.php', "animatronik", {action:'move_gallery', dir: direction}, "+end");
   }
   else if ( direction < 0)
   {
      loadHTML('/actions.php', "animatronik", {action:'move_gallery', dir: direction}, "+start");
   }
}
function showpic(id, x, y)
{
   div = id;
   if ( !photomove )
   {
      $("#"+id).animate({ width: x, height: y }, 0);
      $("#"+id).attr("class", "photo greyborder");
   }
   photomove = 1;
   
   needx = ox*1.0 + x*1.0;
   needy = oy*1.0 + y*1.0;
   
   leftpos = ax*1.0+20
   toppos = ay*1.0+20
   
   if ( getClientWidth() >  needx )
   {
      leftpos = ax*1.0 + 20
   }
   else 
   {
      leftpos = ax*1.0 - x*1.0 - 10
   }
   
   if ( getClientHeight() >  needy )
   {
      toppos = ay*1.0 + 20;
   }
   else
   {
      toppos = ay*1.0 - (y - (getClientHeight() - oy));
   }
   
   $("#"+id).attr("style", "left:"+leftpos+"px;top:"+toppos+"px;");

}
function hidepic(id)
{
   
   $("#"+id).animate({ width: 0, height: 0 }, 0);
   $("#"+id).attr("class", "hide");
   setTimeout('photomove = 0;', 40);
   
}

document.onmousemove = mouseMove
 
function mouseMove(event){ 
    event = fixEvent(event)
    ax = event.pageX
    ay = event.pageY
    ox = event.x
    oy = event.y

    if ( event.clientX != null ) 
    {
       ox = event.clientX
       oy = event.clientY
    }
}

function fixEvent(e) {
    // получить объект событие для IE
    e = e || window.event
 
    // добавить pageX/pageY для IE
    if ( e.pageX == null && e.clientX != null ) {
        var html = document.documentElement
        var body = document.body
        e.pageX = e.clientX + (html && html.scrollLeft || body && body.scrollLeft || 0) - (html.clientLeft || 0)
        e.pageY = e.clientY + (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0)
    }
    else
    {
      
    }

    // добавить which для IE
    if (!e.which && e.button) {
        e.which = e.button & 1 ? 1 : ( e.button & 2 ? 3 : ( e.button & 4 ? 2 : 0 ) )
    }
 
    return e
}
    
function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}
