/* ------------------------------------------------------------------------
	Class: prettyPopin
	Use: Alternative to popups
	Author: Stephane Caron (http://www.no-margin-for-errors.com)
	Version: 1.0
------------------------------------------------------------------------- */

jQuery.fn.prettyPopin = function(settings) {
	settings = jQuery.extend({
		modal : false, /* true/false */
		width : false, /* true/false */
		height: false, /* true/false */
		opacity: 0.5, /* value from 0 to 1 */
		animationSpeed: 'fast' /* slow/medium/fast */
	}, settings);
	return this.each(function(){
		jQuery(this).click(function(){
			buildoverlay();
			buildpopin();
			
			// Load the content
			jQuery.get(jQuery(this).attr('href'),function(responseText){
				jQuery('.prettyPopin .prettyContent .prettyContent-container').html(responseText);
				
				if(!settings.width){
					settings.width = jQuery('.prettyPopin .prettyContent .prettyContent-container').width() + parseFloat(jQuery('.prettyPopin .prettyContent .prettyContent-container').css('padding-left')) + parseFloat(jQuery('.prettyPopin .prettyContent .prettyContent-container').css('padding-right'));
					jQuery('.prettyPopin .prettyContent .prettyContent-container').width(settings.width);
				}else{
					jQuery('.prettyPopin .prettyContent .prettyContent-container').width(settings.width);
				};
				
				if(!settings.height){
					settings.height = jQuery('.prettyPopin .prettyContent .prettyContent-container').height() + parseFloat(jQuery('.prettyPopin .prettyContent .prettyContent-container').css('padding-top')) + parseFloat(jQuery('.prettyPopin .prettyContent .prettyContent-container').css('padding-bottom'));
					jQuery('.prettyPopin .prettyContent .prettyContent-container').height(settings.height);
					
					settings.height = jQuery('.prettyPopin .prettyContent .prettyContent-container').height() + parseFloat(jQuery('.prettyPopin .prettyContent .prettyContent-container').css('padding-bottom'));
					jQuery('.prettyPopin .prettyContent .prettyContent-container').width('auto').height('auto');
				}else{
					jQuery('.prettyPopin .prettyContent .prettyContent-container').height(settings.height);
					jQuery('.prettyPopin .prettyContent .prettyContent-container').width('auto').height('auto');
				};
				
				displayPopin();
			});
			return false;
		});
		
		var displayPopin = function() {
			var scrollPos = getScroll();

			jQuery('.prettyPopin').animate({
				'top': (jQuery(window).height()/2) + scrollPos['scrollTop'] - (settings.height/2),
				'left': (jQuery(window).width()/2) + scrollPos['scrollLeft'] - (settings.width/2),
				'width' : settings.width,
				'height' : settings.height
			},settings.animationSpeed, function(){
				displayContent();
			});
		};
		
		var buildpopin = function() {
			jQuery('body').append('<div class="prettyPopin"><a href="#" id="b_close">Close</a><div class="prettyContent"><img src="images/prettyPopin/loader.gif" alt="Loading" class="loader" /><div class="prettyContent-container"></div></div></div>');
			
			var scrollPos = getScroll();
			
			// Show the popin
			jQuery('.prettyPopin').width(45).height(45).css({
				'top': (jQuery(window).height()/2) + scrollPos['scrollTop'],
				'left': (jQuery(window).width()/2) + scrollPos['scrollLeft']
			}).hide().fadeIn(settings.animationSpeed);
			
			jQuery('a#b_close').click(function(){ closeOverlay(); return false; });
		};
		
		var buildoverlay = function() {
			jQuery('body').append('<div id="overlay"></div>');
			
			// Set the proper height
			jQuery('#overlay').css('height',jQuery(document).height());
			
			// Fade it in
			jQuery('#overlay').css('opacity',0).fadeTo(settings.animationSpeed,settings.opacity);
			
			if(!settings.modal){
				jQuery('#overlay').click(function(){
					closeOverlay();
				});
			};
		};
		
		var displayContent = function() {
			var scrollPos = getScroll();
			
			jQueryc = jQuery('.prettyPopin .prettyContent .prettyContent-container'); // The container
			jQueryc.parent().find('.loader').hide();
			jQueryc.parent().parent().find('#b_close').show();
			jQueryc.fadeIn(function(){
				// Focus on the first form input if there's one
				jQuery(this).find('input[type=text]:first').trigger('focus');

				// Submit the form in ajax
				jQuery('form').bind('submit',function(){
					jQuerytheForm = jQuery(this);
					// Fade out the current content
					jQueryc.fadeOut(function(){
						jQueryc.parent().find('.loader').show();
						
						// Submit the form
						jQuery.post(jQuerytheForm.attr('action'), jQuerytheForm.serialize(),function(responseText){
							// Replace the content
							jQueryc.html(responseText);

							settings.width = jQueryc.width() + parseFloat(jQueryc.css('padding-left')) + parseFloat(jQueryc.css('padding-right'));
							settings.height = jQueryc.height() + parseFloat(jQueryc.css('padding-top')) + parseFloat(jQueryc.css('padding-bottom'));

							jQuery('.prettyPopin').animate({
								'top': (jQuery(window).height()/2) + scrollPos['scrollTop'] - (settings.height/2),
								'left': (jQuery(window).width()/2) + scrollPos['scrollLeft'] - (settings.width/2),
								'width' : settings.width,
								'height' : settings.height
							}, settings.animationSpeed,function(){
								displayContent();
							});
						});
					});
					return false;
				});
			});
			jQuery('a#b_cancel').click(function(){ closeOverlay(); });
		};
		
		var closeOverlay = function() {
			jQuery('#overlay').fadeOut(settings.animationSpeed,function(){ jQuery(this).remove(); });
			jQuery('.prettyPopin').fadeOut(settings.animationSpeed,function(){ jQuery(this).remove(); });
		};
		
		var getScroll = function() {
			scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
			scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
			return {scrollTop:scrollTop,scrollLeft:scrollLeft};
		};
		
	});
};
