﻿/// <reference path = "../system/jquery-1.4.4-vsdoc.js"/>
$(document).ready(function () {    
    //$("div.btnPodcastStream").click(function () { streamPodcast(this); });
    //$("div.btnSavePodcast").click(function () { downloadPodcast(this); });
    
});

$("div.btnPodcastStream").live("click", function () { streamPodcast(this); });
$("div.btnSavePodcast").live("click", function () { downloadPodcast(this); });

soundManager.url = "../Content/soundmanager/";
soundManager.useFlashBlock = true;
soundManager.id = "podcast";
soundManager.autoPlay = true;
soundManager.stream = true;

var currentPodcast;
var soundObject;
function streamPodcast(elem) {
    var podcast = $(elem).attr("path");
    var podcastId = $(elem).attr("fxn");
    $("div.podcastPlayStop").hide();

    if (currentPodcast) {
        soundManager.destroySound(currentPodcast);
    }
    currentPodcast = "podcast_" + podcastId;
    soundManager.id = currentPodcast;
    soundManager.play(currentPodcast, podcast);

    soundObject = soundManager.createSound({
        id: currentPodcast,
        url: podcast,
        autoPlay: true,
        stream: true
    });
        
    $("div.podcastStream").everyTime(3000, function(){getCurrentPodcastTime()});
    logPodcastClick(elem, 1);
    var controls = $("div.podcastPlayStop[fxn=" + podcastId + "]");
    controls.show();
    $("div.podcastStop").click(function () {
        togglePlayback(this);
    });
    $("div.podcastPlay").click(function () {
        togglePlayback(this);
    });
}

function togglePlayback(elem) {
    var podcastId = $(elem).attr("fxn");
    var $elem = $(elem);
    if($elem.hasClass("podcastStopOn")){
        $elem.removeClass("podcastStopOn").addClass("podcastStopOff");
        $("div.podcastPlay[fxn=" + podcastId + "]").removeClass("podcastPlayOff").addClass("podcastPlayOn");
        soundManager.pause(currentPodcast);
    }
    if ($elem.hasClass("podcastPlayOn")) {
        $elem.removeClass("podcastPlayOn").addClass("podcastPlayOff");
        $("div.podcastStop[fxn=" + podcastId + "]").removeClass("podcastStopOff").addClass("podcastStopOn");
        soundManager.play(currentPodcast);
    }
}

function getCurrentPodcastTime() {
    var tmpArr = currentPodcast.split("_");
    var podcastId = tmpArr[tmpArr.length - 1];
    var rawTime = soundObject.position;
    var rawSeconds = rawTime / 1000;
    var minutes = Math.floor(rawSeconds / 60);
    var seconds = Math.round(rawSeconds % 60);    
    var mins = " minutes";
    if (minutes == 1)
        mins = " minute";
    var status = "currently at " + minutes + ":" + seconds + mins;
    $("div.playbackTime[fxn=" + podcastId + "]").html(status);
}

function downloadPodcast(elem) {
    var $elem = $(elem);
    $elem.removeClass("btnSavePodcast").addClass("btnSavePodcastWait");
    var path = $elem.attr("path");
    var fname = $elem.attr("fname");
    var iframe = document.createElement("iframe");
    $(iframe).attr("style", "display:none").attr("src", "/Podcasts/DownloadPodcast/?path=" + path + "&fName=" + fname);
    $elem.parent().append($(iframe));
    logPodcastClick(elem, 2);
    $elem.removeClass("btnSavePodcastWait").addClass("btnSavePodcast");
}

function logPodcastClick(elem, type) {
    var $elem = $(elem);    
    var url = "/Podcasts/LogDownload/";
    //var data = "podcastId=" + $elem.attr("fxn") + "&downloadType=" + type;
    var pcId = $elem.attr("fxn");
    var dlType = type;

    var data = { podcastId: pcId, downloadType: type };
    $.post(url, data,
        function (json) {
            if (json = "success") {
                
            } else {
                // jquery dialog call or
                alert(json.errorMessage);
            }
        }, 'json');
}

