//********************
function UpdateRecordItem()
    {
    var Extension=CurrentRecordQuality.CurrentIndex==7?"wav":"mp3";
    RecordItem.File.FileName=RecordItem.File.SongName+"-"+PrependZeros(RecordItem.File.TrackNumber,2)+"."+Extension;
    }


function StartRecording()
    {
    CurrentRecordingTime=0;
    RecordItem.File.DateStr=GetCurrentTimeString();
    RecordItem.File.TimeStr=GetCurrentTimeString();
    IsPlaying="recording";
    UpdateRecordItem();
    RefreshDisplay();
    }

//********************


function GotoFiles()
    {
    CurrentlyBrowsingUI=FilesUI;
    CurrentJBMMUI=FilesUI;
    CurrentJBMMUI.RefreshDisplay();
    }

function GotoMediaLib()
    {
    CurrentlyBrowsingUI=MediaLibUI;
    CurrentJBMMUI=MediaLibUI;
    CurrentJBMMUI.RefreshDisplay();
    }

function GotoCurrentBrowsing()
    {
    CurrentJBMMUI=CurrentlyBrowsingUI;
    CurrentJBMMUI.RefreshDisplay();
    }

function GotoPlaying()
    {
    PlayReturnJBMMUI=CurrentJBMMUI;
    CurrentJBMMUI=PlayUI;
    CurrentJBMMUI.RefreshDisplay();
    }

function GotoF3Next()
    {
    PlayingListCursorPosesValid=false;
    if (!CurrentJBMMUI.F3Next)
        {
        CurrentJBMMUI=CurrentlyBrowsingUI;
        }
    else
        CurrentJBMMUI=CurrentJBMMUI.F3Next;
    CurrentJBMMUI.RefreshDisplay();
    }

function GotoTopMenu()
    {
    if (SetupReturnJBMMUI)
        {
        CurrentJBMMUI=SetupReturnJBMMUI;
        SetupReturnJBMMUI=null;
        }
    else
        CurrentJBMMUI=TopUI;
    CurrentJBMMUI.RefreshDisplay();
    }

function ImmediatlyRecord()
    {
//    alert("ImmediatlyRecord()");
    CurrentDisplayingImage=null;
    if (CurrentJBMMUI==OffUI)
        {
        RecordReturnJBMMUI=TopUI;
        JBMMRealtime();
        }
    else
        RecordReturnJBMMUI=CurrentJBMMUI;
    ReturnFromRecordIsPlaying=IsPlaying;
    CurrentJBMMUI=RecordUI;
    StartRecording();
    CurrentJBMMUI.RefreshDisplay();
    }

function GotoRecordMenu()
    {
    ReturnFromRecordIsPlaying=IsPlaying;
    RecordReturnJBMMUI=CurrentJBMMUI;
    CurrentJBMMUI=RecordUI;
    CurrentJBMMUI.RefreshDisplay();
    }

function GotoSetupMenu()
    {
    if (CurrentJBMMUI.Mode=="setup") return;
    SetupReturnJBMMUI=CurrentJBMMUI;
    CurrentJBMMUI=SetupUI;
    CurrentJBMMUI.RefreshDisplay();
    }

/*function RefreshDisplay()
    {
    alert("RefreshDisplay()");
    CurrentJBMMUI.RefreshDisplay();
    }*/
//**************************************************************************
//Playing Control
//**************************************************************************
function SetPlayingFromPlayList()
    {
    if (!PlayingPlaylist) return;
    PlayingList=PlaylistMenu.Items;
    PlayingListCursorPoses=null;

    var I;
    for (I=0;I<PlayingList.length;I++)
        {
        var Item=PlayingList[I];
        if (Item==CurrentPlayingAudioItem || Item==CurrentPlayingVideoItem) PlayingListPosition=I;
        if (Item.File.Extension!="mp3") PlayingListIsAllMP3s=false;
        PlayingListTimeLength+=Item.File.TimeLength;
        }
    }

function ResetPlayingList()
    {
    PlayingList=new Array();
    PlayingListCursorPoses=new Array();
    PlayingListPosition=0;
    IsPlaying="stopped";  //"playing", "pause"
    CurrentPlayingAudioItem=null;
    CurrentPlayingVideoItem=null;
    CurrentPlayingItem=null;
    PlayingListIsAllMP3s=true;
    PlayingListTimeLength=0;
    PlayingListTimePos=0;
    CurrentAudioTimeRemaining=0;
    CurrentAudioPlayingItemLength=1;
    CurrentVideoTimeRemaining=0;
    CurrentVideoPlayingItemLength=1;
    PlayingListFinalized=false;
    }

