/**
 * @version 1.2
 * @modified by Yufang Chang
 * @project Description
 * Brightcove 3 player
 */

function onTemplateLoaded(experienceID) {
    CN.brightcove.player.initTemplate(experienceID);
}

CN.brightcove = CN.brightcove || {};

CN.brightcove.player = (function($) {
    var playerContainer = 'brightcovePlayer',
        vContainer = "videoscreen",
        publisherID = "1569970201",
        currentPage = document.location.href,
        host = "http://" + document.location.host + "/video",
        bcExp,
        modVP,
        modExp,
        modCon,
        modSoc,
        modMenu,
        widthSingle = CN.brightcove.singlewidth || 486,
        heightSingle = CN.brightcove.singleheight || 455,
        widthMulti = CN.brightcove.channelwidth || 798,
        heightMulti = CN.brightcove.channelheight || 603,
        featuredPlayList,
        player = {},


        displayPlayer = function(lineupID) {
           var config = {};

           if (player.pid) {
                var videoConfig = {
                  bgcolor: "#FFFFFF",
                width: (player.type == 'multi') ? widthMulti : widthSingle,
                height: (player.type == 'multi') ? heightMulti : heightSingle,
                playerID: player.pid,
                publisherID: publisherID,
                wmode: "opaque",
                isVid: true,
                linkBaseURL: currentPage
               },
               objId = (player.type == 'multi') ? "myExperienceChannel" :"myExperience" + player.vid,
               videoContent = "";

               if (player.type == 'single') {
                videoContent += '<object id="' + objId + '" class="BrightcoveExperience"><param name="bgcolor" value="#FFFFFF" /><param name="width" value="' + videoConfig.width + '" /><param name="height" value="' + videoConfig.height + '" /><param name="playerID" value="' + player.pid + '" /><param name="publisherID" value="' + publisherID + '"/><param name="wmode" value="opaque" /><param name="isVid" value="true" /><param name="dynamicStreaming" value="true" /><param name="videoSmoothing" value="true" /><param name="@videoPlayer" value="' + player.vid + '" /><param name="dynamicStreaming" value="true" /><param name="videoSmoothing" value="true" /><param name="linkBaseURL" value="' + currentPage + '" /></object>';
               } else {
                videoContent = '<object id="'+objId+'" class="BrightcoveExperience"><param name="bgcolor" value="'+videoConfig.bgcolor+'" /><param name="width" value="' + videoConfig.width + '" /><param name="height" value="' + videoConfig.height + '" /><param name="playerID" value="' + player.pid + '" /><param name="publisherID" value="' + publisherID + '"/><param name="wmode" value="opaque" /><param name="isVid" value="true" /><param name="isUI" value="true" /><param name="linkBaseURL" value="' + currentPage + '" /><param name="videoSmoothing" value="true" />';
                if (lineupID != '') {videoContent += '<param name="@playlistTabs.featured" value="' + lineupID + '" />';}
                if (player.vid != '') {videoContent += '<param name="@videoList.featured" value="' + player.vid + '" />';}
                videoContent += '</object>';
               }

                $("#" + playerContainer).html(videoContent);
                if (CN.brightcove.mobile && player.type !== 'single') {
                   CN.brightcove.mobile.init(objId);
                }
                brightcove.createExperiences();
            }
        },

        getPlaylists = function(playerID) {
           var req = "http://api.brightcove.com/services/library?command=find_playlists_for_player_id&token=" + encodeURIComponent(CN.site.videoReadToken || "") + "&player_id=" + playerID + "&fields=videoIds,id,name&page_size=10&callback=?";
           $.getJSON(req,CN.brightcove.player.response);
        },

       onSetLink = function(evt) {
        modSoc.setLink((player.type == "multi") ? host + "?videoID=" + modVP.getCurrentVideo().id : currentPage);
       },


       onContentLoad = function(evt) {
           var currentVideo = modVP.getCurrentVideo();
           modCon.getVideoAsynch(currentVideo.id);
           onSetLink(null);
       }




    return {

          initTemplate: function(experienceID) {
           bcExp = brightcove.getExperience(experienceID);
           modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
           modExp = bcExp.getModule(APIModules.EXPERIENCE);
           modCon = bcExp.getModule(APIModules.CONTENT);
           modSoc = bcExp.getModule(APIModules.SOCIAL);
           modMenu = bcExp.getModule(APIModules.MENU);
           modExp.addEventListener(BCExperienceEvent.CONTENT_LOAD,onContentLoad);
           modCon.addEventListener(BCContentEvent.VIDEO_LOAD,onSetLink);
           modVP.addEventListener(BCVideoEvent.VIDEO_LOAD,onSetLink);
        },



        create: function(videoID, playerID, playerType, lineupID) {
         player = {
                vid : videoID || "",
                pid : playerID || "",
                type : playerType || "",
                lineupId : lineupID || ""
             }

             $("#" + ((player.type == "multi" && $('#' + vContainer).length == 0) ? vContainer+"-"+ player.pid : vContainer).toString()).append(
               $("<div/>")
                .attr('id',playerContainer)
             );

            if (player.type == 'multi' && player.vid != '' && CN.url.params("lineupID") == '') {
              getPlaylists(player.pid);
            } else {
              displayPlayer(player.lineupId);
            }


        },

      response: function(jsonData) {
          var items = jsonData["items"];
          for (var i = items.length - 1; i >= 0; i--) {
           for (var j = 0; j < items[i].videoIds.length; j++) {
                if (items[i].videoIds[j] == player.vid) {
                 featuredPlayList = items[i].id;
                }
           }
           }

           displayPlayer(featuredPlayList);

        },

        getPlayerType: function() {
           return player.type;
        }

    };
})(jQuery);

MAGNET.brightcove = CN.brightcove;





