/**
* 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 oLightbox = Class.create();
var saleLightbox = Class.create();
window.globalLightboxZIndexCounter=105;
window.globalOpenedLightboxCounter=0;
oLightbox.prototype = {
	open : function () {
        window.document.observe('keydown',this._closeOnEscapeHandler);
        this._centerWindow(this.container);
        this.container.fire('lb:beforeopen',this);
//        this.container.style.zIndex = 1 + (++window.globalLightboxZIndexCounter);
        this._fade('open', this.container);
        window.globalOpenedLightboxCounter++;
        /*Feature imeni Jaroslava Kravchuka*/
        //if(!Prototype.Browser.IE)
            //this.container.getElementsByTagName('input')[0].focus();
        /*Spasibo, pojaluista*/

        this.container.fire('lb:afteropen',this);
        window.document.fire('somelb:opened',this);
        if(!/.*onepage.*/.test(window.location.toString())) {
            $('bg_fade').observe('click', function(event){
                    this.close();
            }.bind(this));
        }
	},
	
    observe : function(event,handler){
        this.container.observe(event,handler);
    },
    
    stopObserving : function(event, handler){
    	this.container.stopObserving(event, handler);
    },
    
	close : function () {
        window.globalOpenedLightboxCounter--;
        window.document.stopObserving('keydown',this._closeOnEscapeHandler);
        this.container.fire('lb:beforeclose',this);
		this._fade('close', this.container);
		this.container.fire('lb:afterclose',this);
        $('bg_fade').stopObserving('click');
	},
	
	_fade : function fadeBg(userAction,whichDiv){
		if(userAction=='close'){
            if(window.globalOpenedLightboxCounter==0){
                new Effect.Fade(this.bgFade,
                                       {duration:.5,
                                        from:0.7,
                                        to:0,
                                        afterFinish:this._makeInvisible.bind(this),
                                        afterUpdate:this._hideLayer(whichDiv)});
            }else{
                this._hideLayer(whichDiv);
            }
        }else{
            if(this.bgFade.style.visibility=="visible")
                this._showLayer(whichDiv);
            else
                new Effect.Appear(this.bgFade,
                           {duration:.5,
                            from:0,
                            to:0.7,
                            beforeUpdate:this._makeVisible.bind(this),
                            afterFinish:this._showLayer(whichDiv)});
		}
	},
	
	_makeVisible : function makeVisible(){
        this.bgFade.style.visibility="visible";
        this.bgFade.style.zIndex = "200";
	},

	_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.container.style.zIndex = 1 + (++window.globalLightboxZIndexCounter);
        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');
            if (Prototype.Browser.IE) {
                bgFade.addClassName('class','bg-fade');
            }
			bgFade.innerHTML="<!--[if lte IE 6.5]><iframe></iframe><![endif]-->";
			document.body.insertBefore(bgFade, document.body.firstChild);
		}

//        var bgFade = new Element('div', {'id': 'bg-fade', 'class': 'bg-fade'});
//        document.body.appendChild(bgFade);

		this.bgFade = bgFade;
		if(draggable) {
			new Draggable(this.container);
		}
	}
};

saleLightbox.prototype = {
	open : function () {
//        window.document.observe('keydown',this._closeOnEscapeHandler);
        this._centerWindow(this.container);
        this.container.fire('lb:beforeopen',this);
//        this.container.style.zIndex = 1 + (++window.globalLightboxZIndexCounter);
        this._fade('open', this.container);
        window.globalOpenedLightboxCounter++;
        /*Feature imeni Jaroslava Kravchuka*/
        //if(!Prototype.Browser.IE)
            //this.container.getElementsByTagName('input')[0].focus();
        /*Spasibo, pojaluista*/

        this.container.fire('lb:afteropen',this);
        window.document.fire('somelb:opened',this);
//        if(!/.*onepage.*/.test(window.location.toString())) {
//            $('bg_fade').observe('click', function(event){
//                    this.close();
//            }.bind(this));
//        }
	},

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

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

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

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

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

	_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.container.style.zIndex = 1 + (++window.globalLightboxZIndexCounter);
        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');
            if (Prototype.Browser.IE) {
                bgFade.addClassName('class','bg-fade');
            }
			bgFade.innerHTML="<!--[if lte IE 6.5]><iframe></iframe><![endif]-->";
			document.body.insertBefore(bgFade, document.body.firstChild);
		}

//        var bgFade = new Element('div', {'id': 'bg-fade', 'class': 'bg-fade'});
//        document.body.appendChild(bgFade);

		this.bgFade = bgFade;
		if(draggable) {
			new Draggable(this.container);
		}
	}
};

