/*
Copyright (c) 2010 Church Media Group, Inc (http://churchmedia.cc) All rights reserved.

This code is distributed under the following terms:

Redistribution and use of this software in source and binary forms, with or without modification,
are not permitted without prior written permission by the copyright holders.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
 * ??
 *
 * @category   ??
 * @package    JonahCMS
 * @copyright  2010 Church Media Group, Inc.
 * @version    SVN:
 * @link       n/a
 * @see        n/a
 * @since      File available since Release 1.0.0
 */


$(document).ready(function($) {
  // swap out video links with flash player
  $('.video_player_cmg.autoload').videoPlayer();

  // swap out audio links with flash player
  $('.audio_player_cmg.autoload').each(function() {
    var $this = $(this);
    $this.html('');
    $this.flowplayer(config.siteUrl + 'js/flowplayer/flowplayer.commercial-3.2.2-1.0.swf', {
      clip: {
        autoPlay: $this.hasClass('autoplay')
      },
      key: '#@db5d15c9f3cf250140c',
      plugins: {
        controls: {
			fullscreen: false,
			height: 30,
			url: config.siteUrl + 'js/flowplayer/flowplayer.controls-3.2.1-1.0.swf',
			backgroundColor: 'rgba(0,0,0,0.5)',
			backgroundGradient: "none",
			sliderColor: '#7ac7fc',
			sliderBorder: '1.5px solid rgba(160,160,160,0.7)',
			volumeSliderColor: '#7ac7fc',
			volumeBorder: '1.5px solid rgba(160,160,160,0.7)',
			timeColor: '#ffffff',
			durationColor: '#7ac7fc',
			tooltipColor: 'rgba(255, 255, 255, 0.7)',
			tooltipTextColor: '#000000'       
        }
      }
    });
  });
});

(function($){
  $.fn.videoPlayer = function(options) {
    var defaults = {
      width: '100%',
      height: '100%',
      onStart: null,
      flowplayer: {}
    };
    options = $.extend(defaults, options);

    return this.each(function() {
      var $div  = $(this).css('width', options.width).css('height', options.height);
      var $link = $div.find('a.video_link');
      var src   = $link.attr('href');
      
      // this is a YouTube or Vimeo embed so call the onStart on the first click
      if (src == null) {
        $div.one('click', options.onStart);
        return;
      }

      // check if we need to use captions
      var captionUrl = $div.find('a.captions_link').attr('href');
      if (!/(\.xml|\.srt)$/i.test(captionUrl)) {
        captionUrl = null;
      }

      // use the html5 player if this is a webkit browser and not an FLV
      if ($.browser.webkit && !/(\.flv)$/i.test(src) && !captionUrl && (config.enableHtml5Player || navigator.userAgent.match(/iPad|iPhone|iPod/i))) {
        var autoplay = $div.hasClass('autoplay') ? 'autoplay' : false;
        var $video = $('<video src="' + src + '" controls ' + autoplay + ' preload="none"></video>');
        $video.css('width', options.width).css('height', options.height);
        $video.bind('play', options.onStart);
        $link.replaceWith($video);
      }

      // use flash in all other cases
      else {
        var flowConfig = {
          clip: {
            url: src,
            autoPlay: $div.hasClass('autoplay'),
            scaling: 'fit'
          },
          onStart: defaults.onStart
        };

        // configure the captions
        if (captionUrl) {
          $.extend(true, flowConfig, {
            clip: {
              captionUrl: captionUrl
            },
            plugins:  {
              captions: {
                url: config.siteUrl + 'js/flowplayer/flowplayer.captions.swf',
                captionTarget: 'subtitles'
              },
              subtitles: {
                url: config.siteUrl + 'js/flowplayer/flowplayer.content.swf',
                bottom: 25,
                width: '70%',
                height: 70,
                border: 0,
                borderRadius: 0,
                backgroundColor: 'transparent',
                backgroundGradient: 'none',
                textDecoration: 'outline',
                display: 'hidden',
                style: {
                  body: {
                    fontSize: '18',
                    fontFamily: 'Arial',
                    textAlign: 'center',
                    verticalAlign: 'middle',
                    color: '#ffffff'
                  }
                }
              }
            },
            onFullscreen: function() {
              this.getPlugin('subtitles').css({height: 130, body: {fontSize: '35'}});
            },
            onFullscreenExit: function() {
              this.getPlugin('subtitles').css({height: 70, body: {fontSize: '18'}});
            }
          });
        }

        $div.html(''); // clear out the content, otherwise
        $.extend(true, flowConfig, options.flowplayer); // allow overriding of defaults
        $div.flowplayer(config.siteUrl + 'js/flowplayer/flowplayer.commercial-3.2.2-1.0.swf', flowConfig);
      }
    });
  };
})(jQuery);
