function _ConnectionManager()
{
  this.pool;
  this.requests;
  this.createHttpRequest = function()
  {
    req = null;
    if (window.XMLHttpRequest)
    {
      try
      {
        req = new XMLHttpRequest();
      } catch (e){}
    }
    else if (window.ActiveXObject)
    {
      try
      {
        req = new ActiveXObject('Msxml2.XMLHTTP');
      } catch (e)
      {
        try
        {
          req = new ActiveXObject('Microsoft.XMLHTTP');
        } catch (e){}
      }
    }
    return req;
  }
  this.checkLoad = function()
  {
    for (var k=0;k<5;k++)
    {
      if (this.pool[k]["active"])
      {
        if (this.pool[k]["obj"].readyState == 4)
        {
          if (this.pool[k]["obj"].status == 200)
          {
            this.pool[k]["callback"](this.pool[k]["obj"].responseXML);
            delete this.requests[this.pool[k]["req_id"]];
          }
          else
          {
            this.requests[this.pool[k]["req_id"]]["active"]=false;
          }
          this.pool[k]["active"]=false;
          this.pool[k]["time"]=0;
          this.pool[k]["req_id"]=0;
        }
        else
        {
          if (((new Date).getTime()-this.pool[k]["time"])>5000)
          {
            this.pool[k]["obj"].abort();
            this.requests[this.pool[k]["req_id"]]["active"]=false;
            this.pool[k]["active"]=false;
            this.pool[k]["time"]=0;
            this.pool[k]["req_id"]=0;
            alert("Нет ответа от сервера! Попробуйте позже.");
          }
        }
      }
    }
    this.send();
    setTimeout("ConnectionManager.checkLoad()",200);
  }
  this.init = function()
  {
    this.pool = new Object();
    for (var k=0;k<5;k++)
    {
      this.pool[k]=new Object();
      this.pool[k]["obj"]=this.createHttpRequest();
      this.pool[k]["active"]=false;
      this.pool[k]["req_id"]=0;
      this.pool[k]["time"]=0;
    }
    this.requests = new Object();
    setTimeout("ConnectionManager.checkLoad()",200);
  }
  this.send = function()
  {
    for (var k in this.requests)
    {
      if (!this.requests[k]["active"])
      {
        var ok=false;
        for (var m=0;m<5;m++)
        {
          if (!this.pool[m]["active"])
          {
            this.pool[m]["active"]=true;
            this.pool[m]["req_id"]=k;
            this.pool[m]["callback"]=this.requests[k]["callback"];
            this.pool[m]["time"]=(new Date()).getTime();
            this.requests[k]["active"]=true;
            this.pool[m]["obj"].open("POST", this.requests[k]["url"], true);
            this.pool[m]["obj"].setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
            this.pool[m]["obj"].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            this.pool[m]["obj"].setRequestHeader("Content-length", this.requests[k]["params"].length);
            this.pool[m]["obj"].setRequestHeader("Connection", "close");
            this.pool[m]["obj"].send(this.requests[k]["params"]);
            ok=true;
            break;
          }
        }
        if (!ok)
        return;
      }
    }
  }
  this.get = function(url,params,callback)
  {
    if (!this.pool)
      this.init();
    var k=this.requests.length;
    this.requests[k]=new Object();
    this.requests[k]["url"]=url;
    this.requests[k]["params"]=params;
    this.requests[k]["callback"]=callback;
    this.requests[k]["active"]=false;
    this.send();
  }
}
function _ImageViewPort()
{
  this.handle;
  this.loading;
  this.image;
  this.mask;
  this.opacity=0;
  this.get_text = function(obj)
  {
    if ((document.all) && (!window.opera))
      return obj.text;
    return obj.textContent;
  }
  this.apply_data = function(obj)
  {
    var els=obj.getElementsByTagName("result");
    var blocks=els.item(0);
    var img,w,h,prev_id,next_id,name;
    for (var m=0;m<blocks.childNodes.length;m++)
    {
      var el=blocks.childNodes[m];
      if (el.tagName=="image")
        img=ImageViewer.viewport.get_text(el);
      if (el.tagName=="w")
        w=ImageViewer.viewport.get_text(el)*1;
      if (el.tagName=="h")
        h=ImageViewer.viewport.get_text(el)*1;
      if (el.tagName=="prev_id")
        prev_id=ImageViewer.viewport.get_text(el)*1;
      if (el.tagName=="next_id")
        next_id=ImageViewer.viewport.get_text(el)*1;
      if (el.tagName=="name")
        name=ImageViewer.viewport.get_text(el);
    }
    ImageViewer.viewport.set_image(img,w,h,prev_id,next_id,name);
  }
  this.make_opacity = function()
  {
    if ((document.all) && (!window.opera))
      this.mask.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+this.opacity+");";
    else
      this.mask.style.opacity = this.opacity/100;
    this.opacity -= 10;
    if (this.opacity == -10)
    {
      this.mask.style.display = "none";
      ImageViewer.set_nav();
    }
    else
      setTimeout("ImageViewer.viewport.make_opacity()",20);
  }
  this.image_loaded = function()
  {
    ImageViewer.viewport.remove_loading();
    if (ImageViewer.handle.style.visibility = "visible")
      ImageViewer.viewport.image.style.visibility = "visible";
    ImageViewer.viewport.mask.style.width = ImageViewer.viewport.image.offsetWidth;
    ImageViewer.viewport.mask.style.height = ImageViewer.viewport.image.offsetHeight;
    ImageViewer.viewport.mask.style.top = -ImageViewer.viewport.image.offsetHeight;
    ImageViewer.viewport.mask.style.left = 0;
    ImageViewer.viewport.opacity = 90;
    ImageViewer.viewport.make_opacity();
  }
  this.set_image = function(img,w,h,prev_id,next_id,name)
  {
    this.image = document.createElement("IMG");
    this.image.style.visibility = "hidden";
    this.handle.appendChild(this.image);
    this.image.onclick = function() {ImageViewer.close();};
    var imgname=document.getElementById("image_name");
    var vote_block=document.getElementById("vote_block");
    var dh=0;
    if ((name=="") && (ImageViewer.vote!=1))
    {
      imgname.style.display="none";
      dh=0;
    }
    else
    {
      if (ImageViewer.vote==1)
      {
        imgname.innerHTML="<div style='text-align:center;margin-bottom:10px;margin-right:20px;margin-top:5px;'><button id='vote_button' onclick='make_vote("+this.imgid+");'>Голосовать</button></div>";
        //<div style='float:left;'>"+name+"</div>
        dh=40;
      }
      else
      {
        imgname.innerHTML=name;
        dh=document.getElementById("image_name").offsetHeight;
      }
      imgname.style.display="block";
    }
    if (h > (document.body.clientHeight - (dh + 110)))
    {
      var old_h=h;
      h = document.body.clientHeight - (dh + 110);
      w = Math.round(h / old_h * w);
    }
    if (w > (document.body.clientWidth - (dh + 110)))
    {
      var old_w=w;
      w = document.body.clientWidth - (dh + 110);
      h = Math.round(w / old_w * h);
    }
    this.image.style.width = w;
    this.image.style.height = h;
    ImageViewer.viewarea.style.width = w;
    ImageViewer.viewarea.style.height = h;
    ImageViewer.handle.style.width=w + 10;
    ImageViewer.handle.style.height=h + 10 + dh;
    ImageViewer.pos();
    ImageViewer.prev_id=prev_id;
    ImageViewer.next_id=next_id;
    this.set_loading();
    this.image.onload = ImageViewer.viewport.image_loaded;
    this.image.src = img;
  }
  this.create_self = function()
  {
    this.handle = document.createElement("DIV");
    this.handle.style.position = "relative";
    this.handle.style.zIndex = "110";
    this.handle.style.left = "0";
    this.handle.style.top = "0";
    ImageViewer.viewarea.appendChild(this.handle);
    this.mask = document.createElement("DIV");
    this.mask.style.position = "relative";
    this.mask.style.zIndex = "200";
    this.mask.style.left = "0";
    this.mask.style.top = "0";
    this.mask.style.width = 0;
    this.mask.style.height = 0;
    this.mask.style.backgroundColor = "#ffffff";
    ImageViewer.viewarea.appendChild(this.mask);
  }
  this.create_loading = function()
  {
    this.loading = document.createElement("IMG");
    this.loading.style.position = "relative";
    this.loading.style.zIndex = "120";
    this.loading.style.display = "none";
    this.loading.src = "/img/loading.gif";
    ImageViewer.viewarea.appendChild(this.loading);
  }
  this.remove_loading = function()
  {
    this.loading.style.display = "none";
  }
  this.set_loading = function()
  {
    this.loading.style.left = (ImageViewer.viewarea_width() - 44) / 2;
    this.loading.style.top = (ImageViewer.viewarea_height() - 44) / 2;
    this.loading.style.display = "block";
  }
  this.load = function(imgid)
  {
    if (this.handle)
    {
      ImageViewer.viewarea.removeChild(this.handle);
      delete this.handle;
      ImageViewer.viewarea.removeChild(this.mask);
      delete this.mask;
    }
    this.create_self();
    if (!this.loading)
      this.create_loading();
    this.set_loading();
    this.imgid=imgid;
    ConnectionManager.get(ImageViewer.sce,"id="+imgid,ImageViewer.viewport.apply_data);
  }
}

