/**
 * jQuery popupWindow plugin
 * @name jquery-popupWindow-0.5.js
 * @version 0.5
 * @date August 05, 2010
 * @category jQuery plugin
 */

// Offering a Custom Alias suport - More info: http://docs.jquery.com/Plugins/Authoring#Custom_Alias
(function($) {
	/**
	 * $ is an alias to jQuery object
	 *
	 */
    $.popupWindow = {
        html: '',
        settings: {},

        open: function (settings) {
            this.settings = jQuery.extend({
                width:          510,
                height:         240,
                imgLetter:      '',
                innerHtml:      '',
                // Configuration related to overlay
                overlayBgColor: '#000',		// (string) Background color to overlay; inform a hexadecimal value like: #RRGGBB. Where RR, GG, and BB are the hexadecimal values for the red, green, and blue values of the color.
                overlayOpacity: 0.75		// (integer) Opacity value to overlay; inform: 0.X. Where X are number from 0 to 9
            },settings);

            this._showWindow();
            this._setKeyboardAction();
        },

        _showWindow: function() {
			// Apply the HTML markup into body tag
			$('body').append('<div id="popup_window_overlay"></div><div id="popup_window" style="width:'+this.settings.width+'px;height:'+this.settings.height+'px;"><div id="popup_window_content">'+this.settings.innerHtml+'</div><a id="popup_window_close" href="#"></a></div>');

			// Get page sizes
			var arrPageSizes = ___getPageSize();
            // Get page scroll

			var arrPageScroll = [$(window).scrollLeft(), $(window).scrollTop()];

            // Style overlay and show it
			$('#popup_window_overlay').css({
				backgroundColor:	this.settings.overlayBgColor,
				opacity:			this.settings.overlayOpacity,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1],
				left:               arrPageScroll[0],
				top:                arrPageScroll[1]
			}).fadeIn();
			
			// Calculate top and left offset
			$('#popup_window').css({
				left:	arrPageScroll[0] + (arrPageSizes[2]-this.settings.width)/2,
				top:	arrPageScroll[1] + (arrPageSizes[3]-this.settings.height)/2
			}).show();
            
			// Assigning click events in elements to close overlay
			$('#popup_window_overlay').click(function() {
				$.popupWindow.close();
                return false;
			});
			// Assign the close() function to lightbox-loading-link and lightbox-secNav-btnClose objects
			$('#popup_window_close').click(function() {
				$.popupWindow.close();
				return false;
			});
			// If window was resized, calculate the new overlay dimensions
            var settings = this.settings;
			$(window).resize(function() {
				// Get page sizes and scroll
				var arrPageSizes = ___getPageSize();
				var arrPageScroll = [$(window).scrollLeft(), $(window).scrollTop()];

                // Style overlay and show it
				$('#popup_window_overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1],
                    left:       arrPageScroll[0],
                    top:        arrPageScroll[1]
				});

                // Calculate top and left offset
				$('#popup_window').css({
                    left:	arrPageScroll[0] + (arrPageSizes[2]-settings.width)/2,
                    top:	arrPageScroll[1] + (arrPageSizes[3]-settings.height)/2
				});
			});
        },

        _setKeyboardAction: function () {
			$(document).keydown(function(objEvent) {
                var keycode, escapeKey;
                // To ie
                if ( objEvent == null ) {
                    keycode = event.keyCode;
                    escapeKey = 27;
                // To Mozilla
                } else {
                    keycode = objEvent.keyCode;
                    escapeKey = objEvent.DOM_VK_ESCAPE;
                }
                // ???
                if (null==escapeKey) {
                    escapeKey = 27;
                }
                // Get the key in lower case form
                var key = String.fromCharCode(keycode).toLowerCase();
                // Verify the keys to close the ligthBox
                if (  keycode == escapeKey ) {
                    $.popupWindow.close();
                }
			});
        },

        close: function () {
            $('#popup_window').remove();
            $('#popup_window_overlay').fadeOut(function() { $('#popup_window_overlay').remove(); });
            // Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
            //$('embed, object, select').css({ 'visibility' : 'visible' });
        }
    }

		/**
		 / THIRD FUNCTION
		 * getPageSize() by quirksmode.com
		 *
		 * @return Array Return an array with page width, height and window width, height
		 */
		function ___getPageSize() {
			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;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};

})(jQuery); // Call and execute the function immediately passing the jQuery object

