<!DOCTYPE html>
<html>
    <head>
        <title>CS50 Live</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js    "></script>
        <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css" rel="stylesheet" />
        <script type="text/javascript" src="http://mirror.cs50.net/live50/js/swfobject.js"></script>
        <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>
    </head>
    <body style="padding:0px;">
        <div id="player_wrapper" style="margin:auto; text-align:center; width:640px; padding:none">
<div id="StrobeMediaPlayback">
                <p>
                    Your browser does not support flash or live HTTP Streaming, please install flash or try another device.
                </p>
            </div>
        </div>
            <script type="text/javascript">

                WOWZA_ADDRESS = "d2styuku9jk2cq.cloudfront.net";
                APP_NAME = "liverecdvr";
                
                parse();

                function parse(stringtoparse)
                {
                    // parse get variables
                    var gs;

                    // check if the parse function was passed a get variable
                    if (typeof stringtoparse === "undefined")
                        gs = window.location.search.substr(1);
                    else
                        gs = stringtoparse.split("?")[1];
                    console.log("gs = " + gs)

                    var gsa = gs.split ("&");
                    g = {};

                    for (var i = 0; i < gsa.length; i++) {
                        var ta = gsa[i].split("=");
                        g[ta[0]] = ta[1];
                    }

                    var source = "http://" + WOWZA_ADDRESS + "/";
                    console.log(g);
                    // make sure we got a stream name passed
                    if(typeof g.streamname === "undefined" || g.streamname == "")
                    {
                        document.getElementById("StrobeMediaPlayback").innerHTML = "<p>NO STREAM NAME DEFINED</p>";
                        return false;
                    }
                    else
                    {
                        
                        console.log(g.streamname);

                        //add the app name
                        source += APP_NAME + "/";

                        // if we have DVR, we won't be doing multi-bitrate, user must select a raster
                        if (g.dvr == "true")
                        {
                            source += g.streamname;
                            var r = get_raster(g);
                            if (!r)
                            {
                                document.getElementById("StrobeMediaPlayback").innerHTML = "<p>INVALID OR NO SIZE DEFINED</p>";
                                return false;
                            }

                            // add the raster
                            // source += "_" + r + "/";
                            source += "/";
                        }
                       /* else
                        {
                            // no DVR check if we got a particular raster
                            var r = get_raster(g);
                            if (!r)
                            {
                                // not passed a raster, multi-bitrate
                                source += "ngrp:" + g.streamname + "_all/";
                            }
                            else
                            {
                                source += g.streamname + "_" + r + "/";
                            }
                        }
                        */

                        // check for iOS devices
                        if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) 
                        {
                            // send the appropriate file if it's a dvr
                            if (g.dvr == "true")
                            {
                                //console.log (source + "playlist.m3u8?DVR");
                                window.location = source + "playlist.m3u8?DVR";
                            }
                            else
                            {   
                                // console.log(source + "playlist.m3u8");
                                window.location = source + "playlist.m3u8";
                            }
                        }
                        else
                        {
                            // not in iOS device
                            // send the appropriate file if it's a dvr
                            if (g.dvr == "true")
                            {
                                console.log (source + "manifest.f4m?DVR");
                                source = source + "manifest.f4m?DVR";
                            }
                            else
                            {   
                                console.log(source + "manifest.f4m");
                                source = source + "manifest.f4m";
                            }

                        }                    
                    }
                    
                    
                    var parameters = {
                        src: source,
                        expandedBufferTime: 2,
                        liveBufferTime: 12,
                        liveDynamicStreamingBufferTime: 14,
                        dvrBufferTime: 12,
                        dvrDynamicStreamingBufferTime: 14,
                        dynamicStreamBufferTime: 14,
                        autoPlay: true,
                        verbose: true,
                        controlBarAutoHide: "false",
                        controlBarPosition: "bottom",
                        scale: "exactfit"
                    };
                    
            
                    
                    // Escape the ampersands so any URL params pass through OSMF into Wowza
                    s = parameters['src'];
                    s = escape(s);
                    parameters['src'] = s; 
                   
                    var wmodeValue = "direct";
                    // var wmodeOptions = ["direct", "opaque", "transparent", "window"];
                    // if (parameters.hasOwnProperty("wmode"))
                    // {
                    //         if (wmodeOptions.indexOf(parameters.wmode) >= 0)
                    //         {
                    //                 wmodeValue = parameters.wmode;
                    //         }                            
                    //         delete parameters.wmode;
                    // }
                    
                    // Embed the player SWF:                    
                    swfobject.embedSWF(
                                        "http://mirror.cs50.net/live50/swf/StrobeMediaPlayback.swf"
                                        , "StrobeMediaPlayback"
                                        , 600
                                        , 360
                                        , "10.3.0"
                                        , "http://mirror.cs50.net/live50/swf/expressInstall.swf"
                                        , parameters
                                        , {
                                           allowFullScreen: "true",
                                           wmode: wmodeValue
                                          }
                                        , {
                                           name: "StrobeMediaPlayback"
                                          }
                                );
                    
                    /* Uncomment this code to be notified of playback errors in JavaScript: 
                    
                     function onMediaPlaybackError(playerId, code, message, detail)            
                     {
                             alert(playerId + "\n\n" + code + "\n" + message + "\n" + detail);            
                     }
                     
                     */
                 }

                 function get_raster(g)
                 {
                    if (!(typeof g.raster === "undefined" || g.raster == ""))
                    {
                        switch(g.raster)
                        {
                            case "720p":
                                $("#720p").parent().addClass("active");
                                $("#360p").parent().removeClass("active");
                                $("#160p").parent().removeClass("active");
                                break;
                            case "360p":
                                $("#720p").parent().removeClass("active");
                                $("#360p").parent().addClass("active");
                                $("#160p").parent().removeClass("active");
                                break;
                            case "160p":
                                $("#720p").parent().removeClass("active");
                                $("#360p").parent().removeClass("active");
                                $("#160p").parent().addClass("active");
                                break;
                            default:
                                return false;
                                break;
                        }
                        return g.raster;
                    }
                 }
                
            </script>
        
    </body>
</html>