SelectionZoom = new Class(
{
    Implements: [Chain, Events, Options],

    options:
	{
		linkId: "mxdSelectionZoom",
		loadingCaption: "Chargement de votre sélection en cours..."
	},

    initialize: function(options)
	{
        this.id = $time();
        this.active = false;

        this.setOptions(options);

		this.overlay = new Overlay({ container: document.body, onClick: this.close.bind(this) });

        this.container = new Element("div", { "class": "selectionzoom-container" });

        // affectation d'un evenement click sur le lien mxdSelectionZoom
        $(this.options.linkId).addEvent("click", this.open.bindWithEvent(this));

        this.closeButton = new Element("a", { "href": "javascript:void(0)", "class": "selectionzoom-close", "title": "fermer" });

        this.loadingPanel = new Element("div", { "class": "selectionzoom-loading-panel" });
		this.loadingPanel.set("html", this.options.loadingCaption);
        this.loadingPanel.inject(this.container);

        this.container.setStyle("visibility", "hidden");        
        this.container.inject(document.body);

		this.originalTop = parseInt(this.container.getStyle("top").replace("px", ""));

        this.closeButton.setStyles({ top: 0, right: 0 });
        this.closeButton.addEvent("click", this.close.bind(this));
        this.closeButton.inject(this.container);

        // ajout d'un evenement keydown pour intercepter les touches d'actions speciales
        // (i.e. : esc, left, right, ...)
        document.addEvent("keydown", this.keyboardListener.bindWithEvent(this));
    },

    open: function(e) 
    {
        e.stop();

        // on fait disparaitre d'eventuelles anim flash
        var tagName = (Browser.Engine.trident) ? "object" : "embed";
        var flashObjects = $(document.body).getElements(tagName);

        flashObjects.each(function(el)
		{
            try
			{
                el.setStyle("visibility", "hidden");
            }
            catch (e)
			{
                //
            }
        });

        this.overlay.show();

        this.active = true;

        // on vide le contenu de l'iframe
        this.loadingPanel.setStyle("display", "block");

        // centrage horizontal de la fenetre
        var leftPos = (window.getWidth() - this.container.getStyle("width").toInt()) / 2;
        this.container.setStyle("left", leftPos + "px");

		// positionnement vertical par rapport à un eventuel scrolling dans la page
		var topPos = this.originalTop + window.getScrollTop();
		this.container.setStyle("top", topPos + "px");

        // affichage de la fenetre
        this.container.setStyle("visibility", "visible");

        // affichage de la selection
        this.iframe = new Element("iframe", { "class": "selectionzoom-iframe" });
        this.iframe.setAttribute("scrolling", "no");
        this.iframe.setAttribute("border", "0");
        this.iframe.setAttribute("frameBorder", "0");
        this.iframe.addEvent("load", this.loaded.bindWithEvent(this));
		this.iframe.inject(this.container);

		this.iframe.contentWindow.document.location.replace("/front/Selection.aspx");
    },

    close: function()
	{
		this.iframe.destroy();

        // on masque les elements de la fenetre
        this.container.setStyle("visibility", "hidden");

        // on cache l'overlay
        this.overlay.hide();

        this.active = false;

        // on refait apparaitre les anim flash qui ont pu etre precedemment masquees
        var tagName = (Browser.Engine.trident) ? "object" : "embed";
        var flashObjects = $(document.body).getElements(tagName);

        flashObjects.each(function(el) 
		{
            try
			{
                el.setStyle("visibility", "visible");
            }
            catch (e)
			{
                //
            }
        });
    },

    keyboardListener: function(e)
	{
        if (!this.active) return;

		// recuperation de la reference a l'objet selectionView utilisé
		// dans l'iframe (cf selection.js)
		var selView = this.iframe.contentWindow["selectionView"];

        // on desactive l'appui sur les touches tab & alt
        // (qui foutent un peu le boxon :))
        if (e.code == 9 || e.code == 18)
		{
            e.stopPropagation();
            return;
        }

        switch (e.key)
		{
			// fermeture de la fenetre
            case "esc":
                this.close();
                break;

			// affichage de la page suivante de la selection
            case "right":
                if (selView)
				{
					selView.selectionChangePage(this.iframe.contentWindow["SELECTION_NEXT"]);
				}
                break;

			// affichage de la page precedente de la selection
            case "left":
                if (selView)
				{
					selView.selectionChangePage(this.iframe.contentWindow["SELECTION_PREVIOUS"]);
				}
                break;			
        }
    },

    loaded: function()
	{
		// on cache l'objet loadingPanel et on affiche notre iframe avec la photo
		if (this.loadingPanel.getStyle("display") == "block")
		{
			this.loadingPanel.setStyle("display", "none");
		}

		// on affiche l'iframe qui a terminé de se charger
		this.iframe.setStyle("visibility", "visible");

		// permet de gerer les evenements clavier a partir de l'iframe
		if (this.iframe.contentWindow.document.location.href != "about:blank")
		{
			var iframeDoc = this.iframe.contentWindow.document;

			iframeDoc.addEvent("keydown", this.keyboardListener.bindWithEvent(this));
		}
    }
});