var curpath = "/";

// This function is executed after the page load completes on the client
$(document).ready(function() {
   $("#path").click(function() { changeFolder("/", "/"); });
   getFolderContents();
});

var lightbox = null;

function getFolderContents() {
   $("#list").html("");
   $.ajax({
      url      : 'ajax/getfoldercontents.php',
      dataType : 'json',
      success  : function(data, stat, jqo) {
                    curpath = data.compath;
                    updatePathNavigator();
                    data.contents.forEach(function(entry, index) {
                       $("#list").append(entry.div);
                    });
                    $(".folder").click(function() { changeFolder($(this).data("name"), $(this).data("path")); });
                    $(".issue").click(function() { $(this).parent().addClass("readborder"); showComic($(this).data("relcomic"), $(this).data("comname")); });
                    toastr.remove();
                 }
   });
}

function updatePathNavigator() {
   var navpath = "";
   if ( curpath == "/" ) {
      navpath = "/";
   } else {
      navpath = "/Comics";
   }
   $("#path").html(navpath);
}

function changeFolder(name, path) {
   if ( path == curpath ) return;
   toastr.success("Opening comic \"" + name + "\"<br>If this is the first time it may take a while to generate thumbnails.");
   $.ajax({
      url      : 'ajax/setpath.php',
      data     : {path: path},
      dataType : 'json',
      success  : function(data, stat, jqo) {
                    console.log(data.message);
                    curpath = path;
                    getFolderContents();
                 }
   });
}

function updateCurrentPage() {
   var currentPage = lightbox.currentPosition;
   $.ajax({
      url      : 'ajax/setpage.php',
      data     : {page: currentPage},
      dataType : 'json',
      success  : function(data, stat, jqo) {
                    console.log(data.message);
                 }
   });
}

//
// This JS function is called when a user clicks on a comic.
// "showcomic.php" is called with the comic to view as a parameter.
// It returns a JSON object representing the collection of images.
// If in debug mode the javascript code is displayed in the browser.
// If not in debug mode a lightbox object is created to display the comic.
//
function showComic(comic, name) {
   toastr.success("Extracting and showing the comic \"" + name + "\"...", "Showing Comic");
   $.ajax({
      url      : 'ajax/showcomic.php',
      data     : {comic: comic},
      dataType : 'json',
      success  : function(data, stat, jqo) {
                    // Clear out the debug DIV and start the fancybox.
                    $("#debug").html("");
                    lightbox = SimpleLightbox.open({
                       items: data.images,
                       captions: data.captions,
                       startAt: data.startindex,
                       beforeSetContent: updateCurrentPage
                    });
                 }
   });
}