//TODO: force reload of iframe on open
var ExternalProductLightboxLayer = Class.create();
ExternalProductLightboxLayer.prototype = Object
		.extend(
				new AbstractLightboxLayer,
				{
					initialize : function(node, trigger, idstring, withCurtain, refreshOnOpen) {
//						if (pageType == "1" || pageType == "2")
//							return;
						this.idstring = idstring;
						this.layerIdstring = "layer-content-" + idstring;
						this.node = node;
						this.targetDomChangeColor
						this.refreshOnOpen = refreshOnOpen;
						
						this.targetDomChangeColor = false;
						if (NOTEPAD_LAYER_REL_REGEX.test(trigger.rel)){
							this.targetDomChangeColor = trigger;
							this.register = $("notepad-register-layer-" + this.idstring);
						}
						
						this.iframeorigin = trigger.href;
						this.iframehref = trigger.href;
						this.iframeidstring = "iframe-" + idstring;
						this.iframeisloaded = false;
						this.iframeheight = 50;
						this.iframewidth = 936;
						this.withCurtain = withCurtain;
						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', '100%');
						iframe.setAttribute('height', this.iframeheight + 'px');
						//iframe.setAttribute('width', this.iframewidth + 'px');
						iframe.setAttribute('width', '100%');
						iframe.setAttribute('frameBorder', '0');
						iframe.setAttribute('border', '0');
						iframe.setAttribute('marginwidth', '0');
						iframe.setAttribute('marginheight', '0');
						iframe.setAttribute('scrolling', 'auto');
//						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);";
						if (Info.browser.isIEpre7) {
							this.iframeLining = false
						}
						if(this.withCurtain === true) {
							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);
							}							
							Event.observe(window, "resize", function() {
								this.resizeCurtain();
							}.bindAsEventListener(this));
						}
						this.listener = {
							'augmentDone' :this.handleOpen.bind(this)
						};
						$(this.layerIdstring).appendChild(iframe);
					},
					beforeOpen : function() {
							if(this.targetDomChangeColor){
								this.register.setStyle({"display" :"block"});
							}
							if (!this.headerHeight)
								this.headerHeight = $('header-zone').getDimensions().height
										+ $('toolbar-zone').getDimensions().height;
							if(this.withCurtain === true) {
								this.curtain.setStyle( {
									'top' :this.headerHeight + 'px'
								});
							}
							this.node.setStyle( {
								'display' :'block'
							});
							if (!this.nodeTop){
								if(this.withCurtain == false) {
									this.nodeTop = parseInt(this.node.getStyle('top'));
								} else {
									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'
							});

							var wrapper = $('footer-position-wrapper');
							this.diff = parseInt(wrapper.getDimensions().height) - this.nodeTop
									- this.nodeHeight;
							
							if (this.diff < 0) {
								var layerOccupation = this.nodeHeight + this.nodeTop;
								layerOccupation = layerOccupation - this.headerHeight;
								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 && !Info.browser.isIEpre7){
								this.iframeLining.show();
							}
							if(this.withCurtain === true) {
								this.curtain.setStyle( {
									'display' :'block'
								});
							}
							return true;
					},
					afterOpen : function() {
						if (!this.iframeisloaded) {
							this.setIFrameLocation();
							this.iframeisloaded = true;
						} else if(this.iframehref != this.iframecurrenthref) {
							this.setIFrameLocation();
						} else {
							if(this.withCurtain === true) {
								this.correctCurtain();
							}
						}
					},
					beforeClose : function() {
						if(this.targetDomChangeColor){
							this.register.setStyle({"display" :"none"});
						}
						if(this.withCurtain === true) {
							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;
					},
					afterClose : function() {
										
						this.iframehref = this.iframeorigin;
						
						if (!this.refreshOnOpen)
							return;

						$(this.iframeidstring).src = 'about:blank';
						this.iframeisloaded = false;
						this.iframeheight = 50;
						this.iframecurrenthref = null;
					},
					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;
						this.iframecurrenthref = this.iframehref;
					},
					showIFrame: function(evt) {
						this.iframeisloaded = true;
						if ($('lightbox-preloader')) {
							$('lightbox-preloader').remove();
						}
						//this.correctCurtain();
						$(this.iframeidstring).style.visibility = 'visible';
					},
					rechunkIFrame : function(loadevt) {
						var crossevt = (window.event) ? event : loadevt
						var iframeroot = (crossevt.currentTarget) ? crossevt.currentTarget
								: crossevt.srcElement

						if (iframeroot) {
							
							var h = 400;
							if(this.withCurtain == true) {
								//h = 1000;
							}
							iframeroot.height = h;
							this.iframeheight = h;
							
							this.iframeisloaded = true;
							if ($('lightbox-preloader')) {
								$('lightbox-preloader').remove();
							}
							if(this.withCurtain === true) {
								this.correctCurtain();
							}
							$(this.iframeidstring).style.visibility = 'visible';
						}
					},
					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) > 640) {
								this.diff = -1;
								var contentZoneHeight = parseInt(this.iframeheight)
										+ this.nodeTop;
								$('content-zone').setStyle( {
									'height' :contentZoneHeight + 'px'
								});
								curtainHeight = contentZoneHeight + 44;
								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();
								}
							}
						}
					},
					deeplinkParam: function(params) {
						this.iframehref = this.iframeorigin + params;
						this.toggle();
					}
				});

