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