$(document).ready(function(){
    jQuery.fn.selectTab = function() {
		$(this).parents ('ul').find ('li').find ('a').removeClass ('jp-playlist-tab-active');
        $(this).addClass ('jp-playlist-tab-active');
    }

    var Playlist = function(instance, id, name, playlist, options) {
        var self = this;
		
        this.instance = instance; // String: To associate specific HTML with this playlist
        this.id = id; // String: associate an ID with this playlist
        this.name = name; // String: to associate a name 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_",
            tabs: "jp_playlist_tabs_"
        };
        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++) {
            	// mark alternating list items even/odd for styling
            	if (i%2) {
	                var listItem = (i === this.playlist.length-1) ? "<li class='jp-playlist-last jp-playlist-even'>" : "<li class='jp-playlist-even'>"; // even list items
            	} else {
	                var listItem = (i === this.playlist.length-1) ? "<li class='jp-playlist-last jp-playlist-odd'>" : "<li class='jp-playlist-odd'>"; // odd list items
            	}
                listItem += "<span class='jp-playlist-song'><a href='#' id='" + this.cssId.playlist + this.instance + "_item_" + i +"' tabindex='1'>"+ this.playlist[i].name +"</a>" + " - " + this.playlist[i].composer + "</span>";


                // Create links to free media
                if(this.playlist[i].free) {
                    var first = true;
                    listItem += "<div class='jp-free-media'>(";
                    /* old full free media link creator â we only want mp3
                    $.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>";
                        }
                    });
                    */
                    $.each(this.playlist[i], function(property,value) {
                        if (property === "mp3") { // Check property is mp3.
                            listItem += "<a id='" + self.cssId.playlist + self.instance + "_item_" + i + "_" + property + "' href='" + value + "' tabindex='1'>" + property + "</a>";
                        }
                    });
                    listItem += ")</div>";
                }

				// Create song length things
				if(this.playlist[i].time) {
					listItem += "<div class='jp-playlist-duration'>" + this.playlist[i].time + "</div>";
				}


                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").closest('.jp-playlist-song').removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
            $(this.cssSelector.playlist + "_item_" + index).addClass("jp-playlist-current").closest('.jp-playlist-song').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 : 0;
            this.playlistChange(index);
        },
        playlistPrev: function() {
            var index = (this.current - 1 >= 0) ? this.current - 1 : this.playlist.length - 1;
            this.playlistChange(index);
        }
    };
    
    var jazzPlaylist = new Playlist("1", "jazzPlaylist", "Jazz", [
        {
            name:"Someday My Prince Will Come",
            composer: "Frank Churchill, Larry Morey",
            free:true,
            time:"3:02",
            mp3:"/flipbook/mp3/Someday_My_Prince_Will_Come_(mp3).mp3",
            oga:"/flipbook/mp3/Someday_My_Prince_Will_Come_(mp3).ogg"
        },
        {
            name:"Misty",
            composer: "Erroll Garner, Johnny Burke",
            free: true,
            time:"2:30",
            mp3:"/flipbook/mp3/Misty_JC_2007.mp3",
            oga:"/flipbook/mp3/Misty_JC_2007.ogg"
        },
        {
            name:"Moonlight In Vermont",
            composer: "Karl Suessdorf, John Blackburn",
            free: true,
            time:"1:43",
            mp3:"/flipbook/mp3/Moonlight_In_Vermont_JC_2007.mp3",
            oga:"/flipbook/mp3/Moonlight_In_Vermont_JC_2007.ogg"
        },
        {
            name:"My Shining Hour",
            composer: "Harold Arlen, Johnny Mercer",
            free: true,
            time: "1:54",
            mp3:"/flipbook/mp3/My_Shining_Hour_(mp3).mp3",
            oga:"/flipbook/mp3/My_Shining_Hour_(mp3).ogg"
        },
        {
            name:"All The Things You Are",
            composer: "Jerome Kern, Oscar Hammerstein II",
            free: true,
            time: "3:51",
            mp3:"/flipbook/mp3/All_The_Things_You_Are_JC_2007.mp3",
            oga:"/flipbook/mp3/All_The_Things_You_Are_JC_2007.ogg"
        },
        {
            name:"Autumn Leaves",
            composer: "Joesph Kosma, Johnny Mercer",
            free: true,
            time: "3:29",
            mp3:"/flipbook/mp3/Autumn_Leaves_JC_2007.mp3",
            oga:"/flipbook/mp3/Autumn_Leaves_JC_2007.ogg"
        },
        {
            name:"Stella By Starlight",
            composer: "Victor Young, Ned Washington",
            free: true,
            time: "1:00",
            mp3:"/flipbook/mp3/Stella_By_Starlight_JC_2007.mp3",
            oga:"/flipbook/mp3/Stella_By_Starlight_JC_2007.ogg"
        },
        {
            name:"Ain't Got Nothing But The Blues",
            composer: "Erroll Garner, Johnny Burke",
            free: true,
            time: "3:29",
            mp3:"/flipbook/mp3/Ain't_Got_Nothing_But_The_Blues_JC_2007.mp3",
            oga:"/flipbook/mp3/Ain't_Got_Nothing_But_The_Blues_JC_2007.ogg"
        },
    ], {
    	ready: function() {
    		jazzPlaylist.displayPlaylist();
    		jazzPlaylist.playlistInit(false); // Boolean for autoplay
    	},
    	ended: function() {
    		jazzPlaylist.playlistNext();
    	},
    	play: function() {
    		jazzPlaylist.jPlayer("pauseOthers");
    	}
    });
    	
    var latinPlaylist = new Playlist("1", "latinPlaylist", "Latin", [
        {
            name:"Europa",
            composer:"Carlos Santana",
            free:true,
            time: "3:15",
            mp3:"/flipbook/mp3/EUROPA_JC_2007.mp3",
            oga:"/flipbook/mp3/EUROPA_JC_2007.ogg"
        },
        {
            name:"Latin 1",
            composer: "John Chapman",
            free: true,
            time: "4:44",
            mp3:"/flipbook/mp3/Latin_1_JC_2007.mp3",
            oga:"/flipbook/mp3/Latin_1_JC_2007.ogg"
        },
        {
            name:"New Day",
            composer: "John Chapman",
            free:true,
            time: "2:52",
            mp3:"/flipbook/mp3/New_Day_mp3_(Chords_Solo_Nylon).mp3",
            oga:"/flipbook/mp3/New_Day_mp3_(Chords_Solo_Nylon).ogg"
        },
        {
            name: "Wave",
            composer: "Antonio Carlos Jobim",
            free: true,
            time: "5:03",
            mp3:"/flipbook/mp3/Wave_JC_2007.mp3",
            oga:"/flipbook/mp3/Wave_JC_2007.ogg",
        },
        {
            name: "B&#9837; Bossa Nova",
            composer: "Rhythm Changes",
            free: true,
            time: "3:16",
            mp3:"/flipbook/mp3/Bossa_Nova_1_(Upbeat-Bb)_Mp3.mp3",
            oga:"/flipbook/mp3/Bossa_Nova_1_(Upbeat-Bb)_Mp3.ogg",
        },
        {
            name: "The Girl From Ipanema",
            composer: "Antonio Carlos Jobim",
            free: true,
            time: "1:20",
            mp3:"/flipbook/mp3/The_Girl_From_Ipanema_(mp3).mp3",
            oga:"/flipbook/mp3/The_Girl_From_Ipanema_(mp3).ogg",
        },
        {
            name: "Triste",
            composer: "Antonio Carlos Jobim",
            free: true,
            time: "1:06",
            mp3:"/flipbook/mp3/Triste_(mp3).mp3",
            oga:"/flipbook/mp3/Triste_(mp3).ogg",
        },
        {
            name: "How Insensative",
            composer: "Antonio Carlos Jobim",
            free: true,
            time: "1:52",
            mp3:"/flipbook/mp3/How_Insensative_(mp3).mp3",
            oga:"/flipbook/mp3/How_Insensative_(mp3).ogg",
        },
        {
            name: "Wave 2",
            composer: "Antonio Carlos Jobim",
            free: true,
            time: "1:15",
            mp3:"/flipbook/mp3/Wave_2_(Short)_(mp3).mp3",
            oga:"/flipbook/mp3/Wave_2_(Short)_(mp3).ogg",
        }
    ], {
        ready: function() {
            latinPlaylist.displayPlaylist();
            latinPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
        },
        ended: function() {
            latinPlaylist.playlistNext();
        },
        play: function() {
            latinPlaylist.jPlayer("pauseOthers");
        },
        swfPath: "js",
        supplied: "oga, mp3"
    });
    var popPlaylist = new Playlist("1", "popPlaylist", "Pop", [
        {
            name:"A Minor Prelude",
            composer:"John Chapman",
            free:true,
            mp3:"http://john.hackthetruth.org/flipbook/mp3/01_A_Minor_Prelude_(Spanish_Classical,_JC).mp3",
            oga:"http://john.hackthetruth.org/flipbook/mp3/01_A_Minor_Prelude_(Spanish_Classical,_JC).ogg"
        },
        {
            name:"Hidden",
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-02-Hidden.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-02-Hidden.ogg"
        },
        {
            name:"Lentement",
            free:true,
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-03-Lentement.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-03-Lentement.ogg"
        },
        {
            name:"Bubble",
            free:true,
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-07-Bubble.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-07-Bubble.ogg"
        }
    ], {
        ready: function() {
            popPlaylist.displayPlaylist();
            popPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
        },
        ended: function() {
            popPlaylist.playlistNext();
        },
        play: function() {
            popPlaylist.jPlayer("pauseOthers");
        },
        swfPath: "js",
        supplied: "oga, mp3"
    });
    var folkPlaylist = new Playlist("1", "folkPlaylist", "Folk", [
        {
            name:"A Minor Prelude",
            composer:"John Chapman",
            free:true,
            mp3:"http://john.hackthetruth.org/flipbook/mp3/01_A_Minor_Prelude_(Spanish_Classical,_JC).mp3",
            oga:"http://john.hackthetruth.org/flipbook/mp3/01_A_Minor_Prelude_(Spanish_Classical,_JC).ogg"
        },
        {
            name:"Hidden",
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-02-Hidden.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-02-Hidden.ogg"
        },
        {
            name:"Lentement",
            free:true,
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-03-Lentement.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-03-Lentement.ogg"
        },
        {
            name:"Bubble",
            free:true,
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-07-Bubble.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-07-Bubble.ogg"
        },
        {
            name:"Stirring of a Fool",
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-08-Stirring-of-a-fool.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-08-Stirring-of-a-fool.ogg"
        }
    ], {
        ready: function() {
            folkPlaylist.displayPlaylist();
            folkPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
        },
        ended: function() {
            folkPlaylist.playlistNext();
        },
        play: function() {
            folkPlaylist.jPlayer("pauseOthers");
        },
        swfPath: "js",
        supplied: "oga, mp3"
    });
    var classicalPlaylist = new Playlist("1", "classicalPlaylist", "Classical", [
        {
            name:"A Minor Prelude",
            composer:"John Chapman",
            free:true,
            mp3:"http://john.hackthetruth.org/flipbook/mp3/01_A_Minor_Prelude_(Spanish_Classical,_JC).mp3",
            oga:"http://john.hackthetruth.org/flipbook/mp3/01_A_Minor_Prelude_(Spanish_Classical,_JC).ogg"
        },
        {
            name:"Hidden",
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-02-Hidden.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-02-Hidden.ogg"
        },
        {
            name:"Lentement",
            free:true,
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-03-Lentement.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-03-Lentement.ogg"
        },
        {
            name:"Bubble",
            free:true,
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-07-Bubble.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-07-Bubble.ogg"
        },
        {
            name:"Stirring of a Fool",
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-08-Stirring-of-a-fool.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-08-Stirring-of-a-fool.ogg"
        },
        {
            name:"Partir",
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-09-Partir.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-09-Partir.ogg"
        }
    ], {
        ready: function() {
            classicalPlaylist.displayPlaylist();
            classicalPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
        },
        ended: function() {
            classicalPlaylist.playlistNext();
        },
        play: function() {
            classicalPlaylist.jPlayer("pauseOthers");
        },
        swfPath: "js",
        supplied: "oga, mp3"
    });
    var bandPlaylist = new Playlist("1", "bandPlaylist", "Band", [
        {
            name:"A Minor Prelude",
            composer:"John Chapman",
            free:true,
            mp3:"http://john.hackthetruth.org/flipbook/mp3/01_A_Minor_Prelude_(Spanish_Classical,_JC).mp3",
            oga:"http://john.hackthetruth.org/flipbook/mp3/01_A_Minor_Prelude_(Spanish_Classical,_JC).ogg"
        },
        {
            name:"Hidden",
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-02-Hidden.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-02-Hidden.ogg"
        },
        {
            name:"Lentement",
            free:true,
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-03-Lentement.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-03-Lentement.ogg"
        },
        {
            name:"Bubble",
            free:true,
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-07-Bubble.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-07-Bubble.ogg"
        },
        {
            name:"Stirring of a Fool",
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-08-Stirring-of-a-fool.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-08-Stirring-of-a-fool.ogg"
        },
        {
            name:"Partir",
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-09-Partir.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-09-Partir.ogg"
        },
        {
            name:"Thin Ice",
            free:true,
            mp3:"http://www.jplayer.org/audio/mp3/Miaow-10-Thin-ice.mp3",
            oga:"http://www.jplayer.org/audio/ogg/Miaow-10-Thin-ice.ogg"
        }
    ], {
        ready: function() {
            bandPlaylist.displayPlaylist();
            bandPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
        },
        ended: function() {
            bandPlaylist.playlistNext();
        },
        play: function() {
            bandPlaylist.jPlayer("pauseOthers");
        },
        swfPath: "js",
        supplied: "oga, mp3"
    });
    
    var playlistList = [jazzPlaylist, latinPlaylist, popPlaylist, folkPlaylist, classicalPlaylist, bandPlaylist];

    // clean up first
	$.each(playlistList, function(index, playlist) {
		$(playlist.cssSelector.tabs + " ul").empty();
	});
	
	// create tabs
	$.each(playlistList, function(index, playlist) {
		var playlistTab = "<li><a href='#' id='jp_playlist_tab_" + playlist.id + "' class='jp-playlist-tab";
		playlistTab += (index == 0) ? " jp-playlist-tab-active'" : "'";
		playlistTab += " tabindex='1'>"+ playlist.name + "</a></li>";
		
		// Associate playlist items with their media
		$(playlist.cssSelector.tabs + " ul").append(playlistTab);
		
		$("#jp_playlist_tab_" + playlist.id).click(function() {
			playlist.displayPlaylist();
			playlist.playlistInit();
			$(this).selectTab();
		});
    });	
	
	// attach a listener to apply scrolling fancyness to every overflowing song title
    $('.jp-playlist-song').livequery(function() {
        $(this).overflowMarquee({'auto': false});
    });
});
