function fullScreen(jPlayerId) {
    $("#" + jPlayerId).jPlayer("resize", {
        width: "100%",
        height: "100%"
    });

}

var iCurrentPlaylist;
var oCurrentAllPlaylistsObject = null;
var isVideo = true; // to check if its the video or audio page

function loadFirstVideoPlaylist() {
    isVideo = true;
    iCurrentPlaylist = 0;
    oCurrentAllPlaylistsObject = oAllVideoPlaylists;
    initiateJPlayerVideo("1", oCurrentAllPlaylistsObject[iCurrentPlaylist], false);
}

function loadFirstAudioPlaylist() {
    isVideo = false;
    iCurrentPlaylist = 0;
    oCurrentAllPlaylistsObject = oAllAudioPlaylists;
    initiateJPlayerAudio("1", oPlaylistArrAudioAction, false);
}

function nextPlaylist() {
    var iNext = (iCurrentPlaylist + 1 < oCurrentAllPlaylistsObject.length) ? iCurrentPlaylist + 1 : 0;
    SwitchPlaylist("1", iNext, true);
}

function previousPlaylist() {
    var iNext = (iCurrentPlaylist - 1 >= 0) ? iCurrentPlaylist - 1 : oCurrentAllPlaylistsObject.length - 1;
    SwitchPlaylist("1", iNext, true);
}

function SwitchPlaylist(instanceNum, iPlaylistIndex, bAutoplay) {
    // set the current index
    iCurrentPlaylist = iPlaylistIndex;
    // initiate a new playlist and set tabs
    if (isVideo) {
        thePlaylist.resetPlaylist(oCurrentAllPlaylistsObject[iCurrentPlaylist], bAutoplay);
        SetVideoTabOn(iCurrentPlaylist);
    }
    else {
        thePlaylist.resetPlaylist(oCurrentAllPlaylistsObject[iCurrentPlaylist], bAutoplay);
        SetAudioTabOn(iCurrentPlaylist)
    }
}

function SetVideoTabOn(iTabIndex) {
    // first set all to normal
    $("#tabFilm").attr("class", "tab-Film");
    $("#tabTV").attr("class", "tab-TV");
    $("#tabCommercial").attr("class", "tab-Commercial");

    // set tab to on
    switch (iTabIndex) {
        case 0:
            $("#tabFilm").attr("class", "tab-Film-on");
            break;
        case 1:
            $("#tabTV").attr("class", "tab-TV-on");
            break;
        case 2:
            $("#tabCommercial").attr("class", "tab-Commercial-on");
            break;
    }
}

function SetAudioTabOn(iTabIndex) {
    // first set all to normal
    $("#tabAction").attr("class", "tab-Action");
    $("#tabDrama").attr("class", "tab-Drama");
    $("#tabRomantic").attr("class", "tab-Romantic");

    // set tab to on
    switch (iTabIndex) {
        case 0:
            $("#tabAction").attr("class", "tab-Action-on");
            break;
        case 1:
            $("#tabDrama").attr("class", "tab-Drama-on");
            break;
        case 2:
            $("#tabRomantic").attr("class", "tab-Romantic-on");
            break;
    }
}

var Playlist = function (instance, playlist, options) {
    var self = this;

    this.instance = instance; // String: To associate specific HTML with this playlist
    this.playlist = playlist; // Array of Objects: The playlist
    this.options = options; // Object: The jPlayer constructor options for this playlist

    this.current = 0;

    this.cssId = {
        jPlayer: "jquery_jplayer_",
        interface: "jp_interface_",
        playlist: "jp_playlist_"
    };
    this.cssSelector = {};

    $.each(this.cssId, function (entity, id) {
        self.cssSelector[entity] = "#" + id + self.instance;
    });

    if (!this.options.cssSelectorAncestor) {
        this.options.cssSelectorAncestor = this.cssSelector.interface;
    }

    $(this.cssSelector.jPlayer).jPlayer(this.options);

    $(this.cssSelector.interface + " .jp-previous").click(function () {
        self.playlistPrev();
        $(this).blur();
        return false;
    });

    $(this.cssSelector.interface + " .jp-next").click(function () {
        self.playlistNext();
        $(this).blur();
        return false;
    });
};

