/* START: abstract lightbox layer */
var AbstractLightboxLayer = Class.create();

AbstractLightboxLayer.prototype = Object.extend(new Layer, {

	initialize: function(node, trigger, idstring) {
	},

	open: function() {
		//DEV NOTE: will not be true in pagetypes 1 and 2 but was implemented for 
		//future use of lightbox layers in these pagetypes
		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 top value of layer
		if(!this.headerHeight) this.headerHeight = $('header-zone').getDimensions().height + $('toolbar-zone').getDimensions().height;

		//position the curtain
		this.curtain.setStyle({'top': this.headerHeight+'px'});

		//set the top value with header height
		this.node.setStyle({'display': 'block'});
		if(!this.nodeTop) this.nodeTop = this.headerHeight + parseInt(this.node.getStyle('top'));
		if(!this.nodeLeft) this.nodeLeft = parseInt(this.node.getStyle('top'));
		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;
			layerOccupation = layerOccupation - this.headerHeight;
			
			//difference between layer height and content height
			var diff2 = $('content-zone').getDimensions().height - layerOccupation;
			$('content-zone').setStyle({'height': $('content-zone').getDimensions().height - diff2 + 'px'});
			this.resizeCurtain();
		};

		this.node.setStyle({'top': this.nodeTop+'px'});
		if(this.iframeLining)
			this.iframeLining.show();
		this.curtain.setStyle({'display': 'block'});
		$('footer-position-wrapper').scrollTo();
		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 = parseInt(toolbar.getDimensions().width) + parseInt(toolbar.getStyle('margin-left')) + parseInt(toolbar.getStyle('margin-right'));
			this.curtain.setStyle({'height': this.contentHeight+'px','width': this.contentWidth+'px'});
			if(this.iframeLining)
				this.iframeLining.refresh();
		}
	}
});

/* END: abstract lightbox layer */
/********************************/
/* START: inline lightbox layer */
var InlineLightboxLayer = Class.create();

InlineLightboxLayer.prototype = Object.extend(new AbstractLightboxLayer, {
	/*
	 * @base		: AbstractLightboxLayer
	 * @node		: layer div holding the hidden content
	 * @trigger		: element that show triggers the layer
	 * @idstring	: unique id of trigger rel attribute, so that multiple layers can be instantiated
	 */
	initialize: function(node, trigger, idstring) {
		//lightbox layers can not occur in these page types
		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.initSuper(node, trigger);
		var closeButton = Helper.getCloseButton(this.node);
		closeButton.observe("click", function(){this.close();}.bindAsEventListener(this));
		trigger.href="javascript:void(0);";
		//create the curtain
		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);
		}
		//store the listener so it can be accessed by functions add and remove
		this.listener = {'augmentDone' : this.handleOpen.bind(this) };
		//add an event handler to resize the curtain when window is resized
		Event.observe(window, "resize", function(){this.resizeCurtain();}.bindAsEventListener(this));
		
		/*
		 * @id	: this block is used to move the original div content into the layercontent	and if necessary back
		 */
		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();   
        }
	}    
});

/* END: inline lightbox layer     */
/**********************************/
/* START: external lightbox layer */

var ExternalLightboxLayer = Class.create();

