// External JavaScript API for sites to embed the CNN iReport player.
//
// Used by:
//  - CNN blogs (via Wordpress shortcode)
//      e.g. http://marquee.blogs.cnn.com/2010/12/27/teena-maries-death-shocks-fans-celebs/
//  - CNN iReport Awards (on nominee pages)
//      e.g. http://ireportawards.cnn.com/nominees/22
//
// Usage example for http://ireport.cnn.com/docs/DOC-504611:
//  <script type="text/javascript" src="http://i.cdn.turner.com/ireport/static/themes/custom/resources/api/embed.js?v=2"></script>
//  ...
//  <div id="example"></div>
//  <script type="text/javascript"
//      src="http://ireport.cnn.com/services/embed-player.jspa?document=504611&amp;domId=example"></script>
//

// Called by JSONP from embed-player.jspa
function irLoadPlayer(docID, domIdStr, numMediaItems) {
    var html;
    var innerId = irGetUniqueDomId("irPlayer-" + docID);
    if (numMediaItems < 1) {
        html =  '<div id="'+innerId+'" style="width:400px; height:300px; margin:0; border:0; padding:0; text-align:center; ' +
                        'background:#000; color:#BBB; font:12px/300px Arial,verdana,sans-serif;">' +
                    'This content is currently unavailable.' +
                '</div>';
    } else {
        var swf    = (numMediaItems > 1) ? "ireport_embed_tabs.swf" : "ireport_embed.swf";
        var player = (numMediaItems > 1) ? "embed_with_tabs" : "embed";
        var width  = 400;
        var height = (numMediaItems > 1) ? 330 : 300;
        var html;
        var useHTML5Player = /\b(iPad|iPod|iPhone)\b/.test(navigator.userAgent);
        if (!useHTML5Player)
            html =
            '<object id="'+innerId+'" width="'+width+'" height="'+height+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">' +
                '<param name="allowfullscreen" value="true" />' +
                '<param name="allowscriptaccess" value="always" />' +
                '<param name="movie" value="http://ireport.cnn.com/themes/custom/resources/cvplayer/' + swf +
                    '?player=' + player + '&amp;configPath=http://ireport.cnn.com&amp;' +
                    'playlistId=' + encodeURIComponent(docID) + '&amp;' +
                    'contentId='  + encodeURIComponent(docID) + '/0&amp;" />' +
                '<param name="bgcolor" value="#FFFFFF" />' +
                '<embed src="http://ireport.cnn.com/themes/custom/resources/cvplayer/' + swf +
                    '?player=' + player + '&amp;configPath=http://ireport.cnn.com&amp;' +
                    'playlistId=' + encodeURIComponent(docID) + '&amp;' +
                    'contentId='  + encodeURIComponent(docID) + '/0&amp;" ' +
                    'type="application/x-shockwave-flash" bgcolor="#FFFFFF" ' +
                    'allowfullscreen="true" allowscriptaccess="always" ' +
                    'width="'+width+'" height="'+height+'">' +
                '</embed>' +
            '</object>';
        else
            html=
            '<iframe ' +
                'src="http://ireport.cnn.com/themes/custom/resources/htmlplayer/iframe.html?' +
                    'docID=' + encodeURIComponent(docID) + '" ' +
                'style="width:'+width+'px; height:'+height+'px; background-color #000000; ' +
                'margin:0; padding:0; border:0; overflow:hidden">' +
            '</iframe>';

    }
    var div = document.getElementById(domIdStr);
    if (div) {
        div.innerHTML = html;
    }
}

function irGetUniqueDomId(baseId) {
    var domId = baseId;
    var suffix = new Date().valueOf();
    var idx = 0;
    
    var el = document.getElementById(domId);
    while (el && idx < 99 /*limit*/) {
        domId = baseId + '-' + (suffix + idx++);
        el = document.getElementById(domId);
    }
    return domId;
}

