SKOBBLERwidgets = (typeof SKOBBLERwidgets =='undefined')?{}: SKOBBLERwidgets;


SKOBBLERwidgets.modalWindow = function(container, trigger , options) {

	this.init(container, trigger , options);
}

SKOBBLERwidgets.modalWindow.prototype = {
    container: null,
    trigger: null,
    options: null,

    closeTrigger: null,
    elements: null,
    visible: false,
    /**
     * constructor
     */
    init: function(container, trigger , options) {
        this.container = $(container);
        this.trigger = trigger;
        this.options = options || {};
        this.applyOptions();

        this.closeTrigger = $$("." + this.container.id + "_close")[0];
        this.prepareContainer();
        this.attachHandlers();
    },


    setVisible: function() {
        this.visible = (this.visible == false)? true: false;
    },

    /**
     * private
     */
    show: function(event) {
        Event.stop(event);
        this.prepareMask();
        this.prepareWindow();
        new Effect.Parallel([
            new Effect.Appear(this.mask, { duration: this.options.duration, from: 0.0, to: this.options.opacity }),
            new Effect.Appear(this.container, { duration: this.options.duration, from: 0.0, to: 0.8 })
        ], {
          duration: 1,
          delay: 0,
          afterFinish: this.afterShowCallback.bind(this)
        });

    },

    afterShowCallback: function() {
        if(this.visible == true) return;
        this.setVisible();
    },

    /**
     * private
     */
    hide: function(event) {
        if(this.visible == false) return;
        new Effect.Parallel([
            new Effect.Appear(this.mask, { duration: this.options.duration, from:  this.options.opacity, to: 0.0}),
            new Effect.Appear(this.container, { duration: this.options.duration, from: 0.8, to: 0 })
        ], {
          duration: 1,
          delay: 0,
          afterFinish: this.afterHideCallback.bind(this)
        });
    },

    afterHideCallback: function() {
         if(this.visible == false) return;
         this.setVisible();
         this.removeMask();
    },

    /**
     * private
     */
    removeMask: function() {
         Event.stopObserving(this.mask, "click", this.hide);
         document.body.removeChild(this.mask); 
         this.mask = null;
    },

    /**
     * private
     */
   prepareMask: function() {
        this.mask = document.createElement("div");
        this.mask = document.body.appendChild(this.mask);
        this.mask = $(this.mask);
        var pageSize = this.getPageSize();
        this.mask.setStyle({
            id: "js_mask",
            width: pageSize[0] + 'px',
            height: pageSize[1] + 'px',
            position: "absolute",
            background: 'black',
            top: "0px",
            left: "0px",
            zIndex: 1012,
            opacity: 0
        });
        Event.observe(this.mask, "click", this.hide.bindAsEventListener(this));
        
        
    },

    /**
     * private
     */
    prepareWindow: function() {
        // calculate top and left offset
        var pageScroll = document.viewport.getScrollOffsets();
        var top = pageScroll[1] + (document.viewport.getHeight() / 10);
        var left = document.viewport.getWidth()/2 - this.container.getWidth()/2;
        this.container.setStyle({ top: (top -100) + 'px', left: left + 'px' }); //.show();
    },



    /**
     * private
     */
    prepareContainer: function() {
        this.container.hide();
        this.container.setStyle({
            position: 'absolute',
            left: '0px', //todo
            top: '0px', //todo
            zIndex: 1013
        });
    },

    /**
     * private
     */
    attachHandlers: function() {
        this.elements = $$("." + this.trigger);
        for(var i=0; i<this.elements.length; i++) {
            //Event.observe(this.elements[i], "click", this.show.bindAsEventListener(this));
            Event.observe(this.elements[i], "mousedown", this.show.bindAsEventListener(this));
        }
        Event.observe(this.closeTrigger, "click", this.hide.bindAsEventListener(this));
    },

    /**
     * private
     */
    applyOptions: function() {
         this.options.duration = this.options.duration || 1;
         this.options.opacity = this.options.opacity || 0.75;
    },

    getPageSize: function() {

	     var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;

		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
}
