/*	name			: ClassBehaviours, the javascript framework based on class-name parsing	update			: 9.3.17	author			: Maurice van Creij	dependencies	: jquery.classbehaviours.js	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.
    
    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};
	
	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};
	
	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// make a slideshow from a list of images
	jQuery.classBehaviours.handlers.slideshowList = {
		// properties
		name: 'slideshowList',
		locked: false,
		index: 0,
		// methods
		start: function(node){
			// get the next and previous buttons
			allButtons = node.getElementsByTagName('button');
			// set the event handlers
			if(allButtons.length>0) allButtons[allButtons.length-1].onclick = this.next;
			if(allButtons.length>1) allButtons[allButtons.length-2].onclick = this.previous;
			// update the instance counter of this class
			this.index += 1;
			// set the default states of the slides
			allSlides = node.getElementsByTagName('li');
			allSlides[0].className += (allSlides[0].className.indexOf('active')<0) ? ' active' : '' ;
			for(var a=0; a<allSlides.length; a++){
				// if the slide doesn't have an id, make one
				allSlides[a].id = (allSlides[a].id) ? allSlides[a].id : 'list' + this.index + 'slide' + a;
				// set the initial display state
				allSlides[a].style.position = (allSlides[a].className.indexOf('active')>-1) ? 'relative' : 'absolute' ;
				// set the initial fade state
				if(allSlides[a].className.indexOf('active')<0) jQuery.classBehaviours.fader.setFade(allSlides[a], 0);
			}
		},
		/* events */
		next: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			if(!jQuery.classBehaviours.handlers.slideshowList.locked){
				jQuery.classBehaviours.handlers.slideshowList.locked = true;
				// get all slides
				allSlides = objNode.parentNode.getElementsByTagName('li');
				// default starting position
				var activeSlide = allSlides[0];
				var nextSlide = allSlides[1];
				// look for the active node
				for(var a=0; a<allSlides.length; a++){
					// if this slide is marked active
					if(allSlides[a].className.indexOf('active')>-1){
						// store the active slide
						activeSlide = allSlides[a];
						// store the next slide
						nextSlide = (a<allSlides.length-1) ? allSlides[a+1] : allSlides[0] ;
					}else{
						// hide the inactive slides
						jQuery.classBehaviours.fader.setFade(allSlides[a], 0);
					}
				}
				// set the display state
				activeSlide.style.position = 'absolute';
				activeSlide.style.zIndex = 2000;
				nextSlide.style.position = 'relative';
				nextSlide.style.zIndex = 1000;
				// change the classes
				activeSlide.className = activeSlide.className.replace(' active','').replace('active','');
				nextSlide.className += ' active';
				// cross-fade between the active and next slide
				jQuery.classBehaviours.fader.setFade(nextSlide, 100);
				jQuery.classBehaviours.fader.fadeOut(activeSlide.id, 5, 10, "jQuery.classBehaviours.handlers.slideshowList.unlock()");
			}
		},
		previous: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			if(!jQuery.classBehaviours.handlers.slideshowList.locked){
				jQuery.classBehaviours.handlers.slideshowList.locked = true;
				// get all slides
				allSlides = objNode.parentNode.getElementsByTagName('li');
				// default starting position
				var activeSlide = allSlides[0];
				var nextSlide = allSlides[allSlides.length-1];
				// look for the active node
				for(var a=0; a<allSlides.length; a++){
					// if this slide is marked active
					if(allSlides[a].className.indexOf('active')>-1){
						// store the active slide
						activeSlide = allSlides[a];
						// store the next slide
						nextSlide = (a>0) ? allSlides[a-1] : allSlides[allSlides.length-1] ;
					}else{
						// hide the inactive slides
						jQuery.classBehaviours.fader.setFade(allSlides[a], 0);
					}
				}
				// set the display state
				activeSlide.style.position = 'absolute';
				activeSlide.style.zIndex = 2000;
				nextSlide.style.position = 'relative';
				nextSlide.style.zIndex = 1000;
				// change the classes
				activeSlide.className = activeSlide.className.replace(' active','').replace('active','');
				nextSlide.className += ' active';
				// cross-fade between the active and next slide
				jQuery.classBehaviours.fader.setFade(document.getElementById(nextSlide.id), 100);
				jQuery.classBehaviours.fader.fadeOut(activeSlide.id, 5, 10, "jQuery.classBehaviours.handlers.slideshowList.unlock()");
			}
		},
		unlock: function(){
			this.locked = false;
		}
	}
		
	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.slideshowList = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.slideshowList.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".slideshowList").slideshowList();
			}
		);
	}


