/*
 * Note: Include this script _after_ /vi/js/script.js 
 * Requires: file named "site-id-layer.htm" in same directory as the page using this JS (configurable)
 * Requires: following HTML Code in the "site-id-zone":
        <div id="site-id-wrapper">
          <p id="site-id">
            <a id="site-id-trigger" href="/corp/apps/siteid/noscript-site-id_en.php" style="background-image: url(../../../entry/cc/framework/vi/img/tb-c-site-id-global.gif); width: 193px;">
              <strong class="access">Siemens Worldwide</strong>
            </a>
          </p>
          <div id="site-id-layer">
            <div class="close">
              <a href="javascript:void(0);">
                <span class="access">Close Site Id Layer</span>
              </a>
            </div>
            <div id="site-id-layer-content" class="toolbar-content clearfix">
              <img src="../../../entry/cc/framework/pub/img/ajax-loader-grey.gif" />
            </div>
          </div>
        </div>
 */
//var __siteIdFile = ROOT_LANG_PATH + 'siteid/noscript-site-id.php';
// use absolute link for CC
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 __siteIdFile = '/corp/' + PAGE_LANG + '/siteid/noscript-site-id.php';
var currentSite = window.location.protocol+'//'+window.location.host;
if(currentSite == 'https://stage.siemens.com')
    __siteIdFile = '/cc/corp/nwa/' + PAGE_LANG + '/siteid/noscript-site-id.php';
    

Event.onDOMReady(function() {
    //init_siteIdLayer(); //is done in script.js
});

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");
    },
    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-explorer">')  ); //+24
        $('site-id-layer-content').update(tmpStr);
        this.siteIdContReady = true;
        this.siteIdContLoading = false;
    },
    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                                         */
/********************************************************************/