/**
* 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 )
            {
				//specify the default settings
                var settings = {
						url: null,
						content: null,
						padding: {x:75, y: 75},
						closeButtonHeight: 16,
						closeButton: '/images/popup/close.png',
						loadingImage: '/images/popup/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);
                }

				$(this).data('popup', settings);
					
				var $this = $(this),
                data = $(this).data('popup');
				/*if(!data)
				{
					$(this).data('popup', settings);
					data = $(this).data('popup');
				}*/
				
				//Start the initial setup
				$('embed, object, select').css({ 'visibility' : 'hidden' }); //hide elements that don't layer properly in IE

				
				$this.popup('show');
				
				
				if(data.url != null)
				{
					$.ajax({ url: data.url, /*data: getVariables, */	  dataType: "html", cache: false, context: this, success: function(response, status, httpRequest){ $this.popup('contentLoaded', response);} });
					//$.ajax(data.content, { context: $('popup-container'), dataType: 'html'});
				}
				
				$(window).bind('resize.popup', function(){$this.popup('update')});
				$('#popup-close-button').bind('click.popup', methods.close);
                return $(this);
            },
            
			show: function()
			{
				if($(this).length)
				{
					var $this = $(this),
	                data = $(this).data('popup');
					
					data.rectangle = {width: pageWidth() - data.padding.x, height: pageHeight() - data.padding.y};
					if(data.endSize != null)
					{
						data.rectangle.width = Math.min(data.rectangle.width, data.endSize.width);
						data.rectangle.height = Math.min(data.rectangle.height, data.endSize.height);
					}
					data.rectangle.x = (pageWidth() - data.rectangle.width)/2;
					data.rectangle.y = (pageHeight() - data.rectangle.height)/2;
					
					//Add the overlay to the DOM if the opacity is greater than 0.
					if(data.overlayOpacity > 0)
					{ 
						$('body').append('<div id="popup-overlay"></div>');
	
						$('#popup-overlay').css({  backgroundColor: data.overlayColor, opacity: data.overlayOpacity }).fadeIn(data.overlayFade);
					}
					
					//Add the popup
					$('body').append('<div id="popup"><div id="popup-image-container"><img id="popup-close-button" src="'+data.closeButton+'" /></div><div id="popup-container"><div id="popup-content"><img src="' + data.loadingImage + '" id="popup-loading" /></div></div></div>');	
					$('#popup').css({  left: (pageWidth() - parseInt(data.startSize.x,10))/2, top: (pageHeight() - parseInt(data.startSize.y,10))/2, width: data.startSize.x, height: data.startSize.y});
					$('#popup-container').css({ top: data.rectangle.y + data.closeButtonHeight, height: data.rectangle.height - data.closeButtonHeight });
					$('#popup-loading').css({  top: pageHeight()/2, left: pageWidth()/2 }).fadeIn(data.resizeTime);
					
					$('#popup').animate({left: data.rectangle.x, top: data.rectangle.y, width: data.rectangle.width, height: data.rectangle.height }, data.resizeTime, function(){ $this.popup('animationEnded'); });
					$('#popup-container').animate({ top: data.rectangle.y + data.closeButtonHeight, height: data.rectangle.height - data.closeButtonHeight }, data.resizeTime, function(){});
					return $(this);
				}
			},
			
			animationEnded: function()
			{
				if($(this).length)
				{
					var $this = $(this),
	                data = $(this).data('popup');
					data.ready = true;
					if(data.content != null)
					{
						$('#popup-loading').fadeOut(data.loaderFadeout, function(){ $this.popup('showContent') });
					}
					return $(this);
				}
			},
			
			contentLoaded: function(response)
			{
				if($(this).length)
				{
					var $this = $(this),
	                data = $(this).data('popup');
					
					data.content = response;
					
					if(data.ready)
					{
						$('#popup-loading').fadeOut(data.loaderFadeout, function(){ $this.popup('showContent') });
					}
					return $(this);
				}
			},
			
			showContent: function()
			{
				if($(this).length)
				{
					var $this = $(this),
	                data = $(this).data('popup');
					
					$('#popup-loading').remove();
					$('#popup-content').append(data.content);
					data.content = null;
					return $(this);
				}
			},
			
			close: function()
			{
				if($(this).length)
				{
					var $this = $(this),
	                data = $(this).data('popup');
					
					$("#popup").remove();
					$("#popup-overlay").remove();
					$this.popup('destroy');
					$('embed, object, select').css({ 'visibility' : 'block' }); //restore elements that don't layer properly in IE
					return $(this);
				}
			},
			update: function()
			{
				if($(this).length)
				{
					var data = $(this).data('popup');
					data.rectangle = {width: pageWidth() - data.padding.x, height: pageHeight() - data.padding.y};
					if(data.endSize != null)
					{
						data.rectangle.width = Math.min(data.rectangle.width, data.endSize.width);
						data.rectangle.height = Math.min(data.rectangle.height, data.endSize.height);
					}
					data.rectangle.x = (pageWidth() - data.rectangle.width)/2;
					data.rectangle.y = (pageHeight() - data.rectangle.height)/2;
					
					$('#popup').css({left: data.rectangle.x, top: data.rectangle.y, width: data.rectangle.width, height: data.rectangle.height });
					$('#popup-container').height( $('#popup').height() - data.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 );
