/* slideshow javascript */

/* SLIDESHOW CLASS */
function slideShow(id){
var me = this;//scope fix
this.box = document.getElementById(id);
this.name = id;
this.slides = [];
this.captions = [];
this.attr = [];
this.lenSlides = 0;
this.at = 0;
//methods
//next slide	
this.next = function(){
var i = me.at;
var x = me.slides.length - 1;
if (i == x){//last slide
    me.doSlide(i, 0);
		}
		else{
        me.doSlide(i, i+1);			
				}
}
//previous slide
this.previous = function(){
var i = me.at;
var x = me.slides.length -1;
if (i == 0){//first slide
    me.doSlide(i, x)
		}
		else{
		    me.doSlide(i, i-1);
		    }
}
//button pressed
this.show = function(){
var y = parseInt(this.id.substring(me.name.length));//slide to go to
me.doSlide(me.at, y)
}
//go to slide
this.doSlide = function(x, y){
//x:slide at
//y:slide to go to
me.slides[x].className = '';
me.captions[x].className = 'caption';
me.attr[x].className = 'attribution';
me.slides[y].className = 'slide';
me.captions[y].className = 'cap';
me.attr[y].className = 'attr';
me.at = y;
//button stuff
var btnOld = document.getElementById(me.name + x);
btnOld.className = '';
var btnNew = document.getElementById(me.name + y);
btnNew.className = 'at';
}	
//show a nice title
this.showTitle = function(){
var old = document.getElementById('blurb')
if (old){
    document.getElementById('content').removeChild(old);
		}
var where = getOffsets(this);
if (!this.id){//prev/next button
    var at = me.at;
		var top = me.slides.length - 1;
    if(this.firstChild.nodeValue == 'next'){
		    if(at == top){
				    i = 0;
				    }
						else{
						    i= at + 1;
								}
		    }
				else{//prev
				    if(at == 0){
						    i = top;
						    }
								else{
								    i = at - 1
								    }
						}
		}
		else{
        var i = this.id.substring(me.name.length);
				}
var txt = me.captions[i].firstChild.nodeValue //+ ' ' + me.attr[i].firstChild.nodeValue;
var blurb = mTag('span', txt);
blurb.style.position = 'absolute';
blurb.style.left = (where[0] + 10) + 'px';
blurb.style.top = (where[1] - 30) + 'px';
blurb.id = 'blurb';
document.getElementById('content').appendChild(blurb);
}
//hide title
this.hideTitle = function(){
var blurb = document.getElementById('blurb');
if(!blurb) return;
document.getElementById('content').removeChild(blurb);
}
}

/* prototype methods */
//load
slideShow.prototype.load = function(){
var arrSlides = this.box.getElementsByTagName('img');
var arrParas = this.box.getElementsByTagName('p');
for (var i = 0; i < arrSlides.length; i++){//load slides
    arrSlides[i].title = '';
    this.slides.push(arrSlides[i]);
		}
for (var i = 0; i < arrParas.length; i++){//load captions
    if (arrParas[i].className == 'caption'){
        this.captions.push(arrParas[i]);
				}
		if (arrParas[i].className == 'attribution'){
		    this.attr.push(arrParas[i]);
				}
		}
this.box.getElementsByTagName('div')[0].style.display = 'none';//hide loader
this.slides[0].className = 'slide';//show first image/caption
this.captions[0].className = 'cap';
this.attr[0].className = 'attr';
this.lenSlides = arrSlides.length;
}
//controls
slideShow.prototype.addControls = function(){
var ctl = mTag('div')
ctl.className = 'control';
var prev = mTag('button', 'previous');//previous
prev.onclick = this.previous;
prev.onmouseover = this.showTitle;
prev.onfocus = this.showTitle;
prev.onmouseout = this.hideTitle;
prev.onblur = this.hideTitle;
ctl.appendChild(prev);
for (var i = 0; i < this.lenSlides; i++){
    var btn = mTag('button', i+1);
		btn.id = this.name + i;
		btn.onclick = this.show;
		btn.onmouseover = this.showTitle;
		btn.onfocus = this.showTitle;
		btn.onmouseout = this.hideTitle;
		btn.onblur = this.hideTitle;
		ctl.appendChild(btn);	
		}
var next = mTag('button', 'next');//next
next.onclick = this.next;
next.onmouseover = this.showTitle;
next.onfocus = this.showTitle;
next.onmouseout = this.hideTitle;
next.onblur = this.hideTitle;
ctl.appendChild(next);
this.box.insertBefore(ctl, this.box.firstChild);
var btn = document.getElementById(this.name + 0);//first button at
btn.className = 'at';
}

//load slideshows
function getShow(){
var divs = tn('div');
for (var i=0; i < divs.length; i++){
    if (divs[i].className == 'slidebox'){
		    var show = new slideShow(divs[i].id);
				show.load();
				show.addControls();
				}
		}
}

addHandler(window, 'load', getShow);//load shows