Playlist.prototype = {
    displayPlaylist: function () {
        var self = this;
        $(this.cssSelector.playlist + " ul").empty();
        for (i = 0; i < this.playlist.length; i++) {
            var listItem = (i === this.playlist.length - 1) ? "<li class='jp-playlist-last'>" : "<li>";
            listItem += "<a href='#' id='" + this.cssId.playlist + this.instance + "_item_" + i + "' tabindex='1'>" + this.playlist[i].name + "</a>";

            // Create links to free media
            if (this.playlist[i].free) {
                var first = true;
                listItem += "<div class='jp-free-media'>(";
                $.each(this.playlist[i], function (property, value) {
                    if ($.jPlayer.prototype.format[property]) { // Check property is a media format.
                        if (first) {
                            first = false;
                        } else {
                            listItem += " | ";
                        }
                        listItem += "<a id='" + self.cssId.playlist + self.instance + "_item_" + i + "_" + property + "' href='" + value + "' tabindex='1'>" + property + "</a>";
                    }
                });
                listItem += ")</span>";
            }

            listItem += "</li>";

            // Associate playlist items with their media
            $(this.cssSelector.playlist + " ul").append(listItem);
            $(this.cssSelector.playlist + "_item_" + i).data("index", i).click(function () {
                var index = $(this).data("index");
                if (self.current !== index) {
                    self.playlistChange(index);
                } else {
                    $(self.cssSelector.jPlayer).jPlayer("play");
                }
                $(this).blur();
                return false;
            });

            // Disable free media links to force access via right click
            if (this.playlist[i].free) {
                $.each(this.playlist[i], function (property, value) {
                    if ($.jPlayer.prototype.format[property]) { // Check property is a media format.
                        $(self.cssSelector.playlist + "_item_" + i + "_" + property).data("index", i).click(function () {
                            var index = $(this).data("index");
                            $(self.cssSelector.playlist + "_item_" + index).click();
                            $(this).blur();
                            return false;
                        });
                    }
                });
            }
        }
    },
    playlistInit: function (autoplay) {
        if (autoplay) {
            this.playlistChange(this.current);
        } else {
            this.playlistConfig(this.current);
        }
    },
    playlistConfig: function (index) {
        $(this.cssSelector.playlist + "_item_" + this.current).removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
        $(this.cssSelector.playlist + "_item_" + index).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
        this.current = index;
        $(this.cssSelector.jPlayer).jPlayer("setMedia", this.playlist[this.current]);
    },
    playlistChange: function (index) {
        this.playlistConfig(index);
        $(this.cssSelector.jPlayer).jPlayer("play");
    },
    playlistNext: function () {
        var index = (this.current + 1 < this.playlist.length) ? this.current + 1 : null;
        if (index == null) {
            nextPlaylist();
        }
        else {
            this.playlistChange(index);
        }
    },
    playlistPrev: function () {
        var index = (this.current - 1 >= 0) ? this.current - 1 : null;
        if (index == null) {
            previousPlaylist();
        }
        else {
            this.playlistChange(index);
        }
    },
    resetPlaylist: function (oPlaylist, bAutoplay) {
        this.current = 0;
        this.playlist = oPlaylist;
        this.displayPlaylist();
        this.playlistInit(bAutoplay); // Parameter is a boolean for autoplay.
    }
};

var thePlaylist = null;
function initiateJPlayerVideo(instanceNum, oPlaylistArr, bAutoplay) {
    thePlaylist = new Playlist(instanceNum, oPlaylistArr, {
	    ready: function () {
	        thePlaylist.displayPlaylist();
	        thePlaylist.playlistInit(bAutoplay); // Parameter is a boolean for autoplay.
	    },
	    ended: function () {
	        thePlaylist.playlistNext();
	    },
	    play: function () {
	        $(this).jPlayer("pauseOthers");
	    },
	    supplied: "m4v",//"ogv, m4v",
		solution: "flash, html",
	    swfPath: "jPlayer"
	});
}

function initiateJPlayerAudio(instanceNum, oPlaylistArr, bAutoplay) {
    thePlaylist = new Playlist(instanceNum, oPlaylistArr, {
        ready: function () {
            thePlaylist.displayPlaylist();
            thePlaylist.playlistInit(bAutoplay); // Parameter is a boolean for autoplay.
        },
        ended: function () {
            thePlaylist.playlistNext();
        },
        play: function () {
            $(this).jPlayer("pauseOthers");
        },
        supplied: "mp3",
		solution: "flash, html",
        swfPath: "jPlayer"
    });
}
