
if(PAGE_LANG == undefined) {
	var url = window.location.href;
	var matches = url.match(/\/(de|en)\//);
	if (matches) {
		var PAGE_LANG = matches[1];	
	} else {
		var PAGE_LANG = "en";
	}
}

var wloc = window.location.host;
//temporarily switch to renamed site ID due to roll out compat reasons
var __siteIdFile = '/hq/' + PAGE_LANG + '/siteidlayer.htm';

if(wloc == 'edit.siemens.dev.publicis.de') {
  __siteIdFile = '/ecc/corp/root/' + PAGE_LANG + '/siteidlayer.htm';
} else if (wloc == 'stage.energy.siemens.com') {
	  __siteIdFile = '/ecc/corp/' + PAGE_LANG + '/siteidlayer.htm';
} else if (wloc == 'www.energy.siemens.com' || wloc == 'origin.energy.siemens.com' ) {
  __siteIdFile = '/hq/' + PAGE_LANG + '/siteidlayer.htm';
}

    
function init_siteIdLayer() {
    if(Info.browser.isIEpre6) { return; }
    if ($("site-id-layer")) {
        trigger = $($("site-id").getElementsByTagName("a")[0]);
        
        new ExtendedSiteIdLayer($("site-id-layer"), trigger);

        //initalize list element hovers
        $$("#site-id-layer a[lang]").each( function(e) {
            Event.observe(e, "mouseover", function() {
                $(e).up("li", 0).addClassName("hover");
            });
            Event.observe(e, "mouseout", function() {
                $(e).up("li", 0).removeClassName("hover");
            });
        });
        
        //replace href attribute to avoid tracking
        $("site-id-trigger").href = "javascript:void(0);";      

        //workaround for missing adjacent sibling combinator ("ul.countries li a.c:hover + a" and "ul.countries li a.worldwide:hover + a") support in IE6 and Safari
        if(Info.browser.isIEpre7 || Info.browser.isSafari) {
            $$("#site-id-layer a.c", "#site-id-layer a.worldwide").each( function(e) {
                Event.observe(e, "mouseover", function() {
                    $(e).next("a").addClassName("hover");
                });
                Event.observe(e, "mouseout", function() {
                    $(e).next("a").removeClassName("hover");
                });
            });
        }
    }
}

/********************************************************************/
/* START: ExtendedSiteIdLayer Class                                 */

var ExtendedSiteIdLayer = Class.create();
ExtendedSiteIdLayer.prototype = Object.extend(new Layer, {
    initialize: function(node, trigger) {
        this.initSuper(node, trigger);
        var closeButton = Helper.getCloseButton(this.node);
        closeButton.observe("click", function(){this.close();}.bindAsEventListener(this));
        
        this.siteIdCont = '';
        this.siteIdContReady = false;
        this.siteIdContLoading = false;
    },
    afterClose: function(newLayer) {
        if (!Layer.toggle) {
            HeaderAnimation.augment();
        }
    },
    afterOpen: function() {
        HeaderAnimation.diminish();
    },
    beforeClose: function() {
        HeaderAnimation.unregisterLayer();
        return true;
    },
    beforeOpen: function() {
        HeaderAnimation.registerLayer(this);
        if( this.siteIdContLoading == false && this.siteIdContReady == false ) {
            this.siteIdContLoading = true;
            this.getSiteIdContent(__siteIdFile);
        }
        return true;
    },
    correctIframe: function() {
        this.iframeLining.correction.left = -1;
        this.iframeLining.correction.top = -1;
    },
    hide: function() {
        $("site-id-wrapper").removeClassName("active");
		//fix IE6 <select> Bug
		if(Info.browser.isIEpre7) {	$$('select').each( function(elm) { elm.show(); } );	}
    },
    getSiteIdContent: function(url) {
        if( this.siteIdContReady == false ) {
            new Ajax.Request(url,
                  {
                    method:'get',
                    onComplete: this.onresult.bind(this),
                    onFailure: function(oXHR) {
                        alert("Error: " + oXHR.status);
                    }  
            });
        }
    },
    onresult: function(xhr) {
        //ok... here we got no xhr.responseXML, only xhr.responseText
        var tmpStr  = xhr.responseText.substring( xhr.responseText.indexOf('<div class="toolbar-content clearfix">')+38 );
        tmpStr      = tmpStr.substring ( 0, tmpStr.indexOf('<div id="site-id-limiter-end">')  ); //+30
        $('site-id-layer-content').update(tmpStr);
        this.siteIdContReady = true;
        this.siteIdContLoading = false;
        
		//fix IE6 <select> Bug
		if(Info.browser.isIEpre7) {	$$('select').each( function(elm) { elm.hide(); } );	}       
    },
    checkSiteIdStatus: function () {
        alert("checking");
        return this.siteIdContReady;
    },
    show: function() {
        $("site-id-wrapper").addClassName("active");
        this.trigger.addClassName("clicked"); // avoids hover effect (only for the first time)
        this.trigger.observe("mouseout",
            function(e) {
                this.trigger.removeClassName("clicked");
                this.trigger.stopObserving("mouseout");
            }.bindAsEventListener(this)
        );
    }
});

/* END: ExtendedSiteIdLayer                                         */
/********************************************************************/