/**
 * jQuery popupWindow plugin
 * @name jquery-popupWindow-0.5.js
 * @author Leandro Vieira Pinho - http://leandrovieira.com
 * @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.4		// (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;'+(this.settings.letter?'background-image: url(\'/images/letters/'+this.settings.letter+'.jpg\')':'')+'"><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 = ___getPageScroll();

            // 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:                0 /*arrPageScroll[1]*/
			}).fadeIn();

			// Calculate top and left offset
			$('#popup_window').css({
				left:	arrPageScroll[0] + (arrPageSizes[2]-$("#popup_window").attr("clientWidth"))/2,
				top:	arrPageScroll[1] + (arrPageSizes[3]-$("#popup_window").attr("clientHeight"))/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 = ___getPageScroll();
				// 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() {
	
			pageWidth = $(document).width();
			//pageHeight = $(document).height();
			pageHeight = $("body").attr("clientHeight");
			windowWidth = $(window).width();
			windowHeight = $(window).height();
			
			if (pageHeight<windowHeight) pageHeight=windowHeight;
			
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		/**
		 / THIRD FUNCTION
		 * getPageScroll() by quirksmode.com
		 *
		 * @return Array Return an array with x,y page scroll values.
		 */
		function ___getPageScroll() {
			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;
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			/*console.log($("body").attr("offsetHeight"));
			console.log(yScroll);			*/
			return arrayPageScroll;
		};

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