ResetPlayingList();

function AddItemToPlayingList(Item,CursorPos)
    {
    if (!Item.File) alert("AddItemToPlayingList Item is not a file item.");
//    alert("AddItemToPlayingList "+Item.File.FileName);
    if (Item.File.Extension!="mp3")
        {
        PlayingListIsAllMP3s=false;
        }
    PlayingList[PlayingList.length]=Item;
    PlayingListCursorPoses[PlayingListCursorPoses.length]=CursorPos;
    PlayingListTimeLength+=Item.File.TimeLength;
    }

function GetMP3SecLen(Item)
    {
    switch(CurrentScanMode.CurrentIndex)
        {
        case 0: return CurrentPlayingAudioItem.File.TimeLength;
        case 1: return 5;
        case 2: return 5;
        case 3: return 10;
        case 4: return 10;
        //=new value(new Array("Off","5s beginning","5s middle", "10s beginning", "10s middle"),0);
        }
    }

function AutoAdvancePlaying()
    {
    if (IsPlaying!="playing") return;
    var Item=PlayingList[PlayingListPosition];
    var NextItem;
    var NextPlayingListPosition=-1;

    if (PlayingListPosition<PlayingList.length-1)
        NextPlayingListPosition=PlayingListPosition+1;
    else if (CurrentRepeatMode.CurrentIndex)
        NextPlayingListPosition=0;
    if (NextPlayingListPosition>=0)
        NextItem=PlayingList[NextPlayingListPosition];

    if (CurrentAudioTimeRemaining==0 && NextItem && NextItem.File.Extension=="mp3")
        {
//        alert("Audio ran out - next is MP3");
        CurrentPlayingItem=
        CurrentPlayingAudioItem=NextItem;
        NextItem.File.LastPlayedDateStr=GetCurrentDateString();
        NextItem.File.LastPlayedTimeStr=GetCurrentTimeString();
        NextItem.File.NumTimesPlayed++;
        CurrentAudioTimeRemaining=CurrentAudioPlayingItemLength=GetMP3SecLen(CurrentPlayingAudioItem);
        PlayingListPosition=NextPlayingListPosition;
        AutoAdvancePlaying();
        }

    if (CurrentVideoTimeRemaining==0 && NextItem && NextItem.File.Extension=="jpg")
        {
//        alert("Video ran out - next is jpg");
        CurrentPlayingItem=
        CurrentPlayingVideoItem=NextItem;
        NextItem.File.LastPlayedDateStr=GetCurrentDateString();
        NextItem.File.LastPlayedTimeStr=GetCurrentTimeString();
        NextItem.File.NumTimesPlayed++;
        CurrentDisplayingImage=CurrentPlayingVideoItem.File.FileName;
        CurrentVideoTimeRemaining=CurrentVideoPlayingItemLength=SlideShowDelaySeconds[CurrentSlideShowDelay.CurrentIndex];
        PlayingListPosition=NextPlayingListPosition;
        AutoAdvancePlaying();
        }

    if (CurrentAudioTimeRemaining==0 && CurrentVideoTimeRemaining==0 && NextItem && NextItem.File.Extension=="avi")
        {
//        alert("Audio and Video ran out - next is AVI");
        CurrentPlayingItem=
        CurrentPlayingVideoItem=
        CurrentPlayingAudioItem=NextItem;
        NextItem.File.LastPlayedDateStr=GetCurrentDateString();
        NextItem.File.LastPlayedTimeStr=GetCurrentTimeString();
        NextItem.File.NumTimesPlayed++;
        CurrentVideoTimeRemaining=CurrentVideoPlayingItemLength=
        CurrentAudioTimeRemaining=CurrentAudioPlayingItemLength=CurrentPlayingAudioItem.File.TimeLength;
        PlayingListPosition=NextPlayingListPosition;
        }

//    if (CurrentAudioTimeRemaining==0 && CurrentPlayingAudioItem) CurrentPlayingAudioItem=null;
//    if (CurrentVideoTimeRemaining==0 && CurrentPlayingVideoItem) CurrentPlayingVideoItem=null;

    if (CurrentAudioTimeRemaining==0 && CurrentVideoTimeRemaining==0)
        {
        CurrentPlayingAudioItem=CurrentPlayingVideoItem=CurrentPlayingItem=null;
        IsPlaying="stopped";
        }

    if (!CurrentPlayingAudioItem && !CurrentPlayingVideoItem) CurrentPlayingItem=null;
    if (CurrentPlayingAudioItem && !CurrentPlayingVideoItem) CurrentPlayingItem=CurrentPlayingAudioItem;
    if (!CurrentPlayingAudioItem && CurrentPlayingVideoItem) CurrentPlayingItem=CurrentPlayingVideoItem;
    }

