﻿
function LightboxBrowser() {

    this.bookmark = (document.location.hash ? document.location.hash : (document.location.href.indexOf("#") > 0 ? "#" : ""));

    this.LightboxId = null;    
    this.ImagePanelId = null;
    this.AjaxLoadingPanelId = null;
    this.pageCount = 1;
}

LightboxBrowser.prototype.show = function() {
    document.getElementById(this.ImagePanelId).style.visibility = "visible";
    document.getElementById(this.AjaxLoadingPanelId).style.display = "none";
}


LightboxBrowser.prototype.goTop = function() {
    $('html, body').animate({ scrollTop: 150 }, 0);
}

// reload the search wi the the new settings
LightboxBrowser.prototype.LoadPage = function(pageNumber, imageId) {
    this.goTop();
    var newbookmark = (imageId == null ? "#" + pageNumber : "#" + pageNumber + "|" + imageId);
    
    //Load using ajax
    var onSuccessClientScript = "lbBrowser.show(); lbBrowser.loadComplete('" + newbookmark + "');";
    var onFailureClientScript = "alert('Error loading image " + pageNumber + ". Please try again later.'); imageViewer.HideLoader();";

    var params = "";
    params += "LightboxId=" + this.LightboxId;
    params += "&ImageId=" + (imageId == null ? "" : imageId);
    params += "&PageNumber=" + pageNumber;
    params += "&ContentPanelId=" + this.ImagePanelId;
    params += "&onSuccessClientScript=" + onSuccessClientScript;
    params += "&onFailureClientScript=" + onFailureClientScript;

    hcAjax.call("/webservices/LightboxBrowser.asmx/LoadImage", params, "lbBrowser.showLoader()", null, true);

    return false;
}

LightboxBrowser.prototype.hrefWithoutHash = function() {
    return document.location.href.indexOf("#") > 0 ? (document.location.href.substring(0, document.location.href.indexOf("#"))) : document.location.href;
}

LightboxBrowser.prototype.loadComplete = function(newbookmark) {

    $.history.register(newbookmark);
    this.bookmark = newbookmark;
    $('.inner').corner().parent().css('padding', '2px').corner();
}

LightboxBrowser.prototype.showLoader = function() {
    /*document.getElementById(this.ImagePanelId).style.visibility = "hidden";
    document.getElementById(this.AjaxLoadingPanelId).style.display = "block";*/
}

LightboxBrowser.prototype.checkBookmark = function() {

    if (this.bookmark == "" || this.bookmark == "#") {
        //there is no bookmark to load
        this.show();
    }
    else {
        //#43|0a02e71993d24e2bba8d31f989eadfe5   (Page Number | Image id)
        var re = new RegExp(/^#\d+(\|\S{32})?$/);
        if (this.bookmark.match(re)) {

            var currentPage = new RegExp(/\d+/).exec(this.bookmark)[0];
            var imageId = new RegExp(/(\|\S{32})?/).exec(this.bookmark)[0]

            imageId = imageId.substring(1);

            var onSuccessClientScript = "lbBrowser.show();";
            var onFailureClientScript = "lbBrowser.show();";

            var params = "";
            params += "LightboxId=" + this.LightboxId;
            params += "&ImageId=" + imageId;
            params += "&PageNumber=" + currentPage;
            params += "&ContentPanelId=" + this.ImagePanelId;
            params += "&onSuccessClientScript=" + onSuccessClientScript;
            params += "&onFailureClientScript=" + onFailureClientScript;

            var beforeFunction = "document.getElementById('" + this.AjaxLoadingPanelId + "').style.display = 'block';";

            hcAjax.call("/webservices/LightboxBrowser.asmx/LoadImage", params, beforeFunction, null, true);

        }
        else this.show();
    }

}

LightboxBrowser.prototype.init = function(LightboxId, ImagePanelId, AjaxLoadingPanelId) {
    //If there is a # in the qs, load that page number and image number

    this.LightboxId = LightboxId;
    this.ImagePanelId = ImagePanelId;
    this.AjaxLoadingPanelId = AjaxLoadingPanelId;
    this.checkBookmark();
}

LightboxBrowser.prototype.isANumber = function(value) {
    return (value.match(/^\d+$/) && value > 0 && value < this.pageCount + 1)
}

LightboxBrowser.prototype.pageNumberKeyPress = function(event, pageBox) {

    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

    if (keyCode == 13) {
        if (this.isANumber(pageBox.value)) {
            lbBrowser.LoadPage(pageBox.value, null);
            return false;
        }
        else {
            alert('Please enter a number between 1 and ' + this.pageCount);
        }

        return false;
    }
}

LightboxBrowser.prototype.SaveNotes = function(ImageNotesId, imageid) {
    //makes an ajax call to add the lightbox images to a zip file and return the link to the zip file
var beforeFunction = "document.getElementById('" + ImageNotesId + "').disabled = true;"; //"lbEffexts.startLoading('" + this.ImageNotesLoaderId + "');";
var afterFunction = "document.getElementById('" + ImageNotesId + "').disabled = false;";  //"lbEffexts.stopLoading('" + this.ImageNotesLoaderId + "');";
    var onSuccessClientScript = null; //"usrLboxes.closeLightboxNotes();";

    var params = "LightboxId=" + this.LightboxId + "&ImageId=" + imageid + "&Notes=" + encodeURIComponent(document.getElementById(ImageNotesId).value) + "&onSuccessClientScript=" + onSuccessClientScript;
    hcAjax.call("/webservices/Lightboxes.asmx/SaveImageNotes", params, beforeFunction, afterFunction, true);

    return false;
}


LightboxBrowser.prototype.HistoryChanged = function(hash) {

    /*       
    This method is called when the user clicks the history button        
    */

    if (hash != "top") {
        var currentPage = -1;
        var imageId = "";

        if (hash == "" || hash == "#") {
            //there is no bookmark to load
            //load by data in url

            currentPage = getQuerystring('imgidx', "0");
            imageId = getQuerystring('lbimg', "");

        }
        else {

            hash = "#" + hash;
            var re = new RegExp(/^#\d+(\|\S{32})?$/);
            if (hash.match(re)) {

                var RegMatches = null;
                RegMatches = new RegExp(/\d+/).exec(hash);
                currentPage = RegMatches[0];

                RegMatches = new RegExp(/(\|\S{32})?/).exec(hash);
                imageId = RegMatches.length > 0 ? RegMatches[0].substring(1) : "";

            }
        }

        if (currentPage.length > 0 && currentPage > -1) {
            var onSuccessClientScript = "lbBrowser.show();";
            var onFailureClientScript = "alert('Error loading image " + currentPage + ". Please try again later.'); imageViewer.HideLoader();";

            var params = "";
            params += "LightboxId=" + this.LightboxId;
            params += "&ImageId=" + imageId;
            params += "&PageNumber=" + currentPage;
            params += "&ContentPanelId=" + this.ImagePanelId;
            params += "&onSuccessClientScript=" + onSuccessClientScript;
            params += "&onFailureClientScript=" + onFailureClientScript;

            var beforeFunction = "document.getElementById('" + this.AjaxLoadingPanelId + "').style.display = 'block';";

            hcAjax.call("/webservices/LightboxBrowser.asmx/LoadImage", params, "lbBrowser.showLoader()", null, true);

        }
        else this.show();
    }
}

var lbBrowser = new LightboxBrowser();



