
var AbstractLightboxLayer = Class.create();
AbstractLightboxLayer.prototype = Object.extend(new Layer, {
	initialize : function(node, trigger, idstring) {
	},
	open : function() {
		if (HeaderAnimation.diminishable && !HeaderAnimation.augmented) {
			HeaderAnimation.listenerQueue.add(this.listener);
			HeaderAnimation.augment();
		} else {
			Layer.prototype.open.call(this);
		}
	},
	show : function() {
		this.node.setStyle( {
			'display' :'block'
		});
	},
	hide : function() {
		this.node.setStyle( {
			'display' :'none'
		});
	},
	beforeOpen : function() {
		//	calculate/set curtain size 
		this.resizeCurtain();

		//set the dimensions of layer
		this.node.setStyle( {
			'display' :'block'
		});
		if (!this.nodeTop)
			this.nodeTop = parseInt(this.node.getStyle('top'));
		if (!this.nodeLeft)
			this.nodeLeft = parseInt(this.node.getStyle('left'));
		if (!this.nodeHeight)
			this.nodeHeight = this.node.getDimensions().height;
		this.node.setStyle( {
			'display' :'none'
		});
		
		//check whether layer is higher than current page
		var wrapper = $('footer-position-wrapper');
		this.diff = parseInt(wrapper.getDimensions().height) - this.nodeTop
				- this.nodeHeight;
		if (this.diff < 0) {
			//resize the content zone
			var layerOccupation = this.nodeHeight + this.nodeTop;
			
			//difference between layer height and content height
			var diff2 = $('content-zone').getDimensions().height
					- layerOccupation;
			$('content-zone').setStyle(
					{
						'height' :$('content-zone').getDimensions().height
								- diff2 + 'px'
					});
			//	after content-zone resizing, check again curtain size
			this.resizeCurtain();
		}
		;
		this.node.setStyle( {
			'top' :this.nodeTop + 'px'
		});
		if (this.iframeLining)
			this.iframeLining.show();
		this.curtain.setStyle( {
			'display' :'block'
		});
		return true;
	},
	beforeClose : function() {
		this.curtain.setStyle( {
			'display' :'none'
		});
		if (this.iframeLining)
			this.iframeLining.hide();
		if (this.diff < 0) {
			if (Info.browser.isIE) {
				$('content-zone').setStyle( {
					'height' :'1%'
				});
			} else {
				$('content-zone').setStyle( {
					'height' :'auto'
				});
			}
		}
		return true;
	},
	handleOpen : function() {
		HeaderAnimation.listenerQueue.remove(this.listener);
		Layer.prototype.open.call(this);
	},
	resizeCurtain : function() {
		
		if(this.curtain) {
			var wrapper = $('footer-position-wrapper').getDimensions();
			var header = $('header-zone').getDimensions();
			var toolbar = $('toolbar-zone');

			this.contentHeight = parseInt(wrapper.height) - parseInt(header.height) - parseInt(toolbar.getDimensions().height);
			this.contentWidth = wrapper.width;
			//982px is toolbar min-width (926px) plus its margins (34px left + 22px right)
			if(this.contentWidth < 982) {
				this.contentWidth = 982;
			}
			this.curtain.setStyle({'height': parseInt(wrapper.height) +'px','width': this.contentWidth+'px'});
			if(this.iframeLining) {
				this.iframeLining.refresh();
			}
		}

		
	}
});
var InlineLightboxLayer = Class.create();
InlineLightboxLayer.prototype = Object.extend(new AbstractLightboxLayer, {
	initialize : function(node, trigger, idstring) {
		if (pageType == "1" || pageType == "2")
			return;
		this.idstring = idstring;
		this.layerIdstring = "layer-content-" + idstring;
		this.node = node;
		this.curtainidstring = "lightbox-curtain" + idstring;
		this.replacer = "replacer-" + idstring;
		this.videoContent = false;
		this.initSuper(node, trigger);
		var closeButton = Helper.getCloseButton(this.node);
		closeButton.observe("click", function() {
			this.close();
		}.bindAsEventListener(this));
		trigger.href = "javascript:void(0);";
		new Insertion.After($('content-zone'), "<div id='"
				+ this.curtainidstring
				+ "' class='lightbox-curtain'>&nbsp;</div>");
		this.curtain = $(this.curtainidstring);
		//this.resizeCurtain();
		if (Info.browser.isIEpre7) {
			this.iframeLining = new IframeLining(this.curtain);
		}
		this.listener = {
			'augmentDone' :this.handleOpen.bind(this)
		};
		Event.observe(window, "resize", function() {
			this.resizeCurtain();
		}.bindAsEventListener(this));
		new Insertion.After($(this.idstring), "<div id='" + this.replacer
				+ "'>&nbsp;</div>");
		var content = $(this.idstring);
		$(this.idstring).remove();
		$(this.layerIdstring).appendChild(content);
	},
	afterOpen : function() {
		if (typeof fireSifrHandler == 'function') {
			fireSifrHandler();
		}
		
		/*	rewrite video content (must be saved in global array) into layer	*/
		if(this.videoContent)layerContent[this.idstring].write(this.idstring);
	},
	afterClose : function() {
		/*	check if layer consists of flash/video content	*/
		/*	delete layer content for stopping flash/video in IE	*/
		this.contentVideoCheck();
		if(this.videoContent) $(this.layerIdstring).down().innerHTML = "";
	},
	contentVideoCheck: function() {
		if ( $$("#"+this.idstring + ' embed[class=sIFR-flash]').length || $$("#"+this.idstring + ' object[class=sIFR-flash]').length)
		{
			/*	sifr Object	*/
			this.videoContent = false;
		} else
		{
			/*	flash/video object	*/
			this.videoContent = true;
		}
			
	}
});
var ExternalLightboxLayer = Class.create();
ExternalLightboxLayer.prototype = Object
		.extend(
				new AbstractLightboxLayer,
				{
					initialize : function(node, trigger, idstring) {
						if (pageType == "1" || pageType == "2")
							return;
						this.idstring = idstring;
						this.layerIdstring = "layer-content-" + idstring;
						this.node = node;
						this.iframehref = trigger.href;
						this.iframeidstring = "iframe-" + idstring;
						this.iframeisloaded = false;
						this.iframeheight = 50;
						this.curtainidstring = "lightbox-curtain" + idstring;
						var iframe = document.createElement('iframe');
						iframe.setAttribute('id', this.iframeidstring);
						iframe.setAttribute('name', this.iframeidstring);
						iframe.setAttribute('src', "about:blank");
						iframe.setAttribute('height', this.iframeheight + 'px');
						iframe.setAttribute('width', '100%');
						iframe.setAttribute('frameBorder', '0');
						iframe.setAttribute('border', '0');
						iframe.setAttribute('marginwidth', '0');
						iframe.setAttribute('marginheight', '0');
						iframe.setAttribute('scrolling', 'no');
						this.initSuper(node, trigger);
						var closeButton = Helper.getCloseButton(this.node);
						closeButton.observe("click", function() {
							this.close();
						}.bindAsEventListener(this));
						trigger.href = "javascript:void(0);";
						new Insertion.After($('content-zone'), "<div id='"
								+ this.curtainidstring
								+ "' class='lightbox-curtain'>&nbsp;</div>");
						this.curtain = $(this.curtainidstring);
						//this.resizeCurtain();
						if (Info.browser.isIEpre7) {
							this.iframeLining = new IframeLining(this.curtain);
						}
						this.listener = {
							'augmentDone' :this.handleOpen.bind(this)
						};
						Event.observe(window, "resize", function() {
							this.resizeCurtain();
						}.bindAsEventListener(this));
						$(this.layerIdstring).appendChild(iframe);
					},
					afterOpen : function() {
						if (!this.iframeisloaded) {
							this.setIFrameLocation();
							this.iframeisloaded = true;
						}
						else if(this.iframehref != this.iframecurrenthref) {
							this.setIFrameLocation();
						}
						else {
							this.correctCurtain();
						}
					},
					afterClose : function() {
						$(this.iframeidstring).src = 'about:blank';
						this.iframeheight = 50;
						this.iframeisloaded = false;
					},
					setIFrameLocation : function() {
						new Insertion.Before($(this.iframeidstring),
								"<div id='lightbox-preloader'>&nbsp;</div>");
						Event.observe($(this.iframeidstring), 'load', function(
								e) {
							this.rechunkIFrame(e);
						}.bindAsEventListener(this));
						$(this.iframeidstring).style.visibility = 'hidden';
						$(this.iframeidstring).src = this.iframehref;
					},
					correctCurtain : function() {
						if (this.curtain) {
							
							var wrapper = $('footer-position-wrapper').getDimensions();
							var header = $('header-zone').getDimensions();
							var toolbar = $('toolbar-zone');
							var footer = $('footer-zone');
							
							//	complete height of layer (close Button + iframe content)
							var iframeLayerHeight = $(Layer.current.layerIdstring).up().up().getDimensions().height;
							// total height for curtain/shadow 
							var wrapperHeight = parseInt(wrapper.height);						
							//	layer top position + layer height
							var iframeHeightTotal = parseInt(iframeLayerHeight) + parseInt(this.nodeTop);
							
							// occupied space for the iframe in the content zone
							//var iframeHeightInContentZone = iframeHeightTotal - (parseInt(header.height) + parseInt(toolbar.getDimensions().height));

							//	if layer distance (page top to the lower edge of the layer) is greater than current curtain, then correct curtain
							//	easy: if iframe greater than page

							//	< + footer.getDimensions().height
							if (wrapperHeight < iframeHeightTotal) {
								
								var contentHeightNew = iframeHeightTotal - header.height - toolbar.getDimensions().height; 
								this.diff = -1;
								//	resize content-zone height to total iframe/layer height + position top
								$('content-zone').setStyle( {
									'height' :contentHeightNew + 'px'
								});
								
								this.contentHeight = contentHeightNew;
								this.contentWidth = parseInt(wrapper.width);
								this.curtain.setStyle( {
									'height' :parseInt($('footer-position-wrapper').getDimensions().height) + 'px',
									'width' : this.contentWidth + 'px'
								});									
								if (this.iframeLining) {
									this.iframeLining.refresh();
								}
							}
							else {
								// no correction, but scroll to content (if lightbox is opened in footer etc)
								$('footer-position-wrapper').scrollTo();
							}
						}
					},
					rechunkIFrame : function(loadevt) {
						//	afterClose: sets iframe src=about:blank (IE video hack)
						//	==> fires this function
						//	==> layer/iframe already closed -> skip this function
						if(!Layer.current) return;
						var crossevt = (window.event) ? event : loadevt
						var iframeroot = (crossevt.currentTarget) ? crossevt.currentTarget
								: crossevt.srcElement
						if (iframeroot) {
							if (iframeroot.contentWindow) {
								iframeroot.contentWindow.document.body.style.backgroundImage = "url('')";
								iframeroot.contentWindow.document.body.style.backgroundColor = '#fff';
								if (iframeroot.contentWindow.document
										.getElementById('logo')) {
									iframeroot.contentWindow.document
											.getElementById('logo').style.display = 'none';
								}
								if (iframeroot.contentWindow.document
										.getElementById('header-zone')) {
									iframeroot.contentWindow.document
											.getElementById('header-zone').style.display = 'none';
								}
								if (iframeroot.contentWindow.document
										.getElementById('toolbar-zone')) {
									iframeroot.contentWindow.document
											.getElementById('toolbar-zone').style.display = 'none';
								}
								if (iframeroot.contentWindow.document
										.getElementById('breadcrumb-zone')) {
									iframeroot.contentWindow.document
											.getElementById('breadcrumb-zone').style.display = 'none';
								}
								if (iframeroot.contentWindow.document
										.getElementById('footer-zone')) {
									iframeroot.contentWindow.document
											.getElementById('footer-zone').style.display = 'none';
								}
								if (iframeroot.contentWindow.document
										.getElementById('llhc')
										&& iframeroot.contentWindow.document
												.getElementById('lrhc')) {
								}
								if (iframeroot.contentWindow.document
										.getElementById('content-zone')) {
									iframeroot.contentWindow.document
											.getElementById('content-zone').style.marginLeft = '0px';
								}
								if (iframeroot.contentWindow.document
										.getElementById('content-zone')) {
									var h = iframeroot.contentWindow.document
											.getElementById('content-zone').offsetHeight + 0;
									iframeroot.height = h;
									this.iframeheight = h;
								}
							} else if (iframeroot.contentDocument) {
								iframeroot.contentDocument.document.body.style.backgroundImage = "url('')";
								iframeroot.contentDocument.document.body.style.backgroundColor = '#fff';
								if (iframeroot.contentDocument.document
										.getElementById('logo')) {
									iframeroot.contentDocument.document
											.getElementById('logo').style.display = 'none';
								}
								if (iframeroot.contentDocument.document
										.getElementById('header-zone')) {
									iframeroot.contentDocument.document
											.getElementById('header-zone').style.display = 'none';
								}
								if (iframeroot.contentDocument.document
										.getElementById('toolbar-zone')) {
									iframeroot.contentDocument.document
											.getElementById('toolbar-zone').style.display = 'none';
								}
								if (iframeroot.contentDocument.document
										.getElementById('breadcrumb-zone')) {
									iframeroot.contentDocument.document
											.getElementById('breadcrumb-zone').style.display = 'none';
								}
								if (iframeroot.contentDocument.document
										.getElementById('footer-zone')) {
									iframeroot.contentDocument.document
											.getElementById('footer-zone').style.display = 'none';
								}
								if (iframeroot.contentDocument.document
										.getElementById('llhc')
										&& iframeroot.contentDocument.document
												.getElementById('lrhc')) {
								}
								if (iframeroot.contentDocument.document
										.getElementById('content-zone')) {
									iframeroot.contentDocument.document
											.getElementById('content-zone').style.marginLeft = '0px';
								}
								if (iframeroot.contentDocument.document
										.getElementById('content-zone')) {
									var h = iframeroot.contentDocument.document
											.getElementById('content-zone').offsetHeight + 0;
									iframeroot.height = h;
									this.iframeheight = h;
								}
							} else {
								return;
							}
							this.iframeisloaded = true;
							if ($('lightbox-preloader')) {
								$('lightbox-preloader').remove();
							}
							this.correctCurtain();
							$(this.iframeidstring).style.visibility = 'visible';
						}
					}
				});

