/**
 * Google Maps Modal
 * This jQuery plugin was inspired on jQuery.lightBox
 *
 * @name jquery.google-maps-modal.js
 * @author Igor Frias Vieira - http://igorvieira.com
 * @version 0.1
 * @date Feb 10, 2009
 * @copyright (c) 2008 Igor Frias Vieira
 * @license CC Attribution-No Derivative Works 2.5 Brazil - http://creativecommons.org/licenses/by-nd/2.5/br/deed.en_US
 * @example Visit http://igorvieira.com/blog/google-maps-modal/
 */

(function(jQuery)
{
	
	jQuery.fn.teste = function(settings) {
		
		// Settings to configure the jQuery lightBox plugin how you like
		settings = jQuery.extend({
			
			//The overlay background
			bgColor: '#000', 		// Background color
			bgOpacity: 0.8,			// Background opacity
			
			//The map size
			mapWidth: 600,		// The map Width
			mapHeight: 350,		// The map Height
			
			//About the Map
			mapText: '',			// A little text
			mapUrl: ''				// You can find it on Google Maps iframe <iframe src="HERE"></iframe>
			
			/*
			 * ============================================================== You Should look for this src URL  \/ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- \/
			 * <iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"	src="http://www.google.com/maps?f=q&amp;source=s_q&amp;hl=pt-BR&amp;geocode=&amp;q=Parque+ibirapuera&amp;sll=-23.623704,-46.561111&amp;sspn=0.092006,0.181961&amp;ie=UTF8&amp;s=AARTsJoZOnljttHb6BAp9fGn-ngks2fbFg&amp;ll=-23.588138,-46.657734&amp;spn=0.027531,0.036478&amp;z=14&amp;output=embed"></iframe>
			 */
			
		},settings);
	
	
		/*
		 * Initializing the plugin
		 * @return boolean false
		 */
		function _initialize(){			
			
			//Create the modal Interface
			_createInterface();
			
			return false;
		}
		
		
		
		/*
		 * Creating the Modal Interface
		 * @return boolean false
		 */
		function _createInterface(){			
			
			jQuery('body').append("<div id='gMapModal-overlay'></div><div id='gMapModal-text'></div><div id='gMapModal-box'></div>");
//			jQuery('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-image-box"><div id="lightbox-container-image"><img id="lightbox-image"><div style="" id="lightbox-nav"><a href="#" id="lightbox-nav-btnPrev"></a><a href="#" id="lightbox-nav-btnNext"></a></div><div id="lightbox-loading"><a href="#" id="lightbox-loading-link"><img src="' + settings.imageLoading + '"></a></div></div></div><div id="lightbox-container-image-data-box"><div id="lightbox-container-image-data"><div id="lightbox-image-details"><span id="lightbox-image-details-caption"></span><span id="lightbox-image-details-currentNumber"></span></div><div id="lightbox-secNav"><a href="#" id="lightbox-secNav-btnClose"><img src="' + settings.imageBtnClose + '"></a></div></div></div></div>');
			
			jQuery('#gMapModal-box').append('<iframe width="' + settings.mapWidth + '" height="' + settings.mapHeight + '" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + settings.mapUrl + '"></iframe>');
			jQuery('#gMapModal-text').append('<p>' + settings.mapText + '</p><a id="gMapModal-closeButton"></a>');
			
			objHidden = jQuery('embed, object, select').css({ 'visibility' : 'hidden' });
			
			
			//Hidding the elements
			jQuery('#gMapModal-overlay, #gMapModal-box, #gMapModal-text').hide();
			
			var arrPageSizes = gMapModal_getPageSize();
			var arrPageScroll = gMapModal_getPageScroll();
			
			// The Background CSS
			jQuery('#gMapModal-overlay').css({
				'position' 	: 'absolute',
				'top' 		: 0,
				'left' 		: 0,
				'width' 	: arrPageSizes[0],
				'height' 	: arrPageSizes[1],
				
				'background-color' : settings.bgColor,
				'opacity' 			: settings.bgOpacity
			});
			
			
			
			mgLeft = ((settings.mapWidth/2) * -1);
			mgTop = jQuery(window).scrollTop() + 50;
			
						
			// The Map Box CSS
			jQuery('#gMapModal-box').css({
				'position'		: 'absolute',
				'left'			: '50%',
				'width'			: settings.mapWidth + "px",
				'height'		: settings.mapHeight + "px",
				'margin-left'	: mgLeft + "px",
				'top'			: arrPageScroll[1] + (arrPageSizes[3] / 10)
			});
			
			
			mgTop = mgTop + settings.mapHeight;
			
			// The text CSS
			jQuery('#gMapModal-text').css({
				'position'		: 'absolute',
				'left'			: '50%',
				'width'			: settings.mapWidth + "px",
				'margin-left'	: mgLeft + "px",
				'top'			:  arrPageScroll[1] + (arrPageSizes[3] / 10) + settings.mapHeight
			});
			
			
			//Calling the setActions Function
			_setActions();
			
			
			jQuery('#gMapModal-overlay, #gMapModal-box').show();
			setTimeout(function(){
				jQuery('#gMapModal-text').slideDown();
			}, 500);
			
			return false;
		}
		
		
		function gMapModal_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;
		};
		
		
		function gMapModal_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);
			return arrayPageScroll;
		};		
		
		
		/*
		 * Set the buttons actions
		 * @return boolean false
		 */
		function _setActions(){
			
			jQuery('#gMapModal-overlay').bind('click', closeModal);
			jQuery('#gMapModal-closeButton').bind('click', closeModal);
			
			return false;
		}
		
		
		
		
		/*
		 * Closing the Modal
		 * @return boolean false
		 */
		closeModal = function(){
			jQuery('#gMapModal-overlay').remove();
			jQuery('#gMapModal-box').remove();
			jQuery('#gMapModal-text').remove();
			
			
			jQuery(objHidden).css({ 'visibility' : 'visible' });
			
			return false;
		}
		
		var objHidden = [];
		return this.unbind('click').click(_initialize);
	}
		
})(jQuery);