﻿$(document).ready(function () {

    var playItem = 0;

    var myPlayList = [
		        { name: "Into The Wild", mp3: "music/intothewild.mp3", ogg: "music/intothewild.ogg" },
		        { name: "Good Man", mp3: "music/goodman.mp3", ogg: "music/goodman.ogg" },
		        { name: "City of Black and White", mp3: "music/blackandwhite.mp3", ogg: "music/blackandwhite.ogg" },
		        { name: "Hey, Soul Sister", mp3: "music/heysoulsister.mp3", ogg: "music/heysoulsister.ogg" },
		        { name: "Dog Days Are Over", mp3: "music/dogdaysareover.mp3", ogg: "music/dogdaysareover.ogg" },
		        { name: "Stop the Dams", mp3: "music/stopthedams.mp3", ogg: "music/stopthedams.ogg" },
		        { name: "Catch The Wind", mp3: "music/catchthewind.mp3", ogg: "music/catchthewind.ogg" },
		        { name: "Rosi", mp3: "music/rosi.mp3", ogg: "music/rosi.ogg" },
		        { name: "Only Living Boy in New York", mp3: "music/onlylivingboy.mp3", ogg: "music/onlylivingboy.ogg" },
		        { name: "New Slang", mp3: "music/newslang.mp3", ogg: "music/newslang.ogg" },
		        { name: "Anyone Else But You", mp3: "music/anyoneelsebutyou.mp3", ogg: "music/anyoneelsebutyou.ogg" },
		        { name: "Chinese Translation", mp3: "music/chinesetranslation.mp3", ogg: "music/chinesetranslation.ogg" },
		        { name: "Meiko", mp3: "music/meiko.mp3", ogg: "music/meiko.ogg" },
		        { name: "'Til It Shines", mp3: "music/tillitshines.mp3", ogg: "music/tillitshines.ogg" },
		        { name: "When The Deal Goes Down", mp3: "music/whenthedealgoesdown.mp3", ogg: "music/whenthedealgoesdown.ogg" },
		        { name: "Pink Moon", mp3: "music/pinkmoon.mp3", ogg: "music/pinkmoon.ogg" },
		        { name: "If You Want To Sing Out", mp3: "music/singout.mp3", ogg: "music/singout.ogg" },
		        { name: "Wild Horses", mp3: "music/wildhorses.mp3", ogg: "music/wildhorses.ogg" },
		        { name: "Last Leaf", mp3: "music/lastleaf.mp3", ogg: "music/lastleaf.ogg" },
		        { name: "Somewhere Over the Rainbow", mp3: "music/somewhereovertherainbow.mp3", ogg: "music/somewhereovertherainbow.ogg" }
	        ];

    $("#jquery_jplayer").jPlayer({
        ready: function () {
            playListInit(false); // Parameter is a boolean for autoplay.
        },
        swfPath: "music",
        preload: "auto",
        volume: 25,
        oggSupport: true
    })
	.jPlayer("onSoundComplete", function () {
	    playListNext();
	});

    $("#music_prev").click(function () { playListPrev(); }); // Listen for Previous Track button click
    $("#music_next").click(function () { playListNext(); }); // Listen for Next Track button click

    function playListInit(autoplay) {
        if (autoplay) {
            playListChange(playItem);
        } else {
            playListConfig(playItem);
        }
    }

    function playListConfig(index) {
        playItem = index;
        $("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
    }

    function playListChange(index) {
        playListConfig(index);
        $("#jquery_jplayer").jPlayer("play");
    }

    function playListNext() {
        if (MusicIsPlaying()) {
            var index = (playItem + 1 < myPlayList.length) ? playItem + 1 : 0;
            playListChange(index);
        }
    }

    function playListPrev() {
        if (MusicIsPlaying()) {
            var index = (playItem - 1 >= 0) ? playItem - 1 : playList.length - 1;
            playListChange(index);
        }
    }

    function playTrack(t, n) {
        $("#jquery_jplayer").jPlayer("setFile", t).jPlayer("play");

        return false;
    }

});

function ToggleMusic(obj) {
    if (obj.src.indexOf('images/music_on.gif', 0) > -1) {
        obj.src = 'images/music_off.gif';
        $("#jquery_jplayer").jPlayer("play");
        return false;
    }
    else {
        obj.src = 'images/music_on.gif';
        $("#jquery_jplayer").jPlayer("pause");
        return false;
    }
}

function TogglePrevNext(id, state) {
    // Only do rollover if music is playing
    if (MusicIsPlaying()) {
        obj = document.getElementById(id);
        obj.src = "images/" + id + "_" + state + ".gif";
    }
}

function MusicIsPlaying() {
    return document.getElementById('music_onoff').src.indexOf('images/music_off.gif', 0) > -1;
}


