/* 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;
		
		//start flash controller
		this.flashcontent = false;
		this.flashwriterWhere = $(this.idstring).down().id;
		//end flash controller

		
		this.initSuper(node, trigger);
		var closeButton = Helper.getCloseButton(this.node);
		closeButton.observe("click", function(){
			this.close();
			//this.contentHolder = $(this.layerIdstring).innerHTML;
			//$(this.layerIdstring).update();	
		}.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
		 */
		if($(this.idstring))
		{
			new Insertion.After($(this.idstring), "<div id='"+this.replacer+"'>&nbsp;</div>");		
			var content = $(this.idstring);
			$(this.idstring).remove();
			$(this.layerIdstring).appendChild(content);
		}
	},
	beforeOpen: function() {
		
		if(this.flashcontent && flashRewriter[this.idstring] != null) {
			flashRewriter[this.idstring].write(this.flashwriterWhere);
			this.flashcontent = false;
		}
		
		//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;
	},
    afterOpen: function() {
		if(typeof fireSifrHandler == 'function') {
            fireSifrHandler();   
        }
	},
	afterClose: function() {
		this.flashcontent = true;
		$(this.flashwriterWhere).update();
	} 
});

/* END: inline lightbox layer     */
/**********************************/

/* END: external lightbox layer */
var INLIGHTBOX_REL_REGEX = /^in-lightbox-(.+)$/;
var EXTLIGHTBOX_REL_REGEX = /^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;
					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;
					new ExternalLightboxLayer(node,trigger,id);
				}
				
			}.bind(this)
		);
	}
}
/********************************************************************/

Event.onDOMReady(function(){
	init_ext_lightboxLayers();
});