(function($){
  $(document).ready(function(){
    function filter_menu() {
      $( 'a.new.modal' ).filter( function() {
        // Update the filters dropdown list
        $( '#filter-menu' ).load( $( '#filter-control' ).attr( 'href' ), function() {
          filter_menu();
        } );
      });

      $( '#filter-control' ).menu( '#filter-menu', { voffset: 4, hoffset: 0 } );
      
      $( 'a.toggle' ).click( function() {
        // Toggle on status to give feedback to user
        $( 'a.toggle' ).removeClass( 'on' )
        $( this ).addClass( 'on' );
        $( '#filter-control' ).addClass( 'filter-load' );
        $( '#filter-menu' ).hide();
        
        // Toggle the filter
        jQuery.get( $( this ).attr( 'href' ), function() {
          // Reload page
          document.location.reload();
        });
        return false;
      })
    }
    
    $( '#anchor' ).menu( '#menu-report-box' );
    
    $( '#reset-results' ).dialog( '#reset_notice' );
    $( '#rss' ).dialog( '#rss_notice' );
    
    $( 'a.swap-resp' ).click( function() {
      $( '.resp_chart' ).hide();
      $( $( this ).attr( 'href' ) ).show();
      $( 'a.swap-resp' ).removeClass( 'on' );
      $( this ).addClass( 'on' );
      return false;
    });
    
    // Favourites on respondent report
    $( 'a.favourite.respondent' ).click( function() {
      var parent = $( this ).parents( 'div:first' );
      
      $( this ).parents( 'form' ).ajaxSubmit( function( response ) {
        if ( !PollDaddy.is_fatal( response ) ) {
          if ( parent.hasClass( 'star' ) )
            parent.removeClass( 'star' ).addClass( 'star-on' );
          else
            parent.removeClass( 'star-on' ).addClass( 'star' );
        }
      });
      return false;
    });
    
    // Favourite item on individual reports.
    $( 'a.favourite.individual' ).click( function() {
      var item = $( this );
      
      item.loading();
      $( this ).parents( 'form' ).ajaxSubmit( function( response ) {
        if ( !PollDaddy.is_fatal( response ) ) {
          item.loading();

          $( 'a.favourite' ).text( response );
          if ( $( 'a.favourite' ).hasClass( 'link-star-on' ) )
            $( 'a.favourite' ).removeClass( 'link-star-on' ).addClass( 'link-star' );
          else
            $( 'a.favourite' ).addClass( 'link-star-on' ).removeClass( 'link-star' );
        }
        else
          item.loading();
      });
      return false;
    });

    $( 'select[name=per_page]' ).change( function() {
      $( this ).parents( 'form' ).submit();
    });
    
    $( 'a.share' ).dialog( function( target, popup ) {
      $( target ).find( 'input[name=url]' ).auto_select();
      $( target ).find( 'a.generate-password' ).auto_password( 'input[name=password]' );
      
      // Hook up the delete buttons
      $( target ).find( 'a.link-delete' ).unbind( 'click' ).click( function() {
        var row = $( this );
        
        row.loading();
        row.parents( 'form' ).ajaxSubmit( function( response ) {
          if ( !PollDaddy.is_fatal( response ) ) {
            row.parents( 'tr' ).fadeOutRemove( function() {
              popup.resize();
            });
          }
        });
        
        return false;
      });
      
      // Hook up the publish button
      $( target ).find( 'a.publish' ).unbind( 'click' ).click( function() {
        var form = $( this ).parents( 'form' );
        
		    $( form ).find( '.editor-buttons' ).hide();
		    $( form ).find( '.dialog-loader' ).show();
		    
        form.ajaxSubmit( function( response ) {
          if ( !PollDaddy.is_fatal( response ) ) {
            popup.reload( response );
          }
        });
        
        return false;
      });
      
      // Save
      $( target ).find( 'form' ).ajaxForm( {
        beforeSubmit: function() {
  		    $( target ).find( '.editor-buttons' ).hide();
  		    $( target ).find( '.dialog-loader' ).show();
        },
        success: function( response ) {
          if ( !PollDaddy.is_fatal( response ) ) {
            popup.refresh( response );
          }
        }
      });
      
      $( target ).find( 'input.toggle-area' ).click( function() {
        if ( $( this ).attr( 'checked' ) )
          $( this ).parents( 'div.label-group' ).removeClass( 'label-group-linked' ).addClass( 'label-group-linked-on' );
        else
          $( this ).parents( 'div.label-group' ).removeClass( 'label-group-linked-on' ).addClass( 'label-group-linked' );
      });
    });
    
    
    $( '#incomplete' ).dialog( '#add_incomplete_dialog', { confirmed: function( popup ) {
      $.fn.colorbox.close();
      document.location.href = $( '#incomplete' ).attr( 'href' );
      return false;
    } } );
    
    $( 'input[name=email]' ).validate( 'email' );
    filter_menu();
  });
})( jQuery );

