ÿþ/ *  
 *   j Q u e r y   h i s t o r y   p l u g i n  
 *  
 *   C o p y r i g h t   ( c )   2 0 0 6   T a k u   S a n o   ( M i k a g e   S a w a t a r i )  
 *   L i c e n s e d   u n d e r   t h e   M I T   L i c e n s e :  
 *       h t t p : / / w w w . o p e n s o u r c e . o r g / l i c e n s e s / m i t - l i c e n s e . p h p  
 *  
 *   M o d i f i e d   b y   L i n c o l n   C o o p e r   t o   a d d   S a f a r i   s u p p o r t   a n d   o n l y   c a l l   t h e   c a l l b a c k   o n c e   d u r i n g   i n i t i a l i z a t i o n  
 *   f o r   m s i e   w h e n   n o   i n i t i a l   h a s h   s u p p l i e d .  
 *   A P I   r e w r i t e   b y   L a u r i s   B u k ýÿi s - H a b e r k o r n s  
 * /  
  
 ( f u n c t i o n ( $ )   {  
  
         f u n c t i o n   H i s t o r y ( )   {  
                 t h i s . _ c u r H a s h   =   ' ' ;  
                 t h i s . _ c a l l b a c k   =   f u n c t i o n ( h a s h )   {   } ;  
         } ;  
  
         $ . e x t e n d ( H i s t o r y . p r o t o t y p e ,   {  
  
                 i n i t :   f u n c t i o n ( c a l l b a c k )   {  
                         t h i s . _ c a l l b a c k   =   c a l l b a c k ;  
                         t h i s . _ c u r H a s h   =   l o c a t i o n . h a s h ;  
  
                         i f   ( $ . b r o w s e r . m s i e )   {  
                                 / /   T o   s t o p   t h e   c a l l b a c k   f i r i n g   t w i c e   d u r i n g   i n i t i l i z a t i o n   i f   n o   h a s h   p r e s e n t  
                                 i f   ( t h i s . _ c u r H a s h   = =   ' ' )   {  
                                         t h i s . _ c u r H a s h   =   ' # ' ;  
                                 }  
  
                                 / /   a d d   h i d d e n   i f r a m e   f o r   I E  
                                 $ ( " b o d y " ) . p r e p e n d ( ' < i f r a m e   i d = " j Q u e r y _ h i s t o r y "   s t y l e = " d i s p l a y :   n o n e ; " > < / i f r a m e > ' ) ;  
                                 v a r   i f r a m e   =   $ ( " # j Q u e r y _ h i s t o r y " ) [ 0 ] . c o n t e n t W i n d o w . d o c u m e n t ;  
                                 i f r a m e . o p e n ( ) ;  
                                 i f r a m e . c l o s e ( ) ;  
                                 i f r a m e . l o c a t i o n . h a s h   =   t h i s . _ c u r H a s h ;  
                         }  
                         e l s e   i f   ( $ . b r o w s e r . s a f a r i )   {  
                                 / /   e t a b l i s h   b a c k / f o r w a r d   s t a c k s  
                                 t h i s . _ h i s t o r y B a c k S t a c k   =   [ ] ;  
                                 t h i s . _ h i s t o r y B a c k S t a c k . l e n g t h   =   h i s t o r y . l e n g t h ;  
                                 t h i s . _ h i s t o r y F o r w a r d S t a c k   =   [ ] ;  
                                 t h i s . _ i s F i r s t   =   t r u e ;  
                                 t h i s . _ d o n t C h e c k   =   f a l s e ;  
                         }  
                         t h i s . _ c a l l b a c k ( t h i s . _ c u r H a s h . r e p l a c e ( / ^ # / ,   ' ' ) ) ;  
                         s e t I n t e r v a l ( t h i s . _ c h e c k ,   1 0 0 ) ;  
                 } ,  
  
                 a d d :   f u n c t i o n ( h a s h )   {  
                         / /   T h i s   m a k e s   t h e   l o o p i n g   f u n c t i o n   d o   s o m e t h i n g  
                         t h i s . _ h i s t o r y B a c k S t a c k . p u s h ( h a s h ) ;  
  
                         t h i s . _ h i s t o r y F o r w a r d S t a c k . l e n g t h   =   0 ;   / /   c l e a r   f o r w a r d S t a c k   ( t r u e   c l i c k   o c c u r e d )  
                         t h i s . _ i s F i r s t   =   t r u e ;  
                 } ,  
  
                 _ c h e c k :   f u n c t i o n ( )   {  
                         i f   ( $ . b r o w s e r . m s i e )   {  
                                 / /   O n   I E ,   c h e c k   f o r   l o c a t i o n . h a s h   o f   i f r a m e  
                                 v a r   i h i s t o r y   =   $ ( " # j Q u e r y _ h i s t o r y " ) [ 0 ] ;  
                                 v a r   i f r a m e   =   i h i s t o r y . c o n t e n t D o c u m e n t   | |   i h i s t o r y . c o n t e n t W i n d o w . d o c u m e n t ;  
                                 v a r   c u r r e n t _ h a s h   =   i f r a m e . l o c a t i o n . h a s h ;  
                                 i f   ( c u r r e n t _ h a s h   ! =   $ . h i s t o r y . _ c u r H a s h )   {  
  
                                         l o c a t i o n . h a s h   =   c u r r e n t _ h a s h ;  
                                         $ . h i s t o r y . _ c u r H a s h   =   c u r r e n t _ h a s h ;  
                                         $ . h i s t o r y . _ c a l l b a c k ( c u r r e n t _ h a s h . r e p l a c e ( / ^ # / ,   ' ' ) ) ;  
  
                                 }  
                         }   e l s e   i f   ( $ . b r o w s e r . s a f a r i )   {  
                                 i f   ( ! $ . h i s t o r y . _ d o n t C h e c k )   {  
                                         v a r   h i s t o r y D e l t a   =   h i s t o r y . l e n g t h   -   $ . h i s t o r y . _ h i s t o r y B a c k S t a c k . l e n g t h ;  
  
                                         i f   ( h i s t o r y D e l t a )   {   / /   b a c k   o r   f o r w a r d   b u t t o n   h a s   b e e n   p u s h e d  
                                                 $ . h i s t o r y . _ i s F i r s t   =   f a l s e ;  
                                                 i f   ( h i s t o r y D e l t a   <   0 )   {   / /   b a c k   b u t t o n   h a s   b e e n   p u s h e d  
                                                         / /   m o v e   i t e m s   t o   f o r w a r d   s t a c k  
                                                         f o r   ( v a r   i   =   0 ;   i   <   M a t h . a b s ( h i s t o r y D e l t a ) ;   i + + )   $ . h i s t o r y . _ h i s t o r y F o r w a r d S t a c k . u n s h i f t ( $ . h i s t o r y . _ h i s t o r y B a c k S t a c k . p o p ( ) ) ;  
                                                 }   e l s e   {   / /   f o r w a r d   b u t t o n   h a s   b e e n   p u s h e d  
                                                         / /   m o v e   i t e m s   t o   b a c k   s t a c k  
                                                         f o r   ( v a r   i   =   0 ;   i   <   h i s t o r y D e l t a ;   i + + )   $ . h i s t o r y . _ h i s t o r y B a c k S t a c k . p u s h ( $ . h i s t o r y . _ h i s t o r y F o r w a r d S t a c k . s h i f t ( ) ) ;  
                                                 }  
                                                 v a r   c a c h e d H a s h   =   $ . h i s t o r y . _ h i s t o r y B a c k S t a c k [ $ . h i s t o r y . _ h i s t o r y B a c k S t a c k . l e n g t h   -   1 ] ;  
                                                 i f   ( c a c h e d H a s h   ! =   u n d e f i n e d )   {  
                                                         $ . h i s t o r y . _ c u r H a s h   =   l o c a t i o n . h a s h ;  
                                                         $ . h i s t o r y . _ c a l l b a c k ( c a c h e d H a s h ) ;  
                                                 }  
                                         }   e l s e   i f   ( $ . h i s t o r y . _ h i s t o r y B a c k S t a c k [ $ . h i s t o r y . _ h i s t o r y B a c k S t a c k . l e n g t h   -   1 ]   = =   u n d e f i n e d   & &   ! $ . h i s t o r y . _ i s F i r s t )   {  
                                                 / /   b a c k   b u t t o n   h a s   b e e n   p u s h e d   t o   b e g i n n i n g   a n d   U R L   a l r e a d y   p o i n t e d   t o   h a s h   ( e . g .   a   b o o k m a r k )  
                                                 / /   d o c u m e n t . U R L   d o e s n ' t   c h a n g e   i n   S a f a r i  
                                                 i f   ( d o c u m e n t . U R L . i n d e x O f ( ' # ' )   > =   0 )   {  
                                                         $ . h i s t o r y . _ c a l l b a c k ( d o c u m e n t . U R L . s p l i t ( ' # ' ) [ 1 ] ) ;  
                                                 }   e l s e   {  
                                                         $ . h i s t o r y . _ c a l l b a c k ( ' ' ) ;  
                                                 }  
                                                 $ . h i s t o r y . _ i s F i r s t   =   t r u e ;  
                                         }  
                                 }  
                         }   e l s e   {  
                                 / /   o t h e r w i s e ,   c h e c k   f o r   l o c a t i o n . h a s h  
                                 v a r   c u r r e n t _ h a s h   =   l o c a t i o n . h a s h ;  
                                 i f   ( c u r r e n t _ h a s h   ! =   $ . h i s t o r y . _ c u r H a s h )   {  
                                         $ . h i s t o r y . _ c u r H a s h   =   c u r r e n t _ h a s h ;  
                                         $ . h i s t o r y . _ c a l l b a c k ( c u r r e n t _ h a s h . r e p l a c e ( / ^ # / ,   ' ' ) ) ;  
                                 }  
                         }  
                 } ,  
  
                 l o a d :   f u n c t i o n ( h a s h )   {  
                         v a r   n e w h a s h ;  
  
                         i f   ( $ . b r o w s e r . s a f a r i )   {  
                                 n e w h a s h   =   h a s h ;  
                         }   e l s e   {  
                                 n e w h a s h   =   ' # '   +   h a s h ;  
                                 l o c a t i o n . h a s h   =   n e w h a s h ;  
                         }  
                         t h i s . _ c u r H a s h   =   n e w h a s h ;  
  
                         i f   ( $ . b r o w s e r . m s i e )   {  
                                 v a r   i h i s t o r y   =   $ ( " # j Q u e r y _ h i s t o r y " ) [ 0 ] ;   / /   T O D O :   n e e d   c o n t e n t D o c u m e n t ?  
                                 v a r   i f r a m e   =   i h i s t o r y . c o n t e n t W i n d o w . d o c u m e n t ;  
                                 i f r a m e . o p e n ( ) ;  
                                 i f r a m e . c l o s e ( ) ;  
                                 i f r a m e . l o c a t i o n . h a s h   =   n e w h a s h ;  
                                 t h i s . _ c a l l b a c k ( h a s h ) ;  
                         }  
                         e l s e   i f   ( $ . b r o w s e r . s a f a r i )   {  
                                 t h i s . _ d o n t C h e c k   =   t r u e ;  
                                 / /   M a n u a l l y   k e e p   t r a c k   o f   t h e   h i s t o r y   v a l u e s   f o r   S a f a r i  
                                 t h i s . a d d ( h a s h ) ;  
  
                                 / /   W a i t   a   w h i l e   b e f o r e   a l l o w i n g   c h e c k i n g   s o   t h a t   S a f a r i   h a s   t i m e   t o   u p d a t e   t h e   " h i s t o r y "   o b j e c t  
                                 / /   c o r r e c t l y   ( o t h e r w i s e   t h e   c h e c k   l o o p   w o u l d   d e t e c t   a   f a l s e   c h a n g e   i n   h a s h ) .  
                                 v a r   f n   =   f u n c t i o n ( )   {   $ . h i s t o r y . _ d o n t C h e c k   =   f a l s e ;   } ;  
                                 w i n d o w . s e t T i m e o u t ( f n ,   2 0 0 ) ;  
                                 t h i s . _ c a l l b a c k ( h a s h ) ;  
                                 / /   N . B .   " l o c a t i o n . h a s h = "   m u s t   b e   t h e   l a s t   l i n e   o f   c o d e   f o r   S a f a r i   a s   e x e c u t i o n   s t o p s   a f t e r w a r d s .  
                                 / /             B y   e x p l i c i t l y   u s i n g   t h e   " l o c a t i o n . h a s h "   c o m m a n d   ( i n s t e a d   o f   u s i n g   a   v a r i a b l e   s e t   t o   " l o c a t i o n . h a s h " )   t h e  
                                 / /             U R L   i n   t h e   b r o w s e r   a n d   t h e   " h i s t o r y "   o b j e c t   a r e   b o t h   u p d a t e d   c o r r e c t l y .  
                                 l o c a t i o n . h a s h   =   n e w h a s h ;  
                         }  
                         e l s e   {  
                                 t h i s . _ c a l l b a c k ( h a s h ) ;  
                         }  
                 }  
         } ) ;  
  
         $ ( d o c u m e n t ) . r e a d y ( f u n c t i o n ( )   {  
                 $ . h i s t o r y   =   n e w   H i s t o r y ( ) ;   / /   s i n g l e t o n   i n s t a n c e  
         } ) ;  
  
 } ) ( j Q u e r y ) ;  
 
