//****************************************************************************
// Inline Player
// The Associated Press
// Revised 12/8/2008 by ctownley
//****************************************************************************

    // User settable variables
    var playerTitle = "Inline Player";
    var playerFParam = "";
    var playerCategory = "";
    //var playerPID = "YWC9kv7h8eVqaRiJNlzklv2KyXHbuaL5"; // Testing
    var playerPID = "PSlvHKIjOdolYsUhSjbpf7ROOkJP5EYR" // Production
    var playerAuto = "false";
    var directPID = "";
    var directOnly = "false";
    var isLocal = "false";
    
    // Required Player variables
    var pathSWF = "http://img.video.ap.org/inline/";
    var adPattern = "count:0|2"
    var playerURL = "http://release.theplatform.com/content.select";
    var feedURL = "http://ap.feeds.theplatform.com/ps/JSON/PortalService/2.2/getReleaseList?PID=";
    var redirectURL = "http://video.ap.org/";
    var debugMode = "true";
    var pidEntities = "oUFmCcyEZuCA7UqChCElKJFTJxFcgO9G";
    var affiliateCMA = "";
    var thisCommID = "";
    var thisPlayerID = "";
    var liveTitle = "";
    var liveDescription = "";
    var liveFeedURL = "http://ap.feeds.theplatform.com/ps/JSON/PortalService/2.2/getReleaseList?PID=VX36Dt2kyMm4oh2JDAZKacgMHCgS3CRP";

    function embedInlinePlayer(commID, playerID) 
    {
        thisCommID = commID;
        thisPlayerID = playerID;
        var entitiesURL = feedURL + pidEntities + "&field=thumbnailURL&field=contentCustomData&query=ContentCustomText|CustomerID|" + playerFParam + "&startIndex=1&endIndex=1";
        jsonpFeeds(entitiesURL);
    }
    
    function doEmbed(commID, playerID) {
        if (isLocal=="true") { playerCategory = ""; }
        
        // Embed Inline Player into the appropriate div
	    tpRegisterID("playerwidget");

	    var soP = new SWFObject(pathSWF + "swf/inlinePlayer.swf", "playerwidget", "302", "389", "9.0.0.0");
	    soP.addParam("quality", "high");
	    soP.addParam("scale", "noscale");
	    soP.addParam("allowScriptAccess", "always");
	    soP.addVariable("ID", "playerwidget");
	    soP.addVariable("commManagerID", tpGetCommManagerID());
	    soP.addVariable("instanceID", tpGetInstanceID());
	    soP.addVariable("useJS", tpGetUseJS());
	    soP.addParam("allowFullScreen", "true");
	    soP.addParam("bgcolor", "Transparent");
	    soP.addParam("wmode", "Transparent");
	    soP.addVariable("height", "389");
	    soP.addVariable("width", "302");
	    soP.addVariable("plugin0", "type=adcomponent|URL=" + pathSWF + "swf/SMIL.swf|priority=3");
	    soP.addVariable("playbackManagerURL", pathSWF + "swf/playback.swf");
	    soP.addVariable("parserURL", pathSWF + "swf/parser.swf");
	    soP.addVariable("inlineURL", pathSWF + "swf/inlineVideo.swf");
	    soP.addVariable("qosURL", pathSWF + "swf/qos.swf");
	    soP.addVariable("plugin2", "type=adcomponent|adcomponent|URL=" + pathSWF + "swf/msn.swf|priority=1|hosts=msnap.adbureau.net,rad.msn-int.com");
	    soP.addVariable("plugin3", "type=tracking|URL=" + pathSWF + "swf/googleAnalytics.swf|ID=UA-6058144-1|histograms=1|trackAds=false|pattern=ap/{title}//{playlist.player}/{histogram}");
	    soP.addVariable("fVal", playerFParam);
	    soP.addVariable("cmaVal", affiliateCMA);
	    soP.addVariable("title", playerTitle);
	    soP.addVariable("catID", playerCategory);
	    soP.addVariable("feedName", playerCategory);
	    soP.addVariable("feedURL", feedURL + playerPID);
	    soP.addVariable("liveFeedURL", liveFeedURL);
	    soP.addVariable("autoPlay", playerAuto);
	    soP.addVariable("showDebug", debugMode);
	    //soP.addVariable("adPattern", adPattern);
	    soP.addVariable("baseURL", playerURL);
	    if (directPID != null && directPID != "") { soP.addVariable("directPID", directPID); }
	    if (directOnly != null && directPID != "") { soP.addVariable("directOnly", directOnly); }
	    soP.addVariable("mainURL", redirectURL);
	    soP.addVariable("isLocalContent", isLocal);
	    soP.addVariable("liveTitle", liveTitle);
	    soP.addVariable("liveDescription", liveDescription);	
	    																															
	    soP.write(playerID);
	    
	     // Embed Comm Manager into the appropriate div
        tpSetCommManagerID("commmanagerwidget");
        
	    var soC = new SWFObject(pathSWF + "swf/commManager.swf", tpGetCommManagerID(), "1", "1", "9.0.0.0");
	    soC.addParam("allowScriptAccess", "always");
	    soC.addParam("wmode", "transparent");
	    soC.addVariable("ID", tpGetCommManagerID());
	    soC.addVariable("instanceID", tpGetInstanceID());
	    soC.addVariable("registeredIDs", tpGetRegisteredIDs());

    	soC.write("commmanagerDiv");
 }

 function jsonpFeeds(pURL) {

     var req = new JSONRequest(pURL, "onQueryAffiliateResult");
     req.send();
 }

 function onQueryAffiliateResult(pJSON) {
     if (pJSON.items.length > 0) {
         var affiliateInfo = pJSON.items[0];

         for (i = 0; i < affiliateInfo.contentCustomData.length; i++) {
             var aInfo = affiliateInfo.contentCustomData[i];
             switch (aInfo.title) {
                 case "CMAccountName":
                     affiliateCMA = aInfo.value;
                     break;
             }
         }
     }
     if (affiliateCMA == "") { affiliateCMA = playerFParam; }

     doEmbed(thisCommID, thisPlayerID);
 }
    
