(function($){
  $.fn.extend({
    filter: function( args ) {
      if ( $.isFunction( args ) )
        args = { finished: args };
      
      // Merge in the user-supplied arguments
      var opts = $.extend( {
        source: '.modal',
        target: '#filter',
        finished: function() {}
      }, args);
      
      return this.each( function() {
        $( this ).click( function() {
          function renumber() {
            var count = 1, text;

            $( opts.target ).find( '.response-filters .response-filter, .response-properties .response-property' ).each( function() {
              text = count;
              if ( count < 10 )
                text = '0' + count;

              // Renumber the filters
              $( this ).find( '.q-number .number' ).text( text );

              // Renumber the input boxes - this keeps our server side simpler
              $( this ).find( 'select,input[type=text]' ).each( function() {
                // Don't renumber properties
                if ( $( this ).attr( 'name' ).indexOf( 'property' ) === -1 )
                  $( this ).attr( 'name', $( this ).attr( 'name' ).replace( /\[\d*\]/, '[' + ( count - 1 ) + ']' ) );
              });

              count++;
            });

            // Ensure that property select types are unique - one type per filter
            var active = $( opts.target ).find( '.response-properties .response-property' );

            active.each( function() {
              var select = $( this ).find( 'select.property' );
              var selected = select.val();
      		    var type = $( this ).attr( 'class' ).match( /response-property-(.*)/ );

      		    type = type[1];

              // Reset to default
      		    select.html( $( '.dummy-property-types option' ).clone() );

              // Remove items that are already present
              active.each( function() {
        		    var subtype = $( this ).attr( 'class' ).match( /response-property-(.*)/ );

        		    subtype = subtype[1];
                if ( subtype != selected )
                  select.find( 'option[value=' + subtype + ']' ).remove();
              });

              // Reset current item back
              select.val( type );
            });

            // Disable/enable 'add property' button depending if there are any more properties to add
            if ( active.length == $( opts.target ).find( '.dummy-properties .response-property' ).length )
              $( opts.target ).find( 'a.add-property' ).hide();
            else
              $( opts.target ).find( 'a.add-property' ).show();

            // Hide/show logic
        		if ( $( opts.target ).find( '.response-filters div.response-filter' ).length > 1 )
        		  $( '#filter-logic' ).show();
            else
        		  $( '#filter-logic' ).hide();
          }

      		function resize() {
      			$( '#colorbox, #cboxWrapper, #cboxContent, #cboxLoadedContent' ).css( 'height', $( opts.target ).height() + 12 );
      			$( '#cboxLoadedContent' ).css( 'overflow', 'visible' );
      		}

          function init_edit() {
            // Renumber
      		  renumber();

            // Remove any existing handlers
            $( opts.target ).find( 'a.delete-filter-response' ).unbind( 'click' );
            $( opts.target ).find( 'select.question-type' ).unbind( 'change' );
            $( opts.target ).find( 'select.hidden' ).unbind( 'change' );

            // Logic stuff
            $( opts.target ).find( 'input.logic' ).click( function() {
              if ( $( this ).val() == 2 )
                $( 'div.custom' ).show();
              else
                $( 'div.custom' ).hide();
            });
            
        		// Delete a filter response
        		$( opts.target ).find( 'a.delete-filter-response' ).click( function() {
        		  $( this ).parents( 'div.response-filter' ).fadeOutRemove( function() {
        		    renumber();
        		  });
        		  return false;
        		});

        		// Delete a filter property
        		$( opts.target ).find( 'a.delete-filter-property' ).click( function() {
        		  $( this ).parents( 'div.response-property' ).fadeOutRemove( function() {
        		    renumber();
        		  });
        		  return false;
        		});

        		// Hidden choices
        		$( opts.target ).find( 'select.hidden' ).change( function() {
        		  var item  = $( this ).parents( 'div.response-filter' );
        		  var pos   = $( this ).val();

      		    item.find( 'tr.hidden' ).hide();
        		  if ( item.find( 'tr.hidden-' + pos ).length > 0 )
        		    item.find( 'tr.hidden-' + pos ).show();
        		});

        		// Change filter/property
        		$( opts.target ).find( 'select.property, select.filter' ).change( function() {
        		  var val = $( this ).val();
        		  var klass = $( this ).attr( 'class' );

        		  // Replace the question with a dummy one for the new question
        		  var insert = $( '.response-' + klass + '-' + val ).clone().hide();

        		  $( this ).parents( 'div.response-' + klass ).before( insert );

              // Set the select box to the one we've just chosen
              $( insert ).find( 'select.property, select.filter' ).val( val );
        		  $( insert ).show();

        		  // Remove the old item
              $( this ).parents( 'div.response-' + klass ).remove();

        		  init_edit();
        		});
          }
          
          function init() {
            $( opts.target ).find( 'a.done' ).click( function() { 
              opts.finished();
        			$.fn.colorbox.close();
        			return false;
        		} );

        		// Edit item
        		$( opts.target ).find( 'a.modal' ).colorbox( {
        			onComplete: function() {
                init();
            	  resize();
        			}
        		});

        		// Delete a filter
        		$( opts.target ).find( 'a.link-delete' ).click( function() {
        		  var item = $( this ).parents( 'li' );
        		  var row  = $( this ).parents( 'tr' );

              $( this ).loading();
        		  item.find( 'form' ).ajaxSubmit( function( response ) {
                if ( PollDaddy.is_fatal( response ) )
                  $( this ).loading();
                else {
                  row.fadeOutRemove( function() {
                    resize();
                  });
                }
        		  });
        		  
        		  return false;
        		});

        		// Submit a form
        		$( opts.target ).find( 'form' ).ajaxForm( {
        		  beforeSubmit: function() {
        		    $( opts.target ).find( '.editor-buttons' ).hide();
        		    $( opts.target ).find( '.dialog-loader' ).show();
        		  },
        		  success: function( response ) {
        		    if ( !PollDaddy.is_fatal( response ) ) {
        		      $( opts.target ).parent().html( response );
          		    init();
          		    resize();
          		  }
        		  }
        		});

        		// Add a filter response
        		$( opts.target ).find( 'a.add-filter' ).click( function() {
        		  // Copy first dummy response filter
        		  $( opts.target ).find( '.response-filters' ).append( $( '.dummy-filters div:first' ).clone() );

        		  init_edit();
        		  return false;
        		});

        		// Add a filter property
        		$( opts.target ).find( 'a.add-property' ).click( function() {
        		  // Copy first dummy response filter that isn't already active
        		  var active = [], next = false;
        		  var current = $( opts.target ).find( '.response-properties select.property' );

        		  // Go through each dummy property
        		  $( opts.target ).find( '.dummy-properties .response-property' ).each( function() {
        		    var item = $( this ).attr( 'class' ).match( /response-property-(.*)/ );
        		    var found = false;

        		    item = item[1];

        		    // Has this property been defined already?
        		    if ( current.length > 0 ) {
            		  current.each( function() {
            		    if ( $( this ).val() == item ) {
            		      found = true;
            		      return false;
            		    }
            		  });

            		  // If not found then use it
            		  if ( found === false )
            		    next = item;
            		}
            		else
            		  next = item;

          		  if ( next !== false )
          		    return false;
        		  });

              // Copy next dummy select
              if ( next ) {
                var insert = $( '.dummy-properties .response-property-' + next ).clone().hide();

        		    $( opts.target ).find( '.response-properties' ).append( insert );

        		    // Change select box
        		    $( insert ).find( 'select.property' ).val( next );
        		    insert.show();
        		  }

        		  init_edit();
        		  return false;
        		});

        		init_edit();
          }

          // Get rid of filter menu
          $( '#filter-menu' ).slideUp();
          $( 'body' ).unbind( 'click' );

      		$.fn.colorbox( {
      		  open: true, initialHeight: 200,
            opacity: PollDaddy.opacity,
            href: $( this ).attr( 'href' ),
      			scrolling: false,
      			speed: 0, close: '',
      			onComplete: function() {
              init();
          	  resize();
      			}
      		} );
      		
      		return false;
        });
      });
    }
  });
})( jQuery );
