﻿/*function openMIPopup(url)
{
    mywindow = window.open (url, "mipopup","location=no,resizable=yes,menubar=no,status=no,toolbar=no,scrollbars=yes,width=860,height=744");
    //mywindow.moveTo(0,0);
}*/

function isPlayerOpen()
{
    return $('#bottom_slidebox')[0].style.bottom=='0px';
}

function togglePlayerVis()
{
    if (isPlayerOpen()) closePlayer(); else openPlayer();
}

function openPlayer()
{
    if (!isPlayerOpen())
    {
        $('#chupchik').addClass('is_open');
    
        //$('#bottom_slidebox #chupchik a')[0].innerHTML = "סגור נגן";
        $('#bottom_slidebox').stop().animate({bottom:'0px'},{queue:false,duration:160});
    }
}

function closePlayer()
{
    if (isPlayerOpen())
    {
        $('#chupchik').removeClass('is_open');
    
        //$('#bottom_slidebox #chupchik a')[0].innerHTML = "פתח נגן";
        $('#bottom_slidebox').stop().animate({bottom:'-71px'},{queue:false,duration:160}); /*must be same value as in layout.css #chupchik height */
    }
}
//local utility:
function isInteger(s) {
  return (s.toString().search(/^-?[0-9]+$/) == 0);
}

//this allows double click on the same link to pause/play:
//TODO: in class_helers: var nowPlayingCB sometimes doesnt exist (from a closed pop-up) and sometimes it is inaccessible due to security:
//         added try/catch  on class helpers for now
var nowPlayingCB = null;
var nowPlayingCB_isPaused = false;
function playAudioTrackFromCB(cb, playlist)
{
    //called from hover_box
    if (nowPlayingCB != cb)
    {
        //if changed  remove the last CB's now_playing state
        if (nowPlayingCB != null)
        {   
            removeClass(nowPlayingCB, "now_playing");
        }
    
        nowPlayingCB_isPaused = false;
        nowPlayingCB = cb;
        
        
        
        //playlist can be also an idx to local js array 'page_playlist':
        if (isInteger(playlist))
        { //idx
		    playListChange( playlist );
        }
        else
        { //playlist
            playAudioPlaylist(playlist); //play
        }
    }
    else
    {
        if (nowPlayingCB_isPaused)
        {
            $("#jquery_jplayer").play();
        }
        else
        {
            $("#jquery_jplayer").pause();
        }
        nowPlayingCB_isPaused = !nowPlayingCB_isPaused;
    }
}

function pausePlayer()
{
    $("#jquery_jplayer").pause();
    nowPlayingCB_isPaused = true;
}


function playAudioTrack(songName, mp3url)
{
    //openPlayer();
    var newPlaylist = [{name: songName, mp3: mp3url}];
    //setNewPlaylist(newPlaylist, true);
    playAudioPlaylist(newPlaylist)
    return false;
}

function playAudioPlaylist(playlist)
{
    openPlayer();
    setNewPlaylist(playlist, true);
    return false;
}








