




/**
 * NBC Player Maker
 * A Javascript widget that will dynamically create an iframe pointing to
 * our video player
 *
 * Created by: Rob Sable
 * Date created: 11/19/2013
 *
 * Last edited by: atarampi
 * Last modified: 06/12/2017
 */

/**
 * Usage:
 * <script type="text/javascript" charset="UTF-8" defer="true" src="/some/path/will/be/here/tomyfile.js?vrid=123456789&tpid=sOm3keyb0ardM4sh"></script>
 */
if(typeof console == "undefined") {

    console = {};
    console.log = function(msg) {
    // fail silently
    }

    console.warn = function(msg) {
    // fail silently
    }

    console.error = function(msg) {
    // fail silently
    }

}

(function() {

    //console.log("/////////////////////////////////////////");
    //console.log("/// NBC PLAYER MAKER ////////////////////");
    //console.log("/////////////////////////////////////////");

    // Gather the scripts on the page.
    var scriptTag = document.currentScript;
    var parentTag = (!!navigator.userAgent.match(/Trident/g) || !!navigator.userAgent.match(/MSIE/g)) ? null : scriptTag.parentNode;
    
    var protocol = "https:"; // default to https

    if ( !!/^http/.exec(location.protocol) ) {
        /**
         * if location.protocol starts with http (e.g.: "http" or "https")
         * then we can set it as protocol.
         */
        protocol = location.protocol;
    }

    window['nbcLMP490211441'] = {};

    // Parameter parsing
    window['nbcLMP490211441'].scriptTagsOnPage = document.getElementsByTagName('script');
    window['nbcLMP490211441'].thisScriptName = 'portableplayer';
    window['nbcLMP490211441'].thisScriptSource = null;
    window['nbcLMP490211441'].activeEmbeds = new Array();
    window['nbcLMP490211441'].usedEmbeds = new Array();
    window['nbcLMP490211441'].scriptSplit = null;
    window['nbcLMP490211441'].scriptParams = null;
    window['nbcLMP490211441'].discreteParams = null;
    window['nbcLMP490211441'].useThisAsParent = null;
    window['nbcLMP490211441'].encodedURI = encodeURIComponent(location.origin+location.pathname);
    window['nbcLMP490211441'].encodedHost = encodeURIComponent(location.origin);
    window['nbcLMP490211441'].origin = null;
    window['nbcLMP490211441'].iframeCount = 0;




    // Comb the scripts!
    for(var a=0; a<window['nbcLMP490211441'].scriptTagsOnPage.length; a++) {
        //console.log("/// NBC PLAYER MAKER IS SEARCHING FOR ITSELF (How zen...) | " + window['nbcLMP490211441'].scriptTagsOnPage[a].src);
            if(window['nbcLMP490211441'].scriptTagsOnPage[a].src.match(window['nbcLMP490211441'].thisScriptName)) {


                window['nbcLMP490211441'].thisScriptSource = window['nbcLMP490211441'].scriptTagsOnPage[a].src;
                window['nbcLMP490211441'].activeEmbeds.push(window['nbcLMP490211441'].scriptTagsOnPage[a].src);
                window['nbcLMP490211441'].iframeCount = a;
                //console.log("/// NBC PLAYER MAKER | " + window['nbcLMP490211441'].scriptTagsOnPage[a].parentNode.id);
                window['nbcLMP490211441'].useThisAsParent = window['nbcLMP490211441'].scriptTagsOnPage[a].parentNode;
                if(!!navigator.userAgent.match(/Trident/g) || !!navigator.userAgent.match(/MSIE/g)){
                    parentTag = window['nbcLMP490211441'].useThisAsParent;
                } 
            }
    };
    var ratioHeight = (parentTag.offsetWidth === 0) ? 600 : (parentTag.offsetWidth * 9) / 16;

    // Do some splits...
    window['nbcLMP490211441'].scriptSplit = window['nbcLMP490211441'].thisScriptSource.split('/portableplayer/');
    window['nbcLMP490211441'].scriptParams = window['nbcLMP490211441'].scriptSplit[1].split('?');
    window['nbcLMP490211441'].discreteParams = window['nbcLMP490211441'].scriptParams[1].split('&');

    // Now dig through the parameters for the origin

    function getParams(requestedParam) {
        for(var q=0; q<window['nbcLMP490211441'].discreteParams.length; q++) {
            var parsedParam = [];
            if(window['nbcLMP490211441'].discreteParams[q].match(requestedParam)) {
                parsedParam = window['nbcLMP490211441'].discreteParams[q].split('=');
                break;
            }
        }
        return ( parsedParam.length > 0 ) ? parsedParam[1] : false;
    }

    window['nbcLMP490211441'].origin = getParams('origin');

    //console.log("/// NBC PLAYER MAKER PARAMETERS | "+ window['nbcLMP490211441'].scriptParams[1]);
    //console.log('/// NBC PLAYER MAKER IFRAME PREVIEW... | <iframe id="window['nbcLMP490211441']layer" width="X" height="Y" src="https://www.'+ window['nbcLMP490211441'].origin + '/templates/nbc_partner_player?'+window['nbcLMP490211441'].scriptParams[1] + "&turl="+location.origin+location.pathname+'"></iframe>');

    // DOM element creation
    var setFullWidth = getParams('fullWidth');
    var setWidth = getParams('width');
    var setHeight = getParams('height');
    var widgetWidth = getParams('widgetWidth');
    var customWidget = (widgetWidth == "300" ? "&customWidget=true" : "");

    window['nbcLMP490211441'].videoframe = document.createElement('iframe');
    window['nbcLMP490211441'].videoframe.style.border = 'none';
    window['nbcLMP490211441'].videoframe.width = (setFullWidth) ? "100%" : (setWidth !=="full") ? setWidth : "100%";
    window['nbcLMP490211441'].videoframe.height = (setFullWidth) ? ratioHeight : (setHeight !=="full") ? setHeight : "100%";
    window['nbcLMP490211441'].videoframe.scrolling = "no";
    window['nbcLMP490211441'].videoframe.id = "nbcLMP490211441" + window['nbcLMP490211441'].iframeCount;
    window['nbcLMP490211441'].videoframe.setAttribute('allowfullscreen', 'true');
    window['nbcLMP490211441'].videoframe.setAttribute('webkitallowfullscreen', 'true');
    window['nbcLMP490211441'].videoframe.setAttribute('mozallowfullscreen', 'true');

if(setFullWidth) {
window['nbcLMP490211441'].videoframe.src = protocol+"//www." + window['nbcLMP490211441'].origin + "/templates/nbc_partner_player?"+window['nbcLMP490211441'].scriptParams[1] + "&turl="+window['nbcLMP490211441'].encodedURI + "&ourl=" + window['nbcLMP490211441'].encodedHost + "&lp=5&fullWidth=y" + "&random=" + Math.random().toString(36).substring(7) + customWidget;
} else {
window['nbcLMP490211441'].videoframe.src = protocol+"//www." + window['nbcLMP490211441'].origin + "/templates/nbc_partner_player?"+window['nbcLMP490211441'].scriptParams[1] + "&turl="+window['nbcLMP490211441'].encodedURI + "&ourl=" + window['nbcLMP490211441'].encodedHost + "&lp=1" + "&random=" + Math.random().toString(36).substring(7) + customWidget;
}

    window['nbcLMP490211441'].useThisAsParent.appendChild(window['nbcLMP490211441'].videoframe);
    window.addEventListener("resize", resizeThrottler, false);

    var resizeTimeout;
    function resizeThrottler() {
      // ignore resize events as long as an actualResizeHandler execution is in the queue
      if ( !resizeTimeout ) {
        resizeTimeout = setTimeout(function() {
          resizeTimeout = null;
          actualResizeHandler();
       
         // The actualResizeHandler will execute at a rate of 15fps
         }, 66);
      }
    }

    function actualResizeHandler() {
        ratioHeight = (parentTag.offsetWidth * 9) / 16;
        var ifrm = window['nbcLMP490211441'].videoframe.id;
        document.getElementById(ifrm).height = ratioHeight;
    }
    //console.log("/// NBC PLAYER MAKER | COMPLETE!");
})();
