(function($) {

$.jpopup = {};
$.fn.jpopup = function(settings)
{
	settings = $.extend({}, $.fn.jpopup.defaults, settings);

	return this.eq(0).each(
		function()
		{
			var $this = $(this);
			var content = $this.html();
			var shadow = '';
			var shadow_html = '<div id="jshadow"></div>';
			var popup_html = ([
				'<div id="jpopup" class="'+$this.attr('id')+'"><div class="jpopuptop"></div><div class="jpopupcontent">',
				'</div><div class="jpopupbottom"></div>',
				'<a href="#close" class="close">' + settings.closeText + '</a>',
				'</div>'
			]).join('');
			
			// remove if already showed
			$('#jpopup,#jshadow').remove(); 
			
			// if shadow enabled > show shadow
			if(settings.shadow){
				$(document.body).append(shadow_html);
				$('#jshadow').css({
					display: 'block',
					opacity: settings.opacity,
					background:	settings.background,
					zIndex: (settings.zIndex-1)
				});
			}
			
			// add popup to DOM
			$(document.body).append(popup_html);
			$('#jpopup').show();
			$('#employee .box-item').css('visibility', 'hidden');
			
			$('#jpopup').css({
				zIndex: settings.zIndex,
				top: $(window).scrollTop() + ($(window).height() - $('#jpopup').height())/4 + settings.heightFix,
				left: ($(window).width() - $('#jpopup').width())/2
			});
			
			$('#jpopup div.jpopupcontent').html( content );
			// show close button
			if(settings.close){
				$('#jpopup a.close').show();
			}

			// add close event
			$('#jpopup a.close,#jshadow').click(function(e){
				e.preventDefault();
				$('#employee .box-item').css('visibility', 'visible');
				$('#jpopup,#jshadow').remove();
				// run onHide
				if( typeof(settings.onHide) == 'function' ){
					settings.onHide();
				}
				return false;
			});
			
			// run onShow
			if( typeof(settings.onShow) == 'function' ){
				settings.onShow();
			}
			
		}
	)
};

$.fn.jpopup.defaults = {
	close: false,
	shadow : false,
	opacity: 0.8,
	background: '#000',
	zIndex: 1000,
	heightFix: 0,
	onShow: null,
	onHide:	null
};

})(jQuery);
