(function($){
	var spd = 0;
	var methods = {
			init : function( options ) {
		 		
			    return this.each(function(){
			      
			      var $this = $(this),
			          data = $this.data('shmoo'),
			          pans = options.pans || '.pan',
			          container = options.container || '#home-feature';
			         
			      if ( ! data ) {
			    	  //set up
			    	  
			    	 
			        $(this).data('shmoo', {
			            target : $this,
			            fps : parseInt(1000/options.fps),
			            bg : $(options.bg || '#shmoo-back'),
			            bg_spd : options.bg_spd || .1,
			            maxSpd : options.speed || 10,
			            spd : 0,
			            x_ : 0,
			            dir : 0,
			            curr :0,
			            lft : options.start_left,
			            bglft : parseInt($(options.bg).css('left')),
			            vp : $(container),
			            vpPos : parseInt($(container).offset().left),
			            pnWidth : parseInt($(pans).first().css('width')),
			            vpWidth : parseInt($(container).css('width')),
			            smWidth : parseInt($('#shmoos0').css('width')),
			            clock : null,
			            getgoing : true,
			            swap: true,
			            slowest: 0,
			            cnt : 0,
			            panstate : 0,
			            shmenter : false,
			            pan_sec:  options.pan_sec || 4,
			            cursor : [],
			            webkit : $.browser.webkit,
			            chasers : { right : $('#right.chaser') , left : $('#left.chaser')},
			            chaserTimer : null,
			            chaserInnerTimer : null,
			            chaserLastPos : 0,
			            stopCnt : 0,
			            slowdown : false,
			            slowdownStep : 0,
			            slowdownFactor : 20,
			            isChasing: false,
			            targetChase : 0,
			            curr_info : null,
			            curr_info_index : 0,
			            info_last : $('.shmoo-info').length - 1,
			            info_divs : $('.shmoo-info')
  
			        });
			        
			        
			      } // if !data
			      
			      /*
			      $this.data('shmoo').webkit = true;
			      $this.data('shmoo').dir = 'left';
			      $this.data('shmoo').spd = 45;
			      methods.scroll.apply($this,[$this])
			      */
			      
			      			      
			    });
			    
			    
			 },
			infinite : function(spd,dir){
				var data = this.data('shmoo'),
				    $this = this;
				$('.shmoo-info').each(function(){
					
					if($(this).offset().left > 200 && !data.curr_info){
						data.curr_info = $(this);
						data.info_divs.each(function(index){
							if($(this).attr('id') == data.curr_info.attr('id')){
								data.curr_info_index = index;
							}
						})
					}
					
					$(this).css('display','none');
					$(this).css('height','auto');
					
				});
				setTimeout(function(){
					data.curr_info.fadeIn(300); 
					setTimeout(function(){methods.scroll.apply($this)} , 500);
					
				  },800);
				data.webkit = true;
				data.dir = dir;
				data.spd = spd;
				//methods.scroll.apply(this);
				
				
			},
			scroll : function($this,e,chasing){
				
				var data = this.data('shmoo'),
					x    = data.spd * .01;
				dir =  data.dir;
			    
					
				
					clearTimeout(data.clock);
					data.getgoing = true;
					data.slowdown = false;
					data.currspeed = 0;

					methods.animate.apply(this,[dir,(x  * data.maxSpd)]);
				
				
			},
			animate : function(dir,spd){
				
			
				var $this = this;
				this.data('shmoo').clock = setTimeout(function(){
			    	//$('#debug2').html('vp : ' + $('#shmoos-wrapper').css('left'));

					var data = $this.data('shmoo'),
					    fps = data.fps,
					    lft  = data.lft,
					    sign  = 'left' == dir ? 1:-1,
					    swap_mod = 	Math.abs((data.lft /*+ -sign*1000*/)%data.smWidth);
					
					

						
						
						//$('#debug').html('speed : ' + (spd*20));

					if(data.getgoing ){
						$('.shmoo-tar').css({'z-index': 100});
						//$('#debug').html(parseInt(data.curr_info.offset().left));
						data.lft = lft + sign*spd;
						data.bglft = data.bglft + (sign*spd*data.bg_spd);
						//methods.swap.apply($this,[dir]);
						if(swap_mod < 20){
							methods.swap.apply($this,[dir]);
						}
						if('left' == dir){
							if(750 < data.curr_info.offset().left){
								data.curr_info.fadeOut(400);
								data.curr_info_index = data.curr_info_index > 0  ? data.curr_info_index -1 : data.info_last;
								data.curr_info = $(data.info_divs[data.curr_info_index]);
								data.curr_info.show();

							}
							if(swap_mod > 4990){
								$this.data('shmoo').swap = true;
							}
							
						}
						else{
							if(swap_mod > 20 && swap_mod < 30){
								$this.data('shmoo').swap = true;
							}
							
							
						
						}
						$this.css('left' , data.lft+'px');
						data.bg.css('left', data.bglft +'px');
						data.clock = setTimeout(arguments.callee,fps);
					}
					else {
						$('.shmoo-tar').css({'z-index': 0});

					}
				},this.data('shmoo').fps);
					
			},
			swap : function(dir) {
				var data = this.data('shmoo'),
					onShmoo = $('#shmoos'+data.curr%2),
				    offShmoo = $('#shmoos' + (data.curr+1)%2),
				    onOffset  = onShmoo.offset().left - data.vpPos,
				    offOffset  = offShmoo.offset().left - data.vpPos,
				    sw = data.smWidth;
				
				if(data.swap){
					data.swap = false;
					
					// if on shmoo is not visible swap
					
					
					if('left' != dir){
						if(onOffset < offOffset){
							onShmoo.css({left : (parseInt(offShmoo.css('left')) + sw) + 'px' });
							++data.curr;
						}
					} else{ // right
						
						if(onOffset < offOffset){
							offShmoo.css({left : (parseInt(onShmoo.css('left')) - sw) + 'px'});
							++data.curr;
						}
						
					}
				}
			}
	};
	$.fn.shmoo = function(method){
		if(methods[method]){
			return methods[method].apply(this, Array.prototype.slice.call(arguments,1));
		}
		else if (typeof method === 'object' || ! method){
			return methods.init.apply(this,arguments);
		}
		else {
			$.error('Shmoo Method ' + method + ' does not exist');
		}
	};
	
})(jQuery);
