function TrigweeConversation() {
    var me = this;

    me.setActions = function( container ) {
        var elements;
        if( container ) elements = container;
        else elements = $('.paging li .message');

        $(elements).each( function() {
            me.setDeleteEvent( $(this) );

            me.setCommentEvents( $(this) );

            me.setLikeEvent( $(this) );
                    
            me.setLikeBarEvent( $(this) );

            me.setPlayEvent( $(this) );

            me.setAllCommentEvent( $(this) );

            // do lots of events setting
        } ); 
    }

    me.setPlayEvent = function( element ) {

        if( $(element).find('[embedUrl]').length ) {

            $(element).find('[embedUrl], span.message-attachment-play').click( function() {
                var ytMessageId = "ytapiplayer-"+$(element).attr('id');
                
    
                $(element).find('.message-attachment').hide();
                $(element).find('.message-body').append( $('<div id="'+ytMessageId+'"></div>') );
 
                var params = { allowScriptAccess: "always" };
                var atts = { id: "myytplayer" };

                swfobject.embedSWF( $(this).attr('embedUrl')+'&autoplay=1', 
                               ytMessageId, "325", "273", "8", null, null, params, atts);

                return false;
            } ).hover( function() { $(this).css( { cursor:'hand', cursor:'pointer' } ) } );
        }

    }

    me.setCommentEvents = function( element ) {

        var form = $(element).find( '.message-add-comment-bar form' );

        $(form).find('textarea').autogrow();
        
        $(form).find('input[type=text]').focus( function() {
            $(this).parents('div.text:first').hide();
            $(form).find('div.textarea').show().find('textarea').focus();
        } );

        $(form).find('textarea')
            .focus( function() { 
                $(this).parents('form').find('button').show(); 
            } )
            .blur( function() {
                if( $(this).val().length == 0 ) {
                    $(this).parents('div.textarea:first').hide();
                    $(this).parents('form').find('div.text').show();
                    $(this).parents('form').find('button').hide();
                }
            } );

        
        $(element).find('.comment-handler').click( function() {

            $(form).find('div.text:first').hide();
            $(form).find('div.textarea').show().find('textarea').focus();
            return false;
            
        } );


        $(form).ajaxForm( {
            dataType: 'json',
            beforeSubmit: function() {

                $(form)
                    .find('.normal-button')
                    .addClass('normal-button-inactive');

            },
            success: function(response, state) {
                if( response.success ) {
                    $('#message-'+response.parent_fk+' .message-child-rs').append( response.html );
                    me.setDeleteEvent(
                        $('div#message-'+response.parent_fk+' .message-child-rs .message-comment:last')
                    );

                    $(form).find('button').hide();
                    $(form).find('div.textarea:first').hide();
                    $(form).find('div.text:first').show();
                }
                
                $(form)
                    .find('.normal-button')
                    .removeClass('normal-button-inactive');
            }
        } );

    }

    me.setLikeEvent = function( element ) {

        $(element).find('.like-handler').click( function() {
            $.post( $(this).attr('action'), undefined, function(response, state) {
                if( response.success ) {
                    var likeBar = $(element).find('.message-like-bar:first');
                    likeBar.remove();
                    $(element).find('.message-children').prepend( response.html );

                    me.setLikeBarEvent( element );
                }
            }, 'json' );
            return false;
        } );        

    }

    me.setLikeBarEvent = function( element ) {
        $(element).find('.message-like-bar a').click( function() {

            trigweeDialog
                .show()
                .showLoader()
                .loadContent( $(this).attr('href') );

            return false;

        } );
    }
            
    me.setAllCommentEvent = function( element ) {
        $(element).find('.message-reply-bar a').click( function() {

            $(element).find('.message-child-rs').load( $(this).attr('href'), function() {
                $(element).find('.message-reply-bar').hide();

                me.setDeleteEvent( element );

            } );

            return false;
        } );
    }
            
    me.setDeleteEvent = function( element ) {
        $(element).find('.delete-handler').click( function() {

            trigweeDialog
                .show()
                .showLoader()
                .loadContent( $(this).attr('action') );

            return false;
        } );
    }

    me.addAttachmentEvents = function() {
        if( !('#add-conversation').length ) return;

        $('#add-conversation #remove_this').remove();

        $('#add-conversation input[default_value]').each( function() {
            new focus_blur( $(this) );
        } );       
 
        $('#attachment-block .attachment-block-close').click( function() {
            $('#add-conversation #attachment-block').hide();
            
            $('#add-conversation .attachment-ajax-content').remove();

            $('#add-conversation #attachment-block #attachment-article-block').hide();   
            $('#add-conversation #attachment-block #attachment-book-block').hide();   
            $('#add-conversation #attachment-block #attachment-video-block').hide();
        } );

        $('#attach-handlers a').click( function() {

            $('#add-conversation #attachment-block').show();   
            $('#add-conversation #attachment-block #attachment-article-block').hide();   
            $('#add-conversation #attachment-block #attachment-book-block').hide();   
            $('#add-conversation #attachment-block #attachment-video-block').hide();

            $('#add-conversation #attachment-block #attachment-'+$(this).attr('handles')+'-block').show();
            
            $('#add-conversation .attachment-ajax-content').remove();
            $('#add-conversation #attachment-block #attachment-'+$(this).attr('handles')+'-block .attachment-block-content').show();

            return false;
        } );

        $('#attachment-block div.button input[type=button]').click( function() {

            var source = $(this).parents('.attachment-block-content').find('input[type=text][name=source]').val();
            var content = $(this).parents('.attachment-block-content');
            var loader = content.siblings('.attachment-block-loader');
            var type = $(this).attr('source_type');

            var params = {
                source: source,
                type: type
            };
            if( type == 'book' ) {
                params['title'] = $(this).parents('.attachment-block-content').find('input[type=text][name=title]').val()
            }
                
            content.hide();
            loader.show();
            $('#add-conversation .normal-button')
                .addClass('normal-button-inactive');

            $.post( '/ajax/attachment', params, function(response) {
                loader.hide();
                
                $('#add-conversation .normal-button')
                    .removeClass('normal-button-inactive');

                if( response.success ) {
                    $(response.html).insertAfter( $(content) );
                }
                else {
                    content.show();
                }
            }, 'json' );

            return false;
        } );
    }


    me.addConversationEvents = function() {
        if( !$('#add-conversation').length ) return;

        $('#add-conversation textarea').autogrow();
        
        $('#add-conversation div.text:first').css( {
            width: '432px',
            'border-top': '1px solid #999'
        } );
        $('#add-conversation input[type=text]:first').focus( function() {
            $(this).parents('div.text:first').hide();
            $('#add-conversation div.textarea:first').show().find('textarea').focus();
        } );

        $('#add-conversation textarea')
            .blur( function() {
                if( $(this).val().length == 0 ) {
                    $(this).parents('div.textarea:first').hide();
                    $(this).parents('form').find('div.text:first').show();
                }
            } );

        me.addAttachmentEvents();

        $('#add-conversation form').ajaxForm( {
            dataType: 'json',
            beforeSubmit: function() {
                var submit = $('#add-conversation .normal-button');
                if( submit.hasClass('normal-button-inactive') ) return false;

                submit.addClass('normal-button-inactive');
            },
            success: function(response, state) {
                $('#add-conversation .normal-button').removeClass('normal-button-inactive');
                if( response.success ) {
                    $('.paging').prepend( response.html );
                    $('.paging li:first').css( {
                        'borderTop': 0,
                        'paddingTop': 0,
                        'marginBottom': '20px',
                        'borderBottom': '1px solid #eee'
                    } );

                    me.setActions( $('.paging li:first .message') );

                    var textarea = $('#add-conversation form textarea');
                    textarea.val( "" );

                    textarea.parents('form').find('div.textarea:first').hide();
                    textarea.parents('form').find('div.text:first').show();

                    $('#add-conversation #attachment-block').hide();
                    $('#add-conversation #attachment-block input[default_value]').each( function() {
                        $(this).val( $(this).attr('default_value') );
                    } );
                    
                    $('#add-conversation .attachment-ajax-content').remove();
                }
                else if( response.errors ) {
                    //do error handling
                }
            }
        } );
    }

    me.addConversationEvents();

}

