/**
 * All common RockBand.com behaviors and functions, including all common
 * startup tasks, will be defined in this file,
 */

// Startup
$( function ( ) { 
	var windowResizeTimeout = null;
	
	//checkMainNavPosition( );
			
	// Disable dynamic nav repositioning for IE
	/*if (!$.browser.msie) {
		window.onresize = function() {
			window.clearTimeout(windowResizeTimeout);
			windowResizeTimeout = window.setTimeout(checkMainNavPosition, 100);		
		};
	} */

	$( document ).scroll( function ( ) { 
		smartPosition( $( '#sidebar-container.fixed' ) );
	} )

	if ( is_logged_into_facebook ) {
	
	}

	var e   = document.createElement( 'script' );
	e.src   = document.location.protocol + '//connect.facebook.net/en_US/all.js';
	e.async = true;
	document.getElementById( 'fb-root' ).appendChild( e );
} );


//------------------------------------------------------------------------------
// Functions
function checkMainNavPosition ( ) { 
	if ( ( $( 'ul#nav' ).height( ) + $( 'ul#nav' ).offset( ).top ) >= $( window ).height( ) ) { 
		$( 'ul#nav' ).css( {position: 'static'} );
	} else { 
		$( 'ul#nav' ).css( {position: 'fixed'} );
	}
}

function smartPosition ( obj ) {
	if ( $( obj ).length > 0 ) {
		if ( $( document ).scrollTop( ) >= $( obj ).offset( ).top ) {
			$( 'body' ).addClass( 'smartposition' );	
		} else {
			$( 'body' ).removeClass( 'smartposition' );
		}
	}
}

