/*
 * PRIMO STUDIO jQuery Slideshow 
 * Author: Henke Do
 * About: 
 * Heavily Modified jQuery Slideshow Plugin v1.3 by Matt Oakes (http://www.matto1990.com/jquery/slideshow/)
 * 
 * Modifications:
 * - slide_element
 * - fadetime
 * - callback functions
 *
 */
;(function(jQuery) {
  //
  // plugin definition
  //
  jQuery.fn.slideshow = function(options) {
		var defaults = {
			slide_element: 'img',
			fadetime: 'slow',
			timeout: '3000',
			type: 'sequence',
			pausestate: 0,
			pauselink: null,
			onPlay: null,
			onPause: null,
			onOver: null,
			onLeave:null,
			onClick:null
		};	
		//if (options) $.extend(defaults, options);
		var opts = jQuery.extend({}, defaults, options);
		//debug(opts);
		var	pauseState = opts.pausestate,
			current = 1,
			last = 0,
			timer = '';
			
			
		var $this = this;	
		
		// PRIVATE & PUBLIC METHODS		
		var change = function () {
		 //console.log('change '+pauseState);
			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');
				jQuery(slides[last]).css('zIndex', '0');
				jQuery(slides[current]).css('zIndex', '1').fadeIn(opts.fadetime);
				
				if ( opts.type == 'sequence' ) {
					if ( ( current + 1 ) < slides.length ) {
						current = current + 1;
						last = current - 1;
					}
					else {
						current = 0;
						last = slides.length - 1;
					}
				}
				else if ( opts.type == 'random' ) {
					last = current;
					while (	current == last ) {
						current = Math.floor ( Math.random ( ) * ( slides.length ) );
					}
				}
				else {
					alert('type must either be \'sequence\' or \'random\'');
				}
				timer = setTimeout(change, opts.timeout);
			}
		};
		//var pause = function() {
		this.pause = function() {	
			if ( pauseState == 0 ) {
				pauseState = 1;
				clearTimeout(timer);
				if ( opts.playcallback != null ) {
					opts.pausecallback(jQuery('#' + opts.pauselink));
				}
			}
			else {
				pauseState = 0;
				change();
				if ( opts.playcallback != null ) {
					opts.playcallback(jQuery('#' + opts.pauselink));
				}
			}
			return false;
		};
		
		this.play = function(){
			//log('play');
			pauseState = 0;
			//console.log('play '+pauseState);
			if ( opts.type == 'sequence' ) {
				 timer = setTimeout(change, opts.timeout);
			}
			else if ( opts.type == 'random' ) {
				do {
					current = Math.floor ( Math.random ( ) * ( slides.length ) );
				} while ( current == 0 );
					timer = setTimeout(change, opts.timeout);
			}
			else {
				alert('type must either be \'sequence\' or \'random\'');
			}
		}	
		
		this.stop = function() {
		//	log('stop');
			pauseState = 1;
			clearTimeout(timer);	
		};
		
		
		//this.bind('mouseover', function() {
		this.mouseover(function() {
			if ( opts.onOver != null ) 	opts.onOver.call($this);
		});
		
		this.mouseout(function() {
			if ( opts.onOut != null ) 	opts.onOut.call($this);
		});
		
		
		
		// SETUP	
		this.css('position', 'relative');
		var slides = this.find(opts.slide_element).get();
		jQuery.each(slides, function(i){
			jQuery(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', '0');			
		});
		
		//debug(slides);
		
		if ( opts.pauselink != null ) {
			jQuery('#' + opts.pauselink).click(pause);
		}
		
		if(!opts.pausestate) this.play();		
		
		//debug(this);
		
		return this;
		
  };
	
	function log($s) {
    if (window.console && window.console.log)
     window.console.log($s);
  };
	
	
	function debug($obj) {
    if (window.console && window.console.debug)
     window.console.debug('debug: %o', $obj);
  };
	/*
	
	*/
})(jQuery);

