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);
		
		if ( this.options.openButton ) {
			this.options.openButton = $(this.options.openButton);
			this.options.openButton.observe(
				'click', 
				this.show.bind(this));
		}
		
		if ( this.options.closeButton ) {
			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();
        $$('body')[0].addClassName('ltBoxOn');		
	},
	hide: function (evt) {
		this.options.lightWindow.hide();
		$('lighterwindow_background').hide();
		if(evt) evt.stop();
        $$('body')[0].removeClassName('ltBoxOn'); 
	},
	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;
    }
});

var LighterboxUpdater = Class.create({
	
	initialize: function (options) {
		this.options = Object.extend({
			openButton: null,
			data: {},
			contentDiv: null,
			divHeight: 615,
			divWidth: 800,
			printButton: true,
			hideCloseButton: false,
			showNow: true,
			url: ''
		}, options || {});
	
		this.objLighterbox = null;

		if ( !this.options.contentDiv ) {
			this.options.contentDiv = generateGuid();
		}
		
		this._buildLB();
	},
	_buildLB: function () {
		$(document.body).fillin({
			template: templatefactory.get('/templates/lighterbox-updater.tmpl'),
			position: 'bottom',
			object: { ID: this.options.contentDiv, HEIGHT: this.options.divHeight, WIDTH: this.options.divWidth },
			callback: this._loadLB.bind(this)
		})	
	},
	_loadLB: function (elm) {
		var lb = new Lighterbox({
			openButton: $(this.options.openButton),
			lightWindow: $(this.options.contentDiv + '-lighter-window'),
			closeButton: $(this.options.contentDiv + '-lighter-window').down('.lighterwindow_close_link')
		});		
		var lw = lb.options.lightWindow;
		var lwc = lw.down('.lb-content');
		var lwprint = lw.down('.lb-content');
		
		this.objLighterbox = lb;

		if ( this.options.printButton ) {
			new PrintButton ( $(this.options.contentDiv + '-lighter-window').down('.lighterwindow_print_link'),
								{ divToPrint: lwprint } );
		} else {
			$(this.options.contentDiv + '-lighter-window').down('.lighterwindow_print_link').hide();
		}
		
		if ( this.options.hideCloseButton ) {
			$(this.options.contentDiv + '-lighter-window').down('.lighterwindow_close_link').hide();
		}
		
		this.ifShowNow();
		
		new Ajax.Updater ( lwc, this.options.url, { 
							evalScripts: false,
							onComplete: this.ifShowNow.bind(this)
							} );
	},
	ifShowNow: function() {
		if ( this.options.showNow ) {
			this.show();
		}
	},
	show: function () {
		this.objLighterbox.show();
	},
	hide: function () {
		this.objLighterbox.hide();
	}
});
