/**
 * LightBox - Builds and manages a set of elements that makeup a lightbox
 * @param	box			mixed		Element id or object that holds the content
 * @param	overlay		mixed		Element id or object that acts as the overlay
 * @param	imgClass	string		optional;CSS class denoting an image to preload for display
 * @return				LightBox	New LightBox instance
 */
function LightBox(box, overlay, imgClass) {
	this.box = null;
	this.overlay = null;
	this.isShowing = false;
	this.initialized = false;
	this.isIE = false;
	this.regexFilename = /^.*\/([^\/]*)$/i;
	this.imgClass = imgClass || '';
	this.images = {};
	
	var args = arguments;
	domLoader.register(function(){
		this.isIE = window.ActiveXObject ? (window.XMLHttpRequest ? 7 : 6) : false;
		this.initialize.apply(this, args);
	},this);
}
LightBox.prototype = {
	'initialize': function() {
		if(this.initialized){ return; }
		var args = slice(arguments);
		this.box = (typeof args[0] == 'object') ? args[0] : did(args[0]);
		this.overlay = (typeof args[1] == 'object') ? args[1] : did(args[1]);
		
		var docbody = dbt('body')[0];
		
		if(!this.box){
			this.box = createElement('div');
			docbody.insertBefore(this.box, first(docbody));
		}
		if(!this.overlay){
			this.overlay = createElement('div');
			docbody.insertBefore(this.overlay, this.box);
		}
		
		this.box.style.display = 'none';
		this.overlay.style.display = 'none';
				
		if(this.isIE) {
			this.shim = createElement('iframe');
			this.shim.style.cssText = 'display: none;position: absolute;top: 0px;left: 0px;filter:progid:DXImageTransform.Microsoft.alpha(opacity=0);z-index:'+(getStyle(this.overlay,'zIndex')-1);
			this.shim.frameBorder = 0;
			this.shim.scrolling = 'no';
			this.shim.src = '';
						
			docbody.insertBefore(this.shim, first(docbody));
		}
		
		var self = this;
		registerEvent(window, 'resize', function(){ self.resize(); });
		registerEvent(window, 'scroll', function(){ self.resize(); });
		registerEvent(this.overlay, 'click', function(){ self.hide(); });
		if(!this.isIE){this.resize();}
		
		if(this.imgClass != '') {
			this.preloadImages();
		}
		this.initialized = true;
	},
	'preloadImages': function(className) {
		this.imgClass = className ? String(className) : this.imgClass;
		var nodes = slice(dbc(this.imgClass)), tagname='', imgsrc='';
		nodes.forEach(function(node) {
			var tag = node.tagName.toLowerCase();
			if(tag == 'a' && node.href != '') {
				imgsrc = node.href;
			} else if(tagname == 'img' && node.src != '') {
				imgsrc = node.src;
			}
			this.preloadImage(imgsrc);
		}, this);
	},
	'preloadImage': function(src) {
		src = src ? String(src) : '';
		var fname = src.replace(this.regexFilename,'$1');
		if(src && !this.images[fname]) {
			this.images[fname] = new Image();
			this.images[fname].src = src;
		}
		return (fname in this.images) ? this.images[fname] : null;
	},
	'showHTML': function(html) {
		if(!this.initialized){
			return false;
		}
		if(this.frame){ this.frame.style.display = 'none'; }
		if(!this.content) {
			this.content = createElement('div');
			this.content.id = 'lbContentWrapper';
			this.box.appendChild(this.content);
		}
		this.content.innerHTML = String(html);
		this.content.style.display = '';
		this.show();
		return this;
	},
	'showDOM': function(node, clone) {
		if(!this.initialized){
			return false;
		}
		this.showHTML('');
		clone = clone || true;
		if(node && node.nodeType == 1) {
			if(clone){
				var nodeClone = node.cloneNode(true);
				this.content.appendChild(nodeClone);
			} else {
				this.content.appendChild(node);
			}			
		}
		return this;
	},
	'showIMG': function(src, width, height, alt) {
		if(!this.initialized){
			return false;
		}
		this.showHTML('<div>Loading Image...</div>');
		
		var theImage = this.preloadImage(src);
		var attributes = {'width':(width || theImage.width), 'height':(height || theImage.height), 'alt': (alt || '')};
		var html = '<img src="'+theImage.src+'"';
		for(var atr in attributes) {
			html += ' '+atr+'="'+attributes[atr]+'"';
		}
		html += ' />';
		this.showHTML(html);
		return this;
	},
	'showURL': function(url) {
		if(!this.initialized){
			return false;
		}
		if(this.content){ this.content.style.display = 'none'; }
		if(!this.frame) {
			this.frame = createElement('iframe');
			this.frame.id = 'lbContentFrame';
			this.frame.frameBorder = 0;
			this.box.appendChild(this.frame);
		}
		
		this.frame.src = url;
		this.frame.style.display = '';
		this.show();
		return this;
	},
	'clear': function() {
		this.content.innerHTML = '';
		return this;
	},
	'show': function() {
		this.resize();
		this.box.style.display = '';
		this.overlay.style.display = '';
		if(this.shim) {
			this.shim.style.display = '';
		}
		this.isShowing = true;
		return this;
	},
	'hide': function() {
		this.box.style.display = 'none';
		this.overlay.style.display = 'none';
		if(this.shim) {
			this.shim.style.display = 'none';
		}
		this.isShowing = false;
		return this;
	},
	'toggle': function() {
		var fn = this.isShowing ? 'hide':'show';
		this[fn]();
	},
	'resize': function() {
		var max = getMaxDocSize();
		['overlay','shim'].forEach(function(item){
			if(this[item]) {
				this[item].style.width = max['width']+'px';
				this[item].style.height = max['height']+'px';
			}
		},this);
		
		var docsize = getDocSize(), scroll = getScrollXY();
		var boxWidth = getStyle(this.box,'width'), boxHeight = getStyle(this.box,'height');
		if(this.isIE) {
			boxWidth = this.box.offsetWidth;
			boxHeight = this.box.offsetHeight;
		}
		var topleft = {'x':Math.round((docsize['width']-boxWidth)/2)+scroll['x'], 'y':Math.round((docsize['height']-boxHeight)/2)+scroll['y'] };
		this.setPosition(topleft.x,topleft.y);
		return this;		 
	},
	'setPosition': function(x, y) {
		if(x && x != 'auto') {
			this.box.style.left = parseInt(x)+'px';
		}
		if(y && y != 'auto') {
			this.box.style.top = parseInt(y)+'px';
		}
	}
}