﻿function registerNS(ns) {
    var nsParts = ns.split(".");
    var root = window;

    for (var i = 0; i < nsParts.length; i++) {
        if (typeof root[nsParts[i]] == "undefined")
            root[nsParts[i]] = new Object();

        root = root[nsParts[i]];
    }
}

registerNS("String");
String.Format = function() {
    var s = arguments[0];
    for (var i = 0; i < arguments.length - 1; i++) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        s = s.replace(reg, arguments[i + 1]);
    }

    return s;
}

registerNS("SoundBuildOff");


SoundBuildOff.ShowItem = function(itemId, title, sequenceId, sequenceSize) {
    if (title == undefined || title == "")
        title = "&nbsp;";

    Boxy.load("/galleryitems/view/" + itemId + "?sequenceId=" + sequenceId + "&sequenceSize=" + sequenceSize,
          {
              modal: true,
              title: title,
              unloadOnHide: true,
              afterShow: function() {
                  // Set total items in sequence and build controls
                  $("#sequenceSize").val(sequenceSize);
                  SoundBuildOff.BuildPopupControls();
              }
          }
      );
}

SoundBuildOff.GetNextPhoto = function(sequenceId, galleryId, boxyInstance) {
    $.post("/galleryitems/nextphoto", { sequenceId: sequenceId, galleryId: galleryId },
            function(data) {
                if (data.Result != 0) {
                    // There was an error for whatever reason
                }
                else {
                    var sequenceSize = parseInt($("#sequenceSize").val());
                    url = "/galleryitems/view/" + data.AdditionalData.GalleryItemId + "?sequenceId=" + sequenceId + "&sequenceSize=" + sequenceSize;
                    $.get(url, function(data) {
                        boxyInstance.setContent(data);
                        $("#sequenceSize").val(sequenceSize);
                        SoundBuildOff.BuildPopupControls();
                    });
                }
            }, "json");
}

SoundBuildOff.BuildPopupControls = function() {
    var thisSequenceId = parseInt($("#sequenceId").val());
    var thisGalleryId = parseInt($("#galleryId").val());
    var nextSequenceId = 0;
    var previousSequenceId = 0;
    var sequenceSize = parseInt($("#sequenceSize").val());

    // Hide previous if first image
    if (thisSequenceId == 0)
        $(".popupPrevious").addClass("invisible");
    else {
        $(".popupPrevious").removeClass("invisible");
        previousSequenceId = thisSequenceId - 1;
    }

    // Hide next if last image
    if (thisSequenceId == sequenceSize - 1)
        $(".popupNext").addClass("invisible");
    else {
        $(".popupNext").removeClass("invisible");
        nextSequenceId = thisSequenceId + 1;
    }

    // Assign next and previous controls
    $(".galleryPrevious").unbind();
    $(".galleryPrevious").click(function() {
        SoundBuildOff.GetNextPhoto(previousSequenceId, thisGalleryId, Boxy.get(this));
    });

    $(".galleryNext").unbind();
    $(".galleryNext").click(function() {
        SoundBuildOff.GetNextPhoto(nextSequenceId, thisGalleryId, Boxy.get(this));
    });
}
