var Popup = {
    
	current: null,
   
    open: function ( id ) {
        this.adjustOverlaySize();
		this.adjustPopupPosition( id );
		this.display( id );
		this.current = id;
    },

    close: function ( id ) {
    	if (!id && this.current != null)
			id = this.current;
		if ( id )
			this.hide( id );
    },

	adjustPopupPosition: function( id ) {
		var scrollOffsets 	= this.viewport.getScrollOffsets();
		var object 			= document.getElementById(id);
		var posTop			= scrollOffsets.top + (this.viewport.getHeight() / 10);
		var posLeft			= (this.viewport.getWidth() - parseInt(object.style.width))/2;
		object.style.top 	= posTop+'px';
		object.style.left 	= posLeft+'px';
	},
	
	adjustOverlaySize: function() {
		var pageSizes 		= this.viewport.getPageSize();
		var object 			= document.getElementById('overlay');
		object.style.width 	= pageSizes.width+'px';
		object.style.height = pageSizes.height+'px';
	},
	
	display: function( id ) {
		var overlay 			= document.getElementById('overlay');
		overlay.style.display	= 'block';
		var object 				= document.getElementById(id);
		object.style.display	= 'block';
	},

	hide: function( id ) {
		var overlay 			= document.getElementById('overlay');
		overlay.style.display	= 'none';
		var object 				= document.getElementById(id);
		object.style.display	= 'none';
	},

	viewport: {
		
		getDimensions: function() {
			var dimensions = { };
			dimensions['width'] = (this.browser.WebKit && !document.evaluate) ? self['innerWidth'] :
				(this.browser.Opera) ? document.body['clientWidth'] : document.documentElement['clientWidth'];
			dimensions['height'] = (this.browser.WebKit && !document.evaluate) ? self['innerHeight'] :
				(this.browser.Opera) ? document.body['clientHeight'] : document.documentElement['clientHeight'];
			return dimensions;
		},
		
		getWidth: function() {
			return this.getDimensions().width;
		},
		
		getHeight: function() {
			return this.getDimensions().height;
		},
		
		getScrollOffsets: function() {
			var offsets 	= { };
			offsets.left 	= (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft);
			offsets.top 	= (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop);
			return offsets;
		}, 
		
		getPageSize: function() {
			var pagesize	= {};
			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;
			}
			
			pagesize.width 	= pageWidth;
			pagesize.height = pageHeight;
			
			return pagesize;
		},
	
		browser : {
			IE:     		!!(window.attachEvent && !window.opera),
			Opera:  		!!window.opera,
			WebKit: 		navigator.userAgent.indexOf('AppleWebKit/') > -1,
			Gecko:  		navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
			MobileSafari: 	!!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
		}	
	}
}
