/*
 * NM_AbstractLayer
 * Abstract class for all "Layers"
 * NM_Layer.js
 *
 * @author		Benjamin Steiner <benjamin.steiner@dmc.de>
 * @version 	$Id: NM_Layer.js 87060 2011-12-05 11:12:13Z mwolf $
 */
var NM_AbstractLayer = new Class({

	/**
	 * @var integer opacity
	 **/
	opacity: 0.8,

	/**
	 * @var integer width
	 **/
	width: 710,

	/**
	 * @var integer height
	 **/
	height: 410,

	/**
	 * @var element close
	 **/
	closeElement: null,

	/**
	 * @var element headline layer
	 **/
	headlineLayer: null,

	/**
	 * @var element background layer
	 **/
	bgLayer: null,

	/**
	 * @var element background iframe
	 **/
	bgIframe: null,

	/**
	 * @var element background fade
	 **/
	bgLayerFade: null,

	/**
	 * @var function position
	 **/
	funcPosition: null,

	/**
	 * @var element content-source used for clone or place into NM_Layer container
	 **/
	contentSourceLayer: null,

	/**
	 * @var element content
	 **/
	contentLayer: null,

	/**
	 * @var element text layer
	 **/
	textLayer: null,

	/**
	 * @var element parent content
	 **/
	parentContainer: null,

	/**
	 * @var close span
	 **/
	closeSpan: null,

	/**
	 * NM_AbstractLayer.hide()
	 * hides an opened layer
	 */
	hide : function()
	{
		this.bgLayerFade.start( { 'opacity': [this.opacity, 0] } ).chain( this.removeLayers.bind(this) );
		this.contentLayer.setStyle( 'opacity', 0 );
		this.contentLayer.addClass( 'hide' );

		if( this.contentSourceLayer != null )
		{
			this.contentSourceLayer.setStyle( 'opacity', 0 );
			this.contentSourceLayer.addClass( 'hide' );
		}

		if( this.headlineLayer != null )
		{
			this.headlineLayer.setStyle( 'opacity', 0 );
			this.headlineLayer.addClass( 'hide' );
		}
	},

	/**
	 * NM_AbstractLayer.removeLayers()
	 * helper function for hide; removes the created layers after fadeout
	 */
	removeLayers : function() {
		window.removeEvent( 'scroll', this.funcPosition );
		if( this.parentContainer != null )
		{
			this.contentLayer.getChildren().injectAfter( this.parentContainer.getLast() );
		}
		this.contentLayer.remove();
		this.bgLayer.remove();
		if(this.bgIframe != null) {
			// ie6 hack
			this.bgIframe.remove();
		}
	},

	/**
	 * NM_AbstractLayer.setBGLayerDimension()
	 * set the Background Layer Dimensions
	 **/
	setBGLayerDimension : function() {
		if(this.bgLayer != null) {
			var intBgWidth = window.getScrollWidth();
			if(window.ie6) {
				intBgWidth -= 28;
			}
			this.bgLayer.setStyle( 'height', window.getScrollHeight()+'px' );
			this.bgLayer.setStyle( 'width', intBgWidth+'px' );
		}
	},

	/**
	 * NM_AbstractLayer.getWindowHeight()
	 * returns the Window Height
	 **/
	getWindowHeight : function()	{
		var intReturn = 0;
		if( document.body && document.body.offsetHeight )
		{
			intReturn = document.body.offsetHeight;
		}
		else if( window.innerHeight > 0 )
		{
			intReturn = window.innerHeight;
		}
		return intReturn;
	},

	/**
	 * NM_AbstractLayer.getWindowWidth()
	 * returns the window Width
	 **/
	getWindowWidth : function()	{
		var intReturn 	= 0;
		var intWidth 	= window.getWidth();
		if( intWidth > 0 )
		{
			intReturn = intWidth;
		}
		else if( document.body && document.body.offsetWidth )
		{
			intReturn = document.body.offsetWidth;
		}
		else if( window.innerWidth > 0 )
		{
			intReturn = window.innerWidth;
		}
		return intReturn;
	},

	/**
	 * NM_AbstractLayer.position()
	 * positions the layers when the user uses the scrollbar
	 */
	position : function() {
		var top = (window.getScrollTop() + (window.getHeight() - this.height)/2);
		if(top<10) {
			top = 10;
		}
		top = top + 'px'

		new Fx.Styles('NM_LayerContent', {duration: 75, transition: Fx.Transitions.sineInOut}).start({
			'left':(window.getScrollLeft() + (window.getWidth() - this.width)/2)+'px',
			'top': top});
	},

	/**
	 * NM_AbstractLayer.setOpacity()
	 * sets opacity
	 */
	setOpacity : function( opacity ) {
		if(opacity) {
			this.opacity = opacity;
		}
	},

	/**
	 * NM_AbstractLayer.initializeCloseElement()
	 * initialize close element
	 */
	initializeCloseElement : function( objParam ) {

		if(objParam.isClosable == null || objParam.isClosable === true) {


			this.generateCloseElement();



			// add close event to button
			this.closeElement.addEvent('click', function() {
				this.hide();
			}.bind(this));
		}
	},

	/**
	 * generateCloseElement()
	 * creates close element
	 */
	generateCloseElement : function() {

		this.closeElement = new Element('span', {
			'id': 'closeSpan'
		});

		var strClose = 'Fermer';

		this.closeElementLink = new Element('a', {'class': 'close' } );
		this.closeElementLink.injectInside( this.closeElement );
		if( false !== $type( $E( '.langNL' ) ) ){
			strClose = 'Sluiten';
		}
		linkTextElement = new Element( 'span', {'class': 'closeText' } );
		linkTextElement.setText( strClose );
		linkTextElement.injectInside( this.closeElementLink );
		buttonElement = new Element( 'span', {'class': 'closeButton mainSprite' } );
		buttonElement.setText( " " );
		buttonElement.injectInside( this.closeElementLink );


	},

	/**
	 * NM_AbstractLayer.injectCloseElement()
	 * injects close element to overgiven container
	 */
	injectCloseElement : function( container ) {
		if(container && this.closeElement != null) {
			this.closeElement.inject(container);
		}
	},

	/**
	 * NM_AbstractLayer.initializeBackgroundLayer()
	 * initialize background layer
	 */
	initializeBackgroundLayer : function( objParam ) {
		// create background layer
		this.generateBackgroundLayer();

		// background layer disappears after clicking
		if(objParam.isBackgroundClosable == null ||  objParam.isBackgroundClosable === true) {
			this.bgLayer.addEvent('click', function() {
				this.hide();
			}.bind(this));
		}

		// Fade Effect for Background Layer / Iframe
		this.bgLayerFade = this.bgLayer.effects( {duration: 250, transition: Fx.Transitions.sineInOut } );
	},

	/**
	 * NM_AbstractLayer.generateBackgroundLayer()
	 * generates background layer
	 */
	generateBackgroundLayer : function() {
		this.bgLayer = new Element('div', {
			'styles': {
				'width': this.getWindowWidth(),
				'height': this.getWindowHeight()
			},
			'id': 'NM_LayerBg'
		});
	},

	/**
	 * NM_AbstractLayer.initializeIE6BackgroundIFrame()
	 * initialize ie6 background iframe
	 */
	initializeIE6BackgroundIFrame : function() {

		// create iframe (ie6 hack to hide combo-boxes)
		if(window.ie6) {
			this.generateIE6BackgroundIFrame();
		}
	},

	/**
	 * NM_AbstractLayer.generateIE6BackgroundIFrame()
	 * generates ie6 background iframe
	 */
	generateIE6BackgroundIFrame : function() {
		this.bgIframe = new Element('iframe', {
			'id': 'bskCoverer',
			'frameborder': '0',
			'src': '',
			'styles': {
				'width': this.getWindowWidth(),
				'height': this.getWindowHeight()
			}
		});
	},

	/**
	 * NM_AbstractLayer.injectIE6BackgroundIFrame()
	 * injects background iframe to body
	 */
	injectIE6BackgroundIFrame : function() {
		if(this.bgIframe != null) {
			this.bgIframe.inject(document.body);
			this.bgIframe.setStyle( 'height', window.getScrollHeight() + 'px' ),
			this.bgIframe.setStyle( 'width', window.getScrollWidth() +'px' );
		}
	},

	/**
	 * NM_AbstractLayer.initializeContentLayer()
	 * initialize content layer
	 */
	initializeContentLayer : function( centralize ) {
		this.generateContentLayer();

		if(centralize == null || centralize === true)
		{
			var left 			= (window.getScrollLeft() + (window.getWidth() - this.width)/2) + 'px';
			var top				= (window.getScrollTop() + (window.getHeight() - this.height)/2);

			if(top<10) {
				top = 10;
			}
			top 				= top + 'px'

			this.positioningContentLayer(left, top);
		}
	},

	/**
	 * NM_AbstractLayer.generateContentLayer()
	 * generates content layer
	 */
	generateContentLayer : function() {
		this.contentLayer = new Element('div', {
			'styles': {
				'width': this.width+'px',
				'height': this.height+'px'
			},
			'id': 'NM_LayerContent',
			'class': 'test_NM_Layer'
		});
	},

	/**
	 * NM_AbstractLayer.positioningContentLayer()
	 * positioning of content layer
	 */
	positioningContentLayer  : function( left, top ) {
		this.contentLayer.setStyle('left', left);
		this.contentLayer.setStyle('top', top);
	}

});

