(function() {

    function update_chars_left() {
    	var max_len = 300, 
    		textarea = $('#new_tweet')[0], 
    		tweet_len = textarea ? textarea.value.length : null;
    	if ( tweet_len >= max_len ) {
    		textarea.value = textarea.value.substring(0, max_len); // truncate
    		$('#chars_left').html("0");
    	} else {
    		$('#chars_left').html(max_len - tweet_len);
    	}
    }
	
    function confirm_submit( ev ) {
        var agree = confirm( "Do you really want to delete this post? (There is NO undo)" );
        if ( ! agree ) {
            ev.preventDefault();
        }
        $( this ).blur();
    }
    
    function reply_handler( ev ) {
        var id = $( this ).attr( "id" ).replace( "reply_", "" );
        toggle_comment_form( id );
        ev.preventDefault();
    }
    
    function love_it_post_handler( data, textStatus ) {
        var instanceId = this.data.replace( 'instance_id=', '' ),
            loveZone = $( '#loveIt_' + instanceId ),
            loveText = loveZone.find( 'span' ),
            image = loveZone.find( 'img' ),
            source = image.attr( "src" ),
            link = image.parent().attr( "href" ),
            loveType = link.match( /\/love/ ) ? 'love' : 'unlove';
        if ( data && textStatus === 'success' ) {
            if ( data.success ) {
                if ( loveType === 'love' ) {
                    image.parent().attr( { "href": link.replace( /\/love/, '/unlove' ) } );
                    image.attr( { "src": source.replace( '_load.gif', '_active.png' ) } ).removeClass( "loading" );  
                    loveText.html( "Unlove it" ); 
                } else {
                    image.parent().attr( { "href": link.replace( /\/unlove/, '/love' ) } );
                    image.attr( { "src": source.replace( '_load.gif', '.png' ) } ).removeClass( "loading" );
                    loveText.html( "Love it" ); 
                }
            } else {
                image.attr( { "src": source.replace( '_load.gif', '_error.png' ) } ).removeClass( "loading" );
                loveText.html( "Too much love error" );
            }
        }
    }
    
    function love_it_post( ev ) {
        ev.preventDefault();
        var link = $( this ),
            image = link.find( "img" ),
            source = image.attr( "src" ),
            instanceId = link.attr( "id" ).replace( 'loveIt_', '' );
        if ( source.match( /(love|love_active).png/i ) ) {
            image.attr( { "src" : source.replace( /(love|love_active).png/, 'love_load.gif' ) } ).addClass( "loading" );
            jQuery.post( link.attr( "href" ), { 'instance_id' : instanceId }, love_it_post_handler, "json" );
        }
        link.blur();
    }
    
    $( document ).ready(function() {
    	$( '#new_tweet' ).keyup( function() {
    		update_chars_left();
    	} );
    	update_chars_left();
    	$( '#new_tweet' ).focus();
    	$( '.loveIt' ).bind( "click", love_it_post );
    	$( '.deletePost' ).bind( "click", confirm_submit ); 
    	$( '.replyPost' ).bind( "click", reply_handler );
    });
    
})();

