////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 9/1/2015	ELL

var Playlist = new Array();
var audio_files = new Array();
var video_files = new Array();

function BuildPlayList(j, player, folder)
{
	for (i = 0; i < audio_files.length; i++)
	{
		if (audio_files[i].player == player)
		{
			switch (audio_files[i].ext)
			{
				case "mp3"	:
					Playlist[j].add(
					{
						title:String(audio_files[i].title),
						artist:audio_files[i].artist,
						mp3:media_dir_rel + folder + "/" + audio_files[i].file,
						poster:audio_files[i].poster
					}
					);
					break;
				case "m4a"	:
					Playlist[j].add(
					{
						title:audio_files[i].title,
						artist:audio_files[i].artist,
						m4a: media_dir_rel + folder + "/" + audio_files[i].file,
						poster: audio_files[i].poster
					}
					);
					break;
				case "oga"	:
					Playlist[j].add(
					{
						title:audio_files[i].title,
						artist:audio_files[i].artist,
						oga: media_dir_rel + folder + "/" + audio_files[i].file,
						poster: audio_files[i].poster
					}
					);
					break;
				case "wma"	:
					Playlist[j].add(
					{
						title:audio_files[i].title,
						artist:audio_files[i].artist,
						wma: media_dir_rel + folder + "/" + audio_files[i].file,
						poster: audio_files[i].poster
					}
					);
					break;
				case "webma"	:
					Playlist[j].add(
					{
						title:video_files[i].title,
						artist:video_files[i].artist,
						webma: media_dir_rel + folder + "/" + video_files[i].file,
						poster: video_files[i].poster
					}
					);
					break;
				case "wav"	:
					Playlist[j].add(
					{
						title:video_files[i].title,
						artist:video_files[i].artist,
						wav: media_dir_rel + folder + "/" + video_files[i].file,
						poster: video_files[i].poster
					}
					);
					break;
				case "fla"	:
					Playlist[j].add(
					{
						title:String(video_files[i].title),
						artist:video_files[i].artist,
						fla:media_dir_rel + folder + "/" + video_files[i].file,
						poster:video_files[i].poster
					}
					);
					break;
				case "rtmpa"	:
					Playlist[j].add(
					{
						title:String(video_files[i].title),
						artist:video_files[i].artist,
						rtmpa:media_dir_rel + folder + "/" + video_files[i].file,
						poster:video_files[i].poster
					}
					);
					break;
				default	:
					Playlist[j].add(
					{
						title:audio_files[i].title,
						artist:audio_files[i].artist,
						mp3: media_dir_rel + folder + "/" + audio_files[i].file,
						poster: audio_files[i].poster
					}
					);
			}
		}
	}
	
	for (i = 0; i < video_files.length; i++)
	{
		if (video_files[i].player == player)
		{
			switch (video_files[i].ext)
			{
				case "flv"	:
					Playlist[j].add(
					{
						title:String(video_files[i].title),
						artist:video_files[i].artist,
						flv:media_dir_rel + folder + "/" + video_files[i].file,
						poster:video_files[i].poster
					}
					);
					break;
				case "m4v"	:
					Playlist[j].add(
					{
						title:video_files[i].title,
						artist:video_files[i].artist,
						m4v: media_dir_rel + folder + "/" + video_files[i].file,
						poster: video_files[i].poster
					}
					);
					break;
				case "ogv"	:
					Playlist[j].add(
					{
						title:video_files[i].title,
						artist:video_files[i].artist,
						ogv: media_dir_rel + folder + "/" + video_files[i].file,
						poster: video_files[i].poster
					}
					);
					break;
				case "webmv"	:
					Playlist[j].add(
					{
						title:video_files[i].title,
						artist:video_files[i].artist,
						webmv: media_dir_rel + folder + "/" + video_files[i].file,
						poster: video_files[i].poster
					}
					);
					break;
				case "rtmpv"	:
					Playlist[j].add(
					{
						title:String(video_files[i].title),
						artist:video_files[i].artist,
						rtmpv:media_dir_rel + folder + "/" + video_files[i].file,
						poster:video_files[i].poster
					}
					);
					break;
				default	:
					Playlist[j].add(
					{
						title:video_files[i].title,
						artist:video_files[i].artist,
						m4v: media_dir_rel + folder + "/" + video_files[i].file,
						poster: video_files[i].poster
					}
					);
			}
		}
	}
}

function GetMediaFileInfo()
{
	for (var i = 0; i < media_players.length; i++)
	{
		$.post( "/php/get_media_info.php", { player: media_players[i].player, folder: media_players[i].folder, media_dir: media_dir } ).done(CompleteGetMediaFileInfo) ;
	}
}
function CompleteGetMediaFileInfo(data)
{
	var data_arr = data.split(":::::");
	var audio_rows = data_arr[0].split("@@@@@");
	var video_rows = data_arr[1].split("@@@@@");
	if (audio_rows != "")
	{
		for (var i = 0; i < audio_rows.length; i++)
		{
			var JSON_row = JSON.parse(audio_rows[i]);
			audio_files[audio_files.length] = {player: JSON_row.player, title: JSON_row.title, artist: JSON_row.artist, file: JSON_row.file, ext: JSON_row.ext, poster: JSON_row.poster};
		}
	}
	
	if (video_rows != "")
	{
		for (var i = 0; i < video_rows.length; i++)
		{
			var JSON_row = JSON.parse(video_rows[i]);
			video_files[video_files.length] = {player: JSON_row.player, title: JSON_row.title, artist: JSON_row.artist, file: JSON_row.file, ext: JSON_row.ext, poster: JSON_row.poster};
		}
	}
}

GetMediaFileInfo();

$(document).ready(function()
{
	for (var i = 0; i < media_players.length; i++)
	{
		Playlist[i] = new jPlayerPlaylist({
			jPlayer: media_players[i].player,
			cssSelectorAncestor: media_players[i].css_selector
		},
		/*
		  [
			{
				title:"Sample",
				artist:"The Samples",
				mp3:media_dir_rel + "sample.mp3",
				poster:media_dir_rel + "sample.jpg"
			}
		  ],
		  */
		{
			playlistOptions: {
				enableRemoveControls: true
			},
			swfPath: "./scripts/jplayer/jplayer",
			supplied: "webmv, ogv, m4v, oga, mp3, m4a, webma, wav, fla, flv, rtmpa, rtmpv",
			useStateClassSkin: true,
			autoBlur: false,
			smoothPlayBar: true,
			keyEnabled: true,
			audioFullScreen: true
		});
	
		var code = "BuildPlayList(" + i + ", '" + media_players[i].player + "', '" + media_players[i].folder + "');";
		setTimeout(code, 5000);
	}
}
);