// Slideshow
// ver 0.4
// May 18, 2011

var SlideshowClass = function(config) {
	
	
	var _slide_duration   = config.duration || 5000;
	var _slide_container  = $('#'+config.slide_container);
	var _slideshow_nav    = $('#'+config.slideshow_nav);
	var _interval_id      = false;
	
	var _num_slides       = $('LI', _slide_container).length;
	var _current_position = 1;	
	var _new_pos          = 1;
	
	
	
	var _init = function() {
	
		// Bind click function
		$('LI A', _slideshow_nav).click(_do_click);
		
		// Cycle through slideshow
		if ($('LI', _slideshow_nav).size() > 1 ) {
			_interval_id = setInterval(_slide_cycle, _slide_duration);
		}	
		
		$('.next', _slideshow_nav).click(_next);
		$('.prev', _slideshow_nav).click(_prev);
		
		_update_nav();
	};
	
	
	
	
	var _do_click = function() {
		
		// Stop cycling through
		if (typeof(_interval_id) != 'undefined') {
			clearInterval(_interval_id);
		}
				
		// Return if already visible
		if ($(this).hasClass('active')) {
			return false;
		}

		var pos = $(this).attr('position');
		
		_change_slide(pos);
		
		_interval_id = setInterval(_slide_cycle, _slide_duration);
		
		return false;

	};
	

	
	
	var _slide_cycle = function() {
		
		// Get position of currently visible slide
		var pos = parseInt($('LI.active A', _slideshow_nav).attr('position'));
		
		// If less than total slide count increment by 1, otherwise reset to 1
		if (pos < $('LI', _slideshow_nav).size()) {
			pos += 1;
		} else {
			pos = 1;
		}
		
		// Run the change_slide function
		_change_slide(pos);
	};
	
	

	
	var _change_slide = function(pos) {
		
		var prev_slide = $('LI:visible', _slide_container);
		var next_slide = $('LI[position='+pos+']', _slide_container);
		
		// Remove all existing active class
		$('LI', _slideshow_nav).removeClass('active');
		
		// Add to current nav item
		$('LI A[position='+pos+']', _slideshow_nav).parent('LI').delay(_slide_duration).addClass('active');
		
		
		// Cross fade - better for images
		next_slide.css('z-index', 1);
		next_slide.fadeIn(function() {
			prev_slide.hide();
			$('LI', _slide_container).css('z-index', 'auto');
		});
		
		// Cross fade - better for text
		/*next_slide.fadeIn();
		prev_slide.fadeOut();
		*/
		
		return false;
	};
	
	
	
	/**
	 * Change to next slide 
	 */
	var _next = function() {
				
		_current_position = parseInt($('LI:visible', _slide_container).attr('position'));

		if (_current_position < _num_slides) {
			_new_pos = (_current_position + 1);
		}
		$('LI:visible', _slide_container).hide();
		$('LI[position='+_new_pos+']', _slide_container).show();
		
		_update_nav();
		return false;
	}
	
	
	/**
	 * Change to previous slide 
	 */
	var _prev = function() {
	
		_current_position = parseInt($('LI:visible', _slide_container).attr('position'));

		if (_current_position > 1) {
			_new_pos = (_current_position - 1);
		}
		$('LI:visible', _slide_container).hide();
		$('LI[position='+_new_pos+']', _slide_container).show();
		
		_update_nav();
		return false;
	}
	
	
	/**
	 * Update navigation
	 */
	var _update_nav = function() {
		
		$('.prev', _slideshow_nav).removeClass('disabled');
		$('.next', _slideshow_nav).removeClass('disabled');
		
		// Disable links
		if (_new_pos === 1) {
			$('.prev').addClass('disabled');
		}
		if (_new_pos === _num_slides) {
			$('.next').addClass('disabled');
		}
		$('#current_page').html(_new_pos);
		$('#total_pages').html(_num_slides);
	}
	
	
	_init();
}

