// Unobtrusive JS Slideshow
// http://icant.co.uk/articles/maintainablejavascriptslideshow/index.html

/* tools.js: helper methods */
tools = {
	addEvent:function( obj, type, fn ) {
		if ( obj.attachEvent ) {
		 obj['e'+type+fn] = fn;
		 obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		 obj.attachEvent( 'on'+type, obj[type+fn] );
		} else
		 obj.addEventListener( type, fn, false );
	},
	removeClass:function(o,c){
		var rep=o.className.match(' '+c)?' '+c:c;
		o.className=o.className.replace(rep,'');
	},
	addClass:function(o,c){
		var test = new RegExp("(^|\\s)" + c + "(\\s|$)").test(o.className);
		if(!test){o.className+=o.className?' '+c:c;}
	},
	cancelClick:function(e){
		if (window.event){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (e && e.stopPropagation && e.preventDefault){
			e.stopPropagation();
			e.preventDefault();
		}
	}
}

// slideshow.js

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();
		}
	},
	createNav:function(o){
		var p = document.createElement('p');
		tools.addClass(p, slideshow.css.slideNavigationClass);
		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){
		if(this === slideshow.next || this === slideshow.prev){
			tools.removeClass(slideshow.items[slideshow.current], slideshow.css.currentClass);
			var addto = this === slideshow.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);

// slideshow-css.js

slideshow.css = {
	
	/*
		These are the classes and IDs used in the slideshow function.
		You can change any of them here. Please make sure to use 
		quotation marks around the names and end all but the last 
		one with a comma.
	*/
  
  showID               :'slideshow',	
  dynamicClass         :'js',
  slideNavigationClass :'slidenav',
  currentClass         :'current'

}

// slideshow-labels.js

slideshow.labels = {

  /*
    These are the text labels used in the slideshow.
    You can change any of them here. Please make sure 
    to use quotation marks around the values and end  
    all but the last one with a comma.
  */

	previous       : '<<',
	next           : '>>',
	counterDivider : ' of '

}