var INLIGHTBOX_REL_REGEX = /^in-lightbox-(.+)$/;
var EXTLIGHTBOX_REL_REGEX = /^ext-lightbox-(.+)$/;
var ADDITIONAL_TRIGGER_LINKS_REL_REGEX = /^trigger-(.+)$/;
function init_ext_lightboxLayers( scope ) {
	scope = scope || $$('#content-zone a','#footer-zone a');
	if ($("content-zone")) {
		$A( scope )
				.each(
						function(trigger) {
							trigger = $(trigger);
							if (INLIGHTBOX_REL_REGEX.test(trigger.rel)) {
								var id = trigger.rel.replace(
										INLIGHTBOX_REL_REGEX, "$1");
								var elm = "<div id='lightbox-layer-"
										+ id
										+ "' class='lightbox-layer'><div class='close'><a href='javascript:void(0);'>";
								elm += "<span class='access'>Close Help Layer</span></a></div>";
								elm += "<div class='layer-content'><div id='layer-content-"
										+ id + "'></div></div></div>";
								new Insertion.Before(
										$('footer-position-placeholder'), elm);
								var node = $("lightbox-layer-" + id);
								var key = "lightbox-layer-" + id;
								GLOBAL_LAYER_CONTROLLER[key] = new InlineLightboxLayer(
										node, trigger, id);
							} else if (EXTLIGHTBOX_REL_REGEX.test(trigger.rel)) {
								var id = trigger.rel.replace(
										EXTLIGHTBOX_REL_REGEX, "$1");
								var elm = "<div id='lightbox-layer-"
										+ id
										+ "' class='lightbox-layer'><div class='close'><a href='javascript:void(0);'>";
								elm += "<span class='access'>Close Help Layer</span></a></div>";
								elm += "<div class='layer-content'><div id='layer-content-"
										+ id + "'></div></div></div>";
								new Insertion.Before(
										$('footer-position-placeholder'), elm);
								var node = $("lightbox-layer-" + id);
								var key = "lightbox-layer-" + id;
								GLOBAL_LAYER_CONTROLLER[key] = new ExternalLightboxLayer(
										node, trigger, id);
							} else if (ADDITIONAL_TRIGGER_LINKS_REL_REGEX
									.test(trigger.rel)) {
								var manualTriggerId = trigger.rel.replace(
										ADDITIONAL_TRIGGER_LINKS_REL_REGEX,
										"$1");
								Event.observe(trigger, 'click', function(e) {
									navigateToLayer(manualTriggerId);
									Event.stop(e);
								}.bind(this));
							}
						}.bind(this));
	}
}

document.observe('dom:loaded', function() {
	init_ext_lightboxLayers();
});
