var Lightbox=new Class({
	Implements:[Options,Events],

	_element:null,
	options:{
		overlayClass:"lightbox-overlay",
		containerClass:"lightbox-container",
		contentClass:"lightbox-content",
		hideOnEnter:true,
		hideOnEsc:true,
		opacity:0.4,
		fixedTop: false,
		reposition: true,
		onBeforeShow:$empty,
		onAfterShow:$empty,
		onBeforeHide:$empty,
		onAfterHide:$empty
	},

	initialize:function (element,options) {
		this._element=element;
		this._elementOldParent=element.getParent();
		if (this._elementOldParent) this._hiddenAtFirst=!element.get("display");
		this.setOptions(options);
	},

	show:function () {
		if (Lightbox.current) Lightbox._hide();
		Lightbox.current=this;

		this.fireEvent("onBeforeShow");
		Lightbox._show(this._element,this.options);
		this.fireEvent("onAfterShow");
	},
	hide:function () {
		this.fireEvent("onBeforeHide");
		Lightbox._hide();
		this.fireEvent("onAfterHide");
	}
});

// static methods/properties
$extend(Lightbox, {
    overlay: null,
    container: null,
    reposition: true,
    _init: function(options) {
        if (!Lightbox.overlay) {
            Lightbox.overlay = new Element("div").inject(document.body).set({
                styles: {
                    backgroundColor: options.backgroundColor || "#000",
                    // ie6 doesn't support position:fixed
                    position: Browser.Engine.trident4 ? "absolute" : "fixed",
                    top: 0,
                    left: 0,
                    zIndex: 1000
                },
                opacity: options.opacity,
                display: false
            });
        }
        else Lightbox.overlay.className = "";
        reposition = options.reposition;
        Lightbox.overlay.addClass(options.overlayClass);

        if (!Lightbox.container) {
            Lightbox.container = new Element("div").inject(document.body).set({
                styles: {
                    // ie6 doesn't support position:fixed
                    position: options.fixed ? "fixed" : "absolute",
                    top: 0,
                    left: 0,
                    zIndex: 1001
                },
                display: false
            });
        }
        else Lightbox.container.className = "";
        Lightbox.container.addClass(options.containerClass);

        if (!Lightbox._fix) Lightbox._fix = new OverlayFix(Lightbox.overlay);
    },

    _onScroll: function() {
        if (!Lightbox.current) removeEvent("scroll", arguments.callee);
        // since ie6 doesn't support position:fixed we need to take care of re-position overlay and container when scrolling
        if (reposition)
            Lightbox.adjustPositions();
    },
    _onResize: function() {
        if (!Lightbox.current) removeEvent("resize", arguments.callee);
        // re define the w/h of the overlay if the page's w/h has changed
        Lightbox._setOverlayHeight();
        if (reposition)
            Lightbox.adjustPositions();
    },
    _setOverlayHeight: function() {
        var size = document.getSize();
        Lightbox.overlay.setStyles({
            width: size.x,
            height: size.y
        });
    },

    _show: function(element, options) {
        Lightbox._init(options);

        Lightbox.fireEvent("onBeforeShow");

        element.set({
            visibility: false,
            display: true
        });

        Lightbox.overlay.show();
        Lightbox.container.show();

        Lightbox._fix.show();

        Lightbox.container.empty().adopt(new Element("div").addClass(options.contentClass).adopt(element));

        // keep set them when resizing
        addEvent("resize", Lightbox._onResize.bind(this));
        Lightbox._setOverlayHeight();
        //if (Browser.Engine.trident4) addEvent("scroll",Lightbox._onScroll);

        Lightbox.adjustPositions();
        if (options.reposition)
            Lightbox.adjustPositionsInterval = Lightbox.adjustPositions.periodical(300);

        element.set("visibility", true);

        Lightbox.fireEvent("onAfterShow");
        var lightboxKeydown = $(document.documentElement).retrieveOrStore("lightboxKeydown", function() {
            return function(e) {
                if (["input", "textarea", "select"].contains(Element.get(e.target, "tag"))) return;
                switch (e.key) {
                    case "esc":
                        Lightbox.fireEvent("onEsc", e); // e could be extended with cancel=true and lightbox won't get closed!
                        Lightbox.current.fireEvent("onEsc", e);
                        if (options.hideOnEsc && !e.cancel) Lightbox._hide();
                        break;
                    case "enter":
                        Lightbox.fireEvent("onEnter", e);
                        Lightbox.current.fireEvent("onEnter", e);
                        if (options.hideOnEnter && !e.cancel) Lightbox._hide();
                        break;
                }
            };
        });
        $(document.documentElement).addEvent("keydown", lightboxKeydown);
    },

    _hide: function() {
        Lightbox.fireEvent("onBeforeHide");

        Lightbox.overlay.set("display", false);
        Lightbox.container.set("display", false);

        Lightbox.adjustPositionsInterval = $clear(Lightbox.adjustPositionsInterval);

        Lightbox._fix.hide();

        $(document.documentElement).removeEvent("keydown", $(document.documentElement).retrieve("lightboxKeydown"));

        Lightbox.fireEvent("onAfterHide");

        if (Lightbox.current) {
            if (Lightbox.current._hiddenAtFirst) Lightbox.current._element.set("display", false);
            // re place the element in its original parent if any
            if (Lightbox.current._elementOldParent) Lightbox.current._elementOldParent.adopt(Lightbox.current._element);
        }

        Lightbox.current = null;
    },

    hide: function() {
        Lightbox._hide();
    },

    adjustPositions: function() {
        if (!Lightbox.current) return;

        var element = Lightbox.current._element,
			options = Lightbox.current.options;

        // TODO: allow fixed top

        var scrollTop = document.getScroll().y;

        var pos = {};
        pos.x = (document.getSize().x - element.offsetWidth) / 2;
        //pos.y=options.fixedTop ? options.fixedTop : document.getScroll().y+(document.getSize().y-element.offsetHeight)/2;
        pos.y = options.fixedTop ? options.fixedTop : scrollTop + (document.getSize().y - element.offsetHeight) / 2;

        pos.x = Math.max(0, pos.x);
        pos.y = Math.max(0, pos.y);

        // ie6 doesn't support fixed position so we have to update the lightbox overlay manually
        if (Browser.Engine.trident4) Lightbox.overlay.setStyle("top", scrollTop);

		Lightbox.fireEvent("onPositioning",[Lightbox.container,pos]);

		Lightbox.container.position(pos);

		Lightbox._fix.show();
	}
});
Events.makeObjectEventable(Lightbox);
Options.makeClassOptionable(Lightbox);