//------------------------------------------------------------------------------
// Behaviors
( function ( $ ) {

	$.fn.extend( {

		emptyOnFocus: function ( params ) {
			$( this ).each( function ( ) {  
				var defaults = {
					default_text: null
				};
				var config = $.extend( { }, defaults, params );

				if ( null == config.default_text )
					config.default_text = $( this ).val( );
				else
					$( this ).val( config.default_text );

				$( this ).focus( function ( e ) {
					if ( config.default_text == $( this ).val( ) )
						$( this ).val( '' );
				} ).blur( function ( ) {
					if ( !$( this ).val( ) || 0 == $( this ).val( ).length )
						$( this ).val( config.default_text );
				} );

				$( this ).bind( 'reset', function ( ) { 
					$( this ).val( config.default_text );
				} );
			} );

			return this;
		},
	
		readMore: function ( params ) {
		  // need to build this for CJ
		},		  

		actAsCloseable: function ( params ) {
			$( this ).each( function ( ) {  
				
				var defaults = {
					close_key: 'default_close_key'
				};
				var config  = $.extend( { }, defaults, params );
						
				var $container = $( this );
				config.close_key = $container.attr( 'rel' );

				$container.find( 'strong' )
					.addClass( 'pointer' )
					.click( function ( ) {
						$container.slideUp( function ( ) {
							if ( config.close_key ) {
								$.post( 
									'/users/' + config.username + '/hide/' + config.close_key	
								);
							}
						} );
				} );
			} );

			return this;
		},

		actAsFancyDropdown: function ( params ) {
			$( this ).each( function ( ) {  
				var defaults = {
					default_value: 'sample_select',
					class_name:    'pulldown',
					selected:      null
				};
				var config  = $.extend( { }, defaults, params );

				var $container = $( this );
				var $menu      = $( this ).find( 'ul' );
				$menu.hide( );

				var selected_text = $menu.find( 'li.current' ).text( );
				$container.prepend( '<h5><a class="pointer">' + selected_text + '</a></h5>' );
				var $header = $container.find( 'h5 a' );
				$header.click( function ( ) { 
					$( 'div.' + config.class_name + ' ul' ).hide( );
					$menu.show( );
					
				} );
				
				$( "body" ).click( function ( ) {
					$menu.hide( );
				} );

				$container.click( function ( event ) {
					event.stopPropagation( );
				} );

				$container.find( 'a' ).addClass( 'pointer' );

				$menu.find( 'a' ).click( function ( ) { 
					$header.html( $( this ).html( ) );
					$( this ).parent( 'li' ).siblings( ).removeClass( 'current' );
					$( this ).parent( 'li' ).addClass( 'current' );
					
					$menu.hide( );
					$( 'div.' + config.class_name + ' ul' ).hide( );
				} );

				// We sometimes want to be able to update the menu item
				// without user interaction, such as when certain URL variables
				// are passed in
				$menu.find( 'li' ).bind( 'update', function ( ) {
					$header.html( $( this ).find( 'a' ).html( ) );
					$( this ).parent( 'li' ).siblings( ).removeClass( 'current' );
					$( this ).parent( 'li' ).addClass( 'current' );
					
					$menu.hide( );
					$( 'div.' + config.class_name + ' ul' ).hide( );
				} );				
			} );

			return this;
		},

		fancySelect: function ( params ) { 
			$( this ).each( function ( ) {  
				var defaults = {
					field_name: 'sample_select',
					class_name: 'pulldown',
					callback:   null,
					selected:   null
				};
				var config = $.extend( { }, defaults, params );

				var selected_item;
				if ( config.selected ) {
					selected_item = $( this ).find( 'ul li#' + config.selected );
				} else {
					selected_item = $( this ).find( 'ul li:first-child' );
				}
				
				selected_item.addClass( 'selected' );
				$( this ).prepend( '<p id="active">' + selected_item.html( ) + '</p>' );

				var initial_value = selected_item.attr( 'rel' );
				$( this ).append( '<input type="hidden" name="' + config.field_name + '" value="' + initial_value + '">' );
					 
				$( this ).find( 'ul li' ).click( function ( ) { 
					$( this ).siblings( '.selected' ).removeClass( 'selected' );
					$( this ).addClass( 'selected' );

					$parent  = $( this ).parents( 'div.' + config.class_name );
					$parent.find( 'input[type=hidden]' ).val( $( this ).attr( 'rel' ) );
					$parent.find( 'p#active' ).html( $( this ).html( ) );

					$( this ).parent().hide();	

					if ( config.callback )
						config.callback( );
				} );
				
				$('p#active' ).click( function ( ) { 
					$( this ).parent().find('ul').show();							   
				} );
				
			} );

			return this;
		},

		actAsErrorable: function ( params ) {
			var defaults = {
				error_class: 'input-error',
				success_class: 'input-success'
			};
			var config = $.extend( { }, defaults, params );

			$( this ).addClass( 'errorable' );

			this.bind( 'error', function ( ) { 
				$( this ).removeClass( config.success_class ).addClass( config.error_class );
			} );

			this.bind( 'success', function ( ) { 
				$( this ).removeClass( config.error_class ).addClass( config.success_class );
			} );

			this.bind( 'reset', function ( ) { 
				$( this ).removeClass( config.success_class ).removeClass( config.error_class );
			} );

			return this;
		},

		/**
		 * I might move this somewhere else; it's not really reusable the way it
		 * is.  It's pretty activity-feed specific.  Which is fine, but that means
		 * it probably should live somewhere else.
		 */
		actAsShareable: function ( params ) { 
			$( this ).each( function ( ) {  
				var defaults = { 
					entry:              $( this ),
					id:                 $( this ).attr( 'id' ),
					like_link:          $( this ).find( 'a#like-link' ),
					like:               $( this ).find( 'a.like' ),
					liked_string:       $( this ).find( 'span#liked' ),
					likes_panel:        $( this ).find( 'p.likes' ),
					hide_likes:         $( this ).find( 'a.hide-likes' ),
					twitter:            $( this ).find( 'a.twitter' ),
					facebook:           $( this ).find( 'a.facebook' ),
					hide_entry:         $( this ).find( 'a.hide' ),
					hide_hide_button:   false,
					bg_dropout_color:   '#f00'
				};
				var config = $.extend( { }, defaults, params );

				if ( config.hide_hide_button ) { 
					config.hide_entry.remove( );
				}

				config.like.click( function ( ) { 
					if ( !config.like.hasClass( 'liked' ) ) { 
						$.get( config.like_url, null, function ( data ) { 
							config.liked_string.text( data + ( ( 1 == data ) ? ' person' : ' people' ) );
						} );
						config.like.addClass( 'liked' );	
					} else { 
						$.get( config.unlike_url, null, function ( data ) { 
							config.liked_string.text( data + ( ( 1 == data ) ? ' person' : ' people' ) );
						} );
						config.like.removeClass( 'liked' );	
					}
				} );

				config.twitter.click( function ( ) { 
					console.log( 'ajax post to twitter' );
				} );

				config.facebook.click( function ( ) { 
					console.log( 'ajax post to facebook' );
				} );

				config.like_link.click( function ( ) { 
					config.likes_panel.slideToggle( );    
					config.like_link.toggleClass( 'show' );
				} );

				config.hide_likes.click( function ( ) { 
					config.likes_panel.slideUp( );    
					config.like_link.removeClass( 'show' );
				} );

				config.hide_entry.click( function ( ) { 
					config.entry.animate( {backgroundColor: config.bg_dropout_color}, 'fast' );
					config.entry.fadeOut( ); 
				} );
			} );
						
			return this;
		},

		actAsToggleable: function ( params ) { 
			
			var defaults = {
				active_class:     'active',
				toggle_container: '.toggle_container',
				callback:         null
			};
			var config = $.extend( { }, defaults, params );

			$( this ).each( function ( ) { 
				$( this ).click( function ( ) {
					$activated      = $( this );
					$activated_pane = $activated.next( config.toggle_container );

					var has_clicked_on_active = $activated.hasClass( config.active_class ); 

					$deactivated      = $activated.parent( ).find( '.' + config.active_class );
					$deactivated_pane = $deactivated.next( config.toggle_container );
					$deactivated_pane.slideUp( 'slow', function ( ) {
						$deactivated.removeClass( config.active_class );
					} );

					if ( !has_clicked_on_active ) {
						$activated.addClass( config.active_class );
						$activated_pane.slideDown( 'slow', function ( ) {
							$.scrollTo( $activated, 350 );
						} );
					}

					if ( config.callback )
						config.callback( $( this ) );
				} );

				if ( !$( this ).hasClass( 'opened' ) ) {
					$( this ).next( config.toggle_container ).slideUp( 0 );
				}
			} );
						
			return this;
		},

		actAsModalWindowTrigger: function ( params ) { 
			$( this ).each( function ( ) { 
				var config = {target: $( '#' + $( this ).attr( 'href' ) )}

				// Create our close button
				var $modal_window = config.target.find( 'div.modal-window' );
				if ( 0 == $modal_window.find( 'a.close' ).length )
					config.target.find( 'div.modal-window' ).append( '<a class="close pointer">Close</a>' );

				config.target.find( 'a.close' ).click( function ( ) { 
					config.target.hide( );
					if ( 'undefined' != typeof( $jwplayer ) )
						$jwplayer().stop(); // stops video if neccessary
				} );

				$( this ).click( function ( ) { 
					config.target.show( );
					if ( 'undefined' != typeof( $jwplayer ) )
						$jwplayer().stop(); // stops video if neccessary
					return false;
				} );	

				config.target.click( function ( e ) { 
					if ( $( e.target ).hasClass( 'modal-window-container' ) )
						config.target.hide( );
						if ( 'undefined' != typeof( $jwplayer ) )
							$jwplayer().stop(); // stops video if neccessary
				} );

			} );
						  
			return this;
		},

		actAsHorizontalAutoScroller: function ( params ) {
			$( this ).each( function ( ) {
				var $this      = $( this );
				var $ul        = $( this ).find( 'ul' );
				var ul_padding = 15;
				var div_width  = $this.width( );
				var last_li    = $ul.find( 'li:last-child' );

				$this.css( {overflow: 'hidden'} );
				$this.mousemove( function ( e ) {
					var ul_width = last_li[0].offsetLeft + last_li.outerWidth( ) + ul_padding;
					var left     = ( e.pageX - $this.offset( ).left ) * ( ul_width - div_width ) / div_width;
					$this.scrollLeft( left );
				} );
			} );

			return this;
		},

		actAsVerticalAutoScroller: function ( params ) {
			$( this ).each( function ( ) {
				var $this      = $( this );
				var $ul        = $( this ).find( 'ul' );
				var ul_padding = 15;
				var div_height = $this.height( );
				var last_li    = $ul.find( 'li:last-child' );

				$this.css( {overflow: 'hidden'} );
				$this.mousemove( function ( e ) {
					var ul_height = last_li[0].offsetTop + last_li.outerHeight( ) + ul_padding;
					var top       = ( e.pageY - $this.offset( ).top ) * ( ul_height - div_height ) / div_height;
					$this.scrollTop( top );
				} );
			} );

			return this;
		},

		actAsCommentable: function ( params ) { 
						
			return this;
		}

	} );

} )( jQuery );

