/*

Source: http://icant.co.uk/articles/maintainablejavascriptslideshow/index.html

*/
function clickNext() {
    slideshow.show(this,true); 
    setTimeout("clickNext()",5000);
}

slideshow = {
	current:0,
	init:function(){
		if(document.getElementById && document.createTextNode){
			var list = document.getElementById(slideshow.css.showID);
			if(list){
				slideshow.items = list.getElementsByTagName('li');
				slideshow.all = slideshow.items.length;
				if(slideshow.all > 1){
					tools.addClass(list, slideshow.css.dynamicClass);
					slideshow.createNav(list);
				}
			}
			slideshow.show();
                        setTimeout("clickNext()",5000);
		}
	},
	createNav:function(o){
		var p = document.createElement('p');
		tools.addClass(p, slideshow.css.slideNavigationClass);
		var title = document.createTextNode(slideshow.labels.title);
                p.appendChild(title);
		slideshow.prev = document.createElement('a');
		slideshow.prev.setAttribute('href', '#');
		var templabel = document.createTextNode(slideshow.labels.previous);
		slideshow.prev.appendChild(templabel);
		tools.addEvent(slideshow.prev, 'click', slideshow.show);		
		p.appendChild(slideshow.prev);
		slideshow.count = document.createElement('span');
		templabel = document.createTextNode( (slideshow.current+1) + slideshow.labels.counterDivider + slideshow.all);
		slideshow.count.appendChild(templabel);
		p.appendChild(slideshow.count);
		slideshow.next = document.createElement('a');
		slideshow.next.setAttribute('href', '#');
		var templabel = document.createTextNode(slideshow.labels.next);
		slideshow.next.appendChild(templabel);
		tools.addEvent(slideshow.next, 'click', slideshow.show);		
		p.appendChild(slideshow.next);
		o.parentNode.insertBefore(p, o);
	},
	show:function(e, next){
		if(this === slideshow.next || this === slideshow.prev || next){
            tools.removeClass(slideshow.items[slideshow.current], slideshow.css.currentClass);
            var addto = this === slideshow.next || next ? 1 : -1;
            slideshow.current = slideshow.current + addto;
            if(slideshow.current < 0){
                slideshow.current = (slideshow.all-1);
            }
        }
        
        if(slideshow.current > slideshow.all-1){
            slideshow.current = 0;
        }

		var templabel = document.createTextNode( (slideshow.current+1) + slideshow.labels.counterDivider + slideshow.all);
		slideshow.count.replaceChild(templabel, slideshow.count.firstChild);
		tools.addClass(slideshow.items[slideshow.current], slideshow.css.currentClass);
		tools.cancelClick(e);
	}
}
tools.addEvent(window,'load',slideshow.init);
