Using JQuery to check for Rel Attribute of Image before Overriding It

/*-----------------------------------------------------------------------------------*/
/* Add rel="lightbox" to image links if the lightbox is enabled */
/*-----------------------------------------------------------------------------------*/

if ( jQuery( 'body' ).hasClass( 'has-lightbox' ) && ! jQuery( 'body' ).hasClass( 'portfolio-component' ) ) {
        jQuery( 'a[href$=".jpg"], a[href$=".jpeg"], a[href$=".gif"], a[href$=".png"]' ).each( function () {
                var imageTitle="";
                if ( jQuery( this ).next().hasClass( 'wp-caption-text' ) ) {
                        imageTitle = jQuery( this ).next().text();
                }
              ;
               if (jQuery(this).attr('rel').indexOf("lightbox") == -1 )
               {
                jQuery( this ).attr( 'rel', 'lightbox' ).attr( 'title', imageTitle );
                }
        });

        jQuery( 'a[rel^="lightbox"]' ).prettyPhoto({social_tools: false});
}