/*
 * jQuery Popup Plugin (sfe edition) - Popupes for jQuery
 * Copyright (C) 2007 Glenn Richmond
 *
 * This file is part of jQuery Popup (sfe edition).
 * 
 * @name jquery_popup: jquery.popup.js
 * @package jQuery Popup Plugin (sfe edition)
 * @version 1.0.0
 * @date September 10, 2008
 * @category jQuery plugin
 * @author Glenn Richmond {@link http://www.spottedfrog.com.au}
 * @copyright (c) 2008 Glenn Richmond {@link http://www.spottedfrog.com.au}
 */

// Start of our jQuery Plugin
(function($)
{	// Create our Plugin function, with $ as the argument (we pass the jQuery object over later)
	// More info: http://docs.jquery.com/Plugins/Authoring#Custom_Alias
	
	// Declare our class
	$.PopupClass = function ( )
	{	
		// This is the handler for our constructor
		this.construct();
	};

	// Extend jQuery elements for Popup
	$.fn.popup = function ( options )
	{	
		// Instantiate
		$.Popup = $.Popup || new $.PopupClass();
		// On the click handler, show the popup
		$(this).click(function() {
			// Set the URL
			var strUrl = $(this).attr('href');
			// Set the title
			var strTitle = $(this).attr('title');
			// No Content - load it via ajax
			$("#popupContent").load(strUrl, {}, function() {
				// Fix attention seekers
				$('embed, object, select').css({ 'visibility' : 'hidden' });
				// Don't hide the items in the popup
				$('#global_status embed, object, select').css({ 'visibility' : 'visible' });
				// Resize the boxes appropriatly
				$.Popup.resizeBoxes();			
				// Display the boxes
				$('#popup-overlay').css({
					opacity:	this.opacity
				}).show();
				// Show the popup
				$('#global_status').show();		
				// Set the title
				$('#popup-title').html(strTitle);
				// Assign close clicks
				$('#btCancel').click(function() {
					// Get rid of popup
					$('#global_status').hide();
					$('#popup-overlay').hide();			
					// Fix attention seekers
					$('embed, object, select').css({ 'visibility' : 'visible' });//.show();
					$("#popupContent").html('');
					// Nothing else to do
					return false;
				});
			});
			// Don't follow the link
			return false;
		});
		// And chain
		return this;
	};
	
	// Define our class
	$.extend($.PopupClass.prototype,
	{	
		// Our PopupClass definition
		
		// Class variables
		baseurl:			'',
		url:				'',
		title:				'',
		opacity:	0.7,	// Set the opacity of the overlay
				
		/*
		*	Class constructor
		*/		
		construct: function( ) {	
			// All good
			return true;
		},
				
		resizeBoxes: function ( )
		{
			// Get the page size
			var pageSize = this.getPageSize();
			
			// Style overlay and show it
			$('#popup-overlay').css({
				width:		pageSize.largestWidth,
				height:		pageSize.largestHeight + 20
			});
		},
		
		repositionBoxes: function ( options )
		{
			// Options
			options = $.extend({}, options);
			
			// Get the page size
			var pageSize = this.getPageSize();
			
			// Get page scroll
			var pageScroll = this.getPageScroll();
			
			// Figure it out
			var nHeight = options.nHeight || parseInt($('#global_status').height(),10) || pageSize.largestHeight/3;
			// Display popup in center
			//var nTop = pageScroll.yScroll + (pageSize.windowHeight - nHeight) / 2.5;
                        var nTop = (pageSize.windowHeight - nHeight) / 2.5;
                        if (nTop < 0 ) nTop = 0;

			//var nLeft = pageScroll.xScroll;
                        var nLeft = (pageSize.windowWidth - 800) / 2;
			
			$('#global_status').animate({left:nLeft, top:nTop}, 'fast');
		},
								
		getPageSize: function ( )
		{
			var xScroll, yScroll;
	
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
	
			var windowWidth, windowHeight;
	
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
	
	
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			
			//
			var largestWidth;
			var largestHeight;
			var smallestWidth;
			var smallestHeight;
			//
			if ( pageWidth >= windowWidth )
			{	largestWidth = pageWidth; smallestWidth = windowWidth;	}
			else
			{	largestWidth = windowWidth; smallestWidth = pageWidth;	}
			//
			if ( pageHeight >= windowHeight )
			{	largestHeight = pageHeight; smallestHeight = windowHeight;	}
			else
			{	largestHeight = windowHeight; smallestHeight = pageHeight;	}
						
			// Return the calculated page sizes
			var arrayPageSize = {'pageWidth':pageWidth,'pageHeight':pageHeight,'windowWidth':windowWidth,'windowHeight':windowHeight,'largestWidth':largestWidth,'largestHeight':largestHeight};
			return arrayPageSize;
		},
		
		getPageScroll: function ( ) {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			var arrayPageScroll = {'xScroll':xScroll,'yScroll':yScroll};
			return arrayPageScroll;
		}
			
	}); // We have finished extending/defining our PopupClass


	// --------------------------------------------------
	// Finish up
	
	// On document load, set up required content and event handlers
	$(function() {
		// Instantiate
		$.Popup = $.Popup || new $.PopupClass();
		// Append markup
		$('body').append('<div id="popup-overlay"></div><div id="global_status"><div id="popupheaderWrap"><p id="close"><a href="#" id="btCancel">Schlie&szlig;en</a></p><div class="popContentContainer"><p id="action" class="quick_add"><a href="#" onclick="return false;" id="action_title"></a></p><div id="progressBar">&nbsp;</div><div class="clearAll"></div></div></div><div id="popupContent" class="popContentContainer"></div></div>');
               
		// Update Boxes - for some crazy reason this has to be before the hide in safari and konqueror
		$.Popup.resizeBoxes();
		$.Popup.repositionBoxes();
		// Hide the popup content
		$('#global_status,#popup-overlay').hide();			
		// If the window resizes, resize the overlay windows
		$(window).resize(function () { 
			// Window has resized - reposition everything
			$.Popup.resizeBoxes();
                        $.Popup.repositionBoxes();
		});
                 $(window).scroll(function(){
                  
                    $.Popup.repositionBoxes();
                });
               
	});

// Finished definition

})(jQuery); // We are done with our plugin, so lets call it with jQuery as the argument

