function Mmf_Search( opts ) {

    var _type, _form;

    _init( opts );

    function _init( opts ) {
        if ( opts.type ) {
            _type = opts.type;
            eval( '_' + opts.type + 'SearchInit()' );
        }
    }

    function _sidebarSearchInit() {
        _form = $( 'div#sidebar form#search' );

        Mmf_Util.storeDefault( $( 'input#search_box', _form ) );
        _addListeners();
    }

    function _advancedSearchInit() {
        var text;
        _form = $( 'div#main form#search' );
        text = $( 'input#search_box', _form );

        if ( ! /\?q=.+/.test( window.location.href ) ) {
            text.val( 'Keyword Search' );
        } else {
            text.data('originalText', text.val());
        }
        Mmf_Util.storeDefault(text);
        _addListeners();
    }

    function _addListeners( unbind ) {
        if ( unbind ) {
            $( 'input[type=checkbox]', _form ).unbind( 'click', _checkboxToggle );
            _form.unbind( 'submit', _submit );
            $( 'select', _form ).unbind( 'change', _sortChange );
        } else {
            $( 'input[type=checkbox]', _form ).bind( 'click', _checkboxToggle );
            _form.bind( 'submit', _submit );
            $( 'select', _form ).bind( 'change', _sortChange );
        }
    }

    function _sortChange( event ) {
        //only submit if there is results
        if ( $( 'div#searchResults div.result' ).length > 0 ) {
            _form.submit();
        }
    }

    function _checkboxToggle( event ) {
        var target, parent;

        target = $( event.target );

        if ( target.attr( 'class' ) == 'parent' ) {     //If parent check or uncheck (depending on parent's checked status) all children
            $( 'input.child_of_' + target.attr( 'id' ) ).attr(
                'checked',
                target.attr( 'checked' ) );
        } else {    
            //If child, check parent if child is checked
            //otherwise only uncheck parent if all children are unchecked
            parent = $( 'input#' + target.attr( 'class' ).match( /child_of_(.*)/ )[ 1 ] );
            if ( target.attr( 'checked' ) ) {
                parent.attr( 'checked', target.attr( 'checked' ) );
            } 
        }
    }

    /*
        This function handles both the submit for the advanced search page and the basic search page.
        For basic search all that has to be done is if the text box has not been cleared than clear
        it and unbind events.  For advanced search, the same, and adding "?q=" to the action attribute of the form.  
    */
    function _submit( event ) {
        var target, text;

        target = $( event.target );     //This is the form
        text = $( 'input[type=text]', _form );

        if (!text.data('cleared')) {
            text.val(
                text.data('originalText') && text.val() != ''
                    ? text.data('originalText')
                    : ''
            );
        }

        _addListeners( true );

        if ( _type == 'advanced' ) {
            _form.attr( 'action',
                _form.attr( 'action' ) + '?q=' + text.val() );
        }

        target.submit();

    }

}
