




/**
 * 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 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['nbcLMP377315121'] = {};

    // Parameter parsing
    window['nbcLMP377315121'].scriptTagsOnPage = document.getElementsByTagName('script');
    window['nbcLMP377315121'].thisScriptName = 'portableplayer';
    window['nbcLMP377315121'].thisScriptSource = null;
    window['nbcLMP377315121'].activeEmbeds = new Array();
    window['nbcLMP377315121'].usedEmbeds = new Array();
    window['nbcLMP377315121'].scriptSplit = null;
    window['nbcLMP377315121'].scriptParams = null;
    window['nbcLMP377315121'].discreteParams = null;
    window['nbcLMP377315121'].useThisAsParent = null;
    window['nbcLMP377315121'].encodedURI = encodeURIComponent(location.origin+location.pathname);
    window['nbcLMP377315121'].encodedHost = encodeURIComponent(location.origin);
    window['nbcLMP377315121'].origin = null;
    window['nbcLMP377315121'].iframeCount = 0;




    // Comb the scripts!
    for(var a=0; a<window['nbcLMP377315121'].scriptTagsOnPage.length; a++) {
        //console.log("/// NBC PLAYER MAKER IS SEARCHING FOR ITSELF (How zen...) | " + window['nbcLMP377315121'].scriptTagsOnPage[a].src);
            if(window['nbcLMP377315121'].scriptTagsOnPage[a].src.match(window['nbcLMP377315121'].thisScriptName)) {


                window['nbcLMP377315121'].thisScriptSource = window['nbcLMP377315121'].scriptTagsOnPage[a].src;
                window['nbcLMP377315121'].activeEmbeds.push(window['nbcLMP377315121'].scriptTagsOnPage[a].src);
                window['nbcLMP377315121'].iframeCount = a;
                //console.log("/// NBC PLAYER MAKER | " + window['nbcLMP377315121'].scriptTagsOnPage[a].parentNode.id);
                window['nbcLMP377315121'].useThisAsParent = window['nbcLMP377315121'].scriptTagsOnPage[a].parentNode;

            }
    };


    // Do some splits...
    window['nbcLMP377315121'].scriptSplit = window['nbcLMP377315121'].thisScriptSource.split('/portableplayer/');
    window['nbcLMP377315121'].scriptParams = window['nbcLMP377315121'].scriptSplit[1].split('?');
    window['nbcLMP377315121'].discreteParams = window['nbcLMP377315121'].scriptParams[1].split('&');

    // Now dig through the parameters for the origin

    function getParams(requestedParam) {
        for(var q=0; q<window['nbcLMP377315121'].discreteParams.length; q++) {
            var parsedParam = [];
            if(window['nbcLMP377315121'].discreteParams[q].match(requestedParam)) {
                parsedParam = window['nbcLMP377315121'].discreteParams[q].split('=');
                break;
            }
        }
        return ( parsedParam.length > 0 ) ? parsedParam[1] : false;
    }

    window['nbcLMP377315121'].origin = getParams('origin');

    //console.log("/// NBC PLAYER MAKER PARAMETERS | "+ window['nbcLMP377315121'].scriptParams[1]);
    //console.log('/// NBC PLAYER MAKER IFRAME PREVIEW... | <iframe id="window['nbcLMP377315121']layer" width="X" height="Y" src="https://www.'+ window['nbcLMP377315121'].origin + '/templates/nbc_partner_player?'+window['nbcLMP377315121'].scriptParams[1] + "&turl="+location.origin+location.pathname+'"></iframe>');

    // DOM element creation
    var setFullWidth = getParams('fullWidth');
    var setWidth = getParams('width');
    var setHeight = getParams('height');
    window['nbcLMP377315121'].videoframe = document.createElement('iframe');
    window['nbcLMP377315121'].videoframe.style.border = 'none';
    if(setFullWidth){
        window['nbcLMP377315121'].videoframe.style.height = '100%';
    }
    window['nbcLMP377315121'].videoframe.width = (setFullWidth) ? "100%" : (setWidth !=="full") ? setWidth : "100%";
    window['nbcLMP377315121'].videoframe.height = (setFullWidth) ? "100%" : (setHeight !=="full") ? setHeight : "100%";
    window['nbcLMP377315121'].videoframe.scrolling = "no";
    window['nbcLMP377315121'].videoframe.id = "nbcLMP377315121" + window['nbcLMP377315121'].iframeCount;
    window['nbcLMP377315121'].videoframe.setAttribute('allowfullscreen', 'true');
    window['nbcLMP377315121'].videoframe.setAttribute('webkitallowfullscreen', 'true');
    window['nbcLMP377315121'].videoframe.setAttribute('mozallowfullscreen', 'true');

if(setFullWidth) {
window['nbcLMP377315121'].videoframe.src = protocol+"//www." + window['nbcLMP377315121'].origin + "/templates/nbc_partner_player?"+window['nbcLMP377315121'].scriptParams[1] + "&turl="+window['nbcLMP377315121'].encodedURI + "&ourl=" + window['nbcLMP377315121'].encodedHost + "&lp=5&fullWidth=y" + "&random=" + Math.random().toString(36).substring(7);
} else {
window['nbcLMP377315121'].videoframe.src = protocol+"//www." + window['nbcLMP377315121'].origin + "/templates/nbc_partner_player?"+window['nbcLMP377315121'].scriptParams[1] + "&turl="+window['nbcLMP377315121'].encodedURI + "&ourl=" + window['nbcLMP377315121'].encodedHost + "&lp=1" + "&random=" + Math.random().toString(36).substring(7);
}

    window['nbcLMP377315121'].useThisAsParent.appendChild(window['nbcLMP377315121'].videoframe);
    //console.log("/// NBC PLAYER MAKER | COMPLETE!");
})();