/*
 * NM_LayerClass
 * Class for all "Layer-Popups"
 * NM_Layer.js
 *
 * @author		Steffen Abel <steffen.abel@dmc.de>
 * @version 	$Id: NM_Layer.js 87060 2011-12-05 11:12:13Z mwolf $
 */

var NM_LayerClass = NM_AbstractLayer.extend ({

	/**
	 * NM_Layer.initialize()
	 */
	initialize: function( objParam ) {

		// set opacity
		this.setOpacity(objParam.opacity);

		// create close element
		this.initializeCloseElement(objParam);

		// create background layer
		this.initializeBackgroundLayer(objParam);

		// create background iframe for ie6
		this.initializeIE6BackgroundIFrame();

		// bind position function
		this.funcPosition = function() { this.position(); }.bind(this);
	},

	/**
	 * NM_Layer.generateBackgroundLayer()
	 * generates background layer
	 */
	generateBackgroundLayer : function() {
		this.bgLayer = new Element('div', {
			'styles': {
				'position': 'absolute',
				'left': '0px',
				'top': '0px',
				'width': this.getWindowWidth(),
				'height': this.getWindowHeight(),
				'background-color': '#333333',
				'z-index': 10,
				'opacity': '0'
			},
			'id': 'NM_LayerBg'
		});
	},

	/**
	 * NM_Layer.generateIE6BackgroundIFrame()
	 * generates ie6 background iframe
	 */
	generateIE6BackgroundIFrame : function() {
		this.bgIframe = new Element('iframe', {
			'id': 'bskCoverer',
			'frameborder': '0',
			'src': '',
			'styles': {
				'position': 'absolute',
				'left': '0px',
				'top': '0px',
				'width': this.getWindowWidth(),
				'height': this.getWindowHeight(),
				'opacity': 0.01
			}
		});
	},

	/**
	 * NM_Layer.generateContentLayer()
	 * generates content layer
	 */
	generateContentLayer : function() {
		this.contentLayer = new Element('div', {
			'styles': {
				'width': this.width+'px',
				'height': this.height+'px',
				'background-color': '#EEEEEE',
				'border': '4px solid #555555',
				'position': 'absolute',
				'opacity': 0,
				'z-index': 11
			},
			'id': 'NM_LayerContent'
		});
	},

	/**
	 * NM_Layer.showUrl()
	 * opens a new Layer with Iframe and displays the submitted url in the iframe
	 */
	showUrl: function ( url, width, height, scrolling, title ) {

		var iframewidth 	= width;
		var iframeheight 	= height - 24;
		this.width	 		= width;
		this.height 		= height;

		if(!scrolling) scrolling = 'auto'

		// create content layer
		this.initializeContentLayer(true);

		// set background layer dimensions
		this.setBGLayerDimension();

		// inject ie background iframe to dom
		this.injectIE6BackgroundIFrame();

		// inject background layer to dom
		this.bgLayer.inject(document.body);

		// create headline Layer
		this.headlineLayer = new Element('div', {
			'id': 'headlineLayer',
			'styles': {
				'width': (this.width)+'px'
			}
		});
		this.headlineLayer.inject(this.contentLayer);

		if( title )
		{
			// create headline span
			headlineSpan = new Element('span', {
				'id': 'headline',
				'styles': {
					'width': (this.width-120)+'px'
				}
			});
			headlineSpan.appendText( title );
			headlineSpan.inject(this.headlineLayer);
		}

		this.injectCloseElement(this.headlineLayer);

		// create content iframe
		iframe = new Element('iframe', {
							src: url,
							frameborder: 0,
							width: iframewidth,
							height: iframeheight,
							id: 'NM_LayerIframe',
							scrolling: scrolling
						});

		// inject iframe to content
		iframe.inject(this.contentLayer);

		// inject content to body
		this.contentLayer.inject(document.body);

		// start fade
		this.bgLayerFade.start( { 'opacity': [0, this.opacity] } );
		this.contentLayer.setStyle( 'opacity', 1 );

		// add eventlistener for scrolling
		window.addEvent( 'scroll', this.funcPosition );
	},

	/**
	 * NN_Layer.showLayer()
	 *
	 * displays a given Layer as Lightbox
	 **/
	showLayer: function ( title, layerID, width, height ) {
		this.width = width;
		this.height = height;

		// create content layer
		this.initializeContentLayer(true);

		// create headline Layer
		this.headlineLayer = new Element('div', {
			'styles': {
				'position': 'absolute',
				'width': (this.width-20)+'px',
				'top': '0px',
				'padding': '3px 3px 10px 10px'
			}
		});

		// create headline span
		headlineSpan = new Element('span', {
			'styles': {
				'width': (this.width-120)+'px',
				'float': 'left',
				'font-weight': 'bold',
				'margin-bottom' : '10px'
			}
		});
		headlineSpan.appendText( title );
		headlineSpan.inject(this.headlineLayer);

		// set background layer dimensions
		this.setBGLayerDimension();

		// create new content
		this.textLayer = $(layerID).clone();

		// add close element to content layer
		this.injectCloseElement(this.headlineLayer);

		// inject headline to content layer
		this.headlineLayer.inject(this.contentLayer);

		// inject text layer to content layer
		this.textLayer.inject(this.contentLayer);
		this.textLayer.setStyles({
				'top': '20px',
				'padding': '10px',
				'position': 'relative',
				'overflow': 'auto',
				'display' : 'block'
		});

		// inject ie background iframe to dom
		this.injectIE6BackgroundIFrame();

		// inject background layer to body
		this.bgLayer.inject(document.body);

		// inject content layer to body
		this.contentLayer.inject(document.body);

		// start fade
		this.bgLayerFade.start( { 'opacity': [0, this.opacity] } );
		this.contentLayer.setStyle( 'opacity', 1 );

		// add eventlistener for scrolling
		window.addEvent('scroll', this.funcPosition);
	},

	/**
	 * NM_Layer.showMessage()
	 * displays the given html-content as a new layer
	 */
	showMessage: function( title, message, width, height ) {
		this.width = width;
		this.height = height;

		// create content layer
		this.initializeContentLayer(true);

		// create headline layer
		this.headlineLayer = new Element('div', {
			'styles': {
				'position': 'absolute',
				'width': (this.width)+'px',
				'top': '0px',
				'padding': '3px 3px 3px 10px'
			}
		});

		// create headline span
		headlineSpan = new Element('span', {
			'styles': {
				'width': (this.width-120)+'px',
				'float': 'left',
				'font-weight': 'bold'
			}
		});
		headlineSpan.appendText( title );
		headlineSpan.inject(this.headlineLayer);

		// create text layer
		this.textLayer = new Element('div', {
			'styles': {
				'width': (this.width-20)+'px',
				'height': (this.height-40)+'px',
				'background-color': '#FFFFFF',
				'top': '20px',
				'padding': '10px',
				'position': 'relative',
				'overflow': 'auto'
			}
		});

		// set html in text layer
		this.updateContent(message);

		// set background layer dimensions
		this.setBGLayerDimension();

		// inject close button
		this.injectCloseElement(this.headlineLayer);

		// inject headline to content layer
		this.headlineLayer.inject(this.contentLayer);

		// inject text layer to content layer
		this.textLayer.inject(this.contentLayer);

		// inject ie background iframe to dom
		this.injectIE6BackgroundIFrame();

		// inject baykground layer to body
		this.bgLayer.inject(document.body);

		// inject content layer to body
		this.contentLayer.inject(document.body);

		// start fade
		this.bgLayerFade.start( { 'opacity': [0, this.opacity] } );
		this.contentLayer.setStyle( 'opacity', 1 );

		// add Eventlistener for Scrolling
		window.addEvent('scroll', this.funcPosition);
	},

	/**
	 * NM_Layer.updateContent()
	 */
	updateContent: function( content ) {
		this.textLayer.innerHTML = content;
	}

});

