var PlayerUtils = {
    currentVideo: null,
    videoPlayer: null,
    experience: null,
    content: null,
    social: null,
    get_current_video_link: function () {
        var site_url = getSiteHost();
        if (this.currentVideo.lineupId) {
            return site_url + '?bclid=' + this.currentVideo.lineupId + '&bctid=' + this.currentVideo.id;
        } else {
            return site_url + '?bctid=' + this.currentVideo.id;
        }
    },
    get_current_video_title: function() {
        return this.currentVideo.displayName;
    },
    load_video: function(video_id) {
        /*TODO: Integrate with combo brightcove function to select proper video*/
        this.videoPlayer.loadVideo(video_id);
        var list = this.experience.getElementByID("videoList");
        list.setSelectedIndex(-1);
    },
    show_current_video_info: function() {
        if (this.currentVideo) {
            new Ajax.Request("/videos/" + this.currentVideo.id + "/show_info", {
                method: 'get'
            });
        }
    },
    show_current_video_tags: function() {
        if (this.currentVideo) {
            // Create paragraph container
            var p = new Element('p').update("STICHWORTE: ");
            // Create a link for each tag
            this.currentVideo.tags.each(function(tag){
                var a = new Element('a', { href: '/tags/' + encodeURIComponent(tag) + '/videos', onclick: "return hs.htmlExpand(this, {objectType:  'ajax'});" }).update(tag);
                p.insert(a, { position: 'bottom' });
                 if (!(PlayerUtils.currentVideo.tags.last() == tag)) {
                    p.insert(', ', { position: 'bottom' });
                }
            });
            // Remove all previous tags and apply new ones
            $("tag_body").childElements()[0].remove();
            $("tag_body").appendChild(p);

            // Since we're adding higshslide enabled links, we need to execute the preloadAjax function again.
            hs.updateAnchors();
        }
    },
    sendCurrentVideoTrackingInfo: function(eventName) {
        var experienceId = this.experience.getExperienceID();
        var action = ""
        switch(eventName) {
        case "templateLoaded":
            var experienceURL = this.experience.getExperienceURL();
            var referrerURL = this.experience.getReferrerURL();
            action = "playerid=" + experienceId + "/url=" + experienceURL
                     + "/refurl=" + referrerURL + "/" + eventName;
            break;
        default:
            if (this.currentVideo) {
                var playlistId = this.currentVideo.lineupId ? this.currentVideo.lineupId : 'none';
                var videoId = this.currentVideo.id;
                action = this.currentVideo.displayName + " (event=" + eventName + "/playerid=" + experienceId + "/plid=" + playlistId
                    + "/vid=" + videoId + ")"
            }

        }
    },
    templateLoaded: function(){
        var array = $$('.delay_load');
        array.each(function(el) {
            el.className = "";
        });
    },
    updateLink: function() {
        var videoId = PlayerUtils.videoPlayer.getCurrentVideo().id;
        var playlistId = PlayerUtils.experience.getElementByID("playlistCombo").getSelectedData().id;
        var playlistKey = "bclid";
        var videoKey = "bctid";
        var currentLink = PlayerUtils.social.getLink();

        if(currentLink.indexOf("?") != -1) {
            currentLink = currentLink.substring(0, currentLink.indexOf("?"));
        }
        var newLink = currentLink + "?" + playlistKey + "=" + playlistId + "&" + videoKey + "=" + videoId;
        PlayerUtils.social.setLink(newLink);
    }

};
