// Package BAM_ERMES_DETAIL / Copyright 2025 Archimed SA / JSE

//loading package...
packages.acknowledge('BAM_ERMES_DETAIL');

// file: detail.js

var ErmesManager = (function (pub) {

    pub.CurrentSearchType = ErmesManager.SearchType.DetailStatic;

    return pub;
} (ErmesManager));

/////////////////////////////////////////////////////////
// Classe 'Form'
ErmesManager.Form = function () {
    this.Query = { query: {} };
    this.EnrichmentLoaded = false;

    //Binding event searchComplete
    $(this).bind("searchCompleted", ErmesManager.event.onSearchComplete);

    this.doSearch = function ($container) {

        ErmesManager.beforeSearch();

        var self = this;

        var callback = function (json) {

            if (!ErmesManager.checkResponse(json)) {
                return;
            }

            //self.Query.query = json.d.Query;
            self.Info = json.d.Infos;
            self.Result = json.d.Result;
            self.FriendlyTitleResults = json.d.FriendlyTitleResults;

            $container.html(json.d.HtmlResult);

            $(self).trigger("searchCompleted", $container);

            try {
                JSPortal_resize(null);
            }
            catch (e) {
                //Nothing
            }
            return false;
        };

        $.ajax(
	    {
	        url: "/" + CFInstance + "/Ermes/Recherche/Openfind.svc/GetOpenfindRecord",
	        data: JSON.stringify(this.Query),
	        success: callback
	    });
    };
};

//H?rite de 'BaseForm' (common.js)
ErmesManager.Form.prototype = ErmesManager.BaseForm;
ErmesManager.Form.prototype.parent = ErmesManager.BaseForm;
ErmesManager.Form.prototype.constructor = ErmesManager.Form;

/////////////////////////////////////////////////////////
// Sous-module 'event'
ErmesManager.event = (function (pub) {

    var old_onSearchComplete = pub.onSearchComplete; //H?ritage

    pub.onSearchComplete = function (event, $container) {
        $("#notice_longue").data("result", this.Result);

        if (ErmesManager.isDefined(this.Result.Resource)) {
            $.address.title(ErmesManager.detailTitle.replace("{0}", this.Result.Resource.Ttl));
        }

        var Record = { Record: { RscId: this.Result.Resource.RscId, Docbase: this.Result.Resource.RscBase} };
        this.getHoldingsFull($container, Record);

        var $form = ErmesManager.findClosestContainer($container);
        var data = $form.data("form");

        if (data.EnrichmentLoaded === false) {
            var title = ErmesManager.isDefined(data.Result.More.Title) ? data.Result.More.Title : null;
            var author = ErmesManager.isDefined(data.Result.More.Author) ? data.Result.More.Author : null;
            var isbn = ErmesManager.isDefined(data.Result.More.Isbn) ? data.Result.More.Isbn : null;
            var isbnList = ErmesManager.isDefined(data.Result.More.IsbnList) ? data.Result.More.IsbnList : null;
            var ean = ErmesManager.isDefined(data.Result.More.Ean) ? data.Result.More.Ean : null;
            var format = ErmesManager.isDefined(data.Result.More.Format) ? data.Result.More.Format : null;


            var query = { query: { Title: title, Author: author, Isbn: isbn, Ean: ean, Format: format, IsbnList: isbnList} };

            data.getEnrichment($container, query);
        }

        // Execution fancybox sur contenu ajax
        $("a.fancybox", $container).fancybox();

        // Restaure la transparence ? 0
        $($container).fadeTo(0, 1);

        $("div#images_scroll").smoothDivScroll({ visibleHotSpots: "always" });

        //SmoothDivScroll (pour les titres amis)
        ErmesManager.afterImgLoad("#images_scroll img", function () {
            $("div#images_scroll").smoothDivScroll("recalculateScrollableArea");
            $("div#images_scroll").smoothDivScroll("showHotSpotBackgrounds");
        });

        //qTip sur les titres amis
        $("img", "div#images_scroll").each(function (i, el) {
            $(this).qtip({
                content: data.FriendlyTitleResults[i].CustomResult,
                position: {
                    my: 'top left',
                    at: 'bottom left',
                    viewport: $(window),
                    adjust: { x: 4, y: 0 }
                },
                style: {
                    classes: 'ui-tooltip-light ui-tooltip-shadow ui-tooltip-ermes' // Inherit from preset style
                },
                show: { solo: true },
                hide: { fixed: true }
            });
        });

        //Addthis
        if (typeof (addthis) !== 'undefined' && this.Result.Resource !== 'undefined') {
            addthis.toolbox(".addthis_toolbox");
        }
        else {
            $("li.partager_document, li.like_document", "#document_actions").remove();
        }

        old_onSearchComplete(event, $container);
    };

    return pub;
} (ErmesManager.event));