function _ImageViewer()
{
  this.handle;
  this.viewarea;
  this.layer;
  this.header;
  this.close_button;
  this.prev_button;
  this.next_button;
  this.viewport;
  this.prev_id;
  this.next_id;
  this.prev_image = function()
  {
    this.prev_button.style.visibility="hidden";
    this.next_button.style.visibility="hidden";
    this.viewport.load(this.prev_id);
  }
  this.next_image = function()
  {
    this.prev_button.style.visibility="hidden";
    this.next_button.style.visibility="hidden";
    this.viewport.load(this.next_id);
  }
  this.set_nav = function ()
  {
    if (this.prev_id>0)
    {
      this.prev_button.style.left = this.handle.offsetLeft - 30;
      this.prev_button.style.top = this.handle.offsetTop + this.handle.offsetHeight / 2 + 14;
      this.prev_button.style.visibility="visible";
    }
    else
      this.prev_button.style.visibility="hidden";
    if (this.next_id>0)
    {
      this.next_button.style.left = this.handle.offsetLeft + this.handle.offsetWidth;
      this.next_button.style.top = this.handle.offsetTop + this.handle.offsetHeight / 2 + 14;
      this.next_button.style.visibility="visible";
    }
    else
      this.next_button.style.visibility="hidden";
  }
  this.viewarea_width = function()
  {
    return this.viewarea.offsetWidth;
  }
  this.viewarea_height = function()
  {
    return this.viewarea.offsetHeight;
  }
  this.pos = function()
  {
    if (!ImageViewer.handle)
      return;
    ImageViewer.handle.style.top = (document.body.clientHeight - ImageViewer.handle.offsetHeight) / 2 + document.body.scrollTop;
    ImageViewer.handle.style.left = (document.body.clientWidth - ImageViewer.handle.offsetWidth) / 2;
    ImageViewer.close_button.style.left = ImageViewer.handle.offsetLeft + ImageViewer.handle.offsetWidth - 10;
    ImageViewer.close_button.style.top = ImageViewer.handle.offsetTop - 30;
    ImageViewer.close_button.style.visibility= "visible";
  }
  this.dark_all = function()
  {
    var els=document.getElementsByTagName("OBJECT");
    for (var k=0;k<els.length;k++)
      els.item(k).style.visibility="hidden";
    if (!this.layer)
    {
      this.layer = document.createElement("DIV");
      this.layer.style.position = "absolute";
      this.layer.style.zIndex = "90";
      this.layer.style.backgroundColor = "#000000";
      if ((document.all) && (!window.opera))
        this.layer.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50);";
      else
        this.layer.style.opacity = 0.5;
      document.body.appendChild(this.layer);
    }
    this.layer.style.left = "0";
    this.layer.style.top = "0";
    this.layer.style.width = document.body.clientWidth;
    this.layer.style.height = document.body.scrollHeight;
  }
  this.light_all = function()
  {
    this.layer.style.left = "0";
    this.layer.style.top = "0";
    this.layer.style.width = "0";
    this.layer.style.height = "0";
    var els=document.getElementsByTagName("OBJECT");
    for (var k=0;k<els.length;k++)
      els.item(k).style.visibility="visible";
  }
  this.create_self = function()
  {
    this.handle = document.createElement("DIV");
    this.handle.style.position = "absolute";
    this.handle.style.zIndex = "100";
    this.handle.style.left=0;
    this.handle.style.top=0;
    this.handle.style.backgroundColor = "#FFFFFF";
    this.handle.style.border = "1px solid #2d549c";
    this.handle.visibility = "hidden";
    document.body.appendChild(this.handle);
    this.header = document.createElement("DIV");
    this.header.innerHTML = "<div style='font-size:12px;color:#002E7D;font-weight:bold;padding:5px;padding-top:3px;padding-bottom:0;' id=image_name></div>";
    this.handle.appendChild(this.header);
    this.close_button = document.createElement("IMG");
    this.close_button.src = "/img/high.gif";
    this.close_button.style.background = "url('/img/close.png')";
    this.close_button.className = "png";
    this.close_button.style.position = "absolute";
    this.close_button.style.zIndex = 200;
    this.close_button.style.width = "40px";
    this.close_button.style.height = "40px";
    this.close_button.style.cursor = "pointer";
    this.close_button.onclick = function() {ImageViewer.close();this.style.display="none";};
    this.close_button.style.visibility = "hidden";
    document.body.appendChild(this.close_button);
    this.prev_button = document.createElement("IMG");
    this.prev_button.src = "/img/high.gif";
    this.prev_button.style.background = "url('/img/nav_prev.png')";
    this.prev_button.className = "png";
    this.prev_button.style.position = "absolute";
    this.prev_button.style.zIndex = 200;
    this.prev_button.style.width = "30px";
    this.prev_button.style.height = "28px";
    this.prev_button.style.cursor = "pointer";
    this.prev_button.onclick = function() {ImageViewer.prev_image();};
    this.prev_button.style.visibility = "hidden";
    document.body.appendChild(this.prev_button);
    this.next_button = document.createElement("IMG");
    this.next_button.src = "/img/high.gif";
    this.next_button.style.background = "url('/img/nav_next.png')";
    this.next_button.className = "png";
    this.next_button.style.position = "absolute";
    this.next_button.style.zIndex = 200;
    this.next_button.style.width = "29px";
    this.next_button.style.height = "28px";
    this.next_button.style.cursor = "pointer";
    this.next_button.onclick = function() {ImageViewer.next_image();};
    this.next_button.style.visibility = "hidden";
    document.body.appendChild(this.next_button);
    this.viewarea = document.createElement("DIV");
    this.viewarea.style.margin = "5px";
    this.viewarea.style.overflow = "hidden";
    this.viewarea.style.width="200px";
    this.viewarea.style.height="200px";
    this.handle.appendChild(this.viewarea);
    this.handle.style.width=this.viewarea.offsetWidth + 10;
    this.handle.style.height=this.viewarea.offsetHeight + 10;
  }
  this.show = function(sce,imgid,vote)
  {
    if (!this.handle)
      this.create_self();
    this.sce = sce;
    this.vote=vote;
    this.dark_all();
    this.pos();
    this.close_button.style.display="block";
    this.handle.style.visibility = "visible";
    if (!this.viewport)
      this.viewport=new _ImageViewPort();
    this.viewport.load(imgid);
  }
  this.close = function()
  {
    document.body.removeChild(this.handle);
    document.body.removeChild(this.prev_button);
    document.body.removeChild(this.next_button);
    document.body.removeChild(this.close_button);
    delete this.handle;
    delete this.viewarea;
    delete this.viewport;
    this.light_all();
  }
}
var ConnectionManager = new _ConnectionManager();
var ImageViewer = new _ImageViewer();
window.onresize = ImageViewer.pos;
window.onscroll = ImageViewer.pos;
function make_vote(id)
{
  if (!window.confirm("Проголосовать за эту работу?\nВнимание! Вы можете отдать голос только за одну работу!"))
    return;
  location.href="/konkurs/?action=vote&id="+id;
}
