/*	GLOBAL NAV AND FOOTER JS (Older template usage)
	$Id: globalnav.js,v 1.84.2.1 2012/01/16 23:40:18 dgasior Exp $
*/

var hideEvidon = false;

Event.observe(window, 'load', function() {
	if ($('SiteHeader')) {
		var wp = $('WelcomePanel');
		var screenName = $('screenName');
		if(screenName) {
			var screenNameValue = adobe.Cookie.get("SCREENNAME"),
			authenticAdobeId = adobe.Cookie.get('AUID'),
			WCDServerGUID = adobe.Cookie.get('WCDServer'),
			rememberMe = adobe.Cookie.get('RMID');
			
			if(screenNameValue) {
				screenName.innerHTML = screenNameValue;
			}
			
			if ((screenNameValue) && ((authenticAdobeId) || (rememberMe) || (WCDServerGUID))) {
				$('shWelcome').show();
				$('shSignInBlock').hide();
			}
			else {
				$('shWelcome').hide();
				$('shSignInBlock').show();
			}
		}
		
		if ($('search-input')) {
			adobe.InputTitleOverlay.init("search-input");
		}	
	}	
	
	if ($('SiteFooter')) {
		var rp = $('RegionPanel');
		var countryCode = adobe.Cookie.get("international");
		if (countryCode) {
			$('sfRegionSet').show();
			$('sfRegion').hide();
		}
	}
	
	if (($('SiteHeader')) && ($('SiteFooter'))) {
		Event.observe(document, 'click', function(event) {
			var el = event.element();
			var elid = el.id;
					
			if (elid == "shProducts" || elid == "shSolutions" || elid == "shLearning" || elid == "shHelp" || elid == "shDownloads" || elid == "shStore" || elid == "shCompany") {
				$(elid).addClassName('SiteHeaderBarItemActive');
			}
			else if (elid == "shWelcome" || elid == "screenName") {
				var wpHeight = $('WelcomePanel').getHeight();
				$('WelcomePanelShadow').style.height = (wpHeight + 2) + 'px';
				$('WelcomePanelShadow', 'WelcomePanel').invoke('toggle');
				if ($('SiteFooter')) {
					$('RegionPanel').hide();
				}
				Event.stop(event);
			}
			else if ((el == wp) || (el.descendantOf(wp))) {
				if (!el.descendantOf(wp)) {
					Event.stop(event);
				}
			}
			else if (elid == "sfRegion" || elid == "sfRegionChange" || elid == "sfRegionClose") {
				$('RegionPanel').toggle();
				if ($('SiteHeader')) {
					$('WelcomePanel', 'WelcomePanelShadow').invoke('hide');
				}
				Event.stop(event);
			}
			else if ((el == rp) || (el.descendantOf(rp))) {
				if (!el.descendantOf(rp)) {
					Event.stop(event);
				}
			}
			else {
				if ($('SiteHeader')) {
					$('WelcomePanel', 'WelcomePanelShadow').invoke('hide');
				}
				if ($('SiteFooter')) {
					$('RegionPanel').hide();
				}
			}
		});
	}

	if ($('shStore')) {
		Event.observe('shStore', 'mouseover', function() { 
			$('WelcomePanel', 'WelcomePanelShadow').invoke('hide');
			if ($('SiteFooter')) {
				$('RegionPanel').hide();
			}
		});
	}
	
	if ($('shProducts')) {
		Event.observe('shProducts', 'mouseover', function() { 
			$('WelcomePanel', 'WelcomePanelShadow').invoke('hide');
			if ($('SiteFooter')) {
				$('RegionPanel').hide();
			}
		});
	}
	
	if ($('shSolutions')) {
		Event.observe('shSolutions', 'mouseover', function() { 
			$('WelcomePanel', 'WelcomePanelShadow').invoke('hide');
			if ($('SiteFooter') != null) {
				$('RegionPanel').hide();
			}
		});
	}
	
	if ($('shInfo')) {
		Event.observe('shInfo', 'mouseover', function() { 
			var ipHeight = $('InfoPanel').getHeight();
			$('InfoPanelShadow').style.height = (ipHeight + 2) + 'px';
			$('InfoPanelShadow', 'InfoPanel').invoke('show');
			if ($('SiteFooter')) {
				$('RegionPanel').hide();
			}
		});
		Event.observe('shInfo', 'mouseout', function() { 
			$('InfoPanelShadow', 'InfoPanel').invoke('hide');
		});
	}
	
	if ($('shProducts1')) {
		Event.observe('shProducts1', 'mouseover', function() { 
			$('shProducts1').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shProducts1', 'mouseout', function() { 
			$('shProducts1').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shProducts2')) {
		Event.observe('shProducts2', 'mouseover', function() { 
			$('shProducts2').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shProducts2', 'mouseout', function() { 
			$('shProducts2').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shProducts3')) {
		Event.observe('shProducts3', 'mouseover', function() { 
			$('shProducts3').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shProducts3', 'mouseout', function() { 
			$('shProducts3').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shProducts4')) {
		Event.observe('shProducts4', 'mouseover', function() { 
			$('shProducts4').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shProducts4', 'mouseout', function() { 
			$('shProducts4').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shProducts5')) {
		Event.observe('shProducts5', 'mouseover', function() { 
			$('shProducts5').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shProducts5', 'mouseout', function() { 
			$('shProducts5').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shProducts6')) {
		Event.observe('shProducts6', 'mouseover', function() { 
			$('shProducts6').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shProducts6', 'mouseout', function() { 
			$('shProducts6').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shProducts7')) {
		Event.observe('shProducts7', 'mouseover', function() { 
			$('shProducts7').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shProducts7', 'mouseout', function() { 
			$('shProducts7').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shSolutions1')) {
		Event.observe('shSolutions1', 'mouseover', function() { 
			$('shSolutions1').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shSolutions1', 'mouseout', function() { 
			$('shSolutions1').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shSolutions2')) {
		Event.observe('shSolutions2', 'mouseover', function() { 
			$('shSolutions2').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shSolutions2', 'mouseout', function() { 
			$('shSolutions2').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shSolutions3')) {
		Event.observe('shSolutions3', 'mouseover', function() { 
			$('shSolutions3').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shSolutions3', 'mouseout', function() { 
			$('shSolutions3').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shSolutions4')) {
		Event.observe('shSolutions4', 'mouseover', function() { 
			$('shSolutions4').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shSolutions4', 'mouseout', function() { 
			$('shSolutions4').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shSolutions5')) {
		Event.observe('shSolutions5', 'mouseover', function() { 
			$('shSolutions5').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shSolutions5', 'mouseout', function() { 
			$('shSolutions5').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shSolutions7')) {
		Event.observe('shSolutions7', 'mouseover', function() { 
			$('shSolutions7').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shSolutions7', 'mouseout', function() { 
			$('shSolutions7').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shStore1')) {
		Event.observe('shStore1', 'mouseover', function() { 
			$('shStore1').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shStore1', 'mouseout', function() { 
			$('shStore1').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shStore2')) {
		Event.observe('shStore2', 'mouseover', function() { 
			$('shStore2').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shStore2', 'mouseout', function() { 
			$('shStore2').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shStore3')) {
		Event.observe('shStore3', 'mouseover', function() { 
			$('shStore3').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shStore3', 'mouseout', function() { 
			$('shStore3').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shStore4')) {
		Event.observe('shStore4', 'mouseover', function() { 
			$('shStore4').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shStore4', 'mouseout', function() { 
			$('shStore4').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shMyAccount')) {
		Event.observe('shMyAccount', 'mouseover', function() { 
			$('shMyAccount').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shMyAccount', 'mouseout', function() { 
			$('shMyAccount').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shMyOrders2')) {
		Event.observe('shMyOrders2', 'mouseover', function() { 
			$('shMyOrders2').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shMyOrders2', 'mouseout', function() { 
			$('shMyOrders2').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shMyInformation')) {
		Event.observe('shMyInformation', 'mouseover', function() { 
			$('shMyInformation').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shMyInformation', 'mouseout', function() { 
			$('shMyInformation').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shMyPreferences')) {
		Event.observe('shMyPreferences', 'mouseover', function() { 
			$('shMyPreferences').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shMyPreferences', 'mouseout', function() { 
			$('shMyPreferences').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	if ($('shSignOut')) {
		Event.observe('shSignOut', 'mouseover', function() { 
			$('shSignOut').addClassName('SiteHeaderPanelLinkHover');
		});
		Event.observe('shSignOut', 'mouseout', function() { 
			$('shSignOut').removeClassName('SiteHeaderPanelLinkHover');
		});
	}
	
	/* 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")) {
				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);
		}());
	}
});

if (!window.XMLHttpRequest) {
	Event.observe(window, 'load', function() {
		if ($$('div.SiteHeaderDropdownLink')) {
			$$('div.SiteHeaderDropdownLink').each( function(e) {
				Event.observe(e, 'mouseenter', function() {
					Element.addClassName(e, 'hover');
					$$('select').each( function(st) {
						st.setStyle({visibility: 'hidden'});
					});
				});
				Event.observe(e, 'mouseleave', function() {
					Element.removeClassName(e, 'hover');
					$$('select').each( function(st) {
						st.setStyle({visibility: 'visible'});
					});
				});
			});
		}
	});
}

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;
		}
	});
}
