/**
* Basic popup window which sizes based on window size, made by Nate.
* Feb 9, 2011
*/
(function( $ )
{   

function pageWidth() {
		return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;} 
		
	function pageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
		} 

    var methods = {
    
            start : function( options )
            {
				//leave out the "var" keyword for variables to be accessible throughout plugin methods
				//$ = $(this);
				//specify the default settings
                settings = {
						url: null,
						content: null,
						padding: {x:75, y: 75},
						closeButtonHeight: 16,
						closeButton: 'close.png',
						loadingImage: 'loader.gif',
						startSize: {x: 300, y: 225 },
						endSize: null,
						overlayOpacity:	0.4,
						overlayFade: 750,
						overlayColor: '#000',
						closeKey: null,
						resizeTime: 1000,
						loaderFadeout: 300
                }
                //override defaults if options are provided
                if( options )
                {
                    $.extend( settings, options);
                }
				
				//Start the initial setup
				$('embed, object, select').css({ 'visibility' : 'hidden' }); //hide elements that don't layer properly in IE

				
				$.fn.popup('show');
				
				
				if(settings.url != null)
				{
					$.ajax({ url: settings.url, /*settings: getVariables, */	  settingsType: "html", cache: false, context: this, success: function(response, status, httpRequest){ $.fn.popup('contentLoaded', response);} });
					//$.ajax(settings.content, { context: $('popup-container'), settingsType: 'html'});
				}
				
				$(window).bind('resize.popup', function(){$.fn.popup('update')});
				$(window).bind('scroll.popup', function(){$.fn.popup('update')});
				$('#popup-close-button').bind('click.popup', methods.close);
                return $(this);
            },
            
			show: function()
			{			
				//Add the overlay to the DOM if the opacity is greater than 0.
				if(settings.overlayOpacity > 0)
				{ 
					$('body').append('<div id="popup-overlay"></div>');

					$('#popup-overlay').css({  backgroundColor: settings.overlayColor, opacity: settings.overlayOpacity }).fadeIn(settings.overlayFade);
				}
				
				//Add the popup
				$('body').append('<div id="popup"><div id="popup-image-container"><img id="popup-close-button" src="'+settings.closeButton+'" /></div><div id="popup-container"><div id="popup-content"><img src="' + settings.loadingImage + '" id="popup-loading" /></div></div></div>');	
				$('#popup').css({  left: (pageWidth() - parseInt(settings.startSize.x,10))/2, top: (pageHeight() - parseInt(settings.startSize.y,10))/2, width: settings.startSize.x, height: settings.startSize.y});
				
				settings.rectangle = {width: pageWidth() - settings.padding.x, height: pageHeight() - settings.padding.y};
				if(settings.endSize != null)
				{
					settings.rectangle.width = Math.min(settings.rectangle.width, settings.endSize.width);
					settings.rectangle.height = Math.min(settings.rectangle.height, settings.endSize.height);
				}
				settings.rectangle.x = (pageWidth() - settings.rectangle.width)/2;
				settings.rectangle.y = (pageHeight() - settings.rectangle.height)/2;
				
				$('#popup-loading').css({  top: pageHeight()/2, left: pageWidth()/2 }).fadeIn(settings.resizeTime);
				
				$('#popup').animate({left: settings.rectangle.x, top: settings.rectangle.y, width: settings.rectangle.width, height: settings.rectangle.height }, settings.resizeTime, function(){ $.fn.popup('animationEnded'); });
				$('#popup-container').animate({ top: settings.rectangle.y + settings.closeButtonHeight, height: settings.rectangle.height - settings.closeButtonHeight }, settings.resizeTime, function(){});
				return $(this);
			},
			
			animationEnded: function()
			{
				settings.ready = true;
				if(settings.content != null)
				{
					$('#popup-loading').fadeOut(settings.loaderFadeout, function(){ $.fn.popup('showContent') });
				}
				return $(this);
			},
			
			contentLoaded: function(response)
			{		
				settings.content = response;
				if(settings.ready)
				{
					$('#popup-loading').fadeOut(settings.loaderFadeout, function(){ $.fn.popup('showContent') });
				}
				return $(this);
			},
			
			showContent: function()
			{			
				$('#popup-loading').remove();
				$('#popup-content').append(settings.content);
				settings.content = null;
				return $(this);
			},
			
			close: function()
			{
				$("#popup").remove();
				$("#popup-overlay").remove();
				$.fn.popup('destroy');
				$('embed, object, select').css({ 'visibility' : 'block' }); //restore elements that don't layer properly in IE
				return $(this);
			},
			update: function()
			{
				settings.rectangle = {width: pageWidth() - settings.padding.x, height: pageHeight() - settings.padding.y};
				if(settings.endSize != null)
				{
					settings.rectangle.width = Math.min(settings.rectangle.width, settings.endSize.width);
					settings.rectangle.height = Math.min(settings.rectangle.height, settings.endSize.height);
				}
				settings.rectangle.x = (pageWidth() - settings.rectangle.width)/2;
				settings.rectangle.y = (pageHeight() - settings.rectangle.height)/2;
				
				$('#popup').css({left: settings.rectangle.x, top: settings.rectangle.y, width: settings.rectangle.width, height: settings.rectangle.height });
				$('#popup-container').height( $('#popup').height() - settings.closeButtonHeight);
				if($('#popup-loading').length)
					$('#popup-loading').css({  top: pageHeight()/2, left: pageWidth()/2 });
				return $(this);
			},
			
            destroy: function( )
            {
               	$(window).unbind('.popup');
				$('#popup-close-button').unbind('.popup');
				return $(this);
            }
    }
    
    //declare the plugin and allow method calling
     $.fn.popup = function( method )
     {
            // Method calling logic
            if ( methods[method] ) {
              return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
            } else if ( typeof method === 'object' || ! method ) {
              return methods.start.apply( this, arguments );
            } else {
              $.error( 'Method ' +  method + ' does not exist on jQuery.popup' );
            }
     };
    
})( jQuery );
