//ContentLayer
/**********************************/
/* START: external contact layer */

var ContactLayer = Class.create();

ContactLayer.prototype = Object.extend(new Layer, {
	/*
	 * @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, navTo) {

		this.layerIdstring = "main-block-iframe";
		this.node = node;

		this.navTo = false;
		this.iframehref = this.getTriggerUrl(trigger.href);
		this.iframeidstring = "iframe-contact-id";
		this.iframeisloaded = false;
		this.iframeheight = 50;

		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');
		iframe.setAttribute('bgColor', '#e7e8eb');
		iframe.setAttribute('unload', 'hideIt();');
		

		this.initSuper(node, trigger);


		var closeButton = Helper.getCloseButton(this.node);
		if(!closeButton) alert("Implementation Error: no close button found");
		closeButton.observe("click", function(){this.close();}.bindAsEventListener(this));

		trigger.href="javascript:void(0);";

		//appending unique iframe to the layer div
		$(this.layerIdstring).appendChild(iframe);

		if(this.navTo) {
			this.toggle();
		}

	},
	getUrl: function(asName) {
		var lsURL = ((typeof(url) == 'undefined') || (url.length == 0)) ? window.location.href : url;
		loU = lsURL.split("?");
		if (loU.length>1){
			var loallPm = loU[1].split("&");
			for (var i=0; i<loallPm.length; i++){
				var loPm = loallPm[i].split("=");
				if (loPm[0]==asName){
					if (loPm.length>1){
						return loPm[1];
					}
				}
			}
		}
		else{
			return null;
		}
	},
	setTriggerUrl: function(triggerHref) {
		this.iframehref = triggerHref;
		this.iframeisloaded = false;
	},
	getTriggerUrl: function(triggerHref) {
		var nav = this.getUrl('contact');
		if(nav != null) {
			this.navTo = true;
			return "/contact"+nav;
		} else {
			return triggerHref;
		}
	},
	afterClose: function(newLayer) {
		if (!Layer.toggle) {
			HeaderAnimation.augment();
		}
	},
	beforeClose: function() {
		HeaderAnimation.unregisterLayer();
		return true;
	},
	beforeOpen: function() {
		HeaderAnimation.registerLayer(this);
		if(!this.iframeisloaded) {
			this.iframeisloaded = false;
			this.addPreloader();
		}
		return true;
	},
	hide: function() {
		this.node.removeClassName("active-layer");
		this.trigger.up().removeClassName("active");
	},
	show: function() {
		this.node.addClassName("active-layer");
		this.trigger.up().addClassName("active");
		this.trigger.addClassName("clicked"); // avoids hover effect (only for the first time)
		this.trigger.observe("mouseout",
			function(e) {
				var elm = Event.findElement(e, "a");
				elm.removeClassName("clicked");
				elm.stopObserving("mouseout");
			}
		);
	},
	setOffset:function(offset) {
		this.node.style.top = offset + "px";
	},
	/*
	 * 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;
		}
	},
	/*
	 * Switch the iframe.src attribute from "about:blank" to its real location
	 */
	setIFrameLocation: function() {

		Event.observe($(this.iframeidstring),'load',function(e){this.rechunkIFrame(e);}.bindAsEventListener(this));

		$(this.iframeidstring).src = this.iframehref;
	},
	showIFrame: function() {
		this.removePreloader();
	},
	hideIFrame: function() {
		this.addPreloader();
	},
	addPreloader: function() {
		if(!$('lightbox-preloader')) {
			new Insertion.Before($(this.iframeidstring), "<div id='lightbox-preloader'>&nbsp;</div>");
		}
		$(this.iframeidstring).style.visibility = 'hidden';
	},
	removePreloader: function() {
		if($('lightbox-preloader')) {
			$('lightbox-preloader').remove();
		}
		$(this.iframeidstring).style.visibility = 'visible';
	},
	/*
	 * 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) {

				var h = iframeroot.contentWindow.document.getElementById('toolbar-layer-contact').offsetHeight;

				iframeroot.contentWindow.document.onUnload =function(){ alert('unloading')};

				iframeroot.height = h;
				this.iframeheight = h;

			} else if(iframeroot.contentDocument) {

				var h = iframeroot.contentDocument.document.getElementById('toolbar-layer-contact').offsetHeight;

				iframeroot.height = h;
				this.iframeheight = h;

			} else {
				//NOTE: ('missing contentWindow or contentDocument. alternative by now not supported');
				return;
			}

			this.iframeisloaded = true;

			//this.removePreloader();

			//$(this.iframeidstring).style.visibility = 'visible';
		}
	}
});


/* END:*/

/********************************************************************/
/*	overwritten function of _resources/js/compiled/script.js (208)	*/
/********************************************************************/
var IFRAME_CONTACT_REL_REGEX = /^ext-contact-layer-(.+)$/;
var IFRAME_CONTACT_LAYER = new Array();

function init_contentLayers() {
	if ($("toolbar-nav")) {
		$A($("toolbar-nav").getElementsByTagName("a")).each(
			function(trigger) {
				trigger = $(trigger);
				if (LINK_REL_REGEX.test(trigger.rel)) { // layer link
					var id = trigger.rel.replace(LINK_REL_REGEX, "$1");
					var node = $("toolbar-layer-" + id);
					new ContentLayer(node, trigger);
				} else if(IFRAME_CONTACT_REL_REGEX.test(trigger.rel)) {
					var id = trigger.rel.replace(IFRAME_CONTACT_REL_REGEX, "$1");
					var node = $("toolbar-layer-" + id);
					IFRAME_CONTACT_LAYER[id] = new ContactLayer(node,trigger);
				} else { // standard link, probably external
					trigger.observe("click", function() {
						HeaderAnimation.animate = false;
						Layer.closeCurrent();
						HeaderAnimation.animate = true;
					}.bindAsEventListener(this));
				}
			}.bind(this)
		);
	}
}

function showContactIFrame() {
	var layer = IFRAME_CONTACT_LAYER["contact"];
	layer.showIFrame();
}
function hideContactIFrame() {
	var layer = IFRAME_CONTACT_LAYER["contact"];
	layer.hideIFrame();
}