/**
 * 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:          600,
                height:         550,                
                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
                nogallerypopup: 0
                
            },settings);

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

        _showWindow: function() {
			// Apply the HTML markup into body tag
			$('body').append('<div id="popup_window_overlay" class="popup_overlay"></div><div id="popup_window" class="popup_wrapper" style="position: absolute; z-index:10000px; background: #fff; width:'+this.settings.width+'px;height:'+this.settings.height+'px;"><div id="popup_window_content">'+this.settings.innerHtml+'</div></div>');
						
			// Get page sizes
			var arrPageSizes = ___getPageSize();
            // Get page scroll
			var arrPageScroll = ___getPageScroll();

			var settings = this.settings;	
			settings.height = settings.height ? settings.height : (arrPageSizes[3] - 40);
			
            // 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('slow');
			*/
			
			$('#popup_window_overlay').show();
			
			// 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('slow');
			
			$("#popup_window").draggable();

			// 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
                        
			$(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
				});
			});
				
			$('#popup_window').css('height', '');
			
			
			
			if(!settings.nogallerypopup) {
		       	var pic = new Image;
				pic.src = $('.popup_image img').attr('src');									
				$(pic).load(function () {								
				    $('.popup_navy').css('top', pic.height+28);
				});	       	
				$('.popup_navy').css('top', pic.height+28);
			}
        },

        _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();			
			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
