jQuery.fn.prettyPopin = function(settings){
	settings = jQuery.extend({
		modal : false,
		width : false,
		height: false,
		opacity: 0.5,
		animationSpeed: 'fast'
	}, settings);
	return this.each(function(){
		$(this).unbind("click");
		$(this).bind("click",function(){
			buildoverlay();
			buildpopin();
			var href = $(this).attr('href');
			href = href.substring(href.indexOf('#'),href.length);
			if(href.indexOf('#') != -1){
				var responseText = $(href).html();
				$('.prettyPopin .prettyContent .prettyContent-container').html(responseText);
					if(!settings.width){
						settings.width = $('.prettyPopin .prettyContent .prettyContent-container').width() + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-left')) + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-right'));
						$('.prettyPopin .prettyContent .prettyContent-container').width(settings.width);
					}else{
						$('.prettyPopin .prettyContent .prettyContent-container').width(settings.width);
					};	
					if(!settings.height){
						settings.height = $('.prettyPopin .prettyContent .prettyContent-container').height() + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-top')) + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-bottom'));
						$('.prettyPopin .prettyContent .prettyContent-container').height(settings.height);	
						settings.height = $('.prettyPopin .prettyContent .prettyContent-container').height() + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-bottom'));
						$('.prettyPopin .prettyContent .prettyContent-container').width('auto').height('auto');
					}else{
						$('.prettyPopin .prettyContent .prettyContent-container').height(settings.height);
						$('.prettyPopin .prettyContent .prettyContent-container').width('auto').height('auto');
					};
					displayPopin();
			}else{
				$.get($(this).attr('href'),function(responseText){
					$('.prettyPopin .prettyContent .prettyContent-container').html(responseText);
					if(!settings.width){
						settings.width = $('.prettyPopin .prettyContent .prettyContent-container').width() + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-left')) + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-right'));
						$('.prettyPopin .prettyContent .prettyContent-container').width(settings.width);
					}else{
						$('.prettyPopin .prettyContent .prettyContent-container').width(settings.width);
					};	
					if(!settings.height){
						settings.height = $('.prettyPopin .prettyContent .prettyContent-container').height() + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-top')) + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-bottom'));
						$('.prettyPopin .prettyContent .prettyContent-container').height(settings.height);	
						settings.height = $('.prettyPopin .prettyContent .prettyContent-container').height() + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-bottom'));
						$('.prettyPopin .prettyContent .prettyContent-container').width('auto').height('auto');
					}else{
						$('.prettyPopin .prettyContent .prettyContent-container').height(settings.height);
						$('.prettyPopin .prettyContent .prettyContent-container').width('auto').height('auto');
					};
					displayPopin();
				});
			}
				return false;
			});	
		var displayPopin = function() {
			var scrollPos = getScroll();
			$('.prettyPopin').animate({
				'top': ($(window).height()/2) + scrollPos['scrollTop'] - (settings.height/2),
				'left': ($(window).width()/2) + scrollPos['scrollLeft'] - (settings.width/2),
				'width' : settings.width,
				'height' : settings.height
			},settings.animationSpeed, function(){
				displayContent();
			});
		};
		var buildpopin = function() {
			$('body').append('<div class="prettyPopin"><a href="#" id="b_close">Close</a><div class="prettyContent"><img src="/images/prettyPhoto/light_rounded/loader.gif" alt="Loading" class="loader" /><div class="prettyContent-container"></div></div></div>');	
			var scrollPos = getScroll();
			$('.prettyPopin').width(45).height(45).css({
				'top': ($(window).height()/2) + scrollPos['scrollTop'],
				'left': ($(window).width()/2) + scrollPos['scrollLeft']
			}).hide().fadeIn(settings.animationSpeed);
			$('a#b_close').unbind('click');
			$('a#b_close').bind('click',function(){
				closeOverlay();
				return false;
			});
		};
		var buildoverlay = function() {
			$('body').append('<div id="overlay"></div>');
			// Set the proper height
			$('#overlay').css('height',$(document).height());	
			// Fade it in
			$('#overlay').css('opacity',0).fadeTo(settings.animationSpeed,settings.opacity);	
			if(!settings.modal){
				$('#overlay').click(function(){
					closeOverlay();
				});
			};
		};
		var displayContent = function() {
			var scrollPos = getScroll();	
			$c = $('.prettyPopin .prettyContent .prettyContent-container'); 
			$c.parent().find('.loader').hide();
			$c.parent().parent().find('#b_close').show();
			$c.fadeIn(function(){
				$(this).find('input[type=text]:first').trigger('focus');
				$('form').unbind('submit');
				$('form').bind('submit',function(){
					$theForm = $(this);
					$c.fadeOut(function(){
						$c.parent().find('.loader').show();	
						$.post($theForm.attr('action'), $theForm.serialize(),function(responseText){
							$c.html(responseText);
							settings.width = $c.width() + parseFloat($c.css('padding-left')) + parseFloat($c.css('padding-right'));
							settings.height = $c.height() + parseFloat($c.css('padding-top')) + parseFloat($c.css('padding-bottom'));
							$('.prettyPopin').animate({
								'top': ($(window).height()/2) + scrollPos['scrollTop'] - (settings.height/2),
								'left': ($(window).width()/2) + scrollPos['scrollLeft'] - (settings.width/2),
								'width' : settings.width,
								'height' : settings.height
							}, settings.animationSpeed,function(){
								displayContent();
							});
						});
					});
					return false;
				});
				$('form.notWithAjax').unbind('submit');
				$('form.notWithAjax').bind('submit',function(){
					$(this).submit();
				});
				$('.popinLien').unbind('click');
				$('.popinLien').bind('click',function(){
					$theForm = $(this);
					$c.fadeOut(function(){
						$c.parent().find('.loader').show();	
						$.post($theForm.attr('href'),function(responseText){
							$c.html(responseText);
							settings.width = $c.width() + parseFloat($c.css('padding-left')) + parseFloat($c.css('padding-right'));
							settings.height = $c.height() + parseFloat($c.css('padding-top')) + parseFloat($c.css('padding-bottom'));
							$('.prettyPopin').animate({
								'top': ($(window).height()/2) + scrollPos['scrollTop'] - (settings.height/2),
								'width' : settings.width,
								'height' : settings.height
							}, settings.animationSpeed,function(){
								displayContent();
							});
						});
					});
					return false;
				});
			});
			$('a#b_cancel').unbind('click')
			$('a#b_cancel').bind('click',function(){
				closeOverlay();
				return false;
			});
		};
		var closeOverlay = function() {
			$('#overlay').fadeOut(settings.animationSpeed,function(){ $(this).remove(); });
			$('.prettyPopin').fadeOut(settings.animationSpeed,function(){ $(this).remove(); });
		};
		var getScroll = function() {
			scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
			scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
			return {scrollTop:scrollTop,scrollLeft:scrollLeft};
		};
	});
};