var ContactPopup =
{
	overlay : null,
	
	init : function()
	{
		$('a').each(function()
		{
			var element = $(this);
			var href = element.attr('href');
			
			if (typeof(href) != 'undefined' && href.toLowerCase().indexOf('mailto:') == 0)
			{
				element.click(function()
				{
					ContactPopup.handleClick($(this));
					return false;
				});
			} // end if
		});
	},
	
	handleClick : function(element)
	{
		this.show(element.html());
	},
	
	show : function(email)
	{
		if (this.overlay == null)
		{
			this.createOverlay();
		}
		
		var url = '/kontakt/popup_index.php?mailto=' + email;
		
		var iframe = document.createElement('iframe');
		iframe.width = '100%';
		iframe.height = '100%';
		iframe.frameBorder = '0';
		
		var self = this;
		
		$(iframe).load(function() 
		{
			self.overlay.main.fadeIn(2000);
		});
		
		iframe.src = url;
		iframe.scrolling = 'no';
		
		this.overlay.frame = $(iframe);
		this.overlay.frameContainer.append(this.overlay.frame);
		
		this.setPosition();
		this.overlay.header.text('Kontakt zu uns');
	},
	
	createOverlay : function()
	{
		this.overlay =
		{
			main : $('#overlay'),
			header : $('#overlay_header'),
			frameContainer : $('#overlay_frameContainer'),
			content : $('#overlay_content')
		};
		
		$(window).resize(function() 
		{
			ContactPopup.setPosition();
		});
		
		$('#overlay_close').click(function()
		{
			ContactPopup.close();
		});
		
		$('#overlay_background').click(function()
		{
			ContactPopup.close();
		});
	},
	
	setPosition : function()
	{
		var x = Math.round($(window).width() / 2 - this.overlay.content.width() / 2);
		var y = Math.round($(window).height() / 2 - this.overlay.content.height() / 2);
		this.overlay.content.css(
		{
			left : x + 'px',
			top : y + 'px'
		});
	},
	
	close : function()
	{
		if (this.overlay == null)
		{
			this.createOverlay();
		}

		var self = this;

		this.overlay.main.fadeOut(2000, function()
		{
			self.overlay.frame.remove();
			delete(self.overlay.frame);
		});
	}
};

$(document).ready(function()
{
	ContactPopup.init();
});
