/*
 * 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:true,
			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 = '',
			$this = this,
			slides = $this.find(opts.slide_element).get(),
			dif = 0;
			
		// PRIVATE & PUBLIC METHODS		
		var setup = function(){					
			//debug($this);
		
			$this.css('position','relative');
			if(opts.pan) $this.css('overflow','hidden');		
		
			var giw =$this.width();
			
			//log(giw);			
			
			jQuery.each(slides, function(i){
				var img = jQuery(slides[i]);
				
				var iw = img.width();
				//log(iw);		
				if(opts.center){
					var dif = (iw < giw) ? Math.round((giw - iw)/2) : 0;						
				}else{					
					var dif = 0;					
				}		
				//log(dif);
				//dif = (opts.center) ? (dif/2) :  
				//jQuery(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', dif+'px');			
				img.css({
					zIndex: slides.length - i,
					position: 'absolute',
					bottom: '0',left: dif+'px'
					});			
			});			
			//debug(slides);		
			//dif = 0;
			
			if ( opts.pauselink != null ) {		jQuery('#' + opts.pauselink).click(pause);	}			
			showSlide();			
			//log('current:: '+ current);			
			if(!opts.pausestate) $this.play();					
			//debug(this);
		}
		
		var showSlide = function(){
		
			var current_slide = jQuery(slides[current]);
			var iw = current_slide.width();
			var giw = $this.width();
			
			//log(giw +' : '+iw);
			
			dif = giw - iw;
		
			if(opts.center){
					//log('center dif: '+dif);
					//current_slide.css({zIndex:'1',left: (dif/2)+'px',top:'0'});
					var ocss = {zIndex:'1',left: (dif/2)+'px',top:'0'};
						
			}else{
				//log('default: ' + jQuery(slides[current]).css('left') );
				var ocss = {zIndex:'1',left:'0',bottom:'0'};
			}				
			//var lpos = 
			
			//jQuery(slides[current])
				//.css({zIndex:'1',left:'0',top:'0'})
			current_slide
				.css(ocss)
				.fadeIn(opts.fadetime,function(){
					//debug(slides[current]);
					if ( opts.onImage != null ) 	opts.onImage.call(slides[current]);					
					
					if(opts.pan){
						
						var iw = jQuery(slides[current]).width();
						var ih = jQuery(slides[current]).height();
						var giw =$this.width();
						var gih =$this.height();	
						var dif = ih - gih;
					
					
						//log('pan dif: '+dif);
						////////////////// PAN IMAGE					
						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({'bottom': '-'+dif+'px'}, dif * opts.pan_speed)
							}
						}
						/////////////////////					
					}	
				});
		}
		var change = function () {
		 //console.log('change '+pauseState);
			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[last]).css('display', 'block').css('zIndex', '0');
				jQuery(slides[last]).css('zIndex', '0');				
				showSlide();				
				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();
		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);


