﻿

$(document).ready(function() {

    var playItem = 0;

    /* 'var myPlayList[]' is an array generated in the back end its pumped in through <asp:Literal ID=songList runat=server/> */

        /* Local copy of jQuery selectors, for performance. */
        var jpPlayTime = $("#jplayer_play_time");
        var jpTotalTime = $("#jplayer_total_time");

        $("#jquery_jplayer").jPlayer({
            ready: function() {
                displayPlayList();
                playListInit(false); // Parameter is a boolean for autoplay.
            },
            oggSupport: false,
            swfPath: "/jPlayer/js"

        })
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
	    jpPlayTime.text($.jPlayer.convertTime(playedTime));
	    jpTotalTime.text($.jPlayer.convertTime(totalTime));
	})
	.jPlayer("onSoundComplete", function() {
	    playListNext();
	});

        $("#jplayer_previous").click(function() {
            playListPrev();
            $(this).blur();
            return false;
        });

        $("#jplayer_next").click(function() {
            playListNext();
            $(this).blur();
            return false;
        });

    function displayPlayList() {
        $("#jplayer_playlist ul").empty();
        $("#songShare").empty();
        
        var songNames = new Array();
        
        for (i=0; i < myPlayList.length; i++) {
            var listItem = (i == myPlayList.length-1) ? "<div class='songNameWrap'><li class='jplayer_playlist_item_last'>" : "<div class='songNameWrap'><li>";
            var shareItem = "<div class='songShareWrap'>"; //(i == myPlayList.length);

            listItem += "<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+ myPlayList[i].name +"</a><a href='http://itunes.apple.com/ca/artist/johnny-hollow/id102706553' class='itunesBuyLink' target='_blank'>BUY</a></li></div>";
            
            shareItem += "<a href='#' id='songShare"+i+"' ><div class='fbShareBtn'><img src='http://www.johnnyhollow.com/imagesProduction/fbShareBtn.jpg' class='fbShareImageBtn' border='0' /></div></a></div><div style='clear:both'></div>";
            
            songNames[i] = myPlayList[i].name;
            
            $("#jplayer_playlist ul").append(listItem);
            $("#jplayer_playlist ul").append(shareItem);
            
            
            $("#songShare"+i).data( "index", i ).click( function() {
                   var index = $(this).data("index");


                    function songShare(songName) {

                        var song = 'http://www.johnnyhollow.com/adminAudio/' + encodeURI(songName + '.mp3');

                        FB.login(function (response) {
                        if (response.session) {
                            if (response.perms) {
                                // user is logged in and granted some permissions.
                                // perms is a comma separated list of granted permissions

                                /*
                                FB.ui({
                                    method: 'stream.publish',
                                    message: 'Check out this track \'' + songName + '\' by Johnny Hollow ...',
                                    attachment: {
                                        media: [{
                                            type: 'mp3',
                                            src: song,
                                            title: songName,
                                            artist: 'Johnny Hollow'
                                        }],
                                        href: 'http://www.johnnyhollow.com/'
                                    }
                                });
                                */
                                FB.ui({
                                    method: 'feed',
                                    message: 'Check out this track \'' + songName + '\' by Johnny Hollow ...',
                                    link: 'http://www.johnnyhollow.com/',
                                    name: songName,
                                    source: song
                                });







                            } else {
                                // user is logged in, but did not grant any permissions
                            }
                        } else {
                            // user is not logged in
                        }
                    }, { perms: 'publish_stream' });



















                        /*
                            var params = new Object();
                            params.songName = songName;

                            $.ajax({ type: "POST", url: "jh.aspx/ShareSong", data: $.toJSON(params), contentType: "application/json; charset=utf-8", dataType: "json",
                                success: function(url) {
                                
                                    var url = url.d;
                                    $(location).attr('href',url);
                                }
                            });
                        */
                    }
                    
                    songShare(songNames[index]); //call the above ajax function
                   
            });
            
            $("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
            
                var index = $(this).data("index");
                if (playItem != index) {
                    playListChange( index );
                } else {
                    $("#jquery_jplayer").jPlayer("play");
                }
                $(this).blur();
                
                return false;
            });   
        }
    }

        function playListInit(autoplay) {
            if (autoplay) {
                playListChange(playItem);
            } else {
                playListConfig(playItem);
            }
        }

        function playListConfig(index) {
            $("#jplayer_playlist_item_" + playItem).removeClass("jplayer_playlist_current").parent().removeClass("jplayer_playlist_current");
            $("#jplayer_playlist_item_" + index).addClass("jplayer_playlist_current").parent().addClass("jplayer_playlist_current");
            playItem = index;
            $("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
        }

        function playListChange(index) {
            playListConfig(index);
            $("#jquery_jplayer").jPlayer("play");

            $('#displaySongName').html( '"' + myPlayList[index].name + '"' );
        }

        function playListNext() {
            var index = (playItem + 1 < myPlayList.length) ? playItem + 1 : 0;
            playListChange(index);

            $('#displaySongName').html( '"' + myPlayList[index].name + '"' );
        }

        function playListPrev() {
            var index = (playItem - 1 >= 0) ? playItem - 1 : myPlayList.length - 1;
            playListChange(index);

            $('#displaySongName').html( '"' + myPlayList[index].name + '"' );
        }

        /* display the first song */
        $('#displaySongName').html( '"' + myPlayList[0].name + '"' );
    });
    