/*
-- js
var lb=new Lightbox(element);
lb.show();

lb.hide();
or Lightbox.hide();

Lightbox
*/
var LightboxForm=new Class({
	initialize:function (name,properties,lightboxProperties) {
		this.name=name;
		this.properties=properties;
		this.lightboxProperties=$merge(LightboxForm.lightboxProperties,lightboxProperties);
	},
	show:function (name,callback) {
		Mantis.FormGenerator.FormGeneratorService.GetFormSource(this.name,this.properties || null,function (source) {
			var form=Element.fromMarkup(source,{ callback:function () {
				new Lightbox(form,this.lightboxProperties).show();
				if (callback) callback();
			}.bind(this) });
		}.bind(this));
	}
});
$extend(LightboxForm,{
	show:function (name,properties,lightboxProperties) { new LightboxForm(name,properties,lightboxProperties).show(); }
});

LightboxForm.lightboxProperties={ };


var Product = {
    init: function() {
        $$("#product .item").each(function(item) {
            item.getElement(".title").addEvent("click", function() {

                if (this.hasClass("current")) {
                    this.removeClass("current");
                    this.getNext().removeClass("open");
                }
                else {
                    this.addClass("current");
                    this.getNext().addClass("open");
                }
            });
        });
    }
};

$domready(Product.init);
