﻿$('document').ready(function(){
    //title and containter
    $('li.audioListItem').each(function(){
        var $this = $(this);
        $('.audioTitle', $this).hover(function(){
            $(this).css({color:'#B11B2E'});
        }, function(){
            if($this.attr('open') !== 'true'){
                $('.audioTitle', $this).css({color:'#3E4C58'});                 
            }
        }).click(function(){
            if($this.attr('open') === 'true'){
                Sensis.Audio.setAudioRowInactive($this);   
            }
            else{
                $('span.openCloseIcon', $this).css({'background-position':'-30px -253px'});
                $this.animate({height:'124px'}).attr('open', 'true');
                $('ul.audioPlayerRow', $this).fadeIn();
                $('li.audioListItem:not(:animated)').each(function(){
                    var $this = $(this);
                    Sensis.Audio.setAudioRowInactive($this);
                });
            }
        });
    });
    
    //preload loading animation
    var img = new Image();
    img.src = '/DesktopModules/Podcast/images/loadingAnimation.gif';
    
    //Diabetes Prevention Players
    var diabetesOneEnglishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Diabetes One English'
        ,isEnglish: true
        ,isFirstRow: true
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Diabetes_1_English.mp3'
        ,playLength: '03:18'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Diabetes_1_English.mp3'
    });   
    $('#diabetesOne').append(diabetesOneEnglishPlayer.$audioRow);
    
    var diabetesOneSpanishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Diabetes One Spanish'
        ,isEnglish: false
        ,isFirstRow: false
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Diabetes_1_Spanish.mp3'
        ,playLength: '03:53'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Diabetes_1_Spanish.mp3'
    }); 
    $('#diabetesOne').append(diabetesOneSpanishPlayer.$audioRow);
    
    var $diabetesOneBrochure = $(Sensis.Audio.brochureHtml);
    $('.audioBrochureLink', $diabetesOneBrochure).attr('href', '/DesktopModules/Podcast/podcastBrochures/Diabetes_Prevention_Brochure.pdf');
    $('#diabetesOne').append($diabetesOneBrochure);
    
    //Diabetes Prevention Two Players
    var diabetesTwoEnglishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Diabetes Two English'
        ,isEnglish: true
        ,isFirstRow: true
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Diabetes_2_English.mp3'
        ,playLength: '03:24'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Diabetes_2_English.mp3'
    });   
    $('#diabetesTwo').append(diabetesTwoEnglishPlayer.$audioRow);
    
    var diabetesTwoSpanishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Diabetes Two Spanish'
        ,isEnglish: false
        ,isFirstRow: false
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Diabetes_2_Spanish.mp3'
        ,playLength: '04:07'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Diabetes_2_Spanish.mp3'
    }); 
    $('#diabetesTwo').append(diabetesTwoSpanishPlayer.$audioRow);
    
    var $diabetesTwoBrochure = $(Sensis.Audio.brochureHtml);
    $('.audioBrochureLink', $diabetesTwoBrochure).attr('href', '/DesktopModules/Podcast/podcastBrochures/Diabetes_Prevention_Brochure.pdf');
    $('#diabetesTwo').append($diabetesTwoBrochure);

    //Weight Managment Players
    var weightEnglishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Weight Management English'
        ,isEnglish: true
        ,isFirstRow: true
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Weight_Control_English.mp3'
        ,playLength: '04:48'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Weight_Control_English.mp3'
    });   
    $('#weightManagement').append(weightEnglishPlayer.$audioRow);
    
    var weightSpanishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Weight Management Spanish'
        ,isEnglish: false
        ,isFirstRow: false
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Weight_Control_Spanish.mp3'
        ,playLength: '05:25'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Weight_Control_Spanish.mp3'
    }); 
    $('#weightManagement').append(weightSpanishPlayer.$audioRow);
    
    var $weightBrochure = $(Sensis.Audio.brochureHtml);
    $('.audioBrochureLink', $weightBrochure).attr('href', '/DesktopModules/Podcast/podcastBrochures/Weight_Management_Brochure.pdf');
    $('#weightManagement').append($weightBrochure);
    
    //Exercise and Fitness Players
    var exerciseEnglishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Exercise English'
        ,isEnglish: true
        ,isFirstRow: true
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Fitness_Podcast1_English.mp3'
        ,playLength: '07:25'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Fitness_Podcast1_English.mp3'
    });   
    $('#exercise').append(exerciseEnglishPlayer.$audioRow);
    
    var exerciseSpanishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Exercise Spanish'
        ,isEnglish: false
        ,isFirstRow: false
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Fisico_Podcast1_Spanish.mp3'
        ,playLength: '08:59'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Fisico_Podcast1_Spanish.mp3'
    }); 
    $('#exercise').append(exerciseSpanishPlayer.$audioRow);
    
    var $exerciseBrochure = $(Sensis.Audio.brochureHtml);
    $('.audioBrochureLink', $exerciseBrochure).attr('href', '/DesktopModules/Podcast/podcastBrochures/Exercise_and_Fitness_Brochure.pdf');
    $('#exercise').append($exerciseBrochure);
    
    //Stress Management Players
    var stressEnglishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Stress English'
        ,isEnglish: true
        ,isFirstRow: true
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Stress_Podcast1_English.mp3'
        ,playLength: '02:58'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Stress_Podcast1_English.mp3'
    });   
    $('#stressOne').append(stressEnglishPlayer.$audioRow);
    
    var stressSpanishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Stress Spanish'
        ,isEnglish: false
        ,isFirstRow: false
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Estres_Podcast1_Spanish.mp3'
        ,playLength: '03:48'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Estres_Podcast1_Spanish.mp3'
    }); 
    $('#stressOne').append(stressSpanishPlayer.$audioRow);
    
    var $stressBrochre = $(Sensis.Audio.brochureHtml);
    $('.audioBrochureLink', $stressBrochre).attr('href', '/DesktopModules/Podcast/podcastBrochures/Stress_Management_Brochure.pdf');
    $('#stressOne').append($stressBrochre);
    
    //Stress Management Two Players
    var stressTwoEnglishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Stress Two English'
        ,isEnglish: true
        ,isFirstRow: true
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Stress_Podcast2_English.mp3'
        ,playLength: '06:43'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Stress_Podcast2_English.mp3'
    });   
    $('#stressTwo').append(stressTwoEnglishPlayer.$audioRow);
    
    var stressTwoSpanishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Stress Two Spanish'
        ,isEnglish: false
        ,isFirstRow: false
        ,audioFilePath: '/DesktopModules/Podcast/podcasts/Estres_Podcast2_Spanish.mp3'
        ,playLength: '08:33'
        ,audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Estres_Podcast2_Spanish.mp3'
    }); 
    $('#stressTwo').append(stressTwoSpanishPlayer.$audioRow);
    
    var $stressTwoBrochre = $(Sensis.Audio.brochureHtml);
    $('.audioBrochureLink', $stressTwoBrochre).attr('href', '/DesktopModules/Podcast/podcastBrochures/Stress_Management_Brochure.pdf');
    $('#stressTwo').append($stressTwoBrochre);

    //How to navigate your health plan
    var navigateYourHealthPlanEnglishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Navigate Your Health Plan English'
        , isEnglish: true
        , isFirstRow: true
        , audioFilePath: '/DesktopModules/Podcast/podcasts/Navigate_Health_Plan_English.mp3'
        , playLength: '02:52'
        , audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Navigate_Health_Plan_English.mp3'
    });
    $('#yourHealthPlan').append(navigateYourHealthPlanEnglishPlayer.$audioRow);

    var navigateYourHealthPlanSpanishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Navigate Your Health Plan Spanish'
        , isEnglish: false
        , isFirstRow: false
        , audioFilePath: '/DesktopModules/Podcast/podcasts/Navigate_Health_Plan_Spanish.mp3'
        , playLength: '03:36'
        , audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Navigate_Health_Plan_Spanish.mp3'
    });
    $('#yourHealthPlan').append(navigateYourHealthPlanSpanishPlayer.$audioRow);

    var $yourHealthPlanBrochure = $(Sensis.Audio.brochureHtml);
    $('.audioBrochureLink', $yourHealthPlanBrochure).attr('href', '/DesktopModules/Podcast/podcastBrochures/HowtoNavigateYourPlanBienHealthCoveragePlan_BILINGUAL.pdf');
    $('#yourHealthPlan').append($yourHealthPlanBrochure);

    //How to select a primary physician
    var primayPhysicianEnglishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Select a Primary Physician English'
        , isEnglish: true
        , isFirstRow: true
        , audioFilePath: '/DesktopModules/Podcast/podcasts/Primary_Physician_English.mp3'
        , playLength: '03:23'
        , audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Primary_Physician_English.mp3'
    });
    $('#primaryPhysician').append(primayPhysicianEnglishPlayer.$audioRow);

    var navigateYourHealthPlanSpanishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Select a Primary Physician Spanish'
        , isEnglish: false
        , isFirstRow: false
        , audioFilePath: '/DesktopModules/Podcast/podcasts/Primary_Physician_Spanish.mp3'
        , playLength: '04:14'
        , audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Primary_Physician_Spanish.mp3'
    });
    $('#primaryPhysician').append(navigateYourHealthPlanSpanishPlayer.$audioRow);
    //same brochure as "How to navigate your health plan"
    $('#primaryPhysician').append($yourHealthPlanBrochure.clone());

    //Understanding your payment responsibilities
    var yourResponsibilitiesEnglishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Your Responsibilities English'
        , isEnglish: true
        , isFirstRow: true
        , audioFilePath: '/DesktopModules/Podcast/podcasts/Payment_Responsibility_English.mp3'
        , playLength: '02:55'
        , audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Payment_Responsibility_English.mp3'
    });
    $('#paymentResponsibilities').append(yourResponsibilitiesEnglishPlayer.$audioRow);

    var yourResponsibilitiesSpanishPlayer = Sensis.Audio.CreateAudioPlayerRow({
        title: 'Your Responsibillities Spanish'
        , isEnglish: false
        , isFirstRow: false
        , audioFilePath: '/DesktopModules/Podcast/podcasts/Payment_Responsibility_Spanish.mp3'
        , playLength: '03:34'
        , audioDownloadPath: '/DesktopModules/Podcast/podcasts/download.aspx?f=Payment_Responsibility_Spanish.mp3'
    });
    $('#paymentResponsibilities').append(yourResponsibilitiesSpanishPlayer.$audioRow);
    //same brochure as "How to navigate your health plan"
    $('#paymentResponsibilities').append($yourHealthPlanBrochure.clone());

});

