/* ------------------------------------------------------------------------
	Class: prettyPopin
	Use: Alternative to popups
	Author: Stephane Caron (http://www.no-margin-for-errors.com)
	Version: 1.3
------------------------------------------------------------------------- */

var _followScroll = false;
var _readyBound = false;

var customWidthAdjustment = 90;
var customHeightAdjustment = 90;

var initialWidth = 114;
var initialHeight = 114;

(function($) {
	$.fn.prettyPopin = function(settings) {
		settings = jQuery.extend({
			modal : false, /* true/false */
			width : false, /* false/integer */
			height: false, /* false/integer */
			opacity: 0.5, /* value from 0 to 1 */
			animationSpeed: 'medium', /* slow/medium/fast/integer */
			followScroll: true, /* true/false */
			loader_path: '/assets/img/layout/prettyPopin/loader.gif', /* path to your loading image */
			callback: function(){} /* callback called when closing the popin */
		}, settings);
		
		// Pre-load images
		var upperLeftImg = new Image();
		$(upperLeftImg).addClass("hide");
		$(upperLeftImg).attr("src","/assets/img/layout/prettyPopin/upperLeft.png");
		
		var topImg = new Image();
		$(topImg).addClass("hide");
		$(topImg).attr("src","/assets/img/layout/prettyPopin/top.png");
		
		var upperRightImg = new Image();
		$(upperRightImg).addClass("hide");
		$(upperRightImg).attr("src","/assets/img/layout/prettyPopin/upperRight.png");
		
		var leftImg = new Image();
		$(leftImg).addClass("hide");
		$(leftImg).attr("src","/assets/img/layout/prettyPopin/left.png");
		
		var rightImg = new Image();
		$(rightImg).addClass("hide");
		$(rightImg).attr("src","/assets/img/layout/prettyPopin/right.png");
		
		var lowerLeftImg = new Image();
		$(lowerLeftImg).addClass("hide");
		$(lowerLeftImg).attr("src","/assets/img/layout/prettyPopin/lowerLeft.png");
		
		var bottomImg = new Image();
		$(bottomImg).addClass("hide");
		$(bottomImg).attr("src","/assets/img/layout/prettyPopin/bottom.png");
		
		var lowerRightImg = new Image();
		$(lowerRightImg).addClass("hide");
		$(lowerRightImg).attr("src","/assets/img/layout/prettyPopin/lowerRight.png");
		
		var closeImg = new Image();
		$(closeImg).addClass("hide");
		$(closeImg).attr("src","/assets/img/layout/prettyPopin/close.png");
		
		var closeHoverImg = new Image();
		$(closeHoverImg).addClass("hide");
		$(closeHoverImg).attr("src","/assets/img/layout/prettyPopin/close_o.png");
		
		var loaderImg = new Image();
		$(loaderImg).addClass("hide");
		$(loaderImg).attr("src","/assets/img/layout/prettyPopin/loader.gif");
		
		function bindReady(){ // To bind them only once
			if(_readyBound) return;
			_readyBound = true;
			$(window).scroll(function(){ _centerPopin(); });
			$(window).resize(function(){ _centerPopin(); });
		};
		bindReady();
		
		return this.each(function(){
			var popinWidth = 0;
			var popinHeight = 0;
			var contentWidth = 0;
			var contentHeight = 0;
			var $c;
		
			$(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
					contentWidth = settings.width || $('.prettyPopin .prettyContent .prettyContent-container').width();
					popinWidth = contentWidth + customWidthAdjustment;
					
					contentHeight = settings.height || $('.prettyPopin .prettyContent .prettyContent-container').height();
					popinHeight = contentHeight + customHeightAdjustment;
					
					$('#bellaPopinContent').width(contentWidth);
					$('#bellaPopinContent').height(contentHeight);
					
					displayPopin();
					PEPS.rollover.init();
				});
				return false;
			});

			var displayPopin = function() {
				var scrollPos = _getScroll();

				projectedTop = ($(window).height()/2) + scrollPos['scrollTop'] - (popinHeight/2);
				if(projectedTop < 0) {
					projectedTop = 10;
					_followScroll = false;
				}else{
					_followScroll = settings.followScroll;
				}

				$('.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"><div class="bellaPopin"><table border="0" cellpadding="0" cellspacing="0"><tr><td id="bellaPopinUpperLeft">&nbsp;</td><td id="bellaPopinTop"></td><td id="bellaPopinUpperRight"></td></tr><tr><td id="bellaPopinLeft"></td><td id="bellaPopinContent"><div class="prettyContent"><img src="'+settings.loader_path+'" alt="Loading" class="loader" /><div class="prettyContent-container"></div></div></td><td id="bellaPopinRight"></td></tr><tr><td id="bellaPopinLowerLeft"></td><td id="bellaPopinBottom"></td><td id="bellaPopinLowerRight"></td></tr></table><div id="bellaPopinClose"><a href="#" rel="close"><img src="/assets/img/layout/prettyPopin/close.png" class="ro" width="25" height="26" alt="close" /></a></div></div></div>');
				$c = $('.prettyPopin .prettyContent .prettyContent-container'); // The content container
			
				$('.prettyPopin a[rel=close]:eq(0)').click(function(){ closeOverlay(); return false; });
			
				var scrollPos = _getScroll();
			
				// Show the popin
				//$('.prettyContent').width(24).height(24);
				$('.prettyPopin').width(initialWidth).height(initialHeight).css({
					'top': ($(window).height()/2) + scrollPos['scrollTop'],
					'left': ($(window).width()/2) + scrollPos['scrollLeft']
				}).hide().fadeIn(settings.animationSpeed);
			};
		
			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() {
				$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');

					// Check for paging
					$('.prettyPopin a[rel=internal]').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);

								_refreshContent($c);
							});
						});
						return false;
					});
				

					// Submit the form in ajax
					$('.prettyPopin 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);

								_refreshContent($c);
							});
						});
						return false;
					});
				});
				$('.prettyPopin a[rel=close]:gt(0)').click(function(){ closeOverlay(); return false; });
			};
	
			var _refreshContent = function(){
				var scrollPos = _getScroll();
				
				// if(!settings.width)	popinWidth = $c.width() + parseFloat($c.css('padding-left')) + parseFloat($c.css('padding-right'));
				// popinWidth = popinWidth + customWidthAdjustment;
				
				popinWidth = settings.width || $('.prettyPopin .prettyContent .prettyContent-container').width();
				$('#bellaPopinContent').width(popinWidth);
				popinWidth = popinWidth + customWidthAdjustment;
				$('.prettyPopin').width(popinWidth);
				
				// if(!settings.height) popinHeight = $c.height() + parseFloat($c.css('padding-top')) + parseFloat($c.css('padding-bottom'));
				// popinHeight = popinHeight + customHeightAdjustment;
				
				popinHeight = settings.height || $('.prettyPopin .prettyContent .prettyContent-container').height();
				$('#bellaPopinContent').height(popinHeight);
				popinHeight = popinHeight + customHeightAdjustment;
				$('.prettyPopin').height(popinHeight);
				
				projectedTop = ($(window).height()/2) + scrollPos['scrollTop'] - (popinHeight/2);
				if(projectedTop < 0) {
					projectedTop = 10;
					_followScroll = false;
				}else{
					_followScroll = settings.followScroll;
				}

				$('.prettyPopin').animate({
					'top': projectedTop,
					'left': ($(window).width()/2) + scrollPos['scrollLeft'] - (popinWidth/2),
					'width' : popinWidth,
					'height' : popinHeight
				}, settings.animationSpeed,function(){
					displayContent();
					PEPS.rollover.init();
				});
			};
		
			var closeOverlay = function() {
				$('#overlay').fadeOut(settings.animationSpeed,function(){ $(this).remove(); });
				$('.prettyPopin').fadeOut(settings.animationSpeed,function(){ $(this).remove(); settings.callback() });
			};
		});
	
		function _centerPopin(){
			if(!_followScroll) return;

			// Make sure the popin exist
			if(!$('.prettyPopin')) return;
			
			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;
				_followScroll = false;
			}else{
				_followScroll = true;
			}

			$('.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};
		};
	};
})(jQuery);
