/*
 * Light Slideshow - Query plugin for simple slideshow
 *
 * Copyright (c) 2007 Christophe Le Bars
 *
 * based on :
 *
 * Copyright (c) 2007 Matt Oakes, http://www.gizone.co.uk/
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
        
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/


(function($) {
	
	// timeout id
	var timer;
	var pauseState = 0;
	var current = 0;
	var last = 0;
	var slides;
	var settings;

	// the public plugin method
	$.fn.Slideshow = function(user_settings) {

		settings = $.extend($.extend({}, arguments.callee.defaults), user_settings || {});
	
        this.css('position', 'relative');
        slides = this.find('li').get();
        jQuery.each(slides, function(i){
            jQuery(slides[i]).css('position', 'absolute').css('top', '0').css('left', '0').hide();
        });
        timer = setTimeout(change, settings.delay);

		return this;
	};
	
	function change() {

		if ( pauseState == 0 ) {
			//for (var i = 0; i < slides.length; i++) {
			//	jQuery(slides[i]).css('display', 'none');
			//}
			jQuery(slides[last]).css('display', 'block').css('zIndex', '0').hide();
			jQuery(slides[current]).css('zIndex', '1').show();
			
            if ( ( current + 1 ) < slides.length ) {
                current = current + 1;
                last = current - 1;
            }
            else {
                current = 0;
                last = slides.length - 1;
            }
			timer = setTimeout(change, settings.delay);
		}
	}
	
	function pause() {
		if ( pauseState == 0 ) {
			pauseState = 1;
			clearTimeout(timer);
			if ( settings.playcallback != null ) {
				settings.pausecallback(jQuery('#' + settings.pauselink));
			}
		}
		else {
			pauseState = 0;
			change();
			if ( settings.playcallback != null ) {
				settings.playcallback(jQuery('#' + settings.pauselink));
			}
		}
		return false;
	}

	// define global defaults, editable by client
	$.fn.Slideshow.defaults = {
		delay: '2000',
		pauselink: null,
		playcallback: null,
		pausecallback: null
	};

})(jQuery);

