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