/**
 *	Interim jQuery popup by Adam Passey. 
 */
(function($) {
	$.fn.interim = function(options) {
	
		//first, attach the required html to the body
		if( $("#interim").length == 0 ) {
			$("body").append('<div id="interimBg"></div><a href="#close" id="interimClose">close</a><div id="interim"><div id="interimContent"></div></div><div id="interimLoader"></div>');
			//bind global window elements
			$(window).bind('resize', resizeWindow);	//bind resizeWindow to window resize
			$(window).bind('scroll', scrollWindow); //bind scrollWindow to window scroll
		}
	
		//defaults
		var defaults = {
			//options here
			overlayOpacity: 0.5,
			fadeInSpeed: 100,
			interimResizeSpeed: 300,
			interimContentSpeed: 300,
			defaultHeight: 300,
			defaultWidth: 300,
			closeOffset: 12,
			action: 'click',
			message: '',
			interimRequest: false	//added specifically for pacificcrestfcu.com
		};
		
		//extend options	
		var options = $.extend(defaults, options);
		
		//set the overlay objects
		var overlay 		= $("#interimBg");
		var interim 		= $("#interim");
		var interimContent 	= $("#interimContent");
		var interimLoader 	= $("#interimLoader");
		var interimClose 	= $("#interimClose");
		
		/** 
		 *	Window resize / scroll functions
		 */
		// on window resize, resize overlay
		function resizeWindow() {
		
			//reposition overlay
			$(overlay).css({height: $(document).height(), width: $(window).width(), top: $(window).scrollTop()});
			
			centerInterim();
		}
		
		//on window scroll, move overlay to match the screen
		function scrollWindow() {
			$(overlay).css({top:$(window).scrollTop()+'px'});
			centerInterim();
		}
		
		/**
		 *	Overlay functions
		 */
		//set the overlay opacity and fade in
		function startInterim(href){
			
			$(overlay).css({opacity:options.overlayOpacity}).fadeIn(options.speed,function() {
			
			centerInterim();
			
				$(interim).fadeIn(options.speed,function() {	
				
					//if we're displaying a simple message without ajax content
					if( options.message != '' ) {
						
						//set the loader with the content so we can
						//see how big it is.
						setInterimLoaderContent( options.message );
						
						//set its height and width
						resizeHeight = $(interimLoader).height();
						resizeWidth = $(interimLoader).width();
						
						//add the content to the interim, and load
						setInterimContent( options.message );
						animateInterim(resizeHeight,resizeWidth);
						setInterimLoaderContent('');
					} 
					
					else if( href.match('.jpg|.gif|.png|.bmp$') ) {
						//preload the image, set the height and width of the resize
											
						var img = new Image();
						img.src = href;
												
						$(img).load(function(e) {
							
							//when the image is loaded, gets its height
							//and width, set it as interim content and display
							var resizeHeight = img.height;
							var resizeWidth = img.width;
							
							setInterimContent('<img id="interimImg" src="'+href+'"'+" "+' />');
							animateInterim(resizeHeight,resizeWidth);
						});
					
						
					} else {
						//preload the content, set the height and width to default
						
						var resizeHeight = options.defaultHeight;
						var resizeWidth = options.defaultWidth;

						//added specifically for pacificcrestfcu.com - can take out in 
						//final version
						if(options.interimRequest == true) {
							href = href.replace('/\/$','');
							href += '&ajax=true';
						}
						
						//load the contents of the requested href
						$.ajax({
							type: 	"GET",
							url:	href,
							success:function(html) {
								
								//set the loader with the content so we can
								//see how big it is.
								setInterimLoaderContent(html);
								
								//set its height and width
								resizeHeight = $(interimLoader).height() + 80;
								//add 100 pixels to height,
								//for some reason it isn't sizing the entire loaded doc
								//and it needs to be lengthened. Probably because
								//it's not taking into consideration the margin/padding
								resizeWidth = $(interimLoader).width() + 8;
								
								//add the content to the interim, and load
								setInterimContent(html);
								animateInterim(resizeHeight,resizeWidth);
								setInterimLoaderContent('');
							}
						});
					}

				});
			});
		}
		
		function animateInterim(resizeHeight, resizeWidth) {
			//$("body").append("resizing interim to: "+resizeHeight+" x "+resizeWidth+"<br />");
			
			centerInterim();
					
			if( $(interim).width() == resizeWidth && $(interim).height() == resizeHeight ) {
			    //its already been sized to this size, show contents
			    $(interimContent).fadeIn(options.interimContentSpeed);
			    $(interimClose).fadeIn(options.interimContentSpeed);
			} else {
			    //determine size and resize before displaying
			    var pos = $(interim).offset();
			    var topOffset = ( $(window).height() / 2 ) - ( resizeHeight / 2 ) + $(window).scrollTop();
			    $(interim).animate({height:(resizeHeight + 20 )+'px',top: topOffset+'px'}, options.interimResizeSpeed,function() {
			    	
			    	//resize interim width as position is being adjusted
			    	var leftOffset = ( $(window).width() / 2 ) - ( resizeWidth / 2 );
			    	$(interim).animate({width:resizeWidth+'px',left:leftOffset+'px'}, options.interimResizeSpeed, function() {
			    		
			    		//now show the contents
			    		$(interimContent).fadeIn(options.interimContentSpeed,function() {
			    			$(interimClose).css({top:topOffset - options.closeOffset+'px',left:leftOffset - options.closeOffset+'px'}).fadeIn(options.interimContentSpeed);
			    		});
			    	});
			    });
			}
		}

		
		//hide the overlay
		function hideInterim() {
			$(interimClose).fadeOut(options.speed);
			$(interim).fadeOut(options.speed, function() {
				$(overlay).fadeOut(options.speed,function() {
					setInterimContent('');	//null content when closed
					$(interimContent).hide();
				});
			});
		}
		
		/**
		 *	Interim document functions
		 */
		//this function centers the interim box according to it's
		//current width / height - this is for pre-animation fading
		function centerInterim() {
			var interimWidth = $(interim).width();
			var interimHeight = $(interim).height();
			var topOffset = ( $(window).height() / 2 ) - ( $(interim).height() / 2 ) + $(window).scrollTop();
			var leftOffset = ( $(window).width() / 2 ) - ( $(interim).width() / 2 );
				
			$(interim).css({top:topOffset+'px', left:leftOffset+'px'});
			$(interimClose).css({top:topOffset - options.closeOffset+'px',left:leftOffset - options.closeOffset+'px'});
		}
		
		//set the interim content
		function setInterimContent(content) {
			$(interimContent).html(content);
		}
		
		//set the loader content
		function setInterimLoaderContent(content) {
			$(interimLoader).html(content);
		}
		
		$(interimClose).click(function() {
			hideInterim();
			return false;
		});
		
		//if they click the interim box
		$(interim).mousedown(function(e) {
			//get the point where it clicked
			var startMouseTop = e.pageY;
			var startMouseLeft = e.pageX;
			$(this).mousemove(function(e) {
				//move the window
			});
		});
		
		//apply for each object		
		return this.each(function() {
		
			//get the object
			var obj = $(this);
			
			if( options.action == 'click' ) {
				//if the object itself gets clicked
				$(obj).click(function() {
					href = $(this).attr("href");
					startInterim( href );
					return false;
				});
			}
			
			if( options.action == 'hover' ) {
				$(obj).hover(function() {
					href = $(this).attr("href");
					startInterim( href );
					return false;
				});
			}
			
			if( options.action == 'now' ) {
				if( options.message != '' ) {
					startInterim( options.message );
				} else {
					href = $(this).attr("href");
					startInterim( href );
				}
				return false;
			}
			
			//if the overlay gets clicked 
			$(overlay).click(function() {
				hideInterim();
				return false;
			});

		}); //end of return this.each
	};
}) (jQuery);

//apply the interim to all rel="interim"
$(document).ready(function() {
	$("a").each(function() {
		if( $(this).attr("rel") == 'interimRequest' ) {
			$(this).interim({ interimRequest: true });
		} else if( $(this).attr("rel") == 'interimRequestImage' ) {
			$(this).interim({
				interimRequest: true
			});
		}
	});
});
