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