/**

 Script used to get the logo image name, depending on
 * The country
 * The language
 * The site area

 Logo name pattern is <area>-logo-<type>-<country>-<language>.jpg 
 <area>
	home
	inside
	flash
 <type>
	pb (private banker)
	wm (wealth managers)
	am (actifs managers)
 <country>
	AE		UAE
	BE		Belgium
	BM		Bermuda
	BS		Bahamas
	CZ	CS	Czech Republic
	CH		Switzerland
	ES		Spain
	GB		UK (y c. Gibraltar, Jersey)
	NL		Netherlands
	PO	PT	Portugal
	TR	TU	Turkey
			Other
	CA		Canada
	CN	ZH	China (y c. Hong Kong, Taiwan)
	JP	JA	Japan
	SG		Singapore
	FR		France
	DE		Germany
<language>
	en
	fr
 	de
 	nl
 
*/

// ===============================================================
//
// Constants definition
//
// ===============================================================

var LOGO_AREA_HOME  = "home";
var LOGO_AREA_INSIDE  = "inside";
var LOGO_AREA_FLASH = "flash";

var LOGO_TYPE_PRIVATE_BANKERS = "pb";
var LOGO_TYPE_WEALTH_MANAGERS = "wm";
var LOGO_TYPE_ACTIF_MANAGERS  = "am";

var LOGO_FILENAME = "logo";

var LOGO_EXTENSION = ".jpg";

var IP_TO_COUNTRY_SERVLET_LOCATION = "/ip-to-country/ip";

var NB_LANGUAGES = 4; // fr, en, de, nl
var NB_COUNTRIES = 18;

// Cookie info
var COOKIE_MAX_DAYS_OK = 15;
var COOKIE_MAX_DAYS_NOT_FOUND = 1;
var COOKIE_COUNTRY_NAME = "userCountry";
var COOKIE_LANGUAGE_NAME = "userPreferredLanguage";
var COOKIE_PATH = "/";

var DEFAULT_LOGO_INDICATOR = LOGO_AREA_HOME;
var DEFAULT_LANGUAGE = "en";
var DEFAULT_COUNTRY = "CH";

var arrayType = new Array(NB_LANGUAGES);
arrayType["fr"] = new Array(NB_COUNTRIES);
arrayType["en"] = new Array(NB_COUNTRIES);
arrayType["nl"] = new Array(NB_COUNTRIES);
arrayType["de"] = new Array(NB_COUNTRIES);