// Using the following values for testing purposes
//------------------------------------------------
//  prod PID: PSlvHKIjOdolYsUhSjbpf7ROOkJP5EYR
//  staging PID: YWC9kv7h8eVqaRiJNlzklv2KyXHbuaL5
//  prod Ad URL:type%3Dadcomponent%7Cadcomponent%7CURL%3Dswf/msn.swf%7Cpriority%3D1%7Chosts%3Dmsnap.adbureau.net%2Crad.msn-int.com
//  staging Ad URL: type%3Dadcomponent%7Cadcomponent%7CURL%3Dswf/msn.swf%7Cpriority%3D1%7Chosts%3Dmsnap.adbureau.net%2Crad.msn-int.com
 //------------------------------------------------

 // JSONRequest file...
 function JSONRequest(URL, Callback, Context) {
     // get the head tag
     var head = document.getElementsByTagName("head");

     if (!head) {
         alert("You need to define a <head> element to use the JSONRequest object.");
         return false;
     }
     this.headTag = head.item(0);

     // check the URL
     if (URL == null || URL.length == 0) {
         alert("You need to provide the URL.");
         return false;
     }

     this.url = URL;

     // add callback and/or context
     if (Callback != null && Callback.length > 0) {
         this.url += "&callback=" + escape(Callback);
     }

     if (Context != null && Context.length > 0) {
         this.url += "&context=" + escape(Context);
     }

     // determine the ID for this script
     this.scriptID = "JSONRequest" + JSONRequest.index++;
 }

 // a counter for ID references
 JSONRequest.index = 1;

 // send the request
 JSONRequest.prototype.send = function() {
     // Create a script element
     var scriptObj = document.createElement("script");

     // Set the attributes
     scriptObj.setAttribute("type", "text/javascript");
     scriptObj.setAttribute("src", this.url);
     scriptObj.setAttribute("id", this.scriptID);

     // Add it to the <head>; this will immediately run it
     this.headTag.appendChild(scriptObj);
 }