/*	GLOBAL FOOTER JS - Include file (adobe)
	$Id: GlobalFooter.js,v 1.2 2010/09/30 17:49:00 qare Exp $
*/

/*	GLOBAL FOOTER JS - Main code
	$Id: globalfooter.js.ssi,v 1.12 2011/11/22 22:10:39 dgasior Exp $
*/

var hideEvidon = false;

Event.observe(window, 'load', function() {
	var currURL = window.location.pathname;
	var countryCode = adobe.Cookie.get("international");
	
	if (currURL.startsWith('/cfusion/store')) {
		$('sfRegionSet').hide();
		$('sfRegion').hide();
	}
	else {
		if (countryCode) {
			$('sfRegionSet').show();
			$('sfRegion').hide();
		}
	}

	var rp = $('RegionPanel');
	if ($('SiteHeader') != null) {
		var wp = $('WelcomePanel');
	}
		
	Event.observe(document, 'click', function(event) {
		var el = event.element();
		var elid = el.id;

		if (elid == "sfRegion" || elid == "sfRegionChange" || elid == "sfRegionClose") {
			$('RegionPanel').toggle();
			if ($('SiteHeader') != null) {
				$('WelcomePanel', 'WelcomePanelShadow').invoke('hide');
			}
			Event.stop(event);
		}
		else if ((el == rp) || (el.descendantOf(rp))) {
			if (!el.descendantOf(rp)) {
				Event.stop(event);
			}
		}
		else if ((el == wp) || (el.descendantOf(wp)) || elid == "shWelcome" || elid == "screenName" || elid == "shProducts" || elid == "shIndustries" || elid == "shLearning" || elid == "shHelp" || elid == "shDownloads" || elid == "shStore" || elid == "shCompany") {
		}
		else {
			$('RegionPanel').hide();
			if ($('SiteHeader') != null) {
				$('WelcomePanel', 'WelcomePanelShadow').invoke('hide');				
			}
		}
	});
	
	/* Evidon ad icon JS */
	if (($('_bapw-link') != null) && (hideEvidon != true)) {
		$('_bapw-link').show();

		(function () {
			var adobe_host = window.location.hostname;
			if ((adobe_host == "www.adobe.com") || (adobe_host == "adobe.com") || (adobe_host == "get.adobe.com") || (adobe_host == "kb2.adobe.com") || (adobe_host == "community.adobe.com")) {
				page_id = "86";
			} else {
				page_id = "126";
			}
			
			var d = document,
				pixel,
				URL_SCHEME = (d.location.protocol == 'https:' ? 'https' : 'http'),
				CDN_URL = (URL_SCHEME == 'https' ? 'https://info.evidon.com/c/betrad/pub/' : 'http://cdn.betrad.com/pub/');
		
			d.getElementById('_bapw-link').onclick = function () {
				var link = this;
		
				function appendScript(url, callback) {
					var head = d.getElementsByTagName('head')[0] || d.documentElement,
						loaded = false,
						script = d.createElement('script');
		
					function onload() {
						script.onload = script.onreadystatechange = null;
						head.removeChild(script);
		
						callback();
					}
		
					script.src = url;
					script.onreadystatechange = function () {
						if (!loaded && (this.readyState == 'loaded' || this.readyState == 'complete')) {
							loaded = true;
							onload();
						}
					};
					script.onload = onload;
		
					head.insertBefore(script, head.firstChild);
				}
		
				this.onclick = 'return false';
		
				appendScript(URL_SCHEME+'://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', function () {
					appendScript(CDN_URL+'pub1.js', function () {
						BAPW.i(link, { pid: page_id, ocid: 414 }, false);
					});
				});
		
				return false;
			};
		
			pixel = d.createElement('img');
			pixel.src = URL_SCHEME+'://l.betrad.com/pub/p.gif?pid='+page_id+'&ocid='+'414'+'&ii=1&r='+Math.random();
			pixel.height = '1';
			pixel.width = '1';
			pixel.className = 'SiteFooterEvidonPixel';
			d.body.appendChild(pixel);
		}());
	}
});

/*	GLOBAL FOOTER JS - Region Panel change code (adobe)
	$Id: changeregion.js.ssi,v 1.6 2011/12/01 22:11:59 dgasior Exp $
	
	This version is used for the majority of the site, excluding the homepage, older
	template pages, the HTML stores, and the Partner Portal.
*/

function changeRegion(regioncode) {
	if (regioncode.startsWith('be_')) {
		adobe.Cookie.set('international', regioncode, 365, '/', '.adobe.com');
		adobe.Cookie.set('storeregion', 'be', 365, '/', '.adobe.com');
	}
	else if (regioncode.startsWith('ca')) {
		adobe.Cookie.set('international', regioncode, 365, '/', '.adobe.com');
		adobe.Cookie.set('storeregion', 'ca', 365, '/', '.adobe.com');
	}
	else if (regioncode.startsWith('eeur')) {
		adobe.Cookie.set('international', regioncode, 365, '/', '.adobe.com');
		adobe.Cookie.set('storeregion', 'eu', 365, '/', '.adobe.com');
	}
	else if (regioncode.startsWith('hk_')) {
		adobe.Cookie.set('international', regioncode, 365, '/', '.adobe.com');
		adobe.Cookie.set('storeregion', 'cn', 365, '/', '.adobe.com');
	}
	else if (regioncode.startsWith('lu_')) {
		adobe.Cookie.set('international', regioncode, 365, '/', '.adobe.com');
		adobe.Cookie.set('storeregion', 'lu', 365, '/', '.adobe.com');
	}
	else if (regioncode.startsWith('uk')) {
		adobe.Cookie.set('international', regioncode, 365, '/', '.adobe.com');
		adobe.Cookie.set('storeregion', 'gb', 365, '/', '.adobe.com');
	}
	else {
		adobe.Cookie.set('international', regioncode, 365, '/', '.adobe.com');
		adobe.Cookie.set('storeregion', regioncode, 365, '/', '.adobe.com');
	}
	
	var currURL = window.location.pathname;
	var currPath = currURL;
	var geoArray = ["africa","ap","at","au","be_en","be_fr","be_nl","bg","br","ca","ca_fr","ch_de","ch_fr","ch_it","cn","cz","de","dk","eeurope","ee","es","fi","fr","hk_en","hk_zh","hr","hu","ie","il_en","in","it","jp","kr","la","lt","lu_de","lu_en","lu_fr","lv","mena_en","mena_fr","mx","nl","no","nz","pl","pt","ro","rs","ru","si","se","sea","sk","tr","tw","ua","uk"];

	geoArray.each(function(item) {
		if (currURL.startsWith('/'+item+'/')) {
			currPath = currURL.replace('/'+item+'/','/');
			throw $break;
		}
	});
	
	if ((currPath.startsWith('/cfusion')) && (regioncode == 'us')) {
		newURL = "/";
		homeURL = "/";
	}
	else if ((currPath.startsWith('/cfusion')) && (regioncode != 'us')) {
		newURL = "/" + regioncode + "/";
		homeURL = "/" + regioncode + "/";
	}
	else if (regioncode != 'us') {
		newURL = "/" + regioncode + currPath;
		homeURL = "/" + regioncode + "/";
	} 
	else {
		newURL = currPath;
		homeURL = "/";
	}
	
	new Ajax.Request(newURL, {
		method: 'get',
		onSuccess: function(response) {
			window.location = newURL;
		},
		onFailure: function(response) {
			window.location = homeURL;
		}
	});
}