var Sensis = {};
Sensis.Audio = (function(){
    var that = {};
    var audioRowHtml = '<ul class="audioPlayerRow"> \
                            <li class="audioPlayerCell audioPlayerLanguageLabel"></li> \
                            <li class="audioPlayerCell audioPlayerButton audioPlayButton"></li> \
                            <li class="audioPlayerCell audioPlayerButton audioStopButton"></li> \
                            <li class="audioPlayerCell audioPlayerButton audioMuteButton"></li> \
                            <li class="audioPlayerCell audioProgressContainer"> \
                               <div class="audioLoadBar"> \
                                    <div class="audioPlayBar"></div> \
                                </div> \
                                <div class="audioPlayTime"></div> \
                            </li> \
                            <li class="audioPlayerCell audioDownloadLabel"></li> \
                            <li class="audioPlayerCell audioPlayerButton audioDownloadButton"></li> \
                        </ul>'
    that.brochureHtml = '<ul class="audioPlayerRow"> \
                            <li class="audioPlayerCell audioPlayerBrocureLabel">Brochure</li> \
                            <li class="audioPlayerCell audioPlayerButton audioBrochureDownloadButton"><a class="audioBrochureLink" href="" target="_blank"></a></li> \
                        </ul>'         
    //spec {title: String (must be unique), isEnglish: Boolean, isFirstRow: Boolean, audioFilePath: String, playLength: String ('00:00'), audioDownloadPath: String}
    that.CreateAudioPlayerRow = function(spec){
        var that = {};
        var isPlayMode = false;
        var isMuteMode = false;      
        that.$audioRow = $(audioRowHtml);
        if(spec.isFirstRow){
            that.$audioRow.addClass('first');
        }
        if(spec.isEnglish){
            $('.audioPlayerLanguageLabel', that.$audioRow).text('English');
            $('.audioDownloadLabel', that.$audioRow).text('Download'); 
        }
        else{
            $('.audioPlayerLanguageLabel', that.$audioRow).html('Espa&ntilde;ol');
            $('.audioDownloadLabel', that.$audioRow).text('Descarga');  
        }
        var $playerElement = $('<div></div>');
        $playerElement.attr('id', spec.title.replace(/\s/g, '_'));
        $('body').append($playerElement);
        var jPlayerInstance = $playerElement.jPlayer({
            ready: function(){
                this.element.jPlayer('setFile', spec.audioFilePath);
            }
            ,customCssIds: true
            ,swfPath: '/DesktopModules/Podcast/js'
        });
        //set play head slider
        that.slider = $('.audioLoadBar', that.$audioRow).slider({
            slide:function(event, ui){
                jPlayerInstance.jPlayer('playHead', ui.value);
            }
        });
        var $sliderHandle = $('.ui-slider-handle', that.$audioRow);
        var $audioPlayButton = $('.audioPlayButton', that.$audioRow);
        var $audioMuteButton = $('.audioMuteButton', that.$audioRow);
        var $playBar = $('.audioPlayBar', that.$audioRow);
        var $playTime = $('.audioPlayTime', that.$audioRow);
        //handle play button
        $audioPlayButton.click(function(){
            if( ! isPlayMode){
                $(this).css({'background-position':'0px -36px'});
                jPlayerInstance.jPlayer('play');
                $sliderHandle.show();
                isPlayMode = true;
            }
            else{
                $(this).css({'background-position':'0px 0px'});
                jPlayerInstance.jPlayer('pause');
                isPlayMode = false;
            }
        }).mouseover(function(){
            if(isPlayMode){               
                $(this).css({'background-position':'-45px -36px'});//show pause rollover
            }
            else{
                $(this).css({'background-position':'-45px 0px'});//show play rollover
            }
        }).mouseout(function(){
            if(isPlayMode){
                $(this).css({'background-position':'0px -36px'});
            }
            else{
                $(this).css({'background-position':'0px 0px'});
            }
        });
        //handle stop button
        $('.audioStopButton', that.$audioRow).click(function(){
            jPlayerInstance.jPlayer('stop');
            $sliderHandle.hide();
            $audioPlayButton.css({'background-position':'0px 0px'});
            $audioMuteButton.css({'background-position':'0px -72px', 'cursor':'default'});
            isPlayMode = false;
        }).hover(function(){
            if(isPlayMode){
                $(this).css({'background-position':'-45px -275px', 'cursor':'pointer'});
            }
        }
        ,function(){
            $(this).css({'background-position':'0px -275px', 'cursor':'default'});
        });
        //handle mute button
        $audioMuteButton.click(function(){
            if( ! isMuteMode){
                if(isPlayMode){
                    $(this).css({'background-position':'-45px -72px'});
                    jPlayerInstance.jPlayer('volumeMin');
                    isMuteMode = true;
                }   
            }
            else{
                $(this).css({'background-position':'0px -72px'});
                jPlayerInstance.jPlayer('volumeMax');
                isMuteMode = false;
            } 
        }).hover(function(){
            if(isPlayMode){
                $(this).css({'background-position':'-45px -72px', 'cursor':'pointer'});
            }
        }
        ,function(){
            if( ! isMuteMode){
                $(this).css({'background-position':'0px -72px', 'cursor':'default'});
            }
        }); 
        //handle audio progress change
        //Parameters - Number: loadPercent, Number: playedPercentRelative, Number: playedPercentAbsolute, Number: playedTime, Number: totalTime
        jPlayerInstance.jPlayer("onProgressChange", function(lp,ppr,ppa,pt,tt) {
            var _ppa = parseInt(ppa);
            $playBar.width(_ppa + '%');
            that.slider.slider('value',  (_ppa - 2));
            if(pt === 0){
                if( ! isPlayMode){
                    $playTime.text(spec.playLength);
                }
                else{
                   $playTime.html('<img style="padding-top:7px;" src="/DesktopModules/Podcast/images/loadingAnimation.gif" />');
                }
            }
            else{
                $('img', $playTime).fadeOut('normal');
                $playTime.text($.jPlayer.convertTime(pt));
            }
        });
        //handle audio complete
        jPlayerInstance.jPlayer("onSoundComplete", function() {
            isPlayMode = false;
            $audioPlayButton.css({'background-position':'0px 0px'});
        });
        //handle audio download button
        $('.audioDownloadButton', that.$audioRow).click(function(){
            location.href = spec.audioDownloadPath;
        });  
        return that;
    } 
    that.setAudioRowInactive = function($this){
        $('.audioTitle', $this).css({color:'#3E4C58'});
        $('span.openCloseIcon', $this).css({'background-position':'0px -253px'});
        $this.animate({height:'14px'}).attr('open', 'false');
        $('ul.audioPlayerRow', $this).fadeOut();
    }  
    return that;
})();
