var ph_int=null;
var ph_scroll_int=null;
var ph_holder=document.getElementById('photo_holder');
var photogallery;
if(ph_holder==undefined) var photogallery=false;
else photogallery=true;

if(photogallery)
   document.getElementById('gallery_holder').style.left=(document.getElementById('wrap').offsetLeft+820)+'px';

var ph_scroll_limit=0;

function phFirstSpan(obj){ //div
   var tmp=obj.firstChild;
   while(tmp!=undefined && tmp.nodeName!='DIV') tmp=tmp.nextSibling;
   return tmp;
}

if(photogallery) var ph_current=phFirstSpan(ph_holder);

function phSecondSpan(obj){   //div
   var tmp=phFirstSpan(obj).nextSibling;
   while(tmp!=undefined && tmp.nodeName!='DIV') tmp=tmp.nextSibling;
   return tmp;
}

function phLastDiv(obj){
   var tmp=obj.lastChild;
   while(tmp!=undefined && tmp.nodeName!='DIV') tmp=tmp.previousSibling;
   return tmp;
}
function phLastSpan(obj){
   var tmp=obj.lastChild;
   while(tmp!=undefined && tmp.nodeName!='SPAN') tmp=tmp.previousSibling;
   return tmp;
}

function phNextSpan(obj){
   var tmp=obj.nextSibling;
   while(tmp!=undefined && tmp.nodeName!='DIV') tmp=tmp.nextSibling;
   return tmp;
}

function phPreviousSpan(obj){
   var tmp=obj.previousSibling;
   while(tmp!=undefined && tmp.nodeName!='DIV') tmp=tmp.previousSibling;
   return tmp;
}

function phHeader(obj){
   var tmp=obj.firstChild;
   while(tmp!=undefined && tmp.nodeName!='H2') tmp=tmp.nextSibling;
   return tmp;
}

function phH2image(obj){
   var tmp=obj.previousSibling;
   while(tmp!=undefined && tmp.nodeName!='A') tmp=tmp.previousSibling;
   return tmp;
}

function phImage(obj){
   var tmp=obj.firstChild;
   while(tmp!=undefined && tmp.nodeName!='A') tmp=tmp.nextSibling;
   return tmp;
}

function phScrollLeft(){
 var tmp=(-1)*(ph_holder.parentNode.getBoundingClientRect().top-ph_holder.getBoundingClientRect().top-10);
 if(tmp>=0){
   ph_holder.style.marginTop='0px';
   document.getElementById('ph_left_slide').style.visibility='hidden';
   clearInterval(ph_scroll_int);
   }
 else{
   document.getElementById('ph_right_slide').style.visibility='visible';
   ph_holder.style.marginTop=tmp+'px';
 }
}
function phScrollRight(){
 var tmp=(-1)*(ph_holder.parentNode.getBoundingClientRect().top-ph_holder.getBoundingClientRect().top+10);
 var last=phImage(phLastDiv(ph_holder));
 if(tmp*(-1)+250>=last.getBoundingClientRect().top+last.offsetHeight-ph_holder.getBoundingClientRect().top){
   document.getElementById('ph_right_slide').style.visibility='hidden';
   clearInterval(ph_scroll_int);
 }
 else if(ph_scroll_limit!=0 && -tmp>=ph_scroll_limit){
   ph_scroll_limit=0;
   //document.getElementById('ph_right_slide').style.visibility='hidden';
   clearInterval(ph_scroll_int);
 }
 else{
   document.getElementById('ph_left_slide').style.visibility='visible';
   ph_holder.style.marginTop=tmp+'px';
 }
}

function phScroll(){
   var next=phNextSpan(ph_current);
   if(next!=undefined){
      ph_scroll_limit=phImage(next).getBoundingClientRect().top-ph_holder.getBoundingClientRect().top;
      ph_scroll_int=setInterval(phScrollRight,40);
      ph_current=next;
   }
   else{
         ph_scroll_int=setInterval(phScrollLeft,40);
         ph_current=phFirstSpan(ph_holder);
   }
}

function phStopScroll(){
   clearInterval(ph_scroll_int);
}

var ph_photo_cont=document.getElementById('ph_photo_cont');
var ph_photo=document.getElementById('ph_photo');
var ph_current_photo=null;
var photo=new Image();
    photo.onload=function(){phPhotoResize();}
var phNum=null;
var phDesc=null;

function phPhotoResize(){
   ph_photo.style.background="url('"+photo.src+"') top left no-repeat";
   ph_photo.style.display='block';
   ph_photo_cont.style.width=(photo.width)+'px';
   ph_photo_cont.style.height=(photo.height+40)+'px';
   ph_photo_cont.style.border='1px solid white';
   ph_photo.style.width=photo.width+'px';
   ph_photo.style.height=photo.height+'px';
   if(phNum==null){
      if(ph_current_photo==null)return;
      document.getElementById('ph_photo_header').innerHTML=phHeader(ph_current_photo).innerHTML;
   }
   else{
      document.getElementById('ph_photo_header').innerHTML=phDesc;
      phNum=null;
      phDesc=null;
   }
   ph_photo_cont.style.marginTop=Math.max(0,(document.body.clientHeight-ph_photo_cont.offsetHeight)/2)+'px';
   ph_photo_cont.style.marginLeft=Math.max(0,(document.body.clientWidth-ph_photo_cont.offsetWidth)/2)+'px';
}
function phShowFull(num,obj){
   document.getElementById('ph_fade').style.display='block';
   ph_photo.style.display='none';
   ph_photo_cont.style.border='0px';
   ph_photo_cont.style.width='400px';
   ph_photo_cont.style.height='600px';
   ph_current_photo=obj;
   photo.src='photos/'+num+'/0.jpg';
}
function phHideFull(){
   document.getElementById('ph_fade').style.display='none';
   if(phNum==null){
      ph_current_photo=null;
   }
   else{
      phNum=null;
      phDesc=null;
   }
}
function photoShow(num,desc){
   document.getElementById('ph_fade').style.display='block';
   ph_photo.style.display='none';
   ph_photo_cont.style.border='0px';
   ph_photo_cont.style.width='400px';
   ph_photo_cont.style.height='600px';
   phNum=num;
   phDesc=desc;
   photo.src='photos/'+num+'/0.jpg';
}

function phSetInterval(evnt){
   evnt=evnt||window.event;
   if(evnt.pageY) var ypsylon=evnt.pageX;
   else var ypsylon=evnt.clientX + document.body.scrollLeft  - document.body.clientLeft;
   if(document.body.clientWidth- ypsylon>140){ph_int=setInterval(phScroll,7000);}
}
if(photogallery)window.setTimeout('ph_int=setInterval(phScroll,7000);',1000);
