/*
 * 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: 'fast',
			timeout: '3000',
			type: 'sequence',
			pausestate: 0,
			pan:false,
			pan_speed:10,
			pauselink: null,
			onPlay: null,
			onPause: null,
			onOver: null,
			onLeave:null,
			onClick:null,
			onImage:null,
			onChange:null
		};	
		//if (options) $.extend(defaults, options);
		var opts = jQuery.extend({}, defaults, options);
		//debug(opts);
		var	pauseState = opts.pausestate,
			current = 0,
			last = 0,
			timer = '';
			
			
		var $this = this;	
		var slides = $this.find(opts.slide_element).get();
		// PRIVATE & PUBLIC METHODS		
		
		var setup = function(){
			
			$this.css('position','relative');
			if(opts.pan) $this.css('overflow','hidden');
			
			
			jQuery.each(slides, function(i){
				//jQuery(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', '0');			
				jQuery(slides[i]).css('zIndex', slides.length - i).css({position:'absolute',top:'0',left:'0'});		
					//log(i);
					//log(slides[i]);
			});
			
			//debug(slides);
			
			if ( opts.pauselink != null ) {
				jQuery('#' + opts.pauselink).click(pause);
			}
			
			// FIRST IMAGE
			//log('current: '+ current);
			/*jQuery(slides[current]).css('zIndex', '1').fadeIn(opts.fadetime,function(){
				//debug(slides[current]);
				if ( opts.onImage != null ) 	opts.onImage.call(slides[current]);				
			});
			*/
			showSlide();
			
			//log('current:: '+ current);
			
			if(!opts.pausestate) $this.play();		
			
			//debug(this);
		}
		
		var showSlide = function(){
			jQuery(slides[current]).css({zIndex:'1',left:'0',top:'0'}).fadeIn(opts.fadetime,function(){
					//debug(slides[current]);
					if ( opts.onImage != null ) 	opts.onImage.call(slides[current]);
					////////////////// PAN IMAGE
					if(opts.pan){
						//jQuery(slides[current]).fadeIn(opts.fadetime);						
						var iw = jQuery(slides[current]).width();
						var ih = jQuery(slides[current]).height();
						var giw =$this.width();
						var gih =$this.height();
						
						//log(iw+'x'+ih+' : '+ giw+'x'+gih);
						//log(iw>ih);
						
						if(iw > giw){
							var dif = iw - giw;
							if(dif != 0){
								jQuery(slides[current]).animate({'left': '-'+dif+'px'}, dif * opts.pan_speed)
							}
						}
						if(ih > gih){						
							var dif = ih - gih;
							if(dif != 0){
								jQuery(slides[current]).animate({'top': '-'+dif+'px'}, dif * opts.pan_speed)
							}
						}
					}	////////////////// PAN IMAGE					
				});
		}
		
		var change = function () {
		//this.change = function () {
		 //log('current '+current);
		 
		 if ( opts.onChange != null ) 	opts.onChange.call($this);
		 
			if ( pauseState == 0 ) {
				
				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\'');
				}
				
				
				for (var i = 0; i < slides.length; i++) {
					jQuery(slides[i]).css('display', 'none');
					//jQuery(slides[i]).hide();
				}
				//jQuery(slides[last]).css('display', 'block').css('zIndex', '0');
				jQuery(slides[last]).css('zIndex', '0');
				//jQuery(slides[current]).css('zIndex', '1').fadeIn(opts.fadetime);
				
				showSlide();
				/*
				jQuery(slides[current]).css({zIndex:'1',left:'0',top:'0'}).fadeIn(opts.fadetime,function(){
					//debug(slides[current]);
					if ( opts.onImage != null ) 	opts.onImage.call(slides[current]);
					////////////////// PAN IMAGE
					if(opts.pan){
						//jQuery(slides[current]).fadeIn(opts.fadetime);						
						var iw = jQuery(slides[current]).width();
						var ih = jQuery(slides[current]).height();
						var giw =$this.width();
						var gih =$this.height();
						
						//log(iw+'x'+ih+' : '+ giw+'x'+gih);
						//log(iw>ih);
						
						if(iw > giw){
							var dif = iw - giw;
							if(dif != 0){
								jQuery(slides[current]).animate({'left': '-'+dif+'px'}, dif * opts.pan_speed)
							}
						}
						if(ih > gih){						
							var dif = ih - gih;
							if(dif != 0){
								jQuery(slides[current]).animate({'top': '-'+dif+'px'}, dif * opts.pan_speed)
							}
						}
					}	////////////////// PAN IMAGE					
				});
				*/				
				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			
		setup();		
		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);


