﻿$(function () {
    var playItem = 0;

    // Path to song samples
    var pathMC = "Media/MerryChristmas/"
    var pathBTB = "Media/BackToBoston/"
    var pathAFY = "Media/AllForYou/"
    var pathNC = "Media/NewCreation/"

    // Path to Album Art
    var aArtBTB = "Images/albumArt-BackToBoston.jpg"
    var aArtMC = "Images/albumArt-MerryChristmas.jpg"
    var aArtAFY = "Images/albumArt-AllForYou.jpg"
    var aArtNC = "Images/albumArt-NewCreation.jpg"

    // Arrays containing all playlist information
    var playlistBackToBoston = [
    { name: "Back To Boston", mp3: pathBTB + "01_Back_To_Boston_clip_125-165_hifi.mp3", aArt: aArtBTB },
    { name: "Through A Child's Eyes", mp3: pathBTB + "02_Through_A_Child_s_Eyes_clip_41-93_hifi.mp3", aArt: aArtBTB },
    { name: "When I'm With You", mp3: pathBTB + "03_When_I_m_With_You_clip_115-166_hifi.mp3", aArt: aArtBTB },
    { name: "Break The Mold", mp3: pathBTB + "04_Break_The_Mold_clip_0-65_hifi.mp3", aArt: aArtBTB },
    { name: "How Can I Go On", mp3: pathBTB + "05_How_Can_I_Go_On_clip_123-170_hifi.mp3", aArt: aArtBTB },
    { name: "I Will Rise", mp3: pathBTB + "06_I_Will_Rise_clip_142-198_hifi.mp3", aArt: aArtBTB },
    { name: "The Rain Came Down", mp3: pathBTB + "07_The_Rain_Came_Down_clip_95-137_hifi.mp3", aArt: aArtBTB },
    { name: "Real Good Thing", mp3: pathBTB + "08_Real_Good_Thing_clip_0-78_hifi.mp3", aArt: aArtBTB },
    { name: "Stay Here A Little While", mp3: pathBTB + "09_Stay_Here_A_Little_While_clip_91-145_hifi.mp3", aArt: aArtBTB },
    { name: "This Old House", mp3: pathBTB + "10_This_Old_House_clip_127-183_hifi.mp3", aArt: aArtBTB }
    ];
    var playlistAllForYou = [
    { name: "Intro", mp3: pathAFY + "MATT_GOSS_BAND-Intro.mp3", aArt: aArtAFY },
    { name: "Never Be The Same Again", mp3: pathAFY + "MATT_GOSS_BAND-Never_Be_The-clip-0-60.mp3", aArt: aArtAFY },
    { name: "Right Now", mp3: pathAFY + "MATT_GOSS_BAND-Right_Now-clip-0-60.mp3", aArt: aArtAFY },
    { name: "Coming Back", mp3: pathAFY + "MATT_GOSS_BAND-Coming_Back_clip_0-60_hifi.mp3", aArt: aArtAFY },
    { name: "We Are Hungry", mp3: pathAFY + "MATT_GOSS_BAND-We_Are_Hungry-clip-0-60.mp3", aArt: aArtAFY },
    { name: "All For You", mp3: pathAFY + "MATT_GOSS_BAND-All_For_You-clip-90-170.mp3", aArt: aArtAFY },
    { name: "I Know", mp3: pathAFY + "MATT_GOSS_BAND-I_Know-clip-0-60.mp3", aArt: aArtAFY },
    { name: "Big God, Great God", mp3: pathAFY + "MATT_GOSS_BAND-Big_God_Great-clip-0-60.mp3", aArt: aArtAFY },
    { name: "What It's Like", mp3: pathAFY + "MATT_GOSS_BAND-What_Its_Like-clip-0-60.mp3", aArt: aArtAFY },
    { name: "In Your Hands", mp3: pathAFY + "MATT_GOSS_BAND-In_Your_Hands-clip-0-60.mp3", aArt: aArtAFY },
    { name: "To Know You", mp3: pathAFY + "MATT_GOSS_BAND-To_Know_You-clip-0-60.mp3", aArt: aArtAFY },
    { name: "Take My Life And Let It Be", mp3: pathAFY + "MATT_GOSS_BAND-Take_My_Life_-clip-0-90.mp3", aArt: aArtAFY }
    ];
    var playlistNewCreation = [
    { name: "Crying Out", mp3: pathNC + "MATT_GOSS-Crying_Out-3-clip-0-60.mp3", aArt: aArtNC },
    { name: "I'm Here To Praise", mp3: pathNC + "MATT_GOSS-Im_Here_To-clip-0-60.mp3", aArt: aArtNC },
    { name: "New Creation", mp3: pathNC + "MATT_GOSS-New_Creation-clip-0-60.mp3", aArt: aArtNC },
    { name: "Kneel", mp3: pathNC + "MATT_GOSS-Kneel-clip-0-60.mp3", aArt: aArtNC },
    { name: "They That Wait", mp3: pathNC + "MATT_GOSS-They_That_Wait-clip-0-60.mp3", aArt: aArtNC },
    { name: "More Of You", mp3: pathNC + "MATT_GOSS-More_Of_You-clip-0-60.mp3", aArt: aArtNC },
    { name: "Jesus Is", mp3: pathNC + "MATT_GOSS-Jesus_Is-clip-0-60.mp3", aArt: aArtNC },
    { name: "Falling In Love", mp3: pathNC + "MATT_GOSS-Falling_In_Love-clip-0-60.mp3", aArt: aArtNC },
    { name: "No Equal", mp3: pathNC + "MATT_GOSS-No_Equal-clip-0-60.mp3", aArt: aArtNC },
    { name: "My Friend", mp3: pathNC + "MATT_GOSS-My_Friend-clip-0-60.mp3", aArt: aArtNC },
    { name: "This Promise", mp3: pathNC + "MATT_GOSS-This_Promise-clip-0-60.mp3", aArt: aArtNC }
    ];
    var playlistMerryChristmas = [
    { name: "Winter Wonderland", mp3: pathMC + "1CN_Records_sample1.mp3", aArt: aArtMC },
    { name: "Silent Night", mp3: pathMC + "1CN_Records_sample2.mp3", aArt: aArtMC },
    { name: "The Christmas Song", mp3: pathMC + "1CN_Records_sample3.mp3", aArt: aArtMC },
    { name: "Joy To The World", mp3: pathMC + "1CN_Records_sample4.mp3", aArt: aArtMC },
    { name: "White Christmas", mp3: pathMC + "1CN_Records_sample5.mp3", aArt: aArtMC },
    { name: "It Came Upon A Midnight Clear", mp3: pathMC + "1CN_Records_sample6.mp3", aArt: aArtMC },
    { name: "Angels We Have Heard on High", mp3: pathMC + "1CN_Records_sample7.mp3", aArt: aArtMC },
    { name: "Christmas Time Is Here", mp3: pathMC + "1CN_Records_sample8.mp3", aArt: aArtMC }
    ];

    // Combined array to make every song part of the overall playlist
    var playlist = playlistBackToBoston.concat(playlistAllForYou, playlistNewCreation, playlistMerryChristmas);

    // Instantiate the jPlayer plugin
    $('#jQuery_jPlayer').jPlayer({
        ready: function () {
            // Generate the playlists from the above arrays for each individual album
            createPlaylist();
        },
        swfPath: "Scripts/jPlayer",
        volume: 40
    });

    // "onSoundComplte" moves to next song after the most previous song is done playing
    $('#jQuery_jPlayer').jPlayer("onSoundComplete", function () {
        nextSong();
    });

    // Play controls on site.Master "Play" button
    $('li#jplayer_play').click(function () {
        playSong();
    });

    // Return to last song
    $('li#jplayer_prev').click(function () {
        prevSong();
    });

    // Go to the next song in the playlist
    $('li#jplayer_next').click(function () {
        nextSong();
    });

    // Pause song and change classes from "playing" to "Paused"
    $('li#jplayer_pause').click(function () {
        pauseSong();
    });

    // Stop the song and clear all playing/paused classes from the song list.
    $('li#jplayer_stop').click(function () {
        stopSong();
    });
    // Begin function to create playlists
    function createPlaylist() {
        var i = 0;
        for (i; i < playlistBackToBoston.length; i++) {
            $('div#album-btb .playlist ul').append("<li id='item_" + i + "'>" + playlistBackToBoston[i].name + "</li><hr />");
            $('li#item_' + i).data("index", i).click(function () {
                var index = $(this).data("index");
                var me = $(this).data();
                songControls(index, me);
            });
        }
        for (m = 0; m < playlistAllForYou.length; m++) {
            $('div#album-afy .playlist ul').append("<li id='item_" + i + "'>" + playlistAllForYou[m].name + "</li><hr />");
            $('li#item_' + i).data("index", i).click(function () {
                var index = $(this).data("index");
                var me = $(this).data();
                songControls(index, me);
            });
            i++;    // add 1 to variable "i" so the next <li>'s ID does not match another ID on the page
        }
        for (l = 0; l < playlistNewCreation.length; l++) {
            $('div#album-nc .playlist ul').append("<li id='item_" + i + "'>" + playlistNewCreation[l].name + "</li><hr />");
            $('li#item_' + i).data("index", i).click(function () {
                var index = $(this).data("index");
                var me = $(this).data();
                songControls(index, me);
            });
            i++;    // add 1 to variable "i" so the next <li>'s ID does not match another ID on the page
        }
        for (c = 0; c < playlistMerryChristmas.length; c++) {
            $('div#album-christmas .playlist ul').append("<li id='item_" + i + "'>" + playlistMerryChristmas[c].name + "</li><hr />");
            $('li#item_' + i).data("index", i).click(function () {
                var index = $(this).data("index");
                var me = $(this).data();
                songControls(index, me);
            });
            i++;
        }
    }
    function playSong() {   //Play the selected song
        if ($('div.playlist ul li').hasClass("paused")) {
            $('.paused').addClass('playing').removeClass('paused');

        } else {
            $('div.playlist ul li').removeClass("playing");
            $('#jQuery_jPlayer').jPlayer("setFile", playlist[playItem].mp3);
        }
        $('#jQuery_jPlayer').jPlayer('play');
        $('#item_' + playItem).addClass('playing');
        $('#song_playing').text(playlist[playItem].name);
        $('div.header-aArt').html("<img src='" + playlist[playItem].aArt + "' alt='' height='95px' width='95px' />");
    }
    function pauseSong() {  //Pause the selected song
        $('#item_' + playItem).addClass('paused').removeClass('playing');
        $('#jQuery_jPlayer').jPlayer('pause');
    }
    function stopSong() {  // Stop current song
        $('div.playlist ul li').removeClass("playing").removeClass('paused');
        $('#jQuery_jPlayer').jPlayer('stop');
    }
    function prevSong() {
        if (playItem > 0) {
            playItem = playItem - 1
        } else {
            playItem = playlist.length - 1
        }
        playSong(playItem);
    }
    function nextSong() {
        playItem++;
        if (playItem > playlist.length - 1) {
            playItem = 0;
        }
        playSong(playItem);
    }
    function songControls(index, me) {
        if (playItem != index) {
            playItem = index;
        }
        if ($('#item_' + index).hasClass('playing')) {  //If user clicks a song that is already playing, it will pause
            pauseSong();
        } else {                            //Otherwise, the song will start playing
            playSong(playItem);
        }
    }
});
