var pics=0;
var picn=new Array();
var cap=new Array();
var thmb=new Array();
var ti=new Array();
var base=0;
var path='';
var album='';
var themename='';
var numCols=0;
var debug=false;
var myids=new Array();
var pw=null;
var d=document;
var tpf_NS7=(!d.all&&d.getElementById);
var tpf_NS4=(!d.getElementById);
var tpf_IE5=(!tpf_NS4&&!tpf_NS7&&(navigator.userAgent.indexOf('MSIE 5.0')!=-1||navigator.userAgent.indexOf('MSIE	5.2')!=-1));
var tpf_IE5p5=(!tpf_NS4&&!tpf_NS7&&navigator.userAgent.indexOf('MSIE 5.5')!=-1);
var tpf_NS6=(tpf_NS7&&navigator.userAgent.indexOf('Netscape6')!=-1);
var tpf_SAF=(navigator.userAgent.indexOf('Safari')!=-1);

function tp_albumsetup(p,a,t) {
  var node;path=p;album=a;themename=t;
  tp_scanCollection("tp_albumtitle",0,function(node,i,a) {node.innerHTML+=a;},a);
}

function tp_pic(n,tti,tcap) {
  var leaf,ext;
  leaf=n.substring(0,n.lastIndexOf('.'));
  ext=n.substring(n.lastIndexOf('.'),n.length);
  picn[pics]=path+n;ti[pics]=tti;cap[pics]=tcap;
  thmb[pics]=path+leaf+'_thumb'+ext;pics++;
}

function tp_show(pc) {
  var w,h;
  var cb=pc+base;
  var bigimg;
  var strsrc="";

  if(cb>=picn.length) return true;

  strsrc+='<html><head><title>'+'Увеличенное фото'+'</title>';
  strsrc+='<scr'+'ipt>';
  strsrc+='var pics='+pics+';var c='+cb+'; var e;';
  strsrc+='</scr'+'ipt>';
  strsrc+='</head>';
  strsrc+='<body>';
  strsrc+='<table width="100%"><tr><td><a href="javascript:window.opener.tp_nextpic(-1, window)">';
  strsrc+='<img src="./media/prev.gif" border=0 alt="Предыдущее фото" /></a>';
  strsrc+='<a href="javascript:window.opener.tp_nextpic(1, window)">';
  strsrc+='<img src="./media/next.gif" border=0 alt="Следующее фото" /></a></td>';
  strsrc+='<td><b"></b></td><td height=15 ID="posn" >фото i из n</td>';
  strsrc+='<td align="right"><a href="javascript:close()">Закрыть</a></td></tr></table>';
  strsrc+='<table align="center"><tr><td align="center">';
  strsrc+='<a href="javascript:window.opener.tp_nextpic(1, window)">';
  strsrc+='<img src ="'+picn[cb]+'" border=0 ID="pic" onLoad="window.opener.tp_sizetofit(window);" /></a></td></tr>';
  strsrc+='</table></body></html>';
  bigimg=new Image();bigimg.src=picn[cb];w=bigimg.width+10;h=bigimg.height+30;s=0;
  if(w>screen.availWidth-50) {w=screen.availWidth-50;s=1;}
  if(h>screen.availHeight-50) {h=screen.availHeight-50;s=1;}
  o="toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=1,scrollbars="+
    s+",left=40;top=20;screenX=40,screenY=20,width="+w+
    ",height="+h;
  if(pw) {wd=window.open("","tpslideshow",o,true); pw=wd;}
  else {wd=window.open("","",o,true);wd.name="tpslideshow";pw=wd;}
  wd.document.open();  wd.document.writeln(strsrc); wd.document.close();
  tp_nextpic(0,wd);
  wd.focus();
}