function SetupPlayingCurrent()
    {
    var Item=PlayingList[PlayingListPosition];
    var CanAdvanceToJPG=false;
    var CanAdvanceToMP3=false;
    CurrentPlayingItem=Item;

    var DidAdvance=true;
    if (Item.File.Extension=="mp3")
        {
        CurrentPlayingAudioItem=Item;
        Item.File.LastPlayedDateStr=GetCurrentDateString();
        Item.File.LastPlayedTimeStr=GetCurrentTimeString();
        Item.File.NumTimesPlayed++;
        CurrentAudioTimeRemaining=CurrentAudioPlayingItemLength=GetMP3SecLen(Item);
        }
    if (Item.File.Extension=="avi")
        {
        CurrentPlayingAudioItem=Item;
        CurrentPlayingVideoItem=Item;
        CurrentDisplayingImage=Item.File.FileName.substring(0,Item.File.FileName.indexOf('.'))+".jpg";
        Item.File.LastPlayedDateStr=GetCurrentDateString();
        Item.File.LastPlayedTimeStr=GetCurrentTimeString();
        Item.File.NumTimesPlayed++;

        CurrentVideoTimeRemaining=CurrentAudioTimeRemaining=
        CurrentVideoPlayingItemLength=CurrentAudioPlayingItemLength=Item.File.TimeLength;
        }
    if (Item.File.Extension=="jpg")
        {
        CurrentPlayingVideoItem=Item;
        CurrentDisplayingImage=Item.File.FileName;
        Item.File.LastPlayedDateStr=GetCurrentDateString();
        Item.File.LastPlayedTimeStr=GetCurrentTimeString();
        Item.File.NumTimesPlayed++;
//        alert(CurrentDisplayingImage);

        CurrentVideoTimeRemaining=
        CurrentVideoPlayingItemLength=SlideShowDelaySeconds[CurrentSlideShowDelay.CurrentIndex];
        }

/*    alert
        (
        "Before Auto:\n"+
        "CurrentAudioTimeRemaining="+CurrentAudioTimeRemaining+"\n"+
        "CurrentVideoTimeRemaining="+CurrentVideoTimeRemaining
        )*/
    AutoAdvancePlaying();
/*    alert
        (
        "After Auto:\n"+
        "CurrentAudioTimeRemaining="+CurrentAudioTimeRemaining+"\n"+
        "CurrentVideoTimeRemaining="+CurrentVideoTimeRemaining
        )*/
    }

function Play()
    {
    if  (PlayingList.length==0)
        {IsPlaying="stopped";return;}
    if (PlayingListFinalized==false)
        {
        PlayingListFinalized=true;
        if (CurrentShuffleMode.CurrentIndex==1)
            {
            PlayingList.sort(CompareRandom);
            PlayingListCursorPosesValid=false;
            }
        }
    if (IsPlaying=="paused")
        {
        IsPlaying="playing";
        return;
        }
    if (IsPlaying=="playing")
        {
        IsPlaying="paused";
        return;
        }
    IsPlaying="playing";
    if (PlayingListPosition>=PlayingList.TimeLength || PlayingListPosition<0)
        PlayingListPosition=0;
    SetupPlayingCurrent();
    if (PlayingList.length==1 && PlayingList[0].File.Extension=="jpg")
        IsPlaying="stopped";
    }

function Stop()
    {
    IsPlaying="stopped";
    CurrentDisplayingImage=null;
    }

