﻿function SetMusicProjectSelected() {
    $("#musicProjectList>li").each(function() {
        if (!$(this).hasClass("selected")) {
            $(this).animate({
                opacity: 0.5
            }, 500);
            $(this).css("border-color", backgroundColor);
        }
        else {
            $(this).css("border-color", borderColor);
        }
    });
}
function SetMusicDescription(index) {
    $("#musicProjectDescriptions>li").eq(index).show();
    $("#musicProjectImages>li").eq(index).show();
}
function illuminateLink() {
    var count = $("#musicProjectList>li").size();
    var random = Math.floor(Math.random() * count);
    var time = Math.floor(Math.random() * 1000) + 1000;

    $("#musicProjectList>li:not(.hovering):not(.selected)").animate({
        opacity: .5
    }, 1000);

    $("#musicProjectList>li").eq(random).animate({
        opacity: 1
    }, 1000);

    setTimeout("illuminateLink()", time);
}
$(document).ready(function() {
    //    $("#musicProjectList>li").eq(2).addClass("selected");
    SetMusicProjectSelected();
    $("#musicProjectDescriptions>li").hide();
    $("#musicProjectImages>li").hide();
    $("#musicProjectList>li").hover(function() {
        $(this).addClass("hovering");
        $(this).animate({
            opacity: 1
        }, 250);
    }, function() {
        $(this).removeClass("hovering");
        if (!$(this).hasClass("selected")) {
            $(this).animate({
                opacity: .5
            }, 500);
        }
    });
    setTimeout("illuminateLink()", 1000);
    $("#musicProjectList>li").click(function() {
        var index = $("#musicProjectList>li").index(this);
        if (!$(this).hasClass("selected")) {
            $("#musicProjectList>li").removeClass("selected");
            $(this).addClass("selected");
            SetMusicProjectSelected();
            $("#musicProjectDescriptions>li").hide();
            $("#musicProjectImages>li").hide();
            SetMusicDescription(index);
        }
        else {
            $(this).removeClass("selected");
            $(this).css("border-color", backgroundColor);
            $("#musicProjectDescriptions>li").hide();
            $("#musicProjectImages>li").hide();
        }
    });
    $(".jp-interface").css("background-color", randomColor);

    var playItem = 0;

    var myPlayList = [
		{ project: "+10 Mana", album: "Wizard Wars", name: "Once Upon a Time", mp3: "http://www.plus10mana.com/audio/01%20-%20Once%20Upon%20a%20Time.mp3" },
		{ project: "+10 Mana", album: "Wizard Wars", name: "Into the Forest", mp3: "http://www.plus10mana.com/audio/02%20-%20Into%20the%20Forest.mp3" },
		{ project: "+10 Mana", album: "Wizard Wars", name: "Spellweaver", mp3: "http://www.plus10mana.com/audio/03%20-%20Spellweaver.mp3" },
		{ project: "+10 Mana", album: "Wizard Wars", name: "Unicorns", mp3: "http://www.plus10mana.com/audio/04%20-%20Unicorns.mp3" },
		{ project: "+10 Mana", album: "Wizard Wars", name: "Elven Blood", mp3: "http://www.plus10mana.com/audio/05%20-%20Elven%20Blood.mp3" },
		{ project: "+10 Mana", album: "Wizard Wars", name: "Burning Sands", mp3: "http://www.plus10mana.com/audio/06%20-%20Burning%20Sands.mp3" },
		{ project: "+10 Mana", album: "Wizard Wars", name: "Sword Fight", mp3: "http://www.plus10mana.com/audio/07%20-%20Sword%20Fight.mp3" },
		{ project: "+10 Mana", album: "Wizard Wars", name: "Dead Orcs", mp3: "http://www.plus10mana.com/audio/08%20-%20Dead%20Orcs.mp3" },
		{ project: "+10 Mana", album: "Wizard Wars", name: "Fairydust", mp3: "http://www.plus10mana.com/audio/09%20-%20Fairydust.mp3" },
		{ project: "+10 Mana", album: "Wizard Wars", name: "A Village Saved", mp3: "http://www.plus10mana.com/audio/10%20-%20A%20Village%20Saved.mp3" }
	];

    playItem = Math.floor(Math.random() * myPlayList.length);

    // Local copy of jQuery selectors, for performance.
    var jpPlayTime = $("#jplayer_play_time");
    var jpTotalTime = $("#jplayer_total_time");
    var jpStatus = $("#demo_status"); // For displaying information about jPlayer's status in the demo page

    $("#jquery_jplayer").jPlayer({
        ready: function() {
            displayPlayList();
            playListInit(true); // Parameter is a boolean for autoplay.
            demoInstanceInfo(this.element, $("#demo_info")); // This displays information about jPlayer's configuration in the demo page
        },
        oggSupport: false
    })
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
	    jpPlayTime.text($.jPlayer.convertTime(playedTime));
	    jpTotalTime.text("/" + $.jPlayer.convertTime(totalTime));

	    demoStatusInfo(this.element, jpStatus); // This displays information about jPlayer's status in the demo page
	})
	.jPlayer("onSoundComplete", function() {
	    playListNext();
	});

    $("#jplayer_previous").click(function() {
        playListPrev();
        return false;
    });

    $("#jplayer_next").click(function() {
        playListNext();
        return false;
    });

    function displayPlayList() {
        for (i = 0; i < myPlayList.length; i++) {
            $("#jplayer_playlist table").append("<tr id='jplayer_playlist_item_" + i + "'><td>" + myPlayList[i].name + "</td><td>" + myPlayList[i].album + "</td><td>" + myPlayList[i].project + "</td><td><a href='" + myPlayList[i].mp3 + "'>Link</a></td></tr>");
            $("#jplayer_playlist_item_" + i).data("index", i).click(function() {
                var index = $(this).data("index");
                if (playItem != index) {
                    playListChange(index);
                } else {
                    $("#jquery_jplayer").jPlayer("play");
                }
            });
        }
    }

    function playListInit(autoplay) {
        if (autoplay) {
            playListChange(playItem);
        } else {
            playListConfig(playItem);
        }
    }

    function playListConfig(index) {
        $("#jplayer_playlist_item_" + playItem).removeClass("jplayer_playlist_current");
        $("#jplayer_playlist_item_" + index).addClass("jplayer_playlist_current");
        playItem = index;
        $("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3);
    }

    function playListChange(index) {
        playListConfig(index);
        $("#jquery_jplayer").jPlayer("play");
    }

    function playListNext() {
        var index = (playItem + 1 < myPlayList.length) ? playItem + 1 : 0;
        playListChange(index);
    }

    function playListPrev() {
        var index = (playItem - 1 >= 0) ? playItem - 1 : myPlayList.length - 1;
        playListChange(index);
    }
});
