/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* Logical Name    : DCC JW Player Embedding Library                                    */
/*                                                                                      */
/* File Name       : dccjwplayerlib.js                                                  */
/*                                                                                      */
/* System Name     : Media Server                                                       */
/*                                                                                      */
/* Sub-System Name : JW Player                                                          */
/*                                                                                      */
/* Author          : A. Radford (Derby City Council)                                    */
/*                                                                                      */
/* Date            : 01/06/2011                                                         */
/*                                                                                      */
/* Description     : Allow JW Player to be embedded in a page, with fallback to RTSP,   */
/*                   iOS streaming or a static image for non-Flash content.             */
/*                                                                                      */
/* Version History                                                                      */
/*                                                                                      */
/* Version Date       Who   Description                                                 */
/* ------- ---------- ----  ----------------                                            */
/*   0.8   27/10/2010 ATR   Original Version.                                           */
/*   0.9   27/10/2010 ATR   Implemented SWFObject fallback.                             */
/*   1.0   23/11/2010 ATR   First release.                                              */
/*   1.1   26/11/2010 ATR   Added Shadowbox support.                                    */
/*   1.2   03/12/2010 ATR   Added RTMPE support.                                        */
/*   1.3   07/12/2010 ATR   Added Flash version check to Shadowbox playback.            */
/*   1.4   08/12/2010 ATR   Amended Shadowbox to force JW Player for all video formats. */
/*   1.5   10/12/2010 ATR   Added string splitting to check for Android version.        */
/*   1.6   15/12/2010 ATR   Added animate sequence Shadowbox configuration variable.    */
/*   1.7   02/02/2011 ATR   Added screen/background colour parameter                    */
/*   1.8   02/02/2011 ATR   Added function call allowing flashvars to be passed, also   */
/*                          added autostartoverride parameter to override config file.  */
/*   1.9   01/06/2011 ATR   Added default to use config file values for logo, skin and  */
/*                          autostart if flashvars is passed in, but does not contain   */
/*                          settings for these.                                         */
/*   2.0   01/06/2011 ATR   Added default to use config file values for controlbar if   */
/*                          flashvars is passed in, but does not contain settings for   */
/*                          these.                                                      */
/*--------------------------------------------------------------------------------------*/

                                                   /*-----------------------------------*/
                                                   /*  Include the SWFobject and        */
                                                   /*  Shadowbox libraries, these are   */
                                                   /*  going to be used later to embed  */
                                                   /*  Flash player in a browser safe   */
                                                   /*  manner.                          */
                                                   /*-----------------------------------*/

document.write('<script type="text/javascript" src="'          + 
               jwplayerfolder                                  + 
               'swfobject.js"></script>'
	      );

document.write('<link rel="stylesheet" type="text/css" href="' +
               jwplayerfolder                                  + 
               'shadowbox/shadowbox.css">'
	      );

document.write('<script type="text/javascript" src="'          +
               jwplayerfolder                                  + 
               'shadowbox/shadowbox.js"></script>'
	      );

