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


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

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

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

    closeTrigger: null,
    elements: null,
    visible: false,

	//form: null,
	elem: null,
	//href: null,
    /**
     * 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.closeTriggers = $$(".loginTrigger");
        this.prepareContainer();
        this.attachHandlers();
		//this.formCallback = this.stopForm.bindAsEventListener(this);
    },


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

    /**
     * private
     */
    show: function(event) {
		 this.prepareMask();
		//var elem = (typeof event !== 'undefined')? event.srcElement : event.target;
		//elem = (typeof elem != 'undefined')?  elem: event.currentTarget;
		this.elem = event.findElement();
		var pos = $(this.elem).cumulativeOffset();
		this.container.setStyle({ top: pos.top - 80 + 'px', left: pos.left + 'px' });

		/*if(typeof this.elem.form != 'undefined') {
			this.form = this.elem.form;
			Event.observe(this.form, "submit", this.formCallback);
		}
		if(typeof this.elem.href != "undefined") {
			this.href = this.elem.href;
			this.elem.href= "javascript:;";
		}*/
        //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: 1 })
        ], {
          duration: 1,
          delay: 0,
          afterFinish: this.afterShowCallback.bind(this)
        });*/
		new Effect.Appear(this.container, { duration: this.options.duration, from: 0.0, to: 0.9,  afterFinish: this.afterShowCallback.bind(this) });
		Event.stop(event);

    },

	//not used
	stopForm: function(event) {
		Event.stop(event);
		if(this.form != null ){
			Event.stopObserving(this.form, "submit", this.formCallback);
			this.form = null;
		}

	},

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

    /**
     * private
     */
    hide: function(event) {
        if(this.visible == false) return;
		 this.removeMask();
        /*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: 1, to: 0 })
        ], {
          duration: 1,
          delay: 0,
          afterFinish: this.afterHideCallback.bind(this)
        });*/
		new Effect.Appear(this.container, { duration: this.options.duration, from: 0.9, to: 0, afterFinish: this.afterHideCallback.bind(this)});
		Event.stop(event);
    },

	//not used
	hideOnDocument: function(event) {
		var elem = event.findElement();
		if($(elem).hasClassName(this.trigger)){
			return;
		}
		this.hide(event);
	},

    afterHideCallback: function() {
         if(this.visible == false) return;
         this.setVisible();
		 this.container.setStyle({ top: '-1000px', left:'-1000px' });
		 this.elem = null; //todo
		/* if(this.href != null) {
			//this.elem.href = this.href; //hmmm
			this.elem = null;
			this.href = null;
		}*/
         //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
	 * not used
     */
    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 + '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));
		 for(var i=0; i<this.closeTriggers.length; i++) {
            //Event.observe(this.elements[i], "click", this.show.bindAsEventListener(this));
            Event.observe(this.closeTriggers[i], "mousedown", this.hide.bindAsEventListener(this));
        }
		//Event.observe(document.body, "click", this.hideOnDocument.bindAsEventListener(this));
    },

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

	//not used
    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];
	}
}
