var Lighterbox = Class.create({
	initialize: function (options) {
		this.options = Object.extend({
			openButton: null,
			lightWindow: null,
			closeButton: null
		}, options || {});
		
		// make sure we have the prerequestite background
		if (!$('lighterwindow_background')) {
			$(document.body).insert(
				{
					top : '<div id="lighterwindow_background" class="lighterwindow_background" style="display: none"></div>'
				});
		}
		
		this.options.lightWindow = $(this.options.lightWindow);
		
		this.options.openButton = $(this.options.openButton);
		this.options.openButton.observe(
			'click', 
			this.show.bind(this));
			
		this.options.closeButton = $(this.options.closeButton);
		this.options.closeButton.observe(
			'click', 
			this.hide.bind(this));
			
		Event.observe( window, 'resize', this.onResize );
		this.onResize();	
		
	},
	show: function (evt) {
	    var bg = $('lighterwindow_background');
	    if (bg) {
    	    bg.show();
            // Mac FF 2 can't display Flash if there are elements on the page
            // with CSS style opacity < 1.
            var detectMacXFF2 = function() {
              var userAgent = navigator.userAgent.toLowerCase();
              if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
                var ffversion = new Number(RegExp.$1);
                if (ffversion < 3 && userAgent.indexOf('mac') != -1) {
                  return true;
                }
              }
            }
            // insert translucent PNG as BG image
            if (detectMacXFF2()) {
                bg.style.opacity = "1";
                bg.style.backgroundImage = "url(/images/global/translucent.png)";
                bg.style.backgroundRepeat = "repeat";
                bg.style.backgroundColor = "transparent";
            }
    	}
		this.options.lightWindow.center( { update: true, zIndex: 11500 } );
		this.options.lightWindow.show();
		if(evt) evt.stop();
	},
	hide: function (evt) {
		this.options.lightWindow.hide();
		$('lighterwindow_background').hide();
		if(evt) evt.stop();
	},
	onResize: function (evt) {
		var docsize = $(document.body).getDimensions();
		var viewsize = document.viewport.getDimensions();
		if ( viewsize.height > docsize.height )
			docsize.height = viewsize.height;
		if ( viewsize.width > docsize.width )
			docsize.width = viewsize.width;
		$('lighterwindow_background').setStyle(
			{
				height: docsize.height+'px', 
				width: docsize.width+'px'
			});		
	}
});


var AsyncLighterbox = Class.create( Lighterbox, {
    options: {},
    initialize: function() {
		// make sure we have the prerequestite background
		if (!$('lighterwindow_background')) {
			$(document.body).insert(
				{
					top : '<div id="lighterwindow_background" class="lighterwindow_background" style="display: none"></div>'
				});
		}
		Event.observe( window, 'resize', this.onResize );
		this.onResize();	
    },
    setContent: function(ele) {
        this.options.lightWindow = ele;
    }
});
