/*
 * 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,
			pan:false,
			pan_speed:10,
			center:false,
			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);
		
		return this.each(function(k){	
			var	pauseState = opts.pausestate,
			current = 1,
			last = 0,
			timer = '';
			
			var $this = this;	
			
			//log($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');
					
					var iw = jQuery(slides[current]).width();
					var giw = $(this).width();			
					var dif = iw - giw;
					
					if(opts.pan){					
						////////////////// PAN IMAGE					
						if(iw > giw){						
							if(dif != 0){
								jQuery(slides[current]).animate({'left': '-'+dif+'px'}, dif * opts.pan_speed)
							}
						}
						/////////////////////					
					}else if(opts.center){
						jQuery(slides[current]).css('left', (dif/2)+'px');					
					}
					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() {
			this.mouseover = function() {
				if ( opts.onOver != null ) 	opts.onOver.call($this);
			};
			
			//this.mouseout(function() {
			this.mouseout = function() {
				if ( opts.onOut != null ) 	opts.onOut.call($this);
			};			
			
			// SETUP	
			$(this).css('position','relative');
			if(opts.pan){		$(this).css('overflow','hidden');	}
			var slides = $(this).find(opts.slide_element).get();
			
			//debug(slides);
			
			var giw = $(this).width(); 			

			jQuery.each(slides, function(i){
				var iw = jQuery(slides[i]).width();
				var dif = (opts.center) ? ( (iw < giw) ? Math.round((giw - iw)/2) : 0 ) : 0;
				jQuery(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', dif+'px');			
			});
			
			//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);

