//By Dave Bezaire; DaveBezaire.com
//Thanks for the ideas that started me off go to "lighterbox2" and
//Richard Lee - Transcendent Design - tdesignonline.com - RichardAndrewLee@yahoo.com
//Frédéric MADSEN - MadsenFr@laposte.net
var pfNext=-1
var pfStart=-1
var pfPaths=new Array()

function oyDoINIT() {
var oyDocAnchors=document.getElementsByTagName('a')
oyAnchors=new Array()
for(var z=0;z<oyDocAnchors.length;z++)
if(oyDocAnchors[z].className.indexOf("oy")>-1){
oyAnchors.push(oyDocAnchors[z])
pfPaths.push(oyDocAnchors[z].pathname)
oyDocAnchors[z].onclick=oyDoSTART}
pfDoGET()

oyBG1=oyDoEL('div','oyBG1',document.body)
oyBG2=oyDoEL('div','oyBG2',oyBG1)
oyBG3=oyDoEL('div','oyBG3',oyBG2)
oyBG4=oyDoEL('div','oyBG4',oyBG1,'Click to close','none',oyDoCLS)
oyIMG=oyDoEL('img','oyIMG',oyBG3,'Click for next image','none',oyDoNXT)
oyBTN=oyDoEL('div','oyBTN',oyBG3)
oyTTL=oyDoEL('div','oyTTL',oyBG3,'none','')
oyCPT=oyDoEL('div','oyCPT',oyBG3,'none','')
oyPRV=oyDoEL('span','oyPRV',oyBTN,'Previous image','<',oyDoPRV)
oyNXT=oyDoEL('span','oyNXT',oyBTN,'Next image','>',oyDoNXT)
oyCLS=oyDoEL('span','oyCLS',oyBTN,'Close','X',oyDoCLS)
//oyBG1.style.height=document.body.offsetHeight+'px'
//oyBG1.style.height=(window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null)-20+'px'
oyBG1.style.height='100%'
oyIMG.onload=oyDoPlace
oyArrowKeys=true

if (oyAnchors.length==1){
oyNXT.style.visibility='hidden'
oyPRV.style.visibility='hidden'
oyIMG.onclick=''
oyIMG.title=''
oyIMG.style.cursor='default'
oyArrowKeys=false}

document.onkeydown=oyDoKEYS
}

function pfDoGET () {
pfNext += (pfNext >= pfStart) ? 1 : -1
if (pfNext == pfPaths.length) pfNext = pfStart - 1
if (pfNext < 0) return
var pf = new Image()
pf.onload=pfDoGET
pf.src = pfPaths[pfNext]}

function oyDoSTART(){
for(var i=0;i<oyAnchors.length;i++)
if(oyAnchors[i]==this){
oyCUR=i
pfStart=i
pfNext=i
break}
oyDoSHOW(this)
return false}

function oyDoSHOW(anchor){
oyIMG.style.height='auto'
oyIMG.style.width='auto'
oyCPT.firstChild.nodeValue=isBlank(anchor.title)?'':anchor.title
oyTTL.firstChild.nodeValue=isBlank(anchor.firstChild.alt)?'':anchor.firstChild.alt
oyIMG.style.visibility='hidden'
oyBG1.style.display='block'
oyIMG.src=anchor.href}

function oyDoPlace(){
var maxheight=oyBG1.offsetHeight-Math.max(oyBTN.offsetHeight,oyCPT.offsetHeight+oyTTL.offsetHeight)-60
var maxwidth=oyBG1.offsetWidth-60
if (oyIMG.height>maxheight || oyIMG.width>maxwidth){ (oyIMG.height/oyIMG.width>maxheight/maxwidth) ? oyIMG.style.height=maxheight+'px' : oyIMG.style.width=maxwidth+'px' }
oyBG2.style.width=oyIMG.offsetWidth+'px'
oyBG2.style.marginLeft='-'+oyBG3.offsetWidth/2+'px'
oyBG2.style.marginTop='-'+oyBG3.offsetHeight/2+'px'
oyDoFade()}

function oyDoFade(){
oyIMG.style.opacity='0'
oyIMG.style.filter='alpha(opacity=0)'
oyIMG.style.visibility='visible'
for(var fd=0;fd<11;fd++)
setTimeout('oyIMG.style.opacity="'+fd/10+'";oyIMG.style.filter="alpha(opacity='+(fd*10)+')";',fd*50)}

function oyDoCLS(){
oyIMG.src='none'
oyBG1.style.display='none'}

function oyDoNXT(){
if(oyCUR==oyAnchors.length-1)oyCUR=-1
oyDoSHOW(oyAnchors[oyCUR+=1])}

function oyDoPRV(){
if(oyCUR==0)oyCUR=oyAnchors.length
oyDoSHOW(oyAnchors[oyCUR-=1])}

function oyDoKEYS(e){
if(oyBG1.style.display=='block'){
if(e)oyKEY=e.which
else oyKEY=window.event.keyCode
if(oyKEY==27)oyDoCLS() // escape
else if(oyKEY==37 && oyArrowKeys)oyDoPRV() //left arrow
else if(oyKEY==39 && oyArrowKeys)oyDoNXT() // right arrow
}}

function oyDoEL(type,id,parent,title,text,click){
var el=document.createElement(type)
el.id=id
parent.appendChild(el)
if(typeof(title)!="undefined" && title!='none')el.title=title
if(typeof(text)!="undefined" && text!=='none')el.appendChild(document.createTextNode(text))
if(typeof(click)!="undefined")el.onclick=click
return el}

function isBlank(x) {return (!x || /^\s*$/.test(x))}

window.onresize=function(){
  if(oyBG1.style.display=='block')oyDoPlace()
}

