
var lightbox = {
    idPrefix: 'lightBox',
	currentId: null,
    show: function(id, w, h, url){
        //console.log(id);
        //console.log(this.idPrefix + 'Dimmer');
		this.currentId = id;
		
        w = w || 400;
        h = h || 150;

        if (!document.getElementById(this.idPrefix + 'Dimmer')) {
            var tmp = window.opera && parseFloat(window.opera.version()) < 9.5 ? document.documentElement : document.body
            //var viewport = document.viewport.getDimentions);
			
			if(!$(this.currentId)) {
				var maindiv = new Element("DIV", {id:this.currentId,style:'display:none'});
				document.body.appendChild(maindiv);
			}
			
			if (Prototype) {
				var black = new Element('div', {id:this.idPrefix + 'Dimmer'});
				black.setStyle({width:(tmp.clientWidth) + 'px',height:(tmp.clientHeight) + 'px'});
				black.setOpacity(0.3);
				
				
				var inner = new Element('div', {id:this.idPrefix + 'Inner'});
				inner.setStyle({width:w+'px', height:h+'px'});
				
				var close = new Element('img',{id:this.idPrefix + 'Close', src:'../images/close.png'});
				close.observe('click', function(e) {
				 	this.hide();
				}.bindAsEventListener(this));
				
				$(this.currentId).appendChild(close);
				$(this.currentId).appendChild(inner);

			}
			else {
				var black = document.createElement('DIV');
	            black.id = this.idPrefix + 'Dimmer';
	            black.style.width = (tmp.clientWidth) + 'px';
	            black.style.height = (tmp.clientHeight) + 'px';
			}
				
            document.body.appendChild(black);
        }
        else {
            var black = document.getElementById(this.idPrefix + 'Dimmer');
        }

        black.style.display = 'block';
        if (typeof id == "string") {
            id = document.getElementById(id);
        }
 
        id.style.zIndex = 550;
        id.style.display= 'block';
        
		$(id).setStyle({
            width: w + 'px',
            height: h + 'px'
        });
        
		this.setLightBoxPosition(id, w, h);

        
		if(url) {
			new Ajax.Updater(this.idPrefix + 'Inner', url, {
				method: 'post',
				evalScripts: true
			});
		}
		
		
		Event.observe(window, 'scroll', function(e) {
            lightbox.setLightBoxPosition(id, w, h);
        }.bindAsEventListener(this));


    },
    hide: function(id){
		id = id || this.currentId;
        var black = document.getElementById(this.idPrefix + 'Dimmer');
        if (black) {
            black.style.display = 'none';
        }
        if (typeof id == "string") {
            id = document.getElementById(id);
        }
        id.style.display = 'none';
        
    },
    setLightBoxPosition: function(el, w, h){
        var viewport_width = document.viewport.getWidth();
        var viewport_height = document.viewport.getHeight();
        var vpscroll_top = document.viewport.getScrollOffsets()['top'];
        var new_top = ((viewport_height - h) / 2) + vpscroll_top;
        
        el.setStyle({
            marginLeft: '-' + Math.round(w / 2) + 'px',
            left: '50%',
            top: new_top + 'px'
        });
        
    }
}