ExternalLightboxLayer.prototype = Object.extend(new AbstractLightboxLayer, {
	/*
	 * @base		: AbstractLightboxLayer
	 * @node		: layer div holding the hidden content
	 * @trigger		: element that show triggers the layer
	 * @idstring	: unique id of trigger rel attribute, so that multiple layers can be instantiated
	 */
	initialize: function(node, trigger, idstring) {
		//lightbox layers can not occur in these page types
		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%');
		//use camel case here for frameBorder else IE will ignore this attribute
		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);";

		//create the curtain
		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);
		}

		//store the listener so it can be accessed by functions add and remove
		this.listener = {'augmentDone' : this.handleOpen.bind(this) };

		//add an event handler to resize the curtain when window is resized
		Event.observe(window, "resize", function(){this.resizeCurtain();}.bindAsEventListener(this));
		
		//appending unique iframe to the layer div
		$(this.layerIdstring).appendChild(iframe);
		
	},
	/*
	 * Check if the real destination was loaded before. 
	 * If yes, then just correct the curtain else set the location to initiate iframe. 
	 */
	afterOpen: function() {
		if(!this.iframeisloaded) {
			this.setIFrameLocation();
			this.iframeisloaded = true;
		} else {
			this.correctCurtain();
		}
	},
	/*
	 * Can be used to do some cleanup if necessary.
	 */
	afterClose: function() {
		//do some cleanup here if necessary
	},
	/*
	 * Switch the iframe.src attribute from "about:blank" to its real location 
	 */
	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;
	},
	/*
	 * Corrects the content-zone and curtain height if the iframe is higher than the content-zone height.
	 */
	correctCurtain: function() {
		
		if(this.curtain) {
			var wrapper = $('footer-position-wrapper').getDimensions();
			var header = $('header-zone').getDimensions();
			var toolbar = $('toolbar-zone');
			
			var footer = $('footer-zone');
			
			var footerPosY = footer.cumulativeOffset()[1];

			var curtainHeight = parseInt(wrapper.height) - parseInt(header.height) - parseInt(toolbar.getDimensions().height);
			//if(parseInt(this.iframeheight) > curtainHeight) {
			if(parseInt(this.iframeheight) > 640){// && curtainHeight <= 693 ) {

				this.diff = -1;
				var contentZoneHeight = parseInt(this.iframeheight) + this.nodeTop;
				$('content-zone').setStyle({'height': contentZoneHeight + 'px'});
				curtainHeight = contentZoneHeight + 44;//add 44px offset for footer
				
				this.contentHeight = curtainHeight;
				this.contentWidth = parseInt(toolbar.getDimensions().width) + parseInt(toolbar.getStyle('margin-left')) + parseInt(toolbar.getStyle('margin-right'));
				this.curtain.setStyle({'height': this.contentHeight+'px','width': this.contentWidth+'px'});
				if(this.iframeLining) {
					this.iframeLining.refresh();
				}
			}
		}
	},
	/*
	 * This is necessary to cut some parts off from the destination source like header and footer.
	 * TODO: note: do rechunking in "beforeOpen" function.
	 */
	rechunkIFrame: function(loadevt) {
		
		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('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')) {
					//iframeroot.contentWindow.document.getElementById('llhc').style.marginLeft = '0px';
					//iframeroot.contentWindow.document.getElementById('lrhc').style.marginLeft = '376px';			
				}
				if(iframeroot.contentWindow.document.getElementById('content-zone')) {
					iframeroot.contentWindow.document.getElementById('content-zone').style.marginLeft = '0px';
				}
				
				var h = iframeroot.contentWindow.document.getElementById('content-zone').offsetHeight + 0;// ie has 50 px more than ff
				
				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('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')) {
					//iframeroot.contentDocument.document.getElementById('llhc').style.marginLeft = '0px';
					//iframeroot.contentDocument.document.getElementById('lrhc').style.marginLeft = '376px';
				}
				if(iframeroot.contentDocument.document.getElementById('content-zone')) {
					iframeroot.contentDocument.document.getElementById('content-zone').style.marginLeft = '0px';
				}
				
				var h = iframeroot.contentDocument.document.getElementById('content-zone').offsetHeight + 0;
				
				iframeroot.height = h;
				this.iframeheight = h;
			
			} else {
				//NOTE: ('missing contentWindow or contentDocument. alternative by now not supported');
				return;
			}
		
			this.iframeisloaded = true;
				
			if($('lightbox-preloader')) {
				$('lightbox-preloader').remove();
			}
			
			this.correctCurtain();
			
			$(this.iframeidstring).style.visibility = 'visible';	
		}
	}
});

/* END: external lightbox layer */
/* START: simple external lightbox layer */

var VideoExternalLightboxLayer = Class.create();

VideoExternalLightboxLayer.prototype = Object.extend(new AbstractLightboxLayer, {
	/*
	 * @base		: AbstractLightboxLayer
	 * @node		: layer div holding the hidden content
	 * @trigger		: element that show triggers the layer
	 * @idstring	: unique id of trigger rel attribute, so that multiple layers can be instantiated
	 */
	initialize: function(node, trigger, idstring, idvideo) {
	
		if(pageType == "1" || pageType == "2") return;
		this.idstring = idstring;
		this.layerIdstring = "layer-content-"+idstring; 
		this.node = node;
		this.iframeisloaded = false;
		this.iframehref = "../../../pool/videos/player.htm#" + idvideo;
		this.iframeidstring = "iframe-"+idstring;
		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', '400px');
		iframe.setAttribute('width', '100%');
		//use camel case here for frameBorder else IE will ignore this attribute
		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);
	},
	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;
	},
	rechunkIFrame: function(loadevt) {
		if($('lightbox-preloader')) {
			$('lightbox-preloader').remove();
		}
		$(this.iframeidstring).style.visibility = 'visible';
	},
	afterOpen: function() {
		this.setIFrameLocation();
	},
	afterClose: function() {
		$(this.iframeidstring).style.visibility = 'hidden';
		$(this.iframeidstring).src = "about:blank";		
	}
});

/* END: simple external lightbox layer */


var INLIGHTBOX_REL_REGEX = /^in-lightbox-(.+)$/;
var EXTLIGHTBOX_REL_REGEX = /^ext-lightbox-(.+)$/;
var VIDEO_EXTLIGHTBOX_REL_REGEX = /^#video-ext-lightbox-(.+)-(.+)$/;
var ADDITIONAL_TRIGGER_LINKS_REL_REGEX=/^trigger-(.+)$/;

function init_ext_lightboxLayers() {
	if ($("content-zone")) {
		$A($("content-zone").getElementsByTagName("a")).each(
			function(trigger) {
				trigger = $(trigger);
				if (INLIGHTBOX_REL_REGEX.test(trigger.rel)) { // layer link
					
					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 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)) { // layer link
					
					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 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,trigger.href);Event.stop(e);}.bind(this));
				} else if(VIDEO_EXTLIGHTBOX_REL_REGEX.test(trigger.hash)) {
					
					var id = trigger.hash.replace(VIDEO_EXTLIGHTBOX_REL_REGEX, "$1");
					var video = trigger.hash.replace(VIDEO_EXTLIGHTBOX_REL_REGEX, "$2");
					
					var elm = "<div id='lightbox-layer-"+id+"' class='lightbox-layer'><div class='close'><a href='javascript:void(0);'>";
					elm += "<span class='access'>Close 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 VideoExternalLightboxLayer(node,trigger,id,video);					
				}
				
			}.bind(this)
		);
	}
}
/********************************************************************/

Event.onDOMReady(function(){
	init_ext_lightboxLayers();
});