
/* Slide Show scripts */

function Slideshow(slideshowEl,count) {

	this.el = slideshowEl;
	this.el.id = 'show_' + count;
	this.current = 0;
	
	this.css = {
		dynamicClass         :'js',
		slideNavigationClass :'slidenav',
		slidePreviousClass :'slideprev',
		slideNextClass :'slidenext',
		currentClass         :'current'
	}

	this.labels = {
		previous       : '<<',
		next           : '>>',
		counterDivider : ' / '
	}

	this.init();
}

Slideshow.prototype.init = function() {

	this.items = this.el.getElementsByTagName('li');
	this.all = this.items.length;
	if (this.all > 1) {
		tools.addClass(this.el, this.css.dynamicClass);
		this.createNav(this.el);
	}
	
	this.show();
	
}

Slideshow.prototype.createNav = function(o) {

	var tempObj = this;
	var p = document.createElement('p');
	tools.addClass(p, this.css.slideNavigationClass);
	
	this.prev = document.createElement('a');
	tools.addClass (this.prev, this.css.slidePreviousClass);
	this.prev.setAttribute('href', '#');
	var templabel = document.createTextNode(this.labels.previous);
	this.prev.appendChild(templabel);
	tools.addEvent(this.prev, 'click', function(e){
			Slideshow.showPrevNext(tempObj,'prev');
			tools.cancelClick(e);
		});
	p.appendChild(this.prev);
	
	this.count = document.createElement('span');
	templabel = document.createTextNode( (this.current+1) + this.labels.counterDivider + this.all);
	this.count.appendChild(templabel);
	p.appendChild(this.count);
	
	this.next = document.createElement('a');
	tools.addClass (this.next, this.css.slideNextClass);
	this.next.setAttribute('href', '#');
	var templabel = document.createTextNode(this.labels.next);
	this.next.appendChild(templabel);
	tools.addEvent(this.next, 'click', function(e){
			Slideshow.showPrevNext(tempObj,'next');
			tools.cancelClick(e);
		});
	p.appendChild(this.next);
	
	o.parentNode.insertBefore(p, o);
	
}

Slideshow.showPrevNext = function(o,direction) {
	
	tools.removeClass(o.items[o.current], o.css.currentClass);
	var addto = direction === 'next' ? 1 : -1;
	o.current = o.current + addto;
	if(o.current < 0){
		o.current = (o.all-1);
	}
	if(o.current > o.all-1){
		o.current = 0;
	}
	
	o.show();
	
}

Slideshow.prototype.show = function() {
	
	var templabel = document.createTextNode( (this.current+1) + this.labels.counterDivider + this.all);
	this.count.replaceChild(templabel, this.count.firstChild);
	tools.addClass(this.items[this.current], this.css.currentClass);
	
}

// create slideshows array
slideshows = {
	showsList:[],
	init:function(){
		if(document.getElementById && document.createTextNode){
			var tempList = document.getElementsByTagName('ul');
			if(tempList){
				for (var i=0; i<tempList.length; i++) {
					if (tempList[i].className == 'slideshow') {
						slideshows.showsList.push(tempList[i]);
					}
				}
			}
		}
		for (var i=0; i<slideshows.showsList.length; i++) {
			var el = new Slideshow(slideshows.showsList[i],i);
		}
	}
}

// this kicks everything off
tools.addEvent(window,'load',slideshows.init);