/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* Function name : embedPlayerWithFlashvars                                             */
/*                                                                                      */
/* Parameters    :                                                                      */
/*           mediaapp          - Application within Wowza to use.                       */
/*           protocol          - Flash protocol to use (eg. RTMP, RTMPE)                */
/*           mediafile         - Media file to play.                                    */ 
/*           containerstring   - Name of DOM element to overwrite.                      */
/*           width             - Width of player/fallback image.                        */
/*           height            - Height of player/fallback image.                       */
/*           staticimg         - Static image to be displayed if using RTSP streaming.  */
/*           alttext           - Alternate text for static image.                       */
/*           shadowbox         - Use Shadowbox effects.                                 */
/*           flashvarsoverride - Flashvars for JW Player.                               */
/*                                                                                      */
/* Description   : Embed JW Player, passing a flashvars into the method.                */
/*                                                                                      */
/*                                                                                      */
/*--------------------------------------------------------------------------------------*/
function embedPlayerWithFlashvars(
                     mediaapp, 
                     protocol,
                     mediafile, 
                     containerstring, 
                     width, 
                     height, 
                     staticimg, 
                     alttext,
                     shadowbox,
                     flashvarsoverride)
{

                                                   /*-----------------------------------*/
                                                   /* Some variables weren't passed     */
                                                   /* into this function, but we need   */
                                                   /* them to exist for the call to     */
                                                   /* fullEmbedPlayer, so create them   */
                                                   /* as null values.                   */
                                                   /*-----------------------------------*/

    var screencolour      = null;
    var previewimg        = null;
    var autostartoverride = null;

                                                   /*-----------------------------------*/
                                                   /* Call the main embed function.     */
                                                   /*-----------------------------------*/

    fullEmbedPlayer(mediaapp, 
                    protocol,
                    mediafile, 
                    containerstring, 
                    width, 
                    height, 
                    staticimg, 
                    previewimg, 
                    alttext,
                    shadowbox,
                    screencolour,
                    autostartoverride,
                    flashvarsoverride 
                   );

}

/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* Function name : embedPlayer                                                          */
/*                                                                                      */
/* Parameters    :                                                                      */
/*           mediaapp          - Application within Wowza to use.                       */
/*           protocol          - Flash protocol to use (eg. RTMP, RTMPE)                */
/*           mediafile         - Media file to play.                                    */ 
/*           containerstring   - Name of DOM element to overwrite.                      */
/*           width             - Width of player/fallback image.                        */
/*           height            - Height of player/fallback image.                       */
/*           staticimg         - Static image to be displayed if using RTSP streaming.  */
/*           previewimg        - Preview image to diaply in JW Player before streaming. */
/*           alttext           - Alternate text for static image.                       */
/*           shadowbox         - Use Shadowbox effects.                                 */
/*           screencolour      - Colour to use behind video in JW Player.               */
/*           autostartoverride - Override autostart value in config file.               */
/*                                                                                      */
/* Description   : Embed JW Player, not passing in a Flashvars variable.                */
/*                                                                                      */
/*--------------------------------------------------------------------------------------*/
function embedPlayer(mediaapp, 
                     protocol,
                     mediafile, 
                     containerstring, 
                     width, 
                     height, 
                     staticimg, 
                     previewimg, 
                     alttext,
                     shadowbox,
                     screencolour,
                     autostartoverride
                    )
{

                                                   /*-----------------------------------*/
                                                   /* Flashvars variable isn't passed   */
                                                   /* into this function, but we need   */
                                                   /* it to exist for the call to       */
                                                   /* fullEmbedPlayer, so create it     */
                                                   /* with a null value.                */
                                                   /*-----------------------------------*/

    var flashvarsoverride = null;

                                                   /*-----------------------------------*/
                                                   /* Call the main embed function.     */
                                                   /*-----------------------------------*/

    fullEmbedPlayer(mediaapp, 
                    protocol,
                    mediafile, 
                    containerstring, 
                    width, 
                    height, 
                    staticimg, 
                    previewimg, 
                    alttext,
                    shadowbox,
                    screencolour,
                    autostartoverride,
                    flashvarsoverride 
                   )
}