var CONCERT_MYPRODUCTS_REGEX = /^#jump-to-myproducts-(.+)$/;
var CONCERT_CONFIGURE_REGEX = /^#jump-to-configure-(.+)$/;

var EXT_PRODUCT_LIGHTBOX_REL_REGEX = /^ext-product-lightbox-(.+)$/;
var NOTEPAD_LAYER_REL_REGEX = /^ext-notepad-layer-(.+)$/;
function init_ext_productlightboxLayers() {
	if ($("content-zone")) {
		$A($("content-zone").getElementsByTagName("a"))
				.each(
						function(trigger) {
							trigger = $(trigger);
							if (EXT_PRODUCT_LIGHTBOX_REL_REGEX.test(trigger.rel)) {
								var id = trigger.rel.replace(
										EXT_PRODUCT_LIGHTBOX_REL_REGEX, "$1");
								var elm = "<div id='lightbox-layer-"
										+ id
										+ "' class='product-lightbox-layer'><div class='close'><a href='javascript:void(0);'>";
								elm += "<span class='access'>Close Product 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;

								
								/**START: deeplink navigation for concert service*/
								var deeplink = false;
								var deeplinkData = '';
								if(CONCERT_CONFIGURE_REGEX.test(location.hash)) {
									var data = location.hash.replace(CONCERT_CONFIGURE_REGEX, "$1");
									deeplinkData = decodeURIComponent(data);
//									trigger.href += decodeURIComponent(data);
									deeplink = true;
								} else if (location.hash == '#jump-to-configure') {
									deeplink = true;
								}
								/**END: deeplink navigation for concert service*/
								
								var layer = new ExternalProductLightboxLayer(
										node, trigger, id, true, false);
								GLOBAL_LAYER_CONTROLLER[key] = layer;
								
								if(deeplink == true) {
									if(deeplinkData.length > 0) {
										layer.deeplinkParam(deeplinkData);
									} else {
										layer.toggle();
									}
								}
								
							} else if(NOTEPAD_LAYER_REL_REGEX.test(trigger.rel)) {
								trigger.parentNode.parentNode.parentNode.style.position = "relative";
								if (document.all) {
									trigger.parentNode.parentNode.parentNode.style.zIndex = 1;
								}
								
								var id = trigger.rel.replace(NOTEPAD_LAYER_REL_REGEX, "$1");
								
								var elm = "<div id='lightbox-layer-"
										+ id
										+ "' class='notepad-lightbox-layer'><div class='close'><a href='javascript:void(0);'>";
								elm += "<span class='access'>Close Notepad Layer</span></a></div>";
								elm += "<div class='layer-content'><div id='layer-content-"
										+ id + "'></div></div></div>";
								elm += "<div id='notepad-register-layer-" + id + "' class='notepad-register-layer'>" + 
									[
										"<a href='javascript:void(0)' class='link'>", 
											(document.all) ? trigger.innerText : trigger.textContent, 
										"</a>"
									].join("") + "</div>";
									
									
								if (document.all) {
									var myDiv = document.createElement("div");
									myDiv.innerHTML = elm;
									trigger.parentNode.parentNode.parentNode.appendChild(myDiv);
								}
								else {
									trigger.parentNode.parentNode.parentNode.insert(elm, "bottom");
								}
								
								var node = $("lightbox-layer-" + id);
								
								var key = "lightbox-layer-" + id;
								/**START: deeplink navigation for concert service*/
								var deeplink = false;
								var deeplinkData = '';
								if(CONCERT_MYPRODUCTS_REGEX.test(location.hash)) {
									var data = location.hash.replace(CONCERT_MYPRODUCTS_REGEX, "$1");
									deeplinkData = decodeURIComponent(data);
									//trigger.href += decodeURIComponent(data);
									deeplink = true;
								} else if(location.hash == '#jump-to-myproducts') {
									deeplink = true;
								}
								/**END: deeplink navigation for concert service*/
								var layer = new ExternalProductLightboxLayer(node, trigger, id, false, true);
								GLOBAL_LAYER_CONTROLLER[key] = layer;
								
								
								if(deeplink == true) {
									if(deeplinkData.length > 0) {
										layer.deeplinkParam(deeplinkData);
									} else {
										layer.toggle();
									}
								}
							}
						}.bind(this)
					);
	}
}
Event.onDOMReady( function() {
	init_ext_productlightboxLayers();
});

function goToNotepad() {
	navigateToLayer('lightbox-layer-notepad');
}

