/*
 * 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>
 */

// window location
var wloc = window.location.host;

//__siteIdFile keeps location of the site id file, is set dependent on script environment (window location)
var __siteIdFile = '';

// get page language (PAGE_LANG) via URL if the language variable does not exist
if(PAGE_LANG == undefined) {
    var url = window.location.href;
    var matches = url.match(/\/(de|en)\//);
    var PAGE_LANG = (matches) ? matches[1] : 'en';
}

// set PAGE_COUNTRY to correct country codes
if( PAGE_COUNTRY == "uk")	PAGE_COUNTRY = "gb";
if( PAGE_COUNTRY == "cee" && PAGE_LANG == "de")  PAGE_COUNTRY = "at";
var temp_PAGE_LANG = PAGE_LANG; //save the original value of PAGE_LANG
if( PAGE_COUNTRY == "cee" && PAGE_LANG != "de")	 {
	PAGE_COUNTRY = PAGE_LANG;	//set PAGE_COUNTRY to PAGE_LANG for CEE-Pages
	if( PAGE_COUNTRY == "bs") PAGE_COUNTRY = "ba";	//fix for Bosnia and Herzegovina
	PAGE_LANG = "en"; 			//show EN site-id-layer if PAGE_LANG is not DE
}
if( PAGE_COUNTRY == "az") PAGE_LANG = "en";	//show EN site-id-layer
if( PAGE_COUNTRY == "cz") PAGE_LANG = "en";	//show EN site-id-layer
if( PAGE_COUNTRY == "hu") PAGE_LANG = "en";	//show EN site-id-layer
if( PAGE_COUNTRY == "il") PAGE_LANG = "en";	//show EN site-id-layer
if( PAGE_COUNTRY == "tr") PAGE_LANG = "en";	//show EN site-id-layer
if( PAGE_COUNTRY == "ua") PAGE_LANG = "en";	//show EN site-id-layer


// create query string if PAGE_COUNTRY is set 
var country = (PAGE_COUNTRY) ? '?country='+PAGE_COUNTRY : '';



// change path to the site id file for different script environments 
switch (wloc) {
	// #### staging/dev: dev ####
	case 'edit.siemens.dev.publicis.de':
		__siteIdFile = '/cc/corp_nwa/root/' + PAGE_LANG + '/siteid/noscript-site-id.php' + country;
		break;

	// #### staging/dev: stage ####
	case 'stage.siemens.com':
		__siteIdFile = '/cc/corp/nwa/' + PAGE_LANG + '/siteid/noscript-site-id.php' + country;	
		break;
		
	// #### staging/dev: backstage server ####
	case 'backstage.siemens.com':
		__siteIdFile = 'https://backstage.siemens.com/cc/corp/nwa/' + PAGE_LANG + '/siteid/noscript-site-id.php' + country;
		break;
	
	// #### production: energy server ####
	case 'www.energy.siemens.com':
		__siteIdFile = '/' + PAGE_COUNTRY + '/' + PAGE_LANG + '/siteidlayer.htm';
		break;
		
	// #### production: origin energy server ####
	case 'origin.energy.siemens.com':
		__siteIdFile = '/' + PAGE_COUNTRY + '/' + PAGE_LANG + '/siteidlayer.htm';
		break;
	
	// #### production: usa server ####
	case 'www.usa.siemens.com':
		__siteIdFile = '/en/noscript-site-id-en.php' + country; 
		break;

	// #### production: co.uk server ####
	case 'www.siemens.co.uk':
		__siteIdFile = '/en/siteid/noscript-site-id.php' + country;
		break;
	
	// #### production: path defaults to w1/www config ####
	default:
		__siteIdFile = '/corp/' + PAGE_LANG + '/siteid/noscript-site-id.php' + country;
		break;
}

//"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));
		
		trigger.href = "javascript:void(0);";
		
		this.siteIdCont = '';
		this.siteIdContReady = false;
		this.siteIdContLoading = false;
	},
	afterClose: function(newLayer) {
		if (!Layer.toggle) {
			HeaderAnimation.augment();
		}
	},
	afterOpen: function() {
		HeaderAnimation.diminish();	
	},
	beforeClose: function() {
		HeaderAnimation.unregisterLayer();
		$$('select').each( function(elm) { elm.show(); } );
		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
		if (wloc == 'www.energy.siemens.com' || wloc == 'origin.energy.siemens.com' ) {
		    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">')  );
		} else {
		    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;
		if(Info.browser.isIEpre7) {
		    this.iframeLining.refresh(); //why does this set zIndex to 1 ?
		    this.iframeLining.div.setStyle({ zIndex : '0' }); //doesn't help
		    $$('select').each( function(elm) { elm.hide(); } );
		} else if(window.devicePixelRatio) { //fix for Safari
		    $$('ul.countries li.active').each( function(el) {
		        $(el).setStyle({ 'width':'260px'});
		        $(el).up().next('ul').setStyle({ 'marginLeft':'64px' });
		    });
		}
	},
	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)
		);
	}
});

PAGE_LANG = temp_PAGE_LANG; //set PAGE_LANG back to it's original value

/* END: ExtendedSiteIdLayer                                         */
/********************************************************************/
