var Links = new Array();
var picNames = new Array();

function imgIEAttachMouseOver(img) {
  img.attachEvent("onmouseover", function() {
    img.src = img.lowsrc;
  });
}

function printItems(){
  if (!AjaxLink)
     return false;

  document.getElementById("origNav").style.display = "none";
  document.getElementById("album_top_nav").style.display = "block";

  href = AjaxLink;
  div=document.getElementById('previewRow');

  req=new Subsys_JsHttpRequest_Js();
  req.onreadystatechange= function (){		
     if (req.readyState == 4) {      
        if (req.responseJS) {
           var i = 1;

           for (key in req.responseJS['Items']){
               var Item=req.responseJS['Items'][key];

               var td = document.createElement("td");
               var itemLink = document.createElement("a");
               var imgsrc;
               if(Item['Values']['img']) imgsrc = Item['Values']['img'];
               else if(Item['Values']['image']) imgsrc = Item['Values']['image'];
               else imgsrc="/img/b.gif";
               var scene = Item['Values']['scene'];
               if(!scene) scene='0';
               itemLink.href = Item['Core']['Link'];
               itemLink.title = Item['Core']['Title'];
               img = document.createElement('img');
               img.style.width="100px";
               img.style.height="100px";
               img.src='/img/icons/text/ico_mover.gif';
//               img.lowsrc = Item['Values']['preview'];
               img.lowsrc = "http://preview.nnover.ru/small/" + scene + imgsrc;
               curId = "i" + Item['Core']['Ite_Code'];
               img.id = curId;
               picNames[i] = "i" + Item['Core']['Ite_Code'];
//               Links[i++] = Item['Values']['preview'];   
               Links[i++] = "http://preview.nnover.ru/small/" + scene + imgsrc;   

               itemLink.appendChild(img);
               td.appendChild(itemLink);

               if(parseInt(Item['Core']['Comments'])) {
                  cdiv = document.createElement('div');
                  ccdiv = document.createElement('div');
//                  cdiv.style.border = "1px red solid";
//                  ccdiv.style.border = "1px green solid";
                  cdiv.className = "HasComment";
                  if(parseInt(Item['Core']['Comments']) < 5) ccdiv.className = "FewComments";
                  else ccdiv.className = "ManyComments";
                  td.appendChild(cdiv);
                  cdiv.appendChild(ccdiv);
               }

               div.appendChild(td);

               if (window.addEventListener)
                 img.addEventListener("mouseover", function() {this.src = this.lowsrc; }, false);
               else
                 imgIEAttachMouseOver(img);
               
           }			
           rewind();
        }
        else {
              this.OnError();
        }

     }
  }
  //Открываем:
  req.open('GET', href, true);
  // Посылаем данные запроса (задаются в виде хэша).
  req.send(null)
}

function rewind() {
   var open_photos = 6;
   var part_photo = Math.round(open_photos/2)-1;

   var act_name = active_pic;
   if (document.getElementById(act_name)) {
   	document.getElementById(act_name).parentNode.parentNode.id = "act";
   	var Img = document.getElementById(act_name);
   }
   if (document.getElementById("act")) {
      all = false;
      if (!all) {
         //img.onmousemove();
         //img.src = img.lowsrc;
         for (i=1; i<Links.length+1; i++) {

             if (Links[i] == Img.lowsrc) {
                break;
             }

         }

         var begin = (i < part_photo) ? 1 :  i - part_photo;
         var end;
         if ( begin == 0 || begin == 1) {
   	 end = open_photos + 1;
   	 begin = 1;
         }
         else {
   	 end = i + part_photo + 1;
         }
         if  (Links.length - i < open_photos) {
            end = Links.length;
   	 if ((Links.length-1) <= open_photos) begin = 1;

         }

         if ((end == Links.length) && begin > (Links.length - open_photos) && (begin > open_photos)) {
            begin = Links.length - open_photos;
         }
         //alert(begin + ' ' + end);
             for (j=begin; j<end;j++) {
             var photo = document.getElementById(picNames[j]);
   	  if (photo != null) {
               //photo.onmousemove();
               photo.src = photo.lowsrc; 
             }
         }
      }
      else {
         for (i=1; i<Links.length; i++) {
   	  var photo = document.getElementById(picNames[i]);
   	  //photo.onmousemove();
   	  photo.src = photo.lowsrc;
         }
      }

      var source = document.getElementById("act");
      var ifrm = document.getElementById("album_top_nav");
      for (i = 0; i < 100 ; i++){
          if (source.offsetLeft == 0){
             setTimeout('act_name = active_pic',0);
   	  if (document.getElementById(act_name)) {
   	     document.getElementById(act_name).parentNode.parentNode.id = "act";
   	     Img = document.getElementById(act_name);
   	  }
   	  setTimeout('source = document.getElementById("act")',0);
   	  ifrm = document.getElementById("album_top_nav");
   	  x = source.offsetLeft;
   	  w = source.offsetWidth;
   	  ifrm.scrollLeft = i;
             this.focus();

          }
          else break;
      }
      ifrm.scrollLeft = source.offsetLeft - ifrm.offsetWidth/2 + Math.round(source.offsetWidth/2);

   }
}

function show_photo(obj, photo) {
	obj.src = photo;
}

