jQuery(document).ready(
   function () {
       if (navigator.userAgent.match(/blackberry/i)) {
           jQuery('.block-video-youtube').each(function () {
               var tIframe = jQuery(this).find('iframe').get(0);
               var tSt = 'www.youtube.com/embed/';
               if (tIframe.src && tIframe.src.length && tIframe.src.indexOf(tSt) > 0) {
                   var tSrc = tIframe.src;
                   var tStart = tSrc.indexOf(tSt) + tSt.length;
                   var tEnd = tSrc.lastIndexOf('?');
                   jQuery(tIframe).replaceWith('<a style="display:block;padding:40px 0;text-align:center;" href="http://m.youtube.com/watch?v=' + tSrc.substring(tStart, (tEnd < 0 ? undefined : tEnd)) + '">SEE VIDEO</a>');
               }
           });

           function fixImageSize() {
               var tThis = this;
               if (this.complete) {
                   fixImageLoadTrouble(tThis);
               } else if (this.complete === false) {/* do not mix this statement and typeof this.complete == 'undefined' */
                   setTimeout(function () { fixImageLoadTrouble(tThis); }, 200);
               } else if (typeof this.complete == 'undefined') {
                   setTimeout(function () { fixImageLoadTrouble(tThis); }, 200);
               }
           }

           function fixImageLoadTrouble(fImg) {
               if (!fImg) return;
               var tImg = jQuery(fImg);
               if (tImg.width() < 1) {
                   setTimeout(function () { fixImageLoadTrouble(fImg); }, 200);
                   return;
               }
               tImg.width(tImg.width());
           }

           jQuery('img').load(fixImageSize).each(fixImageSize);

          if(navigator.userAgent.toLowerCase().indexOf('blackberry') == 0) {
             jQuery('.block-image-and-text').each(function(){
                var tThis = jQuery(this);
                if(tThis.find('.no-text-wrapping:first').length > 0) {
                   var tImageHolder = tThis.find('.image-holder');
                   var tMaxWidth = tImageHolder.css('max-width');
                   if(tMaxWidth && tMaxWidth.length && tMaxWidth.lastIndexOf('%') == (tMaxWidth.length - 1)) {
                      tImageHolder.width(parseInt(tMaxWidth) * tThis.width() / 100);
                      tImageHolder.css('height', 'auto');
                   }
                }
             });
          }
       }
       else
           if (navigator.userAgent.match(/Android\s2\.1/i)) {
               jQuery('.block-video-youtube').each(function () {
                   var tIframe = jQuery(this).find('iframe').get(0);
                   var tSt = 'www.youtube.com/embed/';
                   if (tIframe.src && tIframe.src.length && tIframe.src.indexOf(tSt) > 0) {
                       var tSrc = tIframe.src;
                       var tStart = tSrc.indexOf(tSt) + tSt.length;
                       var tEnd = tSrc.lastIndexOf('?');
                       jQuery(tIframe).replaceWith('<embed src="http://www.youtube.com/v/' + tSrc.substring(tStart, (tEnd < 0 ? undefined : tEnd)) + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="100%" wmode="transparent"></embed>');
                   }
               });
           }

   }
);
