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

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 */
		followScroll: true, /* true/false */
		loader_path: '/media/images/prettyPopin/loader.gif', /* path to your loading image */
		callback: function(){} /* callback called when closing the popin */
	}, settings);
	
	$(window).scroll(function(){ if(settings.followScroll) _centerPopin(); });
	$(window).resize(function(){ if(settings.followScroll) _centerPopin(); });

	return this.each(function(){
		var popinWidth;
		var popinHeight;
		
		$(this).click(function(){
			buildoverlay();
			buildpopin();
			
			// Load the content
			$.get($(this).attr('href'),function(responseText){
				$('.prettyPopin .prettyContent .prettyContent-container').html(responseText);
				
				// This block of code is used to calculate the width/height of the popin
				popinWidth = settings.width || $('.prettyPopin .prettyContent .prettyContent-container').width() + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-left')) + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-right'));
				$('.prettyPopin').width(popinWidth);
				popinHeight = settings.height || $('.prettyPopin .prettyContent .prettyContent-container').height() + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-top')) + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-bottom'));
				$('.prettyPopin').height(popinHeight);
				popinHeight = $('.prettyPopin .prettyContent .prettyContent-container').height() + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-top')) + parseFloat($('.prettyPopin .prettyContent .prettyContent-container').css('padding-bottom'));
				$('.prettyPopin').height(popinHeight);
				
				// Now reset the width/height
				$('.prettyPopin').height(45).width(45);
				
				displayPopin();
			});
			return false;
		});

		var displayPopin = function() {
			var scrollPos = _getScroll();

			projectedTop = ($(window).height()/2) + scrollPos['scrollTop'] - (popinHeight/2);
			if(projectedTop < 0) {
				projectedTop = 10;
				settings.followScroll = false;
			}

			$('.prettyPopin').animate({
				'top': projectedTop,
				'left': ($(window).width()/2) + scrollPos['scrollLeft'] - (popinWidth/2),
				'width' : popinWidth,
				'height' : popinHeight
			},settings.animationSpeed, function(){
				displayContent();
			});
		};
		
		var buildpopin = function() {
			$('body').append('<div class="prettyPopin"><a href="#" id="b_close">Close</a><div class="prettyContent"><img src="'+settings.loader_path+'" alt="Loading" class="loader" /><div class="prettyContent-container"></div></div></div>');
			
			var scrollPos = _getScroll();
			
			// Show the popin
			$('.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').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'); // The container
			$c.parent().find('.loader').hide();
			$c.parent().parent().find('#b_close').show();
			$c.fadeIn(function(){
				// Focus on the first form input if there's one
				$(this).find('input[type=text]:first').trigger('focus');

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

							popinWidth = $c.width() + parseFloat($c.css('padding-left')) + parseFloat($c.css('padding-right'));
							popinHeight = $c.height() + parseFloat($c.css('padding-top')) + parseFloat($c.css('padding-bottom'));

							projectedTop = ($(window).height()/2) + scrollPos['scrollTop'] - (popinHeight/2);
							if(projectedTop < 0) {
								projectedTop = 10;
								settings.followScroll = false;
							}

							$('.prettyPopin').animate({
								'top': projectedTop,
								'left': ($(window).width()/2) + scrollPos['scrollLeft'] - (popinWidth/2),
								'width' : popinWidth,
								'height' : popinHeight
							}, settings.animationSpeed,function(){
								displayContent();
							});
						});
					});
					return false;
				});
				
				// Submit the form in ajax
				$('a[rel=paging]').click(function(){
					$link = $(this);
					
					// Fade out the current content
					$c.fadeOut(function(){
						$c.parent().find('.loader').show();
						
						// Submit the form
						$.get($link.attr('href'),function(responseText){
							// Replace the content
							$c.html(responseText);

							popinWidth = $c.width() + parseFloat($c.css('padding-left')) + parseFloat($c.css('padding-right'));
							popinHeight = $c.height() + parseFloat($c.css('padding-top')) + parseFloat($c.css('padding-bottom'));

							projectedTop = ($(window).height()/2) + scrollPos['scrollTop'] - (popinHeight/2);
							if(projectedTop < 0) {
								projectedTop = 10;
								settings.followScroll = false;
							}

							$('.prettyPopin').animate({
								'top': projectedTop,
								'left': ($(window).width()/2) + scrollPos['scrollLeft'] - (popinWidth/2),
								'width' : popinWidth,
								'height' : popinHeight
							}, settings.animationSpeed,function(){
								displayContent();
							});
						});
					});
					return false;
				});
			});
			$('a#b_cancel[rel!=paging],a[rel=close]').click(function(){ closeOverlay(); });
		};
		
		var closeOverlay = function() {
			$('#overlay').fadeOut(settings.animationSpeed,function(){ $(this).remove(); });
			$('.prettyPopin').fadeOut(settings.animationSpeed,function(){ $(this).remove(); settings.callback() });
		};
	});
	
	function _centerPopin(){
		// Make sure the popin exist
		if(!$('.prettyPopin')) return;
		
		//Make sure the gallery is open
		var scrollPos = _getScroll();
		
		if($.browser.opera) {
			windowHeight = window.innerHeight;
			windowWidth = window.innerWidth;
		}else{
			windowHeight = $(window).height();
			windowWidth = $(window).width();
		};
		
		projectedTop = ($(window).height()/2) + scrollPos['scrollTop'] - ($('.prettyPopin').height()/2);
		if(projectedTop < 0) {
			projectedTop = 10;
			settings.followScroll = false;
		}
		
		$('.prettyPopin').css({
			'top': projectedTop,
			'left': ($(window).width()/2) + scrollPos['scrollLeft'] - ($('.prettyPopin').width()/2)
		});
	};
	
	function _getScroll(){
		scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
		scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
		return {scrollTop:scrollTop,scrollLeft:scrollLeft};
	};
};