$(document).ready(function()
{
    //player chupchik
	$('#bottom_slidebox #chupchik').click(function()
    {
        togglePlayerVis();
    });

    //Player init from here:
    var activePlaylistItem = 0;
    var activePlaylist = [];

    $("#jquery_jplayer").jPlayer({
        ready: function() {

            playListInit(false); // Parameter is a boolean for autoplay.
        },
        volume: 75,
        oggSupport: false,
        //cssPrefix: "different_prefix_example",
        //swfPath: "http://82.80.235.22/static/ext/jplayer",
        //swfPath: "http://localhost:1123/Pat.Web/static/ext/jplayer",
        swfPath: app_swfPath, //Dan change
        oggSupport: false
    })
    .jPlayerId("play", "player_play")
    .jPlayerId("pause", "player_pause")
    .jPlayerId("stop", "player_stop")
    .jPlayerId("loadBar", "player_progress_load_bar")
    .jPlayerId("playBar", "player_progress_play_bar")
    .jPlayerId("volumeMin", "player_volume_min")
    .jPlayerId("volumeMax", "player_volume_max")
    .jPlayerId("volumeBar", "player_volume_bar")
    .jPlayerId("volumeBarValue", "player_volume_bar_value")
    .onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
        var myPlayedTime = new Date(playedTime);
        var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
        var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
        $("#play_time").text(ptMin+":"+ptSec);

        var myTotalTime = new Date(totalTime);
        var ttMin = (myTotalTime.getUTCMinutes() < 10) ? "0" + myTotalTime.getUTCMinutes() : myTotalTime.getUTCMinutes();
        var ttSec = (myTotalTime.getUTCSeconds() < 10) ? "0" + myTotalTime.getUTCSeconds() : myTotalTime.getUTCSeconds();
        $("#total_time").text(ttMin+":"+ttSec);
    })
    .onSoundComplete( function() {
        //Dan: added if:
        if (activePlaylist!=null && activePlaylist.length>2)
            playListNext();
    });         
    $("#ctrl_prev").click( function() {
        playListPrev();
        return false;
    });         
    $("#ctrl_next").click( function() {
        playListNext();
        return false;
    });
    
    //Dan:
    $("#player_play").click( function() {
        //duplicated on release view (if play was pushed from real button, instead of CB)
        try
        {
            if (window && window.opener && window.opener.pausePlayer)
            {
                window.opener.pausePlayer();
            }        
        }
        catch (e) {}
    
    
    });
    /*function displayPlayList() {
        for (i=0; i < activePlaylist.length; i++) {
            $("#playlist_list ul").append("<li id='playlist_item_"+i+"'>"+ activePlaylist[i].name +"</li>");
            $("#playlist_item_"+i).data( "index", i ).hover(
                function() {
	                if (activePlaylistItem != $(this).data("index")) {
		                $(this).addClass("playlist_hover");
	                }
                },
                function() {
	                $(this).removeClass("playlist_hover");
                }
            ).click( function() {
                var index = $(this).data("index");
                if (activePlaylistItem != index) {
	                playListChange( index );
                } else {
	                $("#jquery_jplayer").play();
                }
            });
        }
    }*/

    function playListInit(autoplay) {
        if(autoplay) {
            playListChange( activePlaylistItem );
        } else {
            playListConfig( activePlaylistItem );
        }
    }

    function playListConfig( index ) {
        //$("#playlist_item_"+activePlaylistItem).removeClass("playlist_current");
        //$("#playlist_item_"+index).addClass("playlist_current");
        activePlaylistItem = index;
        
        //Dan patch: doesnt always have a activeplaylist:
        
        if (activePlaylist !=null && activePlaylist[activePlaylistItem]!=null)
        {
            //alert(activePlaylist[activePlaylistItem].mp3);
            $("#jquery_jplayer").setFile(activePlaylist[activePlaylistItem].mp3); //, activePlaylist[activePlaylistItem].ogg);
        	
            //Dan:
            $("#song_title").text(activePlaylist[activePlaylistItem].name);
        }
        
    }

    //Dan 25feb10: moved outside of jquery block:
    //function playListChange( index ) {
    //    playListConfig( index );
    //    $("#jquery_jplayer").play();
    //}

    function playListNext() {
        //Dan: added if 
        if (activePlaylist!=null && activePlaylist.length!=0)
        {
            var index = (activePlaylistItem+1 < activePlaylist.length) ? activePlaylistItem+1 : 0;
            playListChange( index );
            
            //Dan 25feb10:
            updateNextOrPrevCB(index);
        }
    }

    function playListPrev() {
        //Dan: added if 
        if (activePlaylist!=null && activePlaylist.length!=0)
        {
            var index = (activePlaylistItem-1 >= 0) ? activePlaylistItem-1 : activePlaylist.length-1;
            playListChange( index );
            
            //Dan 25feb10:
            updateNextOrPrevCB(index);
        }
    }
    
    function updateNextOrPrevCB(newIndex)
    {
        //Dan 25feb10: for the release-view page, this will toggle the new relevant 'now_playing' CB
        try
        {
            if (page_playlist!=null)
            {
                //removing old 'now_playing'
                if (nowPlayingCB != null)
                {   
                    removeClass(nowPlayingCB, "now_playing");
                }
                
                //TODO: updating new 'now_playing':
                //var clientId = nowPlayingCB.id; //ctl00_ctl00_phM_phM_uxTracksGrid_ctl02_uxTitleLink for the first one
                //var rowIdx = 
                //alert(clientId);
                
                //TODO: find the new CB, and put 'now_playing' on it.               
                nowPlayingCB = null;
                nowPlayingCB_isPaused = false;
            }
        }
        catch (e) {}
    }

    //Dan:
    setNewPlaylist = function(newPlayList, autoPlay)
    {
        activePlaylist = newPlayList;
        playListInit(autoPlay);
    }
    
    //Dan 25feb10: moved inline function in jquery block:
    playListChange = function( index ) 
    {
        playListConfig( index );
        $("#jquery_jplayer").play();
    }    
});