function tp_albumfill(base) {
  var i;var timg;var tel;var trow;myids=new Array();
  tp_scanIndexedCollection("tp_thumb",tp_img,0);
<!--  tp_scanIndexedCollection("tp_title",tp_text,ti);
  tp_scanIndexedCollection("tp_caption",tp_text,cap);
  tp_scanCollection("tp_positionpages",0,function(tel,i,a) {
    if(base>0||picn.length>base+tp_countslots())tel.style.visibility="visible";
    else tel.style.visibility="hidden";},0);
  tp_scanCollection("tp_prevpage",0,function(tel,i,a) { 
    if(base>0)tel.style.visibility="visible";
    else tel.style.visibility="hidden";},0);
  tp_scanCollection("tp_nextpage",0,function(tel,i,a) {
    if(picn.length>base+tp_countslots())tel.style.visibility="visible";
    else tel.style.visibility="hidden";},0);
  tp_scanCollection("tp_position",0,tp_postext,0);
  tp_scanIndexedCollection("tp_cell",tp_vis,0);
}

function tp_img(timg,i,a) {
  if((base+i)<thmb.length&&(base+i)>=0)
    if(thmb[base+i]!='') {
      timg.src=thmb[base+i];timg.removeAttribute("width");timg.removeAttribute("height");
      }else timg.src=themename+"nothumb.gif";
    else timg.src=themename+"missingthumb.gif";
}

function tp_text(tel,i,a) {
  if((base+i)<a.length&&(base+i)>=0)tel.innerHTML=a[base+i];
  else tel.innerHTML='';
}

function tp_postext(tel,i,a) {
  var top=base+tp_countslots();
  if(top>picn.length)top=picn.length;tel.innerHTML="Показаны изображения: с "+(base+1)+" по "+top+" из "+picn.length;
}

function tp_vis(tel,i,a) {
  if((base+i)<thmb.length&&(base+i)>=0)tel.style.visibility="visible";
  else tel.style.visibility="hidden";
}

function tp_countslots() {
  var i;i=0;timg=document.getElementById("tp_thumb"+i);
  while(timg){i++;timg=document.getElementById("tp_thumb"+i);}
  return i;
}

function tp_prevpage() {
  if(base>0) {base=base-tp_countslots();tp_albumfill(base);}
}

function tp_nextpage() {
  if(picn.length>base+tp_countslots()) {
    base=base+tp_countslots();tp_albumfill(base);
  }
}

function tp_albumcreate(ncols,nrows,startslot) { 
  var posn=document.getElementById("tp_albumdisplay");
  var x,y;var slotnum;var row_startslot;
  if(posn) {
    var oTable=document.createElement("TABLE");
    var oTBody0=document.createElement("TBODY");
    var oRow,oCell;
    var i,j,k;var tcell_collect;var tcell;var tcellInnerHTML;
    if(startslot!=-1)slotnum=startslot;else slotnum=tp_countslots();
    oTable.appendChild(oTBody0);oTable.border=0;oTable.width='100%';
    oTable.className="tp_albumtable";
    for(i=0;i<nrows;i++){
      tcell_collect=document.getElementsByName("tp_albumcelltemplate");
      row_startslot=slotnum;
      for(k=0;k<tcell_collect.length;k++){
        slotnum=row_startslot;tcell=tcell_collect[k];
        if(tcell)tcellInnerHTML=tcell.innerHTML;
        oRow=document.createElement("TR");
        oRow.id="tp_row"+i;
        oRow.className="tp_albumrow";
        oTBody0.appendChild(oRow);
        for(j=0;j<ncols;j++) {
          oCell=document.createElement("TD");
          if(tcell) {
            var slothtml=tcellInnerHTML;
            slothtml=slothtml.replace(/slotnum/gi,slotnum);
            slothtml=slothtml.replace(/%28/gi,"(");
            slothtml=slothtml.replace(/%29/gi,")");
            oCell.innerHTML=slothtml;
          } else {
            oCell.innerHTML='<a href="javascript:tp_show('+slotnum+');"><img src="gradient_photothumb.gif" border=0 hspace=2 vspace=2 ID="tp_thumb'+slotnum+'"></a>';
          }
          oCell.align='center';oCell.vAlign='top';oCell.width=100/ncols+'%';
          oCell.id="tp_cell"+slotnum;oCell.className="tp_albumcell"+k;
          oRow.appendChild(oCell);slotnum++;
        }
      }
    }
    tcell_collect=document.getElementsByName("tp_albumcelltemplate");
    for(k=tcell_collect.length-1;k>=0;k--) {
      tcell=tcell_collect[k];
      if(tcell&&tp_ChildOf(posn,tcell)) {
        var p=tcell.parentNode;p.removeChild(tcell);
      }
    }
    posn.appendChild(oTable);numCols=ncols;
  }
}

