﻿function OnTogglePodcastCollectionComplete(result, userContext) {
    SetCollectionResult(result, userContext);
    CastThree.ChannelService.GetCollectionPodcasts(OnGetCollectionComplete);
}

function SetCollectionResult( result, podcastId )
{
    var text;
    if (result) {
        text = "Remove from Collection";
        $('div.podcast[podcastId=' + podcastId + '] li.collection').addClass("collection-active");
      
    } else {
        text = "Add to Collection";
        $('div.podcast[podcastId=' + podcastId + '] li.collection').removeClass("collection-active");
   
    }
    $('div.podcast[podcastId=' + podcastId + '] li.collection').text(text);
}


function OnUpdatePodcastComplete(result, userContext) {


    if (result > 0) {
       window.location = window.location + "?newEpisodes=" + result.toString();
    } else {
    $('div.podcast[podcastId=' + userContext + '] li.refresh').text("Found no new episodes");
    $('div.podcast[podcastId=' + userContext + '] li.refresh').removeClass("refresh-active");
    }

}

function OnCheckCollectionComplete(result, userContext) {
    SetCollectionResult(result, userContext);
}

function OnGetCollectionComplete(result) {
    $('#channelBar').show();
    $('#channelBar .content').html(result);
}

function OnGetUserChannelsComplete(result, userContext) {
    result = result.replace(/<ul>/, "<ul><li><a href='/channels/add?pid=" + userContext + "'><strong>New Channel</strong></a></li>" );
    
    
    $('div.podcast[podcastId=' + userContext + '] li.channel .channelSelector').html(result);
    $('div.podcast[podcastId=' + userContext + '] li.channel .channelSelector').slideDown();
    
   $('.channelSelector').hover( function(e){}, function(e){ $(this).slideUp(); } );

    $('.channelSelector li').click(function(e) {
        try{
            var channelId = parseInt($(this).attr('objectId'));

            CastThree.ChannelService.AddPodcastChannel(userContext, channelId, OnAddChannelPodcastComplete, null, channelId, null);

            $(this).parents('.channelSelector:first').slideUp();
            
            $('#fade').html("<div>Added to channel</div>");
            DoFade();
            
            return false;
        }catch( ex) {
            return true;
        }
    });
}

function OnAddChannelPodcastComplete(result, userContext) {
    CastThree.ChannelService.GetChannelPodcasts(userContext, OnGetChannelPodcastsComplete, null, userContext, null);
}

function OnGetChannelPodcastsComplete(result) {
    $('#channelBar').show();
    $('#channelBar .content').html(result);
}



$(function() {
    pageCategory = 'Podcast';

    $('.actions .refresh').click(function(e) {

        var podcastId = parseInt($(this).parents("div.podcast:first").attr("podcastId"));

        $(this).text("Updating");
        $(this).addClass("refresh-active");

        CastThree.PodcastService.UpdatePodcast(podcastId, OnUpdatePodcastComplete, null, podcastId, null);

    });


    $('.actions .collection').click(function(ex) {
        if (RequireLogin("Please log in to add this podcast to your CastRoller collection")) {

            var podcastId = parseInt($(this).parents("div.podcast:first").attr("podcastId"));

            CastThree.ChannelService.TogglePodcastCollection(podcastId, OnTogglePodcastCollectionComplete, null, podcastId, null);

            if ($(this).html().match("Remove")) {
                SetCollectionResult(false, podcastId);
                $('#fade').html("<div>Removed from your collection</div>");
            }
            else {
                SetCollectionResult(true, podcastId);
                $('#fade').html("<div>Added to your collection</div>");
            }
            DoFade();
        }

    });

    $('.actions .collection').each(function(ex) {



        var podcastId = parseInt($(this).parents("div.podcast:first").attr("podcastId"));
        CastThree.ChannelService.CheckPodcastCollection(podcastId, OnCheckCollectionComplete, null, podcastId, null);


    });


    $('.podcast .actions .channel').click(function(e) {
        if (RequireLogin("Please log in to add this podcast to a channel")) {
            var podcastId = parseInt($(this).parents("div.podcast:first").attr("podcastId"));
            CastThree.ChannelService.GetUserChannels(podcastId, OnGetUserChannelsComplete, null, podcastId, null);
        }
    });

    var newEpisodes = querySt("newEpisodes");
    if (newEpisodes) {
        if (newEpisodes > 1) {
            text = "Found " + newEpisodes + " new episodes";
        } else if (newEpisodes == 1) {
            text = "Found one new episode"
        }
        $('div.podcast li.refresh').text(text);
    }


});