function Advance()
    {
    if (PlayingList.length==0) {IsPlaying="stopped";return;}
    PlayingListTimePos+=CurrentPlayingItem.File.TimeLength;
    PlayingListPosition++;
    if (PlayingListPosition==PlayingList.length)
        {
        PlayingListTimePos=0;
        PlayingListPosition=0;
        CurrentDisplayingImage=null;
        if (CurrentShuffleMode.CurrentIndex==1)
            PlayingList.sort(CompareRandom);
        }
    SetupPlayingCurrent();
    }

function Backup()
    {
    if (PlayingList.length==0) {IsPlaying="stopped";return;}
    var Old=PlayingListPosition;
    PlayingListPosition--;
    if (PlayingListPosition<0)
        {
        PlayingListPosition=PlayingList.length-1;
        PlayingListTimePos=PlayingListTimeLength;
        }
    SetupPlayingCurrent();

    if (PlayingListPosition==Old)
        {
        PlayingListPosition-=2;
        if (Old==0)
            {
            PlayingListPosition=PlayingList.length-2;
            PlayingListTimePos=PlayingListTimeLength;
            }
        SetupPlayingCurrent();
        }
    PlayingListTimePos-=CurrentPlayingItem.File.TimeLength;
    }

function GetCurrentPlayingImage()
    {
    if (IsPlaying=="playing")   {return "play.gif";}
    if (IsPlaying=="stopped")   {return "stop.gif";}
    if (IsPlaying=="paused")    {return "pause.gif";}
    if (IsPlaying=="recording") {return "record.gif";}
    alert("GetCurrentPlayingImage() IsPlaying is set to something weird: '"+IsPlaying+"'");
    }


//**************************
// Playlist
//**************************

function AddItemToPlaylist(Item)
    {
//    alert("PlayingPlaylist="+PlayingPlaylist);
//    if (PlayingPlaylist)
//        {
//        AddItemToPlayingList(Item,PlaylistMenu.Items.length);
//        }
    var NewItem=Item.Duplicate();
//    NewItem.File=Item.File;
    PlaylistMenu.AddItem(NewItem).Selected=false;
    }

//**************************
//     MomentaryReturnUI
//**************************
function ReturnFromMomentaryUI()
    {
//    alert("ReturnFromAddedJBMMUI1");
    if (MomentaryReturnUI)
        {
        CurrentJBMMUI=MomentaryReturnUI;
        RefreshDisplay();
        MomentaryReturnUI=null;
        }
    }


function GotoSetupMomentaryInteranal(Line1,Line2)
    {
    MomentaryReturnUI=CurrentJBMMUI;
    CurrentJBMMUI=AddedJBMMUI;
    MomentaryLine1="";
    MomentaryLine2=Line1;
    MomentaryLine3=Line2;
    MomentaryLine4="";

    setTimeout(ReturnFromMomentaryUI,CurrentMomentaryDelay.CurrentIndex*1000);
    RefreshDisplay();
    }

function GotoUserALoadedUI()    {GotoSetupMomentaryInteranal("User Settings A","Loaded");}
function GotoUserBLoadedUI()    {GotoSetupMomentaryInteranal("User Settings B","Loaded");}
function GotoUserASavedUI()     {GotoSetupMomentaryInteranal("User Settings A","Saved");}
function GotoUserBSavedUI()     {GotoSetupMomentaryInteranal("User Settings B","Saved");}
function GotoDefaultsLoadedUI() {GotoSetupMomentaryInteranal("Factory Defaults","Loaded");}

function GotoAddedJBMMUI()
    {
    MomentaryReturnUI=CurrentJBMMUI;
//    alert("GotoAddedJBMMUI 1");
//    alert(AddedJBMMUI.Name);
    CurrentJBMMUI=AddedJBMMUI;
//    alert("GotoAddedJBMMUI 2");
    RecalcTotalPlaylistTime();
    MomentaryLine1="Added "+NumAdded+" items";
    MomentaryLine2="Added "+GetTimeStr(TimeAdded);
    MomentaryLine3="Total "+PlaylistMenu.Items.length+" items";
    MomentaryLine4="Total "+GetTimeStr(TotalPlaylistTime);

    setTimeout(ReturnFromMomentaryUI,CurrentMomentaryDelay.CurrentIndex*1000);
    RefreshDisplay();
    }
//**************************
function ReturnFromVolume()
    {
    if (ReturnFromVolumeUI)
        {
        CurrentJBMMUI=ReturnFromVolumeUI;
        ReturnFromVolumeUI=null;
        RefreshDisplay();
        }
    }