var NM_LightBoxClass = NM_AbstractLayer.extend ({

	/**
	 * NM_LightBox.initialize()
	 */
	initialize: function( objParam ) {

		// set opacity
		this.setOpacity(objParam.opacity);

		// create background layer
		this.initializeBackgroundLayer(objParam);

		// create background iframe for ie6
		this.initializeIE6BackgroundIFrame();
	},

	/**
	 * NM_LightBox.removeGridPagePositionRelative()
	 * don't remove, otherwise content doesn't appear in ie7
	 */
	removeGridPagePositionRelative: function( objParam ) {
		$('NM_GridPage').setStyle( 'position', 'static' );
	},

	/**
	 * NM_LightBox.display()
	 */
	display: function( objParam ) {

		if(objParam.layerID != null) {

			this.width 	= objParam.width;
			this.height = objParam.height;

			// create content layer
			this.initializeContentLayer(objParam.centralize);

			// create headline containing close element

			if(objParam.title) {
				// create headline Layer
				this.headlineLayer = new Element('div', {
					'id': 'headlineLayer',
					'styles': {
						'width': (this.width)+'px'
					}
				});

				// create headline span
				headlineSpan = new Element('span', {
					'id': 'headline',
					'styles': {
						'width': (this.width-120)+'px'
					}
				});
				headlineSpan.appendText( objParam.title );
				headlineSpan.inject(this.headlineLayer);

				// create close element
				this.initializeCloseElement(objParam);

				// add close element to content layer
				this.injectCloseElement(this.headlineLayer);

				// inject headline to content layer
				this.headlineLayer.inject(this.contentLayer);
			}

			// set background layer dimensions
			this.setBGLayerDimension();

			this.contentSourceLayer = $(objParam.layerID);

			this.contentSourceLayer.setStyle( 'opacity', 1 );

			// create new content
			if( objParam.uniqueElement == true )
			{
				this.parentContainer = this.contentSourceLayer.getParent();
				this.textLayer = this.contentSourceLayer;
			}
			else
			{
				this.textLayer = this.contentSourceLayer.clone();
			}
			// inject text layer to content layer
			this.textLayer.inject(this.contentLayer);
			this.textLayer.addClass('textLayer');

			// inject ie background iframe to dom
			this.injectIE6BackgroundIFrame();

			// inject background layer to body
			this.bgLayer.inject(document.body);

			// inject content layer to body
			if(objParam.replaceContentContainer != null && objParam.replaceContentContainer === true) {

				// remove position:relative for NM_GridPage, otherwise content layer is outgreyes only in ie7
				this.removeGridPagePositionRelative();

				this.contentSourceLayer.replaceWith(this.contentLayer);
			} else {
				this.contentLayer.inject(document.body);
			}

			// start fade
			this.bgLayerFade.start( { 'opacity': [0, this.opacity] } );
			this.contentLayer.setStyle( 'opacity', 1 );

			// add eventlistener for scrolling
			if(objParam.scrolling == null || objParam.scrolling === true) {
				this.textLayer.addClass( 'scrolling' );
				// bind position function
				this.funcPosition = function() { this.position(); }.bind(this);

				window.addEvent('scroll', this.funcPosition);
			}
			else
			{
				this.textLayer.addClass( 'noScrolling' );
			}
		}
	}

});

var NM_Layer = null;

window.addEvent('load', function() {

	if( null == NM_Layer )
	{
		NM_Layer = new NM_LayerClass( {} );
	}

});
