Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6;
Prototype.Browser.IE7 = Prototype.Browser.IE && !Prototype.Browser.IE6;

// Allows us to attach before and after events to any function call.
Function.prototype.eventWrapper = function(obj, event_name) {
	return obj.wrap(function (event_name, original_method) {
		document.fire('darkbox:before_' + event_name);
		original_method();
		document.fire('darkbox:after_' + event_name);
	}.bind(this, event_name));
}.methodize();

Element.addMethods({
	fetchOrCreate: function(element, find_this_id) {
		return $(find_this_id) || function() {
			element.insert({ bottom: '<div id="' + find_this_id + '" style="display: none;"></div>' });
			return $(find_this_id);
		}.bind(this)();
	}
});

var Darkbox = {
	setup: function() {
		if (Darkbox.overlay) return;

		// Find elements that need to be hidden during lightbox
		Darkbox.obnoxious_elements = $$('object, embed, select');

		var body_elem   = $$('body').first();
		Darkbox.element = body_elem.fetchOrCreate('darkbox');
		Darkbox.overlay = body_elem.fetchOrCreate('overlay');
		
		Darkbox.overlay.observe('click', function() {
			if (!Darkbox.active.options.modal) Darkbox.hide();
		});
	}.eventWrapper('setup'),

	show: function() {
		if (!Darkbox.active) return;

		Darkbox.active.options.style.insertHTML();
		Darkbox.active.options.style.position();

		Darkbox.obnoxious_elements.invoke('hide');
		Darkbox.active.options.effect.show();
	}.eventWrapper('show'),

	hide: function() {
		if (!Darkbox.active) return;
		Darkbox.active.options.effect.hide();
		Darkbox.obnoxious_elements.invoke('show');
		Darkbox.active = undefined;
	}.eventWrapper('hide'),

	// inserting via DOM fails in Safari 2.0, so brute force approach
	require: function(libraryName) {
		if ((arguments[1]) && (arguments[1] == true)) {
			var path = ''; // Do not calculate prefix path
		} else {
			var elem = $$("script").select(function(elem) {
				return (elem.src && elem.src.match(/darkbox2\.js/))
			}).first();
			var path = elem.src.replace(/darkbox2\.js(\?[0-9]+)?$/,'');
		}
		document.write('<script type="text/javascript" src="'+path+libraryName+'.js"><\/script>');
	},
	
	Effect: {
		show: function() {
			if (Prototype.Browser.IE6) {
				var dimensions = getPageSize();
				Darkbox.overlay.setStyle({
					width:  dimensions[0] + 'px',
					height: dimensions[1] + 'px'
				});
			}
			
			Darkbox.overlay.setOpacity(Darkbox.active.options.opacity).show();
			Darkbox.element.show();
		},

		hide: function() {
			Darkbox.element.hide();
			Darkbox.overlay.hide();
		}
	},

	Style: {
		insertHTML: function() {
			Darkbox.element.update(arguments[0] || Darkbox.active.source.innerHTML);
		},

		position: function() {
			var width    = arguments[0] || Darkbox.active.source.getWidth();
			var height   = arguments[1] || Darkbox.active.source.getHeight();
			var from_top = arguments[2] || 100;
			
			// I'm not actually sure IE is broken, I'm just assuming :-p
			if (Prototype.Browser.IE6) {
			 	from_top += document.viewport.getScrollOffsets()[1];
				Darkbox.element.setStyle({ position: 'absolute' });
			}

			Darkbox.element.setStyle({ 
				width:  width  + 'px',
				height: height + 'px',
				top:    from_top + 'px',
				left:   '50%',
				margin: '0 0 0 ' + Math.ceil(width/-2) + 'px'
			});
		}
	}
};

// We'll need some hackery to survice IE6
if (Prototype.Browser.IE6) { Darkbox.require('get_page_size'); }
Darkbox.require('darkbox_popup')