// French
var currentLanguage = "fr";
arrayType[currentLanguage]["AE"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BE"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BM"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BS"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CZ"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CH"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["ES"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["GB"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["NL"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["PO"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["TR"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["others"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CA"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["CN"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["JP"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["SG"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["FR"] = LOGO_TYPE_ACTIF_MANAGERS;
arrayType[currentLanguage]["DE"] = LOGO_TYPE_PRIVATE_BANKERS;

// English
currentLanguage = "en";
arrayType[currentLanguage]["AE"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BE"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BM"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BS"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CZ"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CH"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["ES"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["GB"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["NL"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["PO"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["TR"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["others"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CA"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["CN"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["JP"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["SG"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["FR"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["DE"] = LOGO_TYPE_PRIVATE_BANKERS;

//dutch
currentLanguage = "nl";
arrayType[currentLanguage]["AE"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BE"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BM"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BS"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CZ"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CH"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["ES"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["GB"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["NL"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["PO"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["TR"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["others"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CA"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["CN"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["JP"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["SG"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["FR"] = LOGO_TYPE_WEALTH_MANAGERS;
arrayType[currentLanguage]["DE"] = LOGO_TYPE_PRIVATE_BANKERS;

//deutsch
currentLanguage = "de";
arrayType[currentLanguage]["AE"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BE"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BM"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["BS"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CZ"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CH"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["ES"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["GB"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["NL"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["PO"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["TR"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["others"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CA"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["CN"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["JP"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["SG"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["FR"] = LOGO_TYPE_PRIVATE_BANKERS;
arrayType[currentLanguage]["DE"] = LOGO_TYPE_PRIVATE_BANKERS;
// ===============================================================
//
// Cookie management
//
// ===============================================================

/**
* Writes a cookie
*
* @param	name		Name of the cookie
* @param	value		Value to set
**/
function setCookie(name, value) {
	var argv=setCookie.arguments;
	var argc=setCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=name+"="+escape(value)+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
}

// Helper function
function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	
	if (endstr==-1)
      		endstr=document.cookie.length;
      		
	return unescape(document.cookie.substring(offset, endstr));
}

/**
* Reads a cookie
*
* @param	name	Name of the cookie to read; a value of null indicates that the cookie fas not found.
**/
function getCookie(name) {
	var arg=name+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg)
                        return getCookieVal (j);
                i=document.cookie.indexOf(" ",i)+1;
                        if (i==0) break;}
	return null;
}

// ===============================================================
//
// Servlet management
//
// ===============================================================

/**
* Call the ip to country servlet to get infos on the country and prefered language
* its format should be i.e. "200 CH {fr-ch}" or "200 CH fr-FR,fr,en-US,en"
*/
function getServletResult() {
	return getServerResult(IP_TO_COUNTRY_SERVLET_LOCATION,true);
}
/**
* js caller
*/
function getServerResult(path,isText) {
	var xhr_object = null;
	
	if (window.XMLHttpRequest) {// Firefox
	   xhr_object = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {// Internet Explorer
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		// XMLHttpRequest not supportet by browser
	   return "";
	}

	try {
		xhr_object.open("GET", path, false);
	}
	catch (e) {
		return "";
	}

	xhr_object.send(null);

	if (xhr_object.readyState == 4) {
		return (isText?xhr_object.responseText:xhr_object.responseXml);
	}

	// error
	return "";
}
/**
* Parse the ip to country servlet to get the country and language
*/
function getUserAgentInfo() {
	// read the servlet to know the country and the Accept-Language in header
	var servletResult = getServletResult();

	// correct answer ?
	// long enough with at least the 200 code and the country ?
	if (servletResult.length >= 6) {
	
		if (servletResult.substr(0, 4) == "200 ") {
		
			// after the "200 " we find the country found by ip mapping. If 00 --> no match found
			var countryTemp = servletResult.substr(4, 2);
			if (countryTemp != "00") {
				country = countryTemp;
				countryObtainedFromUserAgent = false;
			}

			// remove the leading "200 " and set an array containing all the values
			var languagesString = servletResult.substring(7, servletResult.length);

			var index = 0;

			var currentLanguage;
			
			while ((languagesString.length > 0) && (country == null || preferredSupportedLanguage == null))
			{
				commaPosition = languagesString.indexOf(",");
				
				if (commaPosition == -1)
					currentLanguage = languagesString;
				else
					currentLanguage = languagesString.substr(0, commaPosition);
			
				// check if we need to find a supported language
				if (preferredSupportedLanguage == null) {
				
					var language = currentLanguage.substr(0, 2).toLowerCase();
						preferredSupportedLanguage = language;
						
					// special rule for Chinese: cz --> cs
					if (preferredSupportedLanguage == "cz")
						preferredSupportedLanguage = "cs";

					// special rule for Chinese: pt --> po
					if (preferredSupportedLanguage == "pt")
						preferredSupportedLanguage = "po";

					// special rule for Chinese: pt --> po
					if (preferredSupportedLanguage == "tr")
						preferredSupportedLanguage = "tu";

					// special rule for Chinese: ja --> jp
					if (preferredSupportedLanguage == "ja")
						preferredSupportedLanguage = "jp";
				}

				// rules for default countries
				if (country == null && preferredSupportedLanguage != null && currentLanguage.length == 2) {
				
					switch (preferredSupportedLanguage) {
						case "de":
							country = "DE";
							break;

						case "en":
							country = "US";
							break;

						case "es":
							country = "ES";
							break;

						case "fr":
							country = "FR";
							break;

						case "it":
							country = "IT";
							break;

						case "nl":
							country = "NL";
							break;

						case "jp":
							country = "JP";
							break;

						case "po":
							country = "PO";
							break;

						case "tu":
							country = "TU";
							break;

						case "ru":
							country = "RU";
							break;
					}
				}
				
				if (country == null) {
					if (currentLanguage.length == 5)
						country = currentLanguage.substr(3, 2).toUpperCase();
				}
				
				// Go to the next language
				languagesString = languagesString.substring(currentLanguage.length +1, languagesString.length);
			}
		}
	}
}

// ===============================================================
//
// Public function
//
// ===============================================================

function getLogoFileName(language, area) {
	// Get visitor's country
	var country = getCountry();
	
	// Check if le arrayType language exists
	if (arrayType[language] == null) {
		language = DEFAULT_LANGUAGE;
	}
		
	// Check if array country exists for language
	if (arrayType[language][country])
		logoType = arrayType[language][country];
	else
		// country not found --> use others
		logoType = arrayType[language]["others"];

	return area + '-' + LOGO_FILENAME + '-' + logoType + '-' + language + LOGO_EXTENSION;
}

function getLogoFileNameForHome(language) {
	return getLogoFileName(language, LOGO_AREA_HOME);
}

function getLogoFileNameForInside(language) {
	return getLogoFileName(language, LOGO_AREA_INSIDE);
}

function getLogoFileNameForFlash(language) {
	return getLogoFileName(language, LOGO_AREA_FLASH);
}

function getLanguage() {
	return preferredSupportedLanguage;
}

function getCountry() {
	return country;
}

//
// Execute
//
// Try to get the country/lang infos from the cookies
var country = getCookie(COOKIE_COUNTRY_NAME);
var preferredSupportedLanguage = getCookie(COOKIE_LANGUAGE_NAME);

var needToSetCookies = (country == null || preferredSupportedLanguage == null);
var countryObtainedFromUserAgent = needToSetCookies;
var languageObtained = false;

var cookieLifetimeOk = new Date();
cookieLifetimeOk.setTime(cookieLifetimeOk.getTime()+(COOKIE_MAX_DAYS_OK*24*3600*1000));

var cookieLifetimeNotFound = new Date();
cookieLifetimeNotFound.setTime(cookieLifetimeNotFound.getTime()+(COOKIE_MAX_DAYS_NOT_FOUND*24*3600*1000));

// Get the country/lang infos from the ip to country servlet
getUserAgentInfo();

// no language or country found ?
if (preferredSupportedLanguage == null)
	preferredSupportedLanguage = DEFAULT_LANGUAGE;
else
	languageObtained = true;
	
if (country == null)
	country = DEFAULT_COUNTRY;

// set the cookie if needed
if (needToSetCookies) {
	var cookieLifeTime = cookieLifetimeOk;

	// do we have to set a shorten lifetime ?
	if (countryObtainedFromUserAgent || !languageObtained) {
		cookieLifeTime = cookieLifetimeNotFound;
	}

	setCookie(COOKIE_LANGUAGE_NAME, preferredSupportedLanguage, cookieLifeTime, COOKIE_PATH);
	setCookie(COOKIE_COUNTRY_NAME, country, cookieLifeTime, COOKIE_PATH);
}
/* hack swfobject */
if(typeof SWFObject!="undefined"){
	var allNews,max,newsIndex=0;
	SWFObject.prototype.oldwrite = SWFObject.prototype.write;
	var nextNextNews = function(){
		var oldIndex=((newsIndex==0)?max:newsIndex)-1;
		allNews[oldIndex].set('tween',{duration: 1500,transition: Fx.Transitions.Quad.easeIn,link:"chain"}).fade('out').tween('display','none');
		nextNews.delay(1500);
	};
	var nextNews = function(){
		allNews[newsIndex++].setStyle('opacity',0).setStyle('display','block').set('tween',{duration: 1500,transition: Fx.Transitions.Quad.easeOut}).fade('in');
		(newsIndex>=max)?(newsIndex=0):0;
	};
	SWFObject.prototype.write = function(_20){
		var tmp=this.getAttribute('swf');
		if(tmp.match(/\/news(_[\d]*)?\.swf/)){
		tmp=tmp.split('.swf?');
			if(tmp.length>1){
				var ctnr=$(_20);
				ctnr.set('html','');
				var xmlDoc = getServerResult(tmp[1].split('xmlPath=')[1]+'news.xml',true);
				var newsArray = xmlDoc.match(/<tickerSummary>(([^<])|(<[^\/]))*<\/tickerSummary><urlLink>(([^<])|(<[^\/]))*<\/urlLink>/gi);
				if(newsArray){
				max=newsArray.length;
				for(var i=0;i<max;i++){
					newsArray[i]=newsArray[i].replace(/<tickerSummary>((([^<])|(<[^\/]))*)<\/tickerSummary><urlLink>((([^<])|(<[^\/]))*)<\/urlLink>/gi,'<a href="$5">$1</a>');
					newsArray[i]=newsArray[i].replace(/&lt;/gi,'<').replace(/&gt;/gi,'>').replace(/<\/?(p|(div)|(script))[^>]*>/gi,'');
				}
				this.news=newsArray;
				ctnr.set('html','<div class="newsitem">'+this.news.join('</div><div class="newsitem">')+'</div>');
				allNews = ctnr.getElements('div.newsitem');
				nextNews();
				nextNextNews.periodical(7000);
				}
				//element collection and settings
			}
		}
		else{
			return this.oldwrite(_20);
		}
	}
}
