//<!--

String.extend({
	getParams: function(){
		var params = new Object();
		var qs = this.split('?');
			qs = qs[1] || '';
		var pairs = qs.split('&');
		for( var i = 0; i < pairs.length; i++ ) {
			var query = pairs[i].split('=');
			params[query[0]] = query[1];
		}
		return params;
	}
});

var TightBox = Object.extend({

	// constructor
initialize: function(options){
			// window.onresize event handler
			window.addEvent('resize', this.resize.bindAsEventListener(this));
			// create TB HTML
			this.dom_body = $E('body');
			this.dom_tb_main = new Element('div').setProperty('id','tightbox').injectInside(this.dom_body);
			this.dom_overlay = new Element('div').setProperty('id','tb-overlay').injectInside(this.dom_tb_main)
					.setProperty('title', 'Click anywhere to close')
					.addEvent('click',this.turnOff.bindAsEventListener(this));
			this.dom_wrapper = new Element('div').setProperty('id','tb-wrapper').injectInside(this.dom_tb_main);
			this.dom_group   = new Element('div').setProperty('id','tb-group'  ).injectInside(this.dom_wrapper);
			this.dom_loading = new Element('div').setProperty('id','tb-loading').injectInside(this.dom_group);
			this.dom_content = new Element('div').setProperty('id','tb-content').injectInside(this.dom_group);
			this.dom_heading = new Element('div').setProperty('id','tb-heading').injectInside(this.dom_content)
					.addEvent('click',this.turnOff.bindAsEventListener(this));
			this.dom_title   = new Element('h3').setProperty('id','tb-title').injectInside(this.dom_heading);
			this.dom_close   = new Element('div').setProperty('id','tb-close').injectInside(this.dom_heading)
					.setProperty('title','Close');
			this.dom_update  = new Element('div').setProperty('id','tb-update' ).injectInside(this.dom_content);
			// default options
			this.options = Object.extend({
					title: 'Holler Media Productions',
					width: 640,
					height: 480,
					overlay: 0.5,
					duration: 400
				}, options || {});
			this.options.padding = 2;
			// create TB Effects
			this.fx_overlay = this.dom_overlay.effect('opacity',{duration:this.options.duration}).set(0);
			this.fx_group = this.dom_group.effects({duration:this.options.duration});
			// run setup
			this.setup();
		},

	setup: function() {
			// create TB Action
			this.el = $$('.tightbox').each(function(el){
					switch(el.getTag()){
							case 'form':
								el.onsubmit=function(){this.turnOn(el);return false;}.bind(this);
								el.setProperty('target','tb-update');
								el.target='tb-update';
								break;
							default:
								el.onclick=function(){this.turnOn(el);return false;}.bind(this);
								break;
						}
				}.bind(this));
		},
	
	turnOn: function(el) {
			this.dom_tb_main.setStyle('display','block');
			this.dom_wrapper.setStyles({
					'visibility':'visible',
					'top':Window.getScrollTop()+'px'
				});
			this.dom_loading.setStyle('display','block');
			this.dom_update.setStyle('visibility','hidden');
			this.resize();
			this.fx_overlay.options.onComplete = this.load.pass(el,this);
			this.fx_overlay.custom(0, this.options.overlay);
		},

	turnOff: function() {
			this.dom_wrapper.setStyle('visibility','hidden');
			this.dom_update.setStyle('visibility','hidden');
			this.dom_loading.setStyle('display','none');
			this.fx_overlay.options.onComplete = (function(){
					this.dom_tb_main.setStyle('display','none');
				}).bind(this);
			this.fx_overlay.custom(this.fx_overlay.now,0);
		},
		
	position: function() {
			this.h = this.dom_update.offsetHeight+this.dom_heading.offsetHeight+this.options.padding*2;
			this.t = this.h < Window.getHeight()
						? (Window.getHeight()/2)-(this.h/2)+Window.getScrollTop()
						: Window.getScrollTop()+10;
		},
		
	scale: function(w,h) {
			this.dom_update.setStyles({
					'width':w+'px',
					'height':h+'px'
				});
			this.position();
			this.fx_group.options.onComplete = (function(){
					this.fx_group.options.onComplete = (function(){
							this.dom_update.replaceWith(this.new_update || this.dom_update);
							this.dom_update = this.new_update || this.dom_update;
							this.dom_update.setStyles({
									'width':w+'px',
									'height':h+'px',
									'visibility':'visible'
								});
							this.dom_content.h = this.h;
							(function(){this.dom_loading.setStyle('display','none');}).delay(100,this);
						}).bind(this);
					this.fx_group.custom({'width':[this.dom_group.offsetWidth,w+this.options.padding*2]});
				}).bind(this);
			this.fx_group.custom({
				'height':[this.dom_content.h||100, this.h],
				'top'	:[this.dom_group.offsetTop, this.t]});
		},
		
	load: function(el) {
			this.dom_update.setStyle('visibility','hidden');
			this.dom_loading.setStyle('visibility','visible');
			switch(el.getTag()){
					case 'form':
					case 'img':
					case 'a':
						var url = el.getProperty('href') || el.getProperty('action') || el.getProperty('src');
						var params = url.getParams();
						var title = el.getProperty('title') || params.title || this.options.title;
						this.dom_title.setHTML(title);
						if(url.test('(\.jpg|\.jpeg|\.gif|\.png)$') || el.hasClass('tb-image')){
							this.new_update = new Element('img').setProperties({
									'id':'tb-update',
									'src':url,
									'alt':'this is an image'
								});
							var preload = new Image();
							preload.onload = (function(){
									this.scale(preload.width,preload.height);
								}).bind(this);
							preload.src = url;
						} else {
							params.width = params.width || this.options.width;
							params.height = params.height || this.options.height;
							params.width = $type(params.width) == 'string' ? params.width.toInt() : params.width;
							params.height = $type(params.height) == 'string' ? params.height.toInt() : params.height;
							this.new_update = new Element('iframe').setProperties({
									'src':'',
									'name':'tb-update','id':'tb-update',
									'frameborder':0,'scrolling':'auto',
									'marginwidth':0,'marginheight':0,
									'width':params.width+'px','height':params.height+'px'
								});
							/*
							this.new_update.onload = function(){
								alert('loaded');
								if(el.submit) (function(){el.submit()}).delay(1000);
								//else this.new_update.setProperty('src',url);
								else this.new_update.src=url;
								this.new_update.onload = this.empty;
							}.bind(this);
							*/
							if(el.submit) (function(){el.submit()}).delay(1000);
							//else this.new_update.setProperty('src',url);
							else this.new_update.src=url;

							this.scale(params.width,params.height);
						}
						break;
					default:
						this.turnOff();
				}
		},
	
	resize: function() {
			// center group
			this.position();
			this.dom_group.setStyle('top',this.t+'px');
			// resize overlay
			var h = Window.getScrollHeight() > Window.getHeight() ? Window.getScrollHeight() : Window.getHeight();
			this.dom_overlay.setStyles({
					'width':Window.getScrollWidth()+'px',
					'height':h+'px'
				});
		}
});

Window.onDomReady(function() {
		TightBox.initialize();
	});
//-->