function VolumeTimeOut()
    {
//    alert("VoluemTimeOut="+VolumeExitCount);
    VolumeExitCount+=1;
    if (VolumeExitCount>=CurrentMomentaryDelay.CurrentIndex)
        {
        ReturnFromVolume();
        }
    else
        {
        setTimeout(VolumeTimeOut, 1000);
        }
    }

function GotoVolumeMenu()
    {
    ReturnFromVolumeUI=CurrentJBMMUI;
    CurrentJBMMUI=VolumeMenuUI;
    setTimeout(VolumeTimeOut, 1000);
    ResetVolumeExitCount();
    RefreshDisplay();
    }


//**************************
//**************************
/*function EditCurrentImage()
    {
    if (CurrentJBMMUI.CurrentMenu.Items.length>0)
        {
        var Item=CurrentJBMMUI.CurrentMenu.Items[CurrentJBMMUI.CurrentMenu.CursorPos];
        if (Item.File.Extension=="jpg")
            {
            EditingImageItem=Item;
            ReturnFromImageEditor=CurrentJBMMUI;
            CurrentJBMMUI=ImageEditorUI;
            RefreshDisplay();
            }
        }
    }*/
//**************************

var LoadingVal=0;


function TurnOffJBMM()
    {
    CurrentJBMMUI=OffUI;
    JBMMCurrentlyOn=false;
    RefreshDisplay();
    }

function FinalizeTurnOnJBMM()
    {
    CurrentDisplayingImage=null;
//    alert("FinalizeTurnOnJBMM IsPlaying="+IsPlaying);
    if (IsPlaying=="paused")
        {
        CurrentJBMMUI=PlayUI;
        }
    else
        {
        ResetPlayingList();
        MediaLibUI.CurrentMenu=MediaLibMenu;
        FilesUI.CurrentMenu=FilesTopMenu;
        CurrentJBMMUI=TopUI;
        }
    JBMMCurrentlyOn=true;
    JBMMRealtime();
    RefreshDisplay();
    }

function AdvanceLoadingVal()
    {
    LoadingVal+=.05;
//    LoadingVal+=.21;
    if (LoadingVal>=1)
        {
        FinalizeTurnOnJBMM();
        }
    else
        {
        RefreshDisplay();
        setTimeout(AdvanceLoadingVal, 50);
        }
    }

function TurnOnJBMM()
    {
    LoadingVal=0;
    CurrentJBMMUI=LoadingUI;
    AdvanceLoadingVal();
    }


//**************************

function GotoRecordingMenu()
    {
//    alert("GotoRecordingMenu");
    ReturnFromMenuUI=CurrentJBMMUI;
    CurrentJBMMUI=RecordingMenuUI;
//    alert("GotoRecordingMenu");
    RefreshDisplay();
    }

function GotoPlayingMenu()
    {
    ReturnFromMenuUI=CurrentJBMMUI;
    CurrentJBMMUI=PlayingMenuUI;
    RefreshDisplay();
    }

function GotoImageEditMenu()
    {
    ReturnFromMenuUI=CurrentJBMMUI;
    CurrentJBMMUI=ImageEditMenuUI;
    RefreshDisplay();
    }

function GotoPLMenu()
    {
    ReturnFromMenuUI=CurrentJBMMUI;
    CurrentJBMMUI=PLMenuUI;
    RefreshDisplay();
    }

function GotoBrowserMenu()
    {
    ReturnFromMenuUI=CurrentJBMMUI;
    CurrentJBMMUI=BrowserMenuUI;
    RefreshDisplay();
    }

function ReturnFromMenuOrGoUp()
    {
    if (CurrentJBMMUI.CurrentMenu.Parent)
        CurrentJBMMUI.CurrentMenu=CurrentJBMMUI.CurrentMenu.Parent;
    else
        CurrentJBMMUI=ReturnFromMenuUI;
    RefreshDisplay();
    }

function ReturnFromMenu()
    {
    while (CurrentJBMMUI.CurrentMenu.Parent)
        CurrentJBMMUI.CurrentMenu=CurrentJBMMUI.CurrentMenu.Parent;
    CurrentJBMMUI=ReturnFromMenuUI;
    RefreshDisplay();
    }