ErmesManager.callback.selection = (function (pub) {

    pub.toggleDetailIntoSelection = function () {
        var data = ErmesManager.findClosestContainer(this).data("form");
        var item = data.Result.Resource;
        ErmesManager.selection.addItem(item);
        return false;
    };

    return pub;
} (ErmesManager.callback.selection || {}));

/////////////////////////////////////////////////////////
// Bindings & Init
$(function () {
    // jQuery Ajax Setup
    $.ajaxSetup({
        async: true,
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        timeout: 60000 //1 min
    });

    $.address.title(ErmesManager.loadingTitle);

    // Chargement de la s?lection flottante
    ErmesManager.selection.load();

    //Init search Container
    $("div.searchContainer").data("form", new ErmesManager.Form());

    //Binding toggle bouton d?tail selection
    $("a.bouton_detail").live("click", ErmesManager.callback.selection.toggleSelection);

    //Binding show tag form
    $("div.themes a.plus").live("click", ErmesManager.callback.tag.showTagForm);

    //Binding ajout de tags
    $("div.themes ul.tagit_container_tag").live("keypress", ErmesManager.callback.tag.addTag);
    $("div.themes a.tagit_ok").live("click", function () { var e = jQuery.Event("keypress"); e.keyCode = 13; $(this).closest("ul.tagit_container_tag").trigger(e); return false; });

    //Binding suppression de tag openfind sur une ressource
    $("div.themes a.supp").live("click", ErmesManager.callback.tag.removeTagResource);

    //Binding show selection form
    $("div.selections a.plus", "#notice_longue").live("click", ErmesManager.callback.ofSelection.showSelectionsForm);

    //Binding toggle comment form
    $("div.actions a.donner_avis").live("click", ErmesManager.callback.comment.showAddCommentForm);

    //Binding ajout panier
    $("div.actions a.memoriser").live("click", ErmesManager.callback.basket.addToBasket);

    //Binding suppression du panier
    $("div.actions a.oublier").live("click", ErmesManager.callback.basket.removeFromBasket);

    //Binding ajout item selection
    $("div.select > input:checkbox", "#resultats").live("click", ErmesManager.callback.selection.toggleItemIntoSelection);

    //Binding suppression item depuis la selection flottante
    $("#multi_selection a.removeItem").live("click", ErmesManager.callback.selection.removeSelectionItem);

    //Binding suppression tous les items depuis la selection flottante
    $("#multi_selection a.removeAll").live("click", ErmesManager.callback.selection.clearSelection);

    //Binding versement s?lection flottante vers le panier
    $("#multi_selection a.memoriser").live("click", ErmesManager.callback.selection.addSelectionToBasket);

    //Binding onglet
    $("li a:not(.on)", "#onglets").live("click", ErmesManager.callback.detail.showTab);

    //Binding suppression d'un commentaire
    $("a.delete_comment", "#avis_lecteurs").live("click", ErmesManager.callback.comment.invalidateComment);

    //Binding validation d'un commentaire
    $("a.enable_comment", "#avis_lecteurs").live("click", ErmesManager.callback.comment.validateComment);

    //Binding rediger avis
    $("li.rediger_avis > a", "#document_actions").live("click", function () { $("#notice_longue").find("a.donner_avis").trigger("click"); return false; });

    //Binding ajout item selection
    $("li.ajouter_selection > a", "#document_actions").live("click", ErmesManager.callback.selection.toggleDetailIntoSelection);

    //Binding onglet on (catch)
    $("li a.on", "#onglets").live("click", function () { return false; });

    //Lancement appli CDRom
    $("#ermesLauncher").live("click", ErmesManager.launchAppli);
});


// [EOF] for file detail.js

//package loaded!
packages.complete('BAM_ERMES_DETAIL');

// Served in 399 ms