/**
 *	@title: Overlay
 *	@description: Creates an Overlay window.
 *	@Usage: new Overlay(content).show();
 */
var Overlay = new Class({
	
	Implements: [Events, Options],
	
	options: {
		modal: true,
		transitionDuration: 800,
		left: 0,
		top: 0
	},
	
	initialize: function(content, options) {
		this.setOptions(options);
		var el = this.element = new Element('div', { 'class': 'overlay' });
		if(this.options.modal) {
			el.addClass('modal');
		}
		el.grab(this.elementalize(content));
		this.setup();
	},
	
	setup: function() {
		this.element
			.set('tween', {
				duration: this.options.transitionDuration
			})
			.setStyles({
				display: 'none',
				opacity: 0
			});
		var el = this.element;
		if(this.options.top || this.options.left) {
			if(this.options.modal) {
				el = this.element.getFirst();
			}
			
			el.setStyles({
				left: this.options.left,
				top: this.options.top,
				position: 'absolute'
			});
		}
		this._hidden = true;
	},
	
	show: function() {
		this.element.setStyle('display', 'block');
		
	/*	var left = this.element.getStyle('left').toInt(),
			top = this.element.getStyle('top').toInt(),
			wWidth = document.body.getWidth().toInt(),
			wHeight = document.body.getHeight().toInt();
			
		this.element.setStyle('left', Math.round((left / wWidth) * 100) + '%');
		this.element.setStyle('top', Math.round((top / wHeight) * 100) + '%');*/
		
		this.element.fade('in');
		this._hidden = false;
		this.fireEvent('show');
	},
	
	hide: function() {
		this.fireEvent('hide');
		var el = this.element;
		el.fade('out').get('tween').chain(function() {
			el.setStyle('display', 'none');
		})
		this._hidden = true;
	},
	
	toggle: function() {
		if(this._hidden) {
			this.show();
		} else {
			this.hide();
		}
	},
	
	toElement: function() {
		return this.element;
	},
	
	elementalize: function(content) {
		var type = $type(content);
		if(type == 'element') {
			return content;
		} else if(type == 'string') {
			return new Element('div', { html: content }).getFirst();
		} else {
			return new Element('div');
		}
	}.protect()
	
});


/**
 *	@title: ShareOverlay
 *	@description: Shows a Modal Overlay with Share options, like Twitter, E-mail, or Facebook.
 *	@usage: new ShareOverlay('Share this Link', 'http://fisker.com/#karma');
 */
var ShareOverlay = new Class((function() {
	var shareEl,
		nonce = + new Date(); 
	new Request({
		url: '/js/plugins/share.html' + (Browser.Engine.webkit ? '?'+nonce : ''),
		async: false,
		method: 'get',
		onSuccess: function(text) {
			shareEl = new Element('div', { html: text }).getFirst();
			if(!('placeholder' in document.createElement('input'))) {
				shareEl.getElements('input, textarea').MooPlaceholder();
			}
		}
	}).send();
	
	var linkFormats = {
		facebook: 'http://www.facebook.com/sharer.php?u={url}&t={title}',
		twitter: 'http://twitter.com/home?status={title} {url}',
		linkedin: 'http://www.linkedin.com/shareArticle?mini=true&url={url}&title={title}&summary={title}&source=Fisker Automotive',
		stumbleupon: 'http://stumbleupon.com/submit?url={url}&title={title}',
		digg: 'http://digg.com/submit?url={url}&title={title}',
		reddit: 'http://reddit.com/submit?url={url}&title={title}',
		delicious: 'http://delicious.com/save?jump=yes&url={url}&title={title}'
	};
	
	var zIndexCounter = 50;
	
	return {
	
		Extends: Overlay,
		
		initialize: function(options) {
			this.parent(shareEl.clone(), options);
			var that = this;
			this.element.getElement('.close-button').addEvent('click', function(e) {
				that.hide();
			});
			this.addEvent('show', function() {
				this.setLinks();
			});
			
			var form = this.element.getElement('form');
			form.addEvent('submit', function (event) {
				event.stop();
				var req = new Request.JSON({
					url: '/rest/newsletter/staf',
					onSuccess: function () {
						that.hide();
					},
					onFailure: function () {
						that.hide();
					}
				});
				req.send(Hash.toQueryString({
					fisker_url: document.location.toString(),
					fisker_to: form.email.value,
					fisker_message: form.message.value
				}));
			});
		},
		
		show: function() {
			this.element.setStyle('z-index', ++zIndexCounter);
			this.parent();
		},
		
		setLinks: function() {
			var el = this.element;
			$each(linkFormats, function(format, website) {
				el.getElement('a.'+website).set('href', format.substitute({ url: (window.location + '').replace(/#/g,'%23'), title: document.title }));
			});
		}
		
	}
})());