﻿/// <reference path="~/TopletsResources/js/jquery-vsdoc.js" />

// Video panels
var VideoTabs = {
    ChangeTab: function(event) {
        var $tab = jQuery(event.target).closest(".js-tab");
        var $content = jQuery("#" + $tab.attr("id") + "_content");
        $tab.siblings(".js-tab").removeClass("js-selected").end().addClass("js-selected");
        $content.siblings(".js-tab_content").removeClass("js-selected").end().addClass("js-selected");
    },
    Related: {
        pendingRequest: new Function(),
        refresh: function(keywords, excludeUID) {
            // fallback if updateRelated is not yet available
            if (typeof updateRelated === 'undefined') {
                this.pendingRequest = function() { this.refresh(keywords, excludeUID); }
                return;
            }
            var lucene = jQuery.map(keywords.split(','), function(tag) {
                if (!!tag) return 'keywords:"' + jQuery.trim(tag) + '"';
            }).join(' OR ');
            var exclude = (!!excludeUID) ? '&Exclude=' + excludeUID : '';
            var query = 'Query=' + encodeURIComponent(lucene) + exclude;
            updateRelated(query, false, query, function() {
                jQuery('a.playlist-button').trigger('update');
            });
        }
    },
    Playlist: {
        pendingRequest: new Function(),
        isPlaying: false,
        isRepeat: false,
        isInPlaylist: function(uid) {
            var uids = VideoTabs.Playlist.get();
            return $.inArray(uid, uids) > -1;
        },
        get: function() {
            var uids = TD.Framework.ReadCookie("videoplaylist");
            return (!uids) ? [] : uids.split(",");
        },
        clear: function() {
            // update cookie and panel
            TD.Framework.WriteCookie("videoplaylist", "", -1, window.location.hostname.replace("www", ""));
            VideoTabs.Playlist.refresh();
        },
        add: function(uid) {
            // update cookie and panel
            var uidsArray = VideoTabs.Playlist.get();
            if (uidsArray.length == 10) {
                Shadowbox.open({
                    player: 'html',
                    content: '<p class="td-popup_message">Maximum of 10 items in playlist has been reached.  Please remove item(s) from the playlist before adding anymore.</p>',
                    height: 80,
                    width: 250,
                    title: "Alert"
                });
                return;
            }
            uidsArray.push(uid);
            TD.Framework.WriteCookie("videoplaylist", uidsArray.join(","), null, window.location.hostname.replace("www", ""));
            VideoTabs.Playlist.refresh();
        },
        remove: function(uid) {
            // update cookie and panel
            var uidsArray = VideoTabs.Playlist.get();
            var uids = jQuery.grep(uidsArray, function(id) { if (typeof id !== "undefined") return id !== uid; }).join(',');
            TD.Framework.WriteCookie("videoplaylist", uids, null, window.location.hostname.replace("www", ""));
            // play the next video if the removing video is the one playing in playlist and there're video remaining in the playlist after removal
            if (VideoTabs.Playlist.isPlaying && VideoPlayer.Players.currentPlayer.metaData.uid === uid && !TD.Framework.ReadCookie("videoplaylist")) {
                VideoTabs.Playlist.next();
            }
            VideoTabs.Playlist.refresh();
        },
        refresh: function() {
            // fallback if updatePlayList is not yet available
            if (typeof updatePlayList === 'undefined') {
                this.pendingRequest = function() { this.refresh(); }
                setTimeout(function() {
                    jQuery('a.playlist-button').trigger('update');
                }, 20);
                return;
            }
            // update panel
            try {
                var uids = TD.Framework.ReadCookie("videoplaylist") || "";
                var query = "UIDs=" + uids;
                updatePlayList(query, false, query, function() {
                    // update buttons
                    setTimeout(function() {
                        jQuery('a.playlist-button').trigger('update');
                    }, 20);
                });
            } catch (e) {
                setTimeout(function() {
                    jQuery('a.playlist-button').trigger('update');
                }, 20);
            }
        },
        next: function() {
            var $nextVideoItem = jQuery("#js-playlist_tab_content .videoItem[uid='" + VideoPlayer.Players.currentPlayer.metaData.uid + "']").next(".videoItem");
            // Repeat the playlist if repeat is On and the current video is the last one in the playlist
            if (VideoTabs.Playlist.isRepeat && !$nextVideoItem.length) {
                $nextVideoItem = jQuery("#js-playlist_tab_content .videoItem:first");
            }

            if ($nextVideoItem.length) {
                VideoItemInfo.update($nextVideoItem.attr("uid"));
            } else {
                VideoTabs.Playlist.isPlaying = false;
            }
        },
        toggleRepeat: function() {
            VideoTabs.Playlist.isRepeat = !VideoTabs.Playlist.isRepeat;
            jQuery('#playlist-repeat').html("Repeat: " + (VideoTabs.Playlist.isRepeat ? 'On' : 'Off'));
        }
    },
    History: {
        pendingRequest: new Function(),
        get: function() {
            var uids = TD.Framework.ReadCookie("videohistory");
            return (!uids) ? [] : uids.split(",");
        },
        clear: function() {
            TD.Framework.WriteCookie("videohistory", "", -1, window.location.hostname.replace("www", ""));
            VideoTabs.History.refresh();
        },
        refresh: function() {
            // fallback if updatePlayList is not yet available
            if (typeof updateHistory === 'undefined') {
                this.pendingRequest = function() { this.refresh(); }
                return;
            }
            // refresh history tab
            try {
                var uids = TD.Framework.ReadCookie("videohistory") || "";
                var query = "UIDs=" + uids;
                updateHistory(query, false, query, function() {
                    setTimeout(function() {
                        jQuery('a.playlist-button').trigger('update');
                    }, 20);
                });
            } catch (e) { }
        },
        add: function(uid) {
            var uidsArray = jQuery.grep(VideoTabs.History.get().slice(0, 9), function(n) {
                if (n != uid) return n;
            });
            uidsArray.unshift(uid);
            uidsArray = jQuery.unique(uidsArray).slice(0, 10);
            TD.Framework.WriteCookie("videohistory", uidsArray.join(","), null, window.location.hostname.replace("www", ""));
            VideoTabs.History.refresh();
        }
    }
}

// playing play video
jQuery('#js-playlist_tab_content a.playUID').live('click.startPlaylist', function() {
    VideoTabs.Playlist.isPlaying = true;
});