function tp_nextpic(d,wnd) {
  wnd.c+=d;
  if(wnd.c>=pics)wnd.c=0;
  if(wnd.c<0)wnd.c=pics-1;
  if(wnd.document.images!=null) {
    var e=wnd.document.getElementById("pic");
    if(e) {e.src=picn[wnd.c];e.align="center";}
    e=wnd.document.getElementById("posn");
    if(e)e.innerHTML="Фото&nbsp;"+(wnd.c+1)+"&nbsp;из&nbsp;"+pics;
    e=wnd.document.getElementById("title");
    if(e)e.innerHTML=ti[wnd.c];
    e=wnd.document.getElementById("caption");
    if(e)e.innerHTML=cap[wnd.c];
    if(wnd.navigator.userAgent.indexOf('MSIE')!=-1)wnd.document.title=ti[wnd.c];
  }
}

function tp_sizetofit(wnd) {
  if(wnd.document.images!=null) {
    e=wnd.document.getElementById("pic");
    if(e){if(e.width!=0||e.height!=0) {
      var w,h,s;w=e.width+18+32;h=e.height+56+32;s=false;
      if(cap[wnd.c]!='')h=h+40;
      if(w>screen.availWidth) { w=screen.availWidth;s=true; }
      if(h>screen.availHeight) {h=screen.availHeight;s=true;}
      wnd.resizeTo(w,h);
      if(s)wnd.moveTo(0,0);
    } else {wnd.setTimeout("tp_sizetofit(window)",50);} }
  }
}

function tp_ChildOf(a,n) {
  var p;p=n.parentNode;
  while(p) {
    if(p==a) return true;
    p=p.parentNode;
  }
  return false;
} 

function tp_scanIndexedCollection(name,func,args) {
  var i;var count;i=0;
  do{count=tp_scanCollection(name+i,i,func,args);i++;}
  while(count>0)
}

function tp_scanCollection(name,base,func,args) {
  var tcell_collect;
  var tcell;
  var i;
  var count; count=0;
  tcell_collect=document.getElementsByName(name);
  if(tcell_collect.length>0) {
    for(k=0;k<tcell_collect.length;k++) {
      tcell=tcell_collect[k];
      if(tcell) func(tcell,base,args);
    }
    return tcell_collect.length;
  } else {
    if(myids.length==0)tp_scanTree3A();
    return tp_scanTree3B(name,base,func,args);
  }
  return 0;
}

function tp_scanTree3A() {
  var i;var c=0;var node=document;
  while(node) {
    if(node.id!=""&&typeof node.id!="undefined") { myids[c]=node;c++; }
    node=tp_getnextnode(node);
   if(node&&!(tpf_NS7||tpf_NS6||tpf_NS4))
     if((typeof node.tagName)!="undefined")
       if(node.tagName.toUpperCase()=="HTML") node=null;
  }
  return c;
}

function tp_scanTree3B(name,base,func,args) {
  var i;var c=0;var node;
  if(document.getElementById(name)==null)return 0;
  for(i=0;i<myids.length;i++) {
    node=myids[i];
    if(node.id==name) {func(node,base,args);c++;}
  }
  return c;
}

function tp_getnextnode(node) {
  if(node==null)return null;
  if(node.firstChild)return node.firstChild;
  while(node.nextSibling==null) {
    if(node.parentNode==null)return null;
    node=node.parentNode;
  }
  return node.nextSibling;
}
