/*
 * special siteexplorer2 layer class
 */

var SiteExplorer2Layer = Class.create();
SiteExplorer2Layer.prototype = Object.extend(new Layer, {
    initialize: function(node, trigger, idstring) {
	
        this.idstring = idstring;
        this.layerIdstring = "toolbar-layer-content-" + idstring;
        this.node = node;
        
        // check if node exists
        if(!node && !$('lightbox-layer-'+idstring))
        {
        	var elm =	"<div id='toolbar-layer-"+idstring+"' class='toolbar-layer'>" +
        					"<div class='toolbar-content clearfix' style='padding:0pt;'>" +
        						"<div class='close'>" +
        							"<a href='javascript:void(0);'>" +
        								"<span class='access'>Close Siteexplorer Layer</span>" +
        							"</a>" +
        						"</div>" +
        						"<div id='toolbar-layer-content-"+idstring+"'></div>" +
        					"</div>" +
        				"</div>";
			
			new Insertion.Before($('footer-position-placeholder'), elm);
			node = $("toolbar-layer-"+idstring);
        }
		else if($('lightbox-layer-'+idstring))
		{
			node = $('lightbox-layer-'+idstring);
		}
        
        // add iframe
        
        this.iframehref = trigger.href;
        this.iframeidstring = "iframe-" + idstring;
        this.iframeisloaded = false;
        this.iframeheight = 50;

        if( !$(this.layerIdstring).down('iframe') ) {
	        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.layerIdstring).appendChild(iframe);
        }

        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();
//        }

    },
    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;
        }
        HeaderAnimation.diminish();
    },
    /*
     * 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';
    },*/
    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;
    	
    	// TO DO
        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 = "none";
                iframeroot.contentWindow.document.body.style.backgroundColor = '#e7e8eb';
                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('content-zone')) {
                    var contZone = $(iframeroot.contentWindow.document
                            .getElementById('content-zone'));
                    contZone.style.backgroundColor = '#e7e8eb';
                    if( contZone.down('.right-content'))contZone.down('.right-content').style.display = 'none';
                }
                
                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 = "none";
                iframeroot.contentDocument.document.body.style.backgroundColor = '#e7e8eb';
                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('content-zone')) {
                    iframeroot.contentDocument.document
                            .getElementById('content-zone').style.marginLeft = '0px';
                }
                if (iframeroot.contentDocument.document
                        .getElementById('content-zone')) {
                    var contZone = $(iframeroot.contentDocument.document
                            .getElementById('content-zone'));
                    contZone.style.backgroundColor = '#e7e8eb';
                    if( contZone.down('.right-content'))contZone.down('.right-content').style.display = 'none';
                }
                
                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.iframeidstring).style.visibility = 'visible';
        }
    }
});


/* END:*/


function init_siteExplorer2() {
	if ($("sitemap-link")) {
		id = 'siteexplorer';
		trigger = $($("sitemap-link").getElementsByTagName("a")[0]);
		GLOBAL_LAYER_CONTROLLER[id] = new SiteExplorer2Layer(null, trigger, id);
	}
}

function init_socialMediaLayer() {
	if ($("socialmedia-layer")) {
		id = 'socialmedia';
		trigger = $($("socialmedia-layer").getElementsByTagName("a")[0]);
		GLOBAL_LAYER_CONTROLLER[id] = new SiteExplorer2Layer(null, trigger, id);
	}
}

//init
document.observe('dom:loaded', function() {
	init_siteExplorer2();
	init_socialMediaLayer();
});


// overwrites the same function in vi/js/compiled/script.js to prevent the social media layer from opening
function init_contentLayers() {
	if ($("toolbar-nav")) {
		$A($("toolbar-nav").getElementsByTagName("a")).each(
			function(trigger) {
				trigger = $(trigger);
				if(trigger.readAttribute('rel') != 'ext-socialmedia-layer-socialmedia')
				{
					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)) { //Wellhoefer 10/2008
						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)
		);
	}
}
