/*
 * 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 wloc = window.location.host;
//temporarily switch to renamed site ID due to roll out compat reasons
//var __siteIdFile = '/corp/' + PAGE_LANG + '/siteid/noscript-site-id.php';
var __siteIdFile = '/corp/' + PAGE_LANG + '/siteid/noscript-site-id-' + PAGE_LANG + '.php';

if(wloc == 'edit.siemens.dev.publicis.de') {
    __siteIdFile = '/cc/corp_nwa/root/' + PAGE_LANG + '/siteid/noscript-site-id.php';
} else if (wloc == 'stage.siemens.com') {
	//temporarily switch to renamed site ID due to roll out compat reasons	
    //__siteIdFile = '/cc/corp/nwa/' + PAGE_LANG + '/siteid/noscript-site-id.php';
	__siteIdFile = '/cc/corp/nwa/' + PAGE_LANG + '/siteid/noscript-site-id-' + PAGE_LANG + '.php';
} else if (wloc == 'backstage.siemens.com') {
    __siteIdFile = 'https://stage.siemens.com/cc/corp/nwa/' + PAGE_LANG + '/siteid/noscript-site-id.php';
}

document.observe('dom:loaded', 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);
	}
}

/********************************************************************/
/* 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('<!--site-id-content-end-->')  );
		$('site-id-layer-content').update(tmpStr);
		
		/**
		 * initialize site ID links (originates from script.js, init_siteIdLayer())
		 */
		var links = $("site-id-layer").select("div.toolbar-content a");
		//initalize list element hovers
		links.filter(function(link) {
			return !$(link).hasClassName('c');
		}).each(function(link) {
			
			link.observe("mouseover", function() {
				
				$(link).up("li").addClassName("hover");
			});
			link.observe("mouseout", function() {
				$(link).up("li").removeClassName("hover");
			});
		});

		// 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) {
			links.filter(function(link) {
				return $(link).hasClassName("c") || $(link).hasClassName("worldwide");
			}).each(function(link) {
				link.observe("mouseover", function() {
					$(link).next("a").addClassName("hover");
				});
				link.observe("mouseout", function() {
					$(link).next("a").removeClassName("hover");
				});
			});
		}
		
		/**
		 * end initialize site ID links (originates from script.js, init_siteIdLayer(), line 271)
		 */	
		
		
		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                                         */
/********************************************************************/