/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* Function name : fullEmbedPlayer                                                      */
/*                                                                                      */
/* Parameters    :                                                                      */
/*           mediaapp          - Application within Wowza to use.                       */
/*           protocol          - Flash protocol to use (eg. RTMP, RTMPE)                */
/*           mediafile         - Media file to play.                                    */ 
/*           containerstring   - Name of DOM element to overwrite.                      */
/*           width             - Width of player/fallback image.                        */
/*           height            - Height of player/fallback image.                       */
/*           staticimg         - Static image to be displayed if using RTSP streaming.  */
/*           previewimg        - Preview image to diaply in JW Player before streaming. */
/*           alttext           - Alternate text for static image.                       */
/*           shadowbox         - Use Shadowbox effects.                                 */
/*           screencolour      - Colour to use behind video in JW Player.               */
/*           autostartoverride - Override autostart value in config file.               */
/*           flashvarsoverride - Flashvars object to use.                               */
/*                                                                                      */
/* Description   : Try to insert an instance of JW Flash Player into the page using     */
/*                 SWFObject (generic Flash embedder). If Flashvars variable is passed  */
/*                 in, then use the settings within that, otherwise use the individual  */
/*                 parameters passing into the function.                                */
/*                                                                                      */
/*                 NOTE: This function should NOT be called directly, use embedPlayer   */
/*                       or embedPlayerWithFlashvars instead.                           */
/*                                                                                      */
/*--------------------------------------------------------------------------------------*/
function fullEmbedPlayer(mediaapp, 
                         protocol,
                         mediafile, 
                         containerstring, 
                         width, 
                         height, 
                         staticimg, 
                         previewimg, 
                         alttext,
                         shadowbox,
                         screencolour,
                         autostartoverride,
                         flashvarsoverride
                        )
{

                                                   /*-----------------------------------*/
                                                   /* Get the container element we are  */
                                                   /* going to replace with the video.  */
                                                   /*-----------------------------------*/

    var container = document.getElementById(containerstring);

                                                   /*-----------------------------------*/
                                                   /* Get the browser user agent.       */
                                                   /*-----------------------------------*/

    var browseragent = navigator.userAgent;

                                                   /*-----------------------------------*/
                                                   /* Parse the Android user agent to   */
                                                   /* see if it is version 2.2 or       */
                                                   /* later. 2.2 is the first version   */
                                                   /* to support Flash content.         */
                                                   /*-----------------------------------*/

    var bandroidflash = true;

    // Search for the string 'Android'
    var androidpos = browseragent.search('Android');    

    // If found, then this is an Android device, so look for the version
    if (androidpos != -1)
    {
        // Cut the string from the word 'Android', and then find the next space
        var fromandroid = browseragent.slice(androidpos);
        var spacepos    = fromandroid.search(' ');

        // No space found, something wierd is going on so don't use flash
        if (spacepos != -1)
        {
            // Cur from the space, removing '.' and ';'
            var fromspace  = fromandroid.slice(spacepos + 1, spacepos + 5);
            var removedot  = fromspace.replace('\.', '');
            var removesemi = removedot.replace(';', '');
          
            // This should give us the version number, without a '.'.
            // If greater than 22, then Flash is supported
            if (removesemi >= 22)
            {
                bandroidflash = true;
            }
            else
            {
                bandroidflash = false;
            }
        }
    }

                                                   /*-----------------------------------*/
                                                   /* Is the browser an iPhone, iPod    */
                                                   /* or iPad? If so, then show an      */
                                                   /* image and link to the video using */
                                                   /* Apple HTTP Live streaming.        */
                                                   /* This will use port 80.            */
                                                   /*-----------------------------------*/

    if ( (browseragent.match(/iPhone/i)) || 
         (browseragent.match(/iPod/i))   || 
         (browseragent.match(/iPad/i)) 
       ) 
    {

        var content = '<a href="http://'       + 
                  mediaserver                  + 
                  '/'                          + 
                  mediaapp                     + 
                  '/'                          + 
                  mediafile                    + 
                  '/playlist.m3u8"><img src="' + 
                  staticimg                    + 
                  '" alt="'                    + 
                  alttext                      + 
                  '"/></a>';

        container.innerHTML = content;
    }

                                                   /*-----------------------------------*/
                                                   /* Is the browser another mobile     */
                                                   /* device that is likely to support  */
                                                   /* RTSP streaming? If so, then show  */
                                                   /* an image and link to the video.   */
                                                   /* This will use port 554.           */
                                                   /* Android 2.2, 2.3 and 3.0 support  */
                                                   /* Flash though.                     */
                                                   /*-----------------------------------*/
    
    else if (
                (  (browseragent.match(/Android/i)   )  &&
                   (bandroidflash != true            )
                )                                          ||
                (  (browseragent.match(/blackberry/i))  || 
                   (browseragent.match(/symbian/i)   )  || 
                   (browseragent.match(/palm/i)      ) 
                )
            )
    {
        var content = '<a href="rtsp://' + 
                      mediaserver        + 
                      '/'                + 
                      mediaapp           + 
                      '/'                + 
                      mediafile          + 
                      '"><img src="'     + 
                      staticimg          + 
                      '" alt="'          + 
                      alttext            + 
                      '"/></a>';
    
        container.innerHTML = content;
    }

                                                   /*-----------------------------------*/
                                                   /* Show JW Player for Flash content  */
                                                   /* playback                          */
                                                   /*-----------------------------------*/

    else
    {


                                                    /*-----------------------------------*/
                                                    /*  If the protocol is RTMPE, then   */
                                                    /*  use RTMPE for the URL, but only  */
                                                    /*  pass RTMP as the provider to     */
                                                    /*  JW Player. Otherwise, pass the   */
                                                    /*  protocol itself to JW Player.    */
                                                    /*-----------------------------------*/

        var provider = '';

        if (protocol == 'rtmpe')
        {
            provider = 'rtmp';                
        }
        else     
        {
            provider = protocol;                
        }

                                                    /*-----------------------------------*/
                                                    /*  Local initialisers.              */
                                                    /*-----------------------------------*/

        var flashstreamer   = protocol            +
                              '://'               + 
                              mediaserver         + 
                              '/'                 + 
                              mediaapp + '/';       

                                                    /*-----------------------------------*/
                                                    /*  We have a Flashvars, so use most */
                                                    /*  of the settings in that, rather  */
                                                    /*  than other parameters.           */
                                                    /*  Note: We use plugins, provider   */
                                                    /*  and streamer from parameters and */
                                                    /*  the settings config file not the */
                                                    /*  values in the passed in the      */
                                                    /*  flashvars variable.              */
                                                    /*-----------------------------------*/

        if (flashvarsoverride != null)
        {
             var flashvars = {};
             flashvars                    = flashvarsoverride;
             flashvars.plugins            = jwplayerfolder + 'gapro.swf';
             flashvars['gapro.accountid'] = googleanalyticsid;
             flashvars.provider           = provider;
             flashvars.streamer           = flashstreamer; 

                                                   /*-----------------------------------*/
                                                   /*  If the caller passed in a        */
                                                   /*  flashvars, but didn't set values */
                                                   /*  for skin, logo, logo hide, logo  */
                                                   /*  position, controlbar or          */
                                                   /*  autostart, then use the value in */
                                                   /*  the config file.                 */
                                                   /*-----------------------------------*/

             if (flashvars.skin == null || flashvars.skin == '')
             {
                 flashvars.skin = jwplayerfolder + skin;
             }
               
             if (flashvars['logo.file'] == null || flashvars['logo.file'] == '')
             {
                 flashvars['logo.file'] = jwplayerfolder + logo;
             }   

             if (flashvars['logo.hide'] == null || flashvars['logo.hide'] == '')
             {
                 flashvars['logo.hide'] = logohide;
             } 

             if (flashvars['logo.position'] == null || flashvars['logo.position'] == '')
             {
                 flashvars['logo.position'] = logoposition;
             }

             if (flashvars.autostart == null || flashvars.autostart == '')
             {
                 flashvars.autostart = autostart;
             }

             if (flashvars.controlbar == null || flashvars.controlbar == '')
             {
                 flashvars.controlbar = controlbarposition;
             }

             if (flashvars.autostart == null || flashvars.autostart == '')
             {
                 flashvars.autostart = autostart;
             }
             
        }

                                                    /*-----------------------------------*/
                                                    /*  No flashvars, so set values from */
                                                    /*  passed in parameters and config  */
                                                    /*  file.                            */
                                                    /*-----------------------------------*/

        else
        {
            var flashvars       = {
                                    controlbar       : controlbarposition, 
                                    'logo.file'      : jwplayerfolder + logo, 
                                    'logo.hide'      : logohide, 
                                    'logo.position'  : logoposition, 
                                    plugins          : jwplayerfolder + 'gapro.swf',
                                    'gapro.accountid': googleanalyticsid,
                                    provider         : provider, 
                                    streamer         : flashstreamer,
                                    skin             : jwplayerfolder + skin,
                                    image            : previewimg,
                                    screencolor      : screencolour
                                  };  


                                                   /*-----------------------------------*/
                                                   /*  If the caller specified          */
                                                   /*  something in the autostart       */
                                                   /*  override parameter, then use the */
                                                   /*  the value they provided.         */
                                                   /*  If they didn't specify a value   */
                                                   /*  then use the value from the      */
                                                   /*  config file.                     */
                                                   /*-----------------------------------*/

            if (autostartoverride != null)
            {
                flashvars.autostart = autostartoverride;
            }
            else
            {
                flashvars.autostart = autostart;
            }

        }

                                                    /*-----------------------------------*/
                                                    /*  Let the user expand the video to */
                                                    /*  full screen, and allow           */
                                                    /*  javascript to interact with the  */
                                                    /*  player.                          */
                                                    /*-----------------------------------*/


        var params          = { allowfullscreen:fullscreen, 
                                allowscriptaccess:'always',
                                wmode:'transparent'
                              };

        var player          = jwplayerfolder + 'player.swf';
        var flashinstall    = 'false';
        var flashversion    = '9.0.115';

                                                   /*-----------------------------------*/
                                                   /*  Show the video without Shadowbox */
                                                   /*  effects.                         */
                                                   /*-----------------------------------*/

        if (shadowbox != 'true')
        {
        
                                                   /*-----------------------------------*/
                                                   /*  Set the filename of the video    */
                                                   /*  to play.                         */
                                                   /*-----------------------------------*/

            flashvars.file      = mediafile;

                                                   /*-----------------------------------*/
                                                   /*  Now use SWFObject to embed JW    */
                                                   /*  Player with the video to play.   */
                                                   /*-----------------------------------*/

            swfobject.embedSWF(player, 
                               container.id, 
                               width, 
                               height, 
                               flashversion, 
                               flashinstall, 
                               flashvars, 
                               params, 
                               null , 
                               null);
        }

                                                   /*-----------------------------------*/
                                                   /*  Show the video with Shadowbox.   */
                                                   /*-----------------------------------*/

        else
        {

                                                   /*-----------------------------------*/
                                                   /*  Only try to use Flash if the     */
                                                   /*  client has Flash 9.0 or above.   */
                                                   /*-----------------------------------*/

            var playerversion = swfobject.getFlashPlayerVersion();
          
            if (playerversion.major >= 9) 
            {
                var content = '<a href="'                +
                              mediafile                  + 
                              '" rel="shadowbox;height=' +
                              height                     +
                              ';width='                  +
                              width                      + 
                              ';player=flv" title="'     +
                              alttext                    +
                              '"><img src="'             +
                              staticimg                  +
                              '" height="'               +
                              height                     +
                              '" width="'                +
                              width                      +
                              '" alt="'                  +
                              alttext                    +
                              '"/></a>';

                container.innerHTML = content;

                Shadowbox.init({
                              overlayOpacity: shadowopacity,
                              modal         : shadowmodal,
                              animate       : shadowanimate,
                              animateFade   : shadowanimatefade,
                              animSequence  : shadowanimatesequence,
                              fadeDuration  : shadowfadeduration,
                              flashParams   : params,
                              flashVars     : flashvars,
                              flashVersion  : "9.0.0"
                          });
            }

                                                   /*-----------------------------------*/
                                                   /*  Not the right version of Flash,  */
                                                   /*  or no Flash, so fallback to a    */
                                                   /*  prompt instructing the user to   */
                                                   /*  install Flash player.            */
                                                   /*-----------------------------------*/

            else
            {
                // Do nothing
            }  
        }
    }
}


/*--------------------------------------------------------------------------------------*/
/*                                   END OF SCRIPT                                      */
/*--------------------------------------------------------------------------------------*/

