ÿþ( f u n c t i o n ( $ )   {  
  
         $ . f n . j s o n S u g g e s t   =   f u n c t i o n ( s e a r c h D a t a ,   s e t t i n g s )   {  
                 v a r   d e f a u l t s   =   {  
                         m i n C h a r a c t e r s :   1 ,  
                         m a x R e s u l t s :   u n d e f i n e d ,  
                         w i l d C a r d :   " " ,  
                         c a s e S e n s i t i v e :   f a l s e ,  
                         n o t C h a r a c t e r :   " ! " ,  
                         m a x H e i g h t :   3 5 0 ,  
                         h i g h l i g h t M a t c h e s :   t r u e ,  
                         o n S e l e c t :   u n d e f i n e d ,  
                         a j a x R e s u l t s :   f a l s e ,  
                         w i d t h :   u n d e f i n e d  
                 } ;  
                 s e t t i n g s   =   $ . e x t e n d ( d e f a u l t s ,   s e t t i n g s ) ;  
  
                 r e t u r n   t h i s . e a c h ( f u n c t i o n ( )   {  
  
                         f u n c t i o n   r e g e x E s c a p e ( t x t ,   o m i t )   {  
                                 v a r   s p e c i a l s   =   [ ' / ' ,   ' . ' ,   ' * ' ,   ' + ' ,   ' ? ' ,   ' | ' ,  
 	 	 	 	 	 	 	 	 ' ( ' ,   ' ) ' ,   ' [ ' ,   ' ] ' ,   ' { ' ,   ' } ' ,   ' \ \ ' ] ;  
  
                                 i f   ( o m i t )   {  
                                         f o r   ( v a r   i   =   0 ;   i   <   s p e c i a l s . l e n g t h ;   i + + )   {  
                                                 i f   ( s p e c i a l s [ i ]   = = =   o m i t )   {   s p e c i a l s . s p l i c e ( i ,   1 ) ;   }  
                                         }  
                                 }  
  
                                 v a r   e s c a p e P a t t   =   n e w   R e g E x p ( ' ( \ \ '   +   s p e c i a l s . j o i n ( ' | \ \ ' )   +   ' ) ' ,   ' g ' ) ;  
                                 r e t u r n   t x t . r e p l a c e ( e s c a p e P a t t ,   ' \ \ $ 1 ' ) ;  
                         }  
  
                         v a r   o b j   =   $ ( t h i s ) ,  
 	 	 	 	 w i l d C a r d P a t t   =   n e w   R e g E x p ( r e g e x E s c a p e ( s e t t i n g s . w i l d C a r d   | |   ' ' ) ,   ' g ' ) ,  
 	 	 	 	 r e s u l t s   =   $ ( ' < d i v   / > ' ) ,  
 	 	 	 	 c u r r e n t S e l e c t i o n ,   p a g e X ,   p a g e Y ;  
  
                         / /   W h e n   a n   i t e m   h a s   b e e n   s e l e c t e d   t h e n   u p d a t e   t h e   i n p u t   b o x ,  
                         / /   h i d e   t h e   r e s u l t s   a g a i n   a n d   i f   s e t ,   c a l l   t h e   o n S e l e c t   f u n c t i o n  
                         f u n c t i o n   s e l e c t R e s u l t I t e m ( i t e m )   {  
                                 o b j . v a l ( i t e m . t e x t ) ;  
                                 $ ( r e s u l t s ) . h t m l ( ' ' ) . h i d e ( ) ;  
  
                                 i f   ( t y p e o f   s e t t i n g s . o n S e l e c t   = = =   ' f u n c t i o n ' )   {  
                                         s e t t i n g s . o n S e l e c t ( i t e m ) ;  
                                 }  
                                 / / k h i   c l i c k   c h u y e n   s a n g   t r a n g   s e a r c h  
                                 / / l a y   d a n h   m u c   p h a n   m e m  
                                 / / v a r   c a t e g o r y i d   =   $ ( ' . d r S e a r c h ' ) . v a l ( ) ;  
                                 w i n d o w . l o c a t i o n . h r e f   =   " / P r o d u c t D e t a i l . a s p x ? P r o d u c t I d = "   +   i t e m . i d   +   " " ;  
                                 / / " / P r o d u c t S e a r c h . a s p x ? C a t e g o r y I d = 1 & k e y w o r d = "   +   i t e m . t e x t   +   " & x = 0 & y = 0 " ;  
                         }  
  
                         / /   U s e d   t o   g e t   r i d   o f   t h e   h o v e r   c l a s s   o n   a l l   r e s u l t   i t e m   e l e m e n t s   i n   t h e  
                         / /   c u r r e n t   s e t   o f   r e s u l t s   a n d   a d d   i t   o n l y   t o   t h e   g i v e n   e l e m e n t .   W e   a l s o  
                         / /   n e e d   t o   s e t   t h e   c u r r e n t   s e l e c t i o n   t o   t h e   g i v e n   e l e m e n t   h e r e .  
                         f u n c t i o n   s e t H o v e r C l a s s ( e l )   {  
                                 $ ( ' d i v . r e s u l t I t e m ' ,   r e s u l t s ) . r e m o v e C l a s s ( ' h o v e r ' ) ;  
                                 $ ( e l ) . a d d C l a s s ( ' h o v e r ' ) ;  
  
                                 c u r r e n t S e l e c t i o n   =   e l ;  
                         }  
  
                         / /   B u i l d   t h e   r e s u l t s   H T M L   b a s e d   o n   a n   a r r a y   o f   o b j e c t s   t h a t   m a t c h e d  
                         / /   t h e   s e a r c h   c r i t e r i a ,   h i g h l i g h t   t h e   m a t c h e s   i f   f e a t u r e   i s   t u r n e d   o n   i n  
                         / /   t h e   s e t t i n g s .  
                         f u n c t i o n   b u i l d R e s u l t s ( r e s u l t O b j e c t s ,   s F i l t e r T x t )   {  
                                 s F i l t e r T x t   =   " ( "   +   s F i l t e r T x t   +   " ) " ;  
  
                                 v a r   b O d d R o w   =   t r u e ,   i ,   i F o u n d   =   0 ,  
 	 	 	 	 	 f i l t e r P a t t   =   s e t t i n g s . c a s e S e n s i t i v e   ?   n e w   R e g E x p ( s F i l t e r T x t ,   " g " )   :   n e w   R e g E x p ( s F i l t e r T x t ,   " i g " ) ;  
  
                                 $ ( r e s u l t s ) . h t m l ( ' ' ) . h i d e ( ) ;  
  
                                 f o r   ( i   =   0 ;   i   <   r e s u l t O b j e c t s . l e n g t h ;   i   + =   1 )   {  
                                         v a r   i t e m   =   $ ( ' < d i v   / > ' ) ,  
 	 	 	 	 	 	 t e x t   =   r e s u l t O b j e c t s [ i ] . t e x t ;  
  
                                         i f   ( s e t t i n g s . h i g h l i g h t M a t c h e s   = = =   t r u e )   {  
                                                 t e x t   =   t e x t . r e p l a c e ( f i l t e r P a t t ,   " < s t r o n g > $ 1 < / s t r o n g > " ) ;  
                                         }  
  
                                         $ ( i t e m ) . a p p e n d ( ' < a > < p   c l a s s = " t e x t " > '   +   t e x t   +   ' < / p > < / a > ' ) ;  
  
                                         i f   ( t y p e o f   r e s u l t O b j e c t s [ i ] . e x t r a   = = =   ' s t r i n g ' )   {  
                                                 $ ( i t e m ) . a p p e n d ( ' < p   c l a s s = " e x t r a " > '   +   r e s u l t O b j e c t s [ i ] . e x t r a   +   ' < / p > ' ) ;  
                                         }  
  
                                         i f   ( t y p e o f   r e s u l t O b j e c t s [ i ] . i m a g e   = = =   ' s t r i n g ' )   {  
                                                 $ ( i t e m ) . p r e p e n d ( ' < i m g   s r c = " '   +   r e s u l t O b j e c t s [ i ] . i m a g e   +   ' "   / > ' ) .  
 	 	 	 	 	 	 	 a p p e n d ( ' < b r   s t y l e = " c l e a r : b o t h ; "   / > ' ) ;  
                                         }  
  
                                         $ ( i t e m ) . a d d C l a s s ( ' r e s u l t I t e m ' ) .  
 	 	 	 	 	 	 a d d C l a s s ( ( b O d d R o w )   ?   ' o d d '   :   ' e v e n ' ) .  
 	 	 	 	 	 	 c l i c k ( f u n c t i o n ( n )   {  
 	 	 	 	 	 	         r e t u r n   f u n c t i o n ( )   {  
 	 	 	 	 	 	                 s e l e c t R e s u l t I t e m ( r e s u l t O b j e c t s [ n ] ) ;  
 	 	 	 	 	 	         } ;  
 	 	 	 	 	 	 }   ( i ) ) .  
 	 	 	 	 	 	 m o u s e o v e r ( f u n c t i o n ( e l )   {  
 	 	 	 	 	 	         r e t u r n   f u n c t i o n ( )   {  
 	 	 	 	 	 	                 s e t H o v e r C l a s s ( e l ) ;  
 	 	 	 	 	 	         } ;  
 	 	 	 	 	 	 }   ( i t e m ) ) ;  
  
                                         $ ( r e s u l t s ) . a p p e n d ( i t e m ) ;  
  
                                         b O d d R o w   =   ! b O d d R o w ;  
  
                                         i F o u n d   + =   1 ;  
                                         i f   ( t y p e o f   s e t t i n g s . m a x R e s u l t s   = = =   ' n u m b e r '   & &   i F o u n d   > =   s e t t i n g s . m a x R e s u l t s )   {  
                                                 b r e a k ;  
                                         }  
                                 }  
  
                                 i f   ( $ ( ' d i v ' ,   r e s u l t s ) . l e n g t h   >   0 )   {  
                                         c u r r e n t S e l e c t i o n   =   u n d e f i n e d ;  
                                         $ ( r e s u l t s ) . s h o w ( ) . c s s ( ' h e i g h t ' ,   ' a u t o ' ) ;  
  
                                         i f   ( $ ( r e s u l t s ) . h e i g h t ( )   >   s e t t i n g s . m a x H e i g h t )   {  
                                                 $ ( r e s u l t s ) . c s s ( {   ' o v e r f l o w ' :   ' a u t o ' ,   ' h e i g h t ' :   s e t t i n g s . m a x H e i g h t   +   ' p x '   } ) ;  
                                         }  
                                 }  
                         }  
  
                         / /   P r e p a r e   t h e   s e a r c h   s t r i n g   b a s e d   o n   t h e   s e t t i n g s   f o r   t h i s   p l u g i n ,  
                         / /   r u n   i t   a g a i n s t   e a c h   i t e m   i n   t h e   s e a r c h D a t a   a n d   d i s p l a y   a n y    
                         / /   r e s u l t s   o n   t h e   p a g e   a l l o w i n g   s e l e c t i o n   b y   t h e   u s e r .  
                         f u n c t i o n   r u n S u g g e s t ( e )   {  
                                 i f   ( t h i s . v a l u e . l e n g t h   <   s e t t i n g s . m i n C h a r a c t e r s )   {  
                                         $ ( r e s u l t s ) . h t m l ( ' ' ) . h i d e ( ) ;  
                                         r e t u r n   f a l s e ;  
                                 }  
  
                                 v a r   r e s u l t O b j e c t s   =   [ ] ,  
 	 	 	 	 	 s F i l t e r T x t   =   ( ! s e t t i n g s . w i l d C a r d )   ?   r e g e x E s c a p e ( t h i s . v a l u e )   :   r e g e x E s c a p e ( t h i s . v a l u e ,   s e t t i n g s . w i l d C a r d ) . r e p l a c e ( w i l d C a r d P a t t ,   ' . * ' ) ,  
 	 	 	 	 	 b M a t c h   =   t r u e ,  
 	 	 	 	 	 f i l t e r P a t t ,   i ;  
  
                                 i f   ( s e t t i n g s . n o t C h a r a c t e r   & &   s F i l t e r T x t . i n d e x O f ( s e t t i n g s . n o t C h a r a c t e r )   = = =   0 )   {  
                                         s F i l t e r T x t   =   s F i l t e r T x t . s u b s t r ( s e t t i n g s . n o t C h a r a c t e r . l e n g t h ,   s F i l t e r T x t . l e n g t h ) ;  
                                         i f   ( s F i l t e r T x t . l e n g t h   >   0 )   {   b M a t c h   =   f a l s e ;   }  
                                 }  
                                 s F i l t e r T x t   =   s F i l t e r T x t   | |   ' . * ' ;  
                                 s F i l t e r T x t   =   s e t t i n g s . w i l d C a r d   ?   ' ^ '   +   s F i l t e r T x t   :   s F i l t e r T x t ;  
                                 f i l t e r P a t t   =   s e t t i n g s . c a s e S e n s i t i v e   ?   n e w   R e g E x p ( s F i l t e r T x t )   :   n e w   R e g E x p ( s F i l t e r T x t ,   " i " ) ;  
  
                                 / /   G e t   t h e   r e s u l t s   f r o m   t h e   c o r r e c t   p l a c e .   I f   s e t t i n g s . a j a x R e s u l t s   t h e n   r e s u l t s   a r e   r e t r i e v e d   f r o m  
                                 / /   a n   e x t e r n a l   f u n c t i o n   e a c h   t i m e   t h e y   a r e   n e e d e d   e l s e   t h e y   a r e   r e t r i e v e d   f r o m   t h e   d a t a  
                                 / /   g i v e n   o n   c o n t r u c t i o n .  
                                 i f   ( s e t t i n g s . a j a x R e s u l t s   = = =   t r u e )   {  
                                         r e s u l t O b j e c t s   =   s e a r c h D a t a ( t h i s . v a l u e ,   s e t t i n g s . w i l d C a r d ,  
 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 s e t t i n g s . c a s e S e n s i t i v e ,  
 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 s e t t i n g s . n o t C h a r a c t e r ) ;  
  
                                         i f   ( t y p e o f   r e s u l t O b j e c t s   = = =   ' s t r i n g ' )   {  
                                                 r e s u l t O b j e c t s   =   J S O N . p a r s e ( r e s u l t O b j e c t s ) ;  
                                         }  
                                 }  
                                 e l s e   {  
                                         / /   L o o k   f o r   t h e   r e q u i r e d   m a t c h   a g a i n s t   e a c h   s i n g l e   s e a r c h   d a t a   i t e m .   W h e n   t h e   n o t  
                                         / /   c h a r a c t e r   i s   u s e d   w e   a r e   l o o k i n g   f o r   a   f a l s e   m a t c h .  
                                         i f   ( s e a r c h D a t a   ! =   n u l l )   {  
                                                 f o r   ( i   =   0 ;   i   <   s e a r c h D a t a . l e n g t h ;   i   + =   1 )   {  
                                                         i f   ( f i l t e r P a t t . t e s t ( s e a r c h D a t a [ i ] . t e x t )   = = =   b M a t c h )   {  
                                                                 r e s u l t O b j e c t s . p u s h ( s e a r c h D a t a [ i ] ) ;  
                                                         }  
                                                 }  
                                         }  
                                 }  
  
                                 b u i l d R e s u l t s ( r e s u l t O b j e c t s ,   s F i l t e r T x t ) ;  
                         }  
  
                         / /   T o   c a l l   s p e c i f i c   a c t i o n s   b a s e d   o n   t h e   k e y s   p r e s s e d   i n   t h e   i n p u t  
                         / /   b o x .   S p e c i a l   k e y s   a r e   u p ,   d o w n   a n d   r e t u r n .   A l l   o t h e r   k e y s  
                         / /   a c t   a s   n o r m a l .  
  
                         f u n c t i o n   k e y L i s t e n e r ( e )   {  
                                 s w i t c h   ( e . k e y C o d e )   {  
                                         c a s e   1 3 :   / /   r e t u r n   k e y  
                                                 $ ( c u r r e n t S e l e c t i o n ) . t r i g g e r ( ' c l i c k ' ) ;  
  
                                                 r e t u r n   f a l s e ;  
                                         c a s e   4 0 :   / /   d o w n   k e y  
                                                 i f   ( t y p e o f   c u r r e n t S e l e c t i o n   = = =   ' u n d e f i n e d ' )   {  
                                                         c u r r e n t S e l e c t i o n   =   $ ( ' d i v . r e s u l t I t e m : f i r s t ' ,   r e s u l t s ) . g e t ( 0 ) ;  
                                                 }  
                                                 e l s e   {  
                                                         c u r r e n t S e l e c t i o n   =   $ ( c u r r e n t S e l e c t i o n ) . n e x t ( ) . g e t ( 0 ) ;  
                                                 }  
  
                                                 s e t H o v e r C l a s s ( c u r r e n t S e l e c t i o n ) ;  
                                                 i f   ( c u r r e n t S e l e c t i o n )   {  
                                                         $ ( r e s u l t s ) . s c r o l l T o p ( c u r r e n t S e l e c t i o n . o f f s e t T o p ) ;  
                                                 }  
  
                                                 r e t u r n   f a l s e ;  
                                         c a s e   3 8 :   / /   u p   k e y  
                                                 i f   ( t y p e o f   c u r r e n t S e l e c t i o n   = = =   ' u n d e f i n e d ' )   {  
                                                         c u r r e n t S e l e c t i o n   =   $ ( ' d i v . r e s u l t I t e m : l a s t ' ,   r e s u l t s ) . g e t ( 0 ) ;  
                                                 }  
                                                 e l s e   {  
                                                         c u r r e n t S e l e c t i o n   =   $ ( c u r r e n t S e l e c t i o n ) . p r e v ( ) . g e t ( 0 ) ;  
                                                 }  
  
                                                 s e t H o v e r C l a s s ( c u r r e n t S e l e c t i o n ) ;  
                                                 i f   ( c u r r e n t S e l e c t i o n )   {  
                                                         $ ( r e s u l t s ) . s c r o l l T o p ( c u r r e n t S e l e c t i o n . o f f s e t T o p ) ;  
                                                 }  
  
                                                 r e t u r n   f a l s e ;  
                                         d e f a u l t :  
                                                 r u n S u g g e s t . a p p l y ( t h i s ,   [ e ] ) ;  
  
                                 }  
                         }  
  
                         / /   P r e p a r e   t h e   i n p u t   b o x   t o   s h o w   s u g g e s t   r e s u l t s   b y   a d d i n g   i n   t h e   e v e n t s  
                         / /   t h a t   w i l l   i n i t i a t e   t h e   s e a r c h   a n d   p l a c i n g   t h e   e l e m e n t   o n   t h e   p a g e  
                         / /   t h a t   w i l l   s h o w   t h e   r e s u l t s .  
                         / /   x ó a   h ¿t   c á c   k ¿t   q u £  c i 
                         $ ( ' d i v . j s o n S u g g e s t R e s u l t s ' ) . r e m o v e ( ) ;  
                         $ ( r e s u l t s ) . a d d C l a s s ( ' j s o n S u g g e s t R e s u l t s ' ) .  
 	 	 	 	 c s s ( {  
 	 	 	 	         ' t o p ' :   ( o b j . p o s i t i o n ( ) . t o p   +   o b j . h e i g h t ( )   +   5 )   +   ' p x ' ,  
 	 	 	 	         ' l e f t ' :   ( o b j . p o s i t i o n ( ) . l e f t   -   5 )   +   ' p x ' ,  
 	 	 	 	         ' w i d t h ' :   ' 3 4 2 p x ' / / s e t t i n g s . w i d t h   | |   ( ( o b j . w i d t h ( ) )   +   ' p x ' )  
 	 	 	 	 } ) . h i d e ( ) ;  
  
                         o b j . a f t e r ( r e s u l t s ) .  
 	 	 	 	 k e y u p ( k e y L i s t e n e r ) .  
 	 	 	 	 b l u r ( f u n c t i o n ( e )   {  
 	 	 	 	         / /   W e   n e e d   t o   m a k e   s u r e   w e   d o n ' t   h i d e   t h e   r e s u l t   s e t  
 	 	 	 	         / /   i f   t h e   i n p u t   b l u r   e v e n t   i s   c a l l e d   b e c a u s e   o f   c l i c k i n g   o n  
 	 	 	 	         / /   a   r e s u l t   i t e m .  
 	 	 	 	         v a r   r e s P o s   =   $ ( r e s u l t s ) . o f f s e t ( ) ;  
 	 	 	 	         r e s P o s . b o t t o m   =   r e s P o s . t o p   +   $ ( r e s u l t s ) . h e i g h t ( ) ;  
 	 	 	 	         r e s P o s . r i g h t   =   r e s P o s . l e f t   +   $ ( r e s u l t s ) . w i d t h ( ) ;  
  
 	 	 	 	         i f   ( p a g e Y   <   r e s P o s . t o p   | |   p a g e Y   >   r e s P o s . b o t t o m   | |   p a g e X   <   r e s P o s . l e f t   | |   p a g e X   >   r e s P o s . r i g h t )   {  
 	 	 	 	                 $ ( r e s u l t s ) . h i d e ( ) ;  
 	 	 	 	         }  
 	 	 	 	 } ) .  
 	 	 	 	 f o c u s ( f u n c t i o n ( e )   {  
 	 	 	 	         $ ( r e s u l t s ) . c s s ( {  
 	 	 	 	                 ' t o p ' :   ( o b j . p o s i t i o n ( ) . t o p   +   o b j . h e i g h t ( )   +   5 )   +   ' p x ' ,  
 	 	 	 	                 ' l e f t ' :   ( o b j . p o s i t i o n ( ) . l e f t   -   5 )   +   ' p x '  
 	 	 	 	         } ) ;  
  
 	 	 	 	         i f   ( $ ( ' d i v ' ,   r e s u l t s ) . l e n g t h   >   0 )   {  
 	 	 	 	                 $ ( r e s u l t s ) . s h o w ( ) ;  
 	 	 	 	         }  
 	 	 	 	 } ) .  
 	 	 	 	 a t t r ( ' a u t o c o m p l e t e ' ,   ' o f f ' ) ;  
                         $ ( ) . m o u s e m o v e ( f u n c t i o n ( e )   {  
                                 p a g e X   =   e . p a g e X ;  
                                 p a g e Y   =   e . p a g e Y ;  
                         } ) ;  
  
                         / /   O p e r a   d o e s n ' t   s e e m   t o   a s s i g n   a   k e y C o d e   f o r   t h e   d o w n  
                         / /   k e y   o n   t h e   k e y u p   e v e n t .   w h y ?  
                         i f   ( $ . b r o w s e r . o p e r a )   {  
                                 o b j . k e y d o w n ( f u n c t i o n ( e )   {  
                                         i f   ( e . k e y C o d e   = = =   4 0 )   {   / /   u p   k e y  
                                                 r e t u r n   k e y L i s t e n e r ( e ) ;  
                                         }  
                                 } ) ;  
                         }  
  
                         / /   E s c a p e   t h e   n o t   c h a r a c t e r   i f   p r e s e n t   s o   t h a t   i t   d o e s n ' t   a c t   i n   t h e   r e g u l a r   e x p r e s s i o n  
                         s e t t i n g s . n o t C h a r a c t e r   =   r e g e x E s c a p e ( s e t t i n g s . n o t C h a r a c t e r   | |   ' ' ) ;  
  
                         / /   W e   n e e d   t o   g e t   t h e   j a v a s c r i p t   a r r a y   t y p e   d a t a   f r o m   t h e   s e a r c h D a t a   s e t t i n g .  
                         / /   S e t t i n g   c a n   e i t h e r   b e   a   s t r i n g ,   a l r e a d y   a n   a r r a y   o r   a   f u n c t i o n   t h a t   r e t u r n s   o n e  
                         / /   o f   t h o s e   t h i n g s .   W e   o n l y   g e t   t h i s   d a t a   i f   i t   i s n ' t   b e i n g   p r o v i d e d   u s i n g   a j a x   o n  
                         / /   e a c h   s e a r c h  
                         i f   ( ! s e t t i n g s . a j a x R e s u l t s )   {  
                                 i f   ( t y p e o f   s e a r c h D a t a   = = =   ' f u n c t i o n ' )   {  
                                         s e a r c h D a t a   =   s e a r c h D a t a ( ) ;  
                                 }  
                                 i f   ( t y p e o f   s e a r c h D a t a   = = =   ' s t r i n g ' )   {  
                                         s e a r c h D a t a   =   J S O N . p a r s e ( s e a r c h D a t a ) ;  
                                 }  
                         }  
                 } ) ;  
         } ;  
  
 } ) ( j Q u e r y ) ; 
