/*
 * BMUI.InlinePopup library
 * Inline popup library
 * @author Dan Neame <dneame@britishmuseum.co.uk>
 */

// define namespace and expected includes
if( typeof( BMUI ) == 'undefined' ) BMUI = {};
if( typeof( BMUI.Page ) == 'undefined' ) BMUI.Page = {};
if( typeof( BMUI.InlinePopup ) != 'undefined' ) BMUI.InlinePopup = {};

var InlinePopup = function(){

	// set local vars
	this.original_offset = new Array( 2 );
	this.width = 0;
	this.height = 0;
	this.slideopen = false;
	this.scrollbars = true;
	this.scrollto = true;
	this.closebutton = true;
	this.closeable = true;
	this.panelY = null;
	this.frameurls = new Array(); /* for obfusication of frame URLs - if required */

	/*
	 * BMUI.InlinePopup.create_popup( args )
	 * Creates the correct popup depending on vars passed.
	 * Chances are all you need to use is this method and show() in controller code
	 */
	this.create_popup = function( args ){
		w = ( args[ 'width' ] == null ) ? 400 : args[ 'width' ];
		h = ( args[ 'height' ] == null ) ? 250 : args[ 'height' ];
		slideopen = ( args[ 'slideopen' ] == null ) ? false : args[ 'slideopen' ];
		scrollbars = ( args[ 'scrollbars' ] == null ) ? true : args[ 'scrollbars' ];
		this.scrollto = ( args[ 'scrollto' ] == null ) ? true : args[ 'scrollto' ];
		this.panelY = ( args[ 'panelY' ] == null ) ? null : args[ 'panelY' ];
		this.closeable = ( args[ 'closeable' ] == null ) ? true : args[ 'closeable' ];
		this.closebutton = ( args[ 'closebutton' ] == null ) ? true : args[ 'closebutton' ];
		this.persistent_iframe = ( args[ 'persistent_iframe' ] == null ) ? false : args[ 'persistent_iframe' ];
		this.default_loading_url = ( args[ 'default_loading_url' ] == null ) ? BMUI.Page.webroot_absolute + '/page/loading' : args[ 'default_loading_url' ];
		this.width = w;
		this.height = h;
		this.slideopen = slideopen;
		this.scrollbars = scrollbars;
		if( args[ 'url' ] == null && this.persistent_iframe == false ){
			this.create_popup_container( w , h , slideopen );
		}else{
			this.create_iframe_container( w , h , slideopen , scrollbars );
			if( args[ 'url' ] != null ) this.set_iframe_url( args[ 'url' ] );
			else this.set_iframe_url( this.default_loading_url );
		}
		if( args[ 'show' ] == true ){
			this.show();
		}
	};

	/*
	 * BMUI.InlinePopup.create_popup_container ( width , height )
	 * Creates the DOM elements required for an inline popup
	 */
	this.create_popup_container = function( w , h , slideopen ){
		this.slideopen = ( slideopen == null ) ? false : slideopen; // default false, save in local var
		// calculate popup panel position
		if( this.panelY == null ){
			var panelY = Math.floor( ( $( document ).height() / 2 ) - ( h / 2 ) );
		}else{
			var panelY = this.panelY;
		}
		var panelX = Math.floor( ( $( document ).width()  / 2 ) - ( w / 2 ) );
		// delete if exists
		$( '#BMUI_popup-container' ).remove();
		// add DOM objects
		if( this.closebutton )
			$( 'body' ).append( '<div id="BMUI_popup-container"><div id="BMUI_popup-background"></div><div id="BMUI_popup-panel"></div><a title="Close" href="#" id="BMUI_popup-close">Close</a></div>' );
		else
			$( 'body' ).append( '<div id="BMUI_popup-container"><div id="BMUI_popup-background"></div><div id="BMUI_popup-panel"></div></div>' );
		// set container, background, panel css
		$( '#BMUI_popup-container, #BMUI_popup-background'  ).css( { position: 'absolute' , top: 0 , left: 0 , padding: 0 , width: $( document ).width() , height: $( document ).height() , zIndex: 98 } );
		$( '#BMUI_popup-container' ).css( {  display: 'none' } );
		$( '#BMUI_popup-background' ).css( { background: '#000' , opacity: 0.6 } );
		$( '#BMUI_popup-panel' ).css( { position: 'absolute' , top: panelY , left: panelX , width: w , height: h , padding: '10px' , background: '#fff' , overflowX: 'hidden' , overflowY: 'auto' , zIndex: 99 } );
		// set close button css
		$( '#BMUI_popup-close' ).css( { position: 'absolute' , top : panelY + 1 , left: ( panelX + w + 3 ) , display: 'block' , width: 16 , height: 16 , textIndent: '-5000px' , cursor: 'pointer' , background: 'url(' + BMUI.Page.webroot_absolute + '/content/ebiz/britishmuseumonlineshop/resources/img/close-cross.gif) top left no-repeat'  , zIndex: 100 } );
		// if rounded, add DOM elements for corners ( top row, popup panel, bottom row )
		// event handlers (close)
		if( this.closeable ){
			$( '#BMUI_popup-background , #BMUI_popup-close' ).mousedown( function(){
				this.blur();
				BMUI.InlinePopup.hide();
				return false; // dont bubble
			} );
		}
		if( this.scrollbars == false )
			$( '#BMUI_popup-panel' ).css( { overflowY: 'hidden' } );
	}; // end create_popup_container

	/*
	 * BMUI.InlinePopup.create_iframe_container ( width , height )
	 * Creates the DOM elements required for an inline popup capable of displaying iframed content
	 */
	this.create_iframe_container = function( w , h , slideopen , scrollbars ){
		slideopen = ( slideopen == null ) ? false : slideopen; // set false if not specified
		// create container
		BMUI.InlinePopup.create_popup_container( w , h , slideopen );
		// set panel HTML to iframe
		$( '#BMUI_popup-panel' ).html( '<iframe name="BMUI_InlinePopup" src="" id="BMUI_popup-iframe" frameborder="0"></iframe>' ).find( 'iframe' ).css( { width: w , height: h , overflowX: 'hidden' , overflowY: 'auto' } );
		if( scrollbars == false ) $( '#BMUI_popup-panel iframe' ).css( { overflowY: 'hidden' } ).attr( 'scrolling' , 'no' );
	}; // end create_iframe_container

	/*
	 * BMUI.InlinePopup.show
	 * Stores current scroll position (see BMUI.InlinePopup.hide) and displays the inline popup
	 */
	this.show = function(){
		// save original offset for close method
		BMUI.InlinePopup.original_offset = BMUI.Page.getScroll();
		// need to scroll window so that popup is in the middle: popupY - ( ( window height - popup height ) / 2 )
		var scrollP = Math.floor( parseInt( $( '#BMUI_popup-panel' ).css( 'top' ) ) - ( ( parseInt( $( window ).height() ) - parseInt( $( '#BMUI_popup-panel' ).css( 'height' ) ) ) / 2 ) );
		if( this.scrollto ) $.scrollTo( scrollP , 500 );
		// if instance is set to slide open, do so
		if( this.slideopen ){
			this.panel_original_w = $( '#BMUI_popup-panel' ).css( 'width' );
			this.panel_original_x = $( '#BMUI_popup-panel' ).css( 'left' );
			$( '#BMUI_popup-iframe , #BMUI_popup-close' ).css( { display: 'none' , opacity: 0 } );
			$( '#BMUI_popup-panel' ).css( { left: Math.floor( $( document ).width()  / 2 ) , width: 1 , display: 'none' } );
			$( '#BMUI_popup-container' ).fadeIn( 'slow' , function(){
				// background is there, slide the panel open
				$( '#BMUI_popup-panel' ).css( { display: 'block' } );
				$( '#BMUI_popup-panel' ).animate( { left: BMUI.InlinePopup.panel_original_x , width: BMUI.InlinePopup.panel_original_w } , 'slow' , function(){
					// panel is there, show iframe + close button
					$( '#BMUI_popup-iframe' ).css( { display: 'block' , opacity: 1 } );
					$( '#BMUI_popup-close' ).css( { display: 'block' } ).animate( { opacity: 1 } , 'fast' );
				});
			});
		}else{
			$( '#BMUI_popup-container' ).fadeIn( 'fast' ); // display the overlay, panel, iframe			
		}
	}; // end show

	/*
	 * BMUI.InlinePopup.hide
	 * hides the inline popup, restores scroll position
	 */
	this.hide = function(){
		if( this.persistent_iframe ){
			// if persistent iframe, destroy and rebuild after fade out
			$( '#BMUI_popup-container' ).fadeOut( 'fast' );//, function(){
			$( '#BMUI_popup-container' ).remove();
			w = this.width;
			h = this.height;
			scrollbars = this.scrollbars;
			slideopen = this.slideopen;
			this.create_iframe_container( w , h , slideopen , scrollbars );
			this.set_iframe_url( this.default_loading_url );
			/* VENDA FIX - allow people to submit forms again */
			window.stopclick = 0;
			//} );
		}else{
			// otherwise it's safe just to fade out
			$( '#BMUI_popup-container' ).fadeOut( 'fast' );
		}
		// scroll to original offset
		if( this.scrollto ) $.scrollTo( BMUI.InlinePopup.original_offset[ 1 ] , 500 );
	}; // end hide
	
	/*
	 * BMUI.InlinePopup.resize_to ( w , h )
	 * Sets the popup width and height
	 */
	this.resize_to = function( w , h ){

		$( '#BMUI_popup-panel' ).add( '#BMUI_popup-panel iframe' ).css( { width: w , height: h } );

	};
	
	/*
	 * BMUI.InlinePopup.move_to( Array args )
	 * expects array containing at least one of 'top' and / or 'left' as an integer
	 */
	this.move_to = function( args ){
		if( args[ 'top' ] != null ){
			$( '#BMUI_popup-panel' ).css( { top: args['top'] } );
			$( '#BMUI_popup-close' ).css( { top: ( args['top'] + 1 ) } );
		}
		if( args[ 'left' ] != null ){
			$( '#BMUI_popup-panel' ).css( { top: args['left'] } );
			$( '#BMUI_popup-close' ).css( { top: ( args['left'] + this.width + 3 ) } );
		}
	};

	/*
	 * BMUI.InlinePopup.set_iframe_url ( url )
	 * Sets the iframe URL
	 */
	this.set_iframe_url = function( url ){ $( '#BMUI_popup-iframe' ).attr( 'src' , url ); };

	/*
	 * BMUI.InlinePopup.resizePopupTo ( width , height )
	 * Resizes the popup to specified w and h
	 */
	this.resizePopupTo = function( w , h , callback ){
		var cb = ( callback == null ) ? null : callback;
		// set iframe w and h immediately
		$( '#BMUI_popup-panel iframe' ).css( { width: w , height: h } );
		// calculate new X and Y based on w and h
		var targetY = Math.floor( ( $( document ).height() / 2 ) - ( h / 2 ) );
		var targetX = Math.floor( ( $( document ).width()  / 2 ) - ( w / 2 ) );
		// animate
		if( cb != null )
			$( '#BMUI_popup-panel' ).animate( { left: targetX , width: w , top: targetY , height: h } , 'slow' , cb );
		else
			$( '#BMUI_popup-panel' ).animate( { left: targetX , width: w , top: targetY , height: h } , 'slow' );
	};
	
	/*
	 * BMUI.InlinePopup.setCloseable ( closeable )
	 * Adds or removes the close button and logic
	 */
	this.setCloseable = function( closeable ){
		if( closeable ){
			// add close button, style and apply event handlers
			$( '#BMUI_popup-container' ).append( '<a title="Close" href="#" id="BMUI_popup-close">Close</a>' ).find( '#BMUI_popup-close' ).css( { position: 'absolute' , top : parseInt( $( '#BMUI_popup-panel' ).css( 'top' ) ) + 1 , left: ( parseInt( $( '#BMUI_popup-panel' ).css( 'left' ) ) + parseInt( $( '#BMUI_popup-panel' ).css( 'width' ) ) + 3 ) , display: 'block' , width: 16 , height: 16 , textIndent: '-5000px' , cursor: 'pointer' , background: 'url(' + BMUI.Page.webroot_absolute + '/content/ebiz/britishmuseumonlineshop/resources/img/close-cross.gif) top left no-repeat'  , zIndex: 100 }  );
			$( '#BMUI_popup-background , #BMUI_popup-close' ).mousedown( function(){
				this.blur();
				BMUI.InlinePopup.hide();
				return false; // dont bubble
			} );
		}else{
			// remove close button and event handlers
			$( '#BMUI_popup-close' ).remove();
			$( '#BMUI_popup-background , #BMUI_popup-close' ).unbind( 'mousedown' );
		}
	}; // end setCloseable

};
BMUI.InlinePopup = new InlinePopup();
