/*
	Urlbox, based on slimbox
	Dave Brown <dave@anticlockwise.com.au>
	
	Slimbox v1.41 - The ultimate lightweight Lightbox clone
	by Christophe Beyls (http://www.digitalia.be) - MIT-style license.
	Inspired by the original Lightbox v2 by Lokesh Dhakar.
*/

var Urlbox = new Class({

	initialize: function(options){
		this.options = $extend({
			resizeDuration: 100,
			resizeTransition: false,	// default transition
			initialWidth: 250,
			initialHeight: 250,
			contentOnload:false,
			contentClose:false,
			overlayCloses:true,
			openUrl:false,
			urlContent:false,
			classPrefix:'ub'
		}, options || {});
		
		// open a single url or search for urlbox rel tags
		(!this.options.openUrl && !this.options.urlContent) && $each(document.links, function(el){
			if (el.rel && el.rel.test(/^urlbox/i)){
				el.onclick = this.click.pass(el, this);
			}
		}, this);
		
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);

		this.overlay = new Element('div', {'id': this.options.classPrefix + 'Overlay'}).injectInside(document.body);

		this.center = new Element('div', {'id': this.options.classPrefix + 'Center', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight, 'marginLeft': -(this.options.initialWidth/2), 'display': 'none'}}).injectInside(document.body);
		new Element('a', {'id': this.options.classPrefix + 'CloseLink', 'href': '#'}).injectInside(this.center).onclick = this.close.bind(this);
		if (this.options.overlayCloses) this.overlay.onclick = this.close.bind(this);
		this.urlContent = new Element('div', {'id': this.options.classPrefix + 'Image'}).injectInside(this.center);

		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 100}).hide(),
			resize: this.center.effects($extend({duration: this.options.resizeDuration}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
			urlContent: this.urlContent.effect('opacity', {duration: 100})
		};
		
		if (this.options.openUrl)
		{
			var hrefObj = new Object();
			hrefObj.href = this.options.openUrl;
			this.click(hrefObj);
		}
		if (this.options.urlContent)
		{
			this.click(false);
		}
	},

	click: function(link){
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 15);
		this.center.setStyles({top: this.top, display: ''});
		this.fx.overlay.start(0.1);
		return this.loadContent(link);
	},

	position: function(){
		this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
	},

	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ie ? 'select' : 'embed'));
		elements.each(function(el){
			if (open) el.lbBackupStyle = el.style.visibility;
			el.style.visibility = open ? 'hidden' : el.lbBackupStyle;
		});
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
	},

	keyboardListener: function(event){
		// close on escape key
		if (event.keyCode == 27){
			this.close();
		}
	},

	loadContent: function(link){
		
		this.fx.urlContent.hide();
		this.center.className = 'ubLoading';
		
		if (!this.options.urlContent)
		{
			// start XHR call to content
			var myXHR = new XHR({
				method: 'get',
				onSuccess: this.setContent.bind(this)
			}).send(link.href);
		} else {
			this.setContent(this.options.urlContent);
		}
		return false;
	},

	setContent: function(req){
			// clear the loading image
			this.center.className = '';
			this.urlContent.setHTML(req);
			this.options.contentOnload && this.options.contentOnload.bind(this)(this);
			var contents = this.urlContent.getFirst();
			this.urlContent.style.width =  contents.getSize().size.x+'px';
			this.urlContent.style.height = contents.getSize().size.y+'px';

			if (this.center.clientHeight != this.urlContent.offsetHeight || this.center.clientWidth != this.urlContent.offsetWidth){
				this.fx.resize.start({height: this.urlContent.offsetHeight, width:this.urlContent.offsetWidth, marginLeft: -this.urlContent.offsetWidth/2});
			}
			this.fx.urlContent.start(1);
	},

	close: function(){
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = 'none';
		// reset the height and width back to default values
		this.center.style.width = this.options.initialWidth + 'px';
		this.center.style.height = this.options.initialHeight + 'px';
		
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		this.options.contentClose && this.options.contentClose.bind(this)();
		this.urlContent.setHTML('');
		return false;
	},
	
	toggle: function(){
		this.overlay.toggle();
		this.center.toggle();
	}
	
});