/**
* Lightbox
*
* This libary is used to create a lightbox in a web application.  This library
* requires the Prototype 1.6 library and Script.aculo.us core, effects, and dragdrop
* libraries.  To use, add a div containing the content to be displayed anywhere on
* the page.  To create the lightbox, add the following code:
*
*	var test;
*
*	Event.observe(window, 'load', function () {
*		test = new Lightbox('idOfMyDiv');
*	});
*
*	Event.observe('lightboxLink', 'click', function () {
*		test.open();
*	});
*
*	Event.observe('closeLink', 'click', function () {
*		test.close();
*	});
*
*/

var SimpleLightbox = Class.create();

SimpleLightbox.prototype = {
	open : function () {
        window.document.observe('keydown',this._closeOnEscapeHandler);
        this._centerWindow(this.container);
        this.container.fire('lb:beforeopen',this);
        this._fade('open', this.container);

        /*Feature imeni Jaroslava Kravchuka*/
        if(this.container.getElementsByTagName('input')[0]){
            this.container.getElementsByTagName('input')[0].focus();
        }
        /*Spasibo, pojaluista*/

        this.container.fire('lb:afteropen',this);
	},

    observe : function(event,handler){
        this.container.observe(event,handler);
    },

    stopObserving : function(event, handler){
    	this.container.stopObserving(event, handler);
    },

	close : function () {
        window.document.stopObserving('keydown',this._closeOnEscapeHandler);
        this.container.fire('lb:beforeclose',this);
		this._fade('close', this.container);
		this.container.fire('lb:afterclose',this);
	},

	_fade : function fadeBg(userAction,whichDiv){
		if(userAction=='close'){
			new Effect.Fade(this.bgFade,
					   {duration:.5,
					    from:1.0,
					    to:0,
					    afterFinish:this._makeInvisible.bind(this),
					    afterUpdate:this._hideLayer(whichDiv)});
		}else{
			new Effect.Appear(this.bgFade,
					   {duration:.5,
					    from:0,
					    to:1.0,
					    beforeUpdate:this._makeVisible.bind(this),
					    afterFinish:this._showLayer(whichDiv)});
		}
	},

	_makeVisible : function makeVisible(){
        this.bgFade.style.visibility="visible";
        this.bgFade.style.zIndex = "102";
	},

	_makeInvisible : function makeInvisible(){
		this.bgFade.style.visibility="hidden";
	},

	_showLayer : function showLayer(userAction){
		$(userAction).style.display="block";
	},

	_hideLayer : function hideLayer(userAction){
		$(userAction).style.display="none";
	},

	_centerWindow : function centerWindow(element) {
		var windowHeight = parseFloat($(element).getHeight())/2;
		var windowWidth = parseFloat($(element).getWidth())/2;
        var scrollTop= self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);

		if(typeof window.innerHeight != 'undefined') {
			$(element).style.top = Math.round(document.body.offsetTop + ((window.innerHeight - $(element).getHeight()))/2)+'px';
			$(element).style.left = Math.round(document.body.offsetLeft + ((window.innerWidth - $(element).getWidth()))/2)+'px';
		} else {
			$(element).style.top = Math.round(document.body.offsetTop + ((document.documentElement.offsetHeight - $(element).getHeight()))/2)+'px';
			$(element).style.left = Math.round(document.body.offsetLeft + ((document.documentElement.offsetWidth - $(element).getWidth()))/2)+'px';
		}
		h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
         $(element).style.top = Math.round(scrollTop  + h/2- windowHeight)+'px';

        if (parseInt($(element).style.top) < 50)
            $(element).style.top = '50px';

	},

    _closeOnEscape : function(event){
        if(event.keyCode == 27)
        {
            this.close()
        }
    },
	initialize : function(containerDiv, draggable, saveDomPosition) {
		this.container = containerDiv;
		this._hideLayer(this.container);
        this._closeOnEscapeHandler = this._closeOnEscape.bind(this);
        if(!saveDomPosition)
        {
		    document.body.insertBefore(this.container, document.body.firstChild);
        }
		var bgFade = $('bg_fade');
		if(!bgFade) {
			bgFade = $(document.createElement('div'));
			bgFade.id = 'bg_fade';
			bgFade.addClassName('bg-fade');
            //bgFade.innerHTML="<img src='bg_fade.png' width='100%' height='100%'/>";
			document.body.insertBefore(bgFade, document.body.firstChild);
		}
		this.bgFade = bgFade;
		if(draggable) {
			new Draggable(this.container);
		}
	}
};

