var myDynamicPopup;

var DynamicPopup = Class.create();

DynamicPopup.prototype = {
	extendLinks:function(){	
		var thisObj = this;
		$$('a[rel=popupStarter]' ).each( function( anchor ){
			anchor.observe( 'click', function( e ){ Event.stop( e ); thisObj.start( e.element() ); } );
		});
	},
	
	// initialize()
	// Constructor runs on completion of the DOM loading. Loops through anchor tags looking for 
	// 'lightbox' references and applies onclick events to appropriate links. The 2nd section of
	// the function inserts html at the bottom of the page which is used to display the shadow 
	// overlay and the image container.
	//
	initialize: function(){
		this.extendLinks();
		// The rest of this code inserts html at the bottom of the page that looks similar to this:
		//
		//	<div id="overlay"></div>
		//	<div id="dynamicPopup"></div>
		var objBody = $$( 'body' )[0];
		
		var objOverlay = new Element( 'div', { id:'popupOverlay', style:'display:none' } );
		objOverlay.observe( 'click', this.end.bindAsEventListener( this ) );  
		objBody.insert( objOverlay );
		
		var objPopup = new Element( 'div', { id:'dynamicPopup' } );
		objPopup.setStyle( { display:'none' } ).observe( 'click', function( e ){ 
			if( e.element().id == 'dynamicPopup' ) myDynamicPopup.end();
		});  
	
		objPopup.insert( new Element( 'div', { id:'popupContainer' } ) );
		
		var objTopNavCloseLink = new Element( 'a', { id:'popupNavClose', href:'/' } ).update( 'X' );
		objTopNavCloseLink.observe( 'click', function( e ){ Event.stop( e ); myDynamicPopup.end(); } );
		objPopup.insert( objTopNavCloseLink );
		
		objBody.insert( objPopup );
	},
	
	//
	//	start()
	//	Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
	//
	start: function( triggerLink ) {	

        $$( 'select', 'object', 'embed', '#mapp' ).each(function(node){ node.style.visibility = 'hidden' });

		// stretch overlay to fill page and fade in
        var arrayPageSize = this.getPageSize();
        $('popupOverlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
		new Effect.Appear( 'popupOverlay', { duration: 0.1, from: 0.0, to: 0.2 });

		// calculate top offset for the lightbox and display 
		var dynamicPopupTop;
		var dynamicPopupLeft;
		var position = triggerLink.readAttribute( 'showAt' );
		if( !position ){
			var scroll = document.viewport.getScrollOffsets()[ 1 ];
			dynamicPopupTop = Math.round( scroll + 210 );
			dynamicPopupLeft = Math.round( ( arrayPageSize[0] - 200 ) / 2 );
		}else{
			var pos = triggerLink.cumulativeOffset();
			dynamicPopupLeft = pos.left;
			dynamicPopupTop = pos.top;
		}
		$( 'dynamicPopup' ).setStyle( { left:dynamicPopupLeft + 'px', top:dynamicPopupTop + 'px' } );
		$( 'popupNavClose' ).hide();
		$( 'dynamicPopup' ).show();
	},

	showContent: function( html ){
		$( 'popupContainer' ).update( html );
		var content = $( 'popupContainer' ).firstDescendant();
		var contW = content.getWidth()
		var arrayPageSize = this.getPageSize();
		var dynamicPopupLeft = new Number( $( 'dynamicPopup' ).style.left.replace( /px/, '' ) );
		if( dynamicPopupLeft + contW > arrayPageSize[ 2 ] - 20 ){
			dynamicPopupLeft = arrayPageSize[ 2 ] - contW - 20;
			$( 'dynamicPopup' ).style.left = dynamicPopupLeft + 'px';
		}
		$( 'popupNavClose' ).style.left = ( contW - 20 ) + 'px';
		$( 'popupNavClose' ).show();
	},

	end: function() {
		$( 'popupContainer' ).update();
		$( 'dynamicPopup' ).hide();
		$( 'popupOverlay' ).hide();
        $$( 'select', 'object', 'embed', '#mapp' ).each(function(node){ node.style.visibility = 'visible' });
	},
	
    getPageSize: function() {
        
	     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;
		}

		return [pageWidth,pageHeight];
	}

}

document.observe( 'dom:loaded', function(){ myDynamicPopup = new DynamicPopup() } );