window.addEvent('load', function() {
	try {
		$A(document.getElementsByTagName('a')).filter(function(item) { return item.href.toLowerCase().indexOf('.pdf') > 0; }).each(function(pdf){pdf.target="_blank";});
	} catch (e) {}
});

GoldmanSachs.AudienceSelector = new Class({
	options : { },
	initialize : function(options) {
		this.setOptions(options||{});
		
		var _self = this;
		
		new Ajax(options.url, {
			method : 'get', 
			evalScripts : true, 
			update : this.options.update,
			onComplete : function() {
				var mo = new GoldmanSachs.ModalOverlay($(_self.options.overlayTrigger), $(_self.options.overlayTarget));
				
				mo.addEvent('doShow', _self.doShow.bind(_self));
				mo.addEvent('doHide', _self.doHide.bind(_self));
				
				mo.doShow(null);
			}
		}).request();
	},
	getCookie : function () { return Cookie.get('audience'); },
	setCookie : function (id) { Cookie.set('audience', id, {path:'/'}); },
	doShow : function() { 
		var cookie = this.getCookie();
		
		if (!cookie || (!this.isApp() && this.requiresRedirect(cookie))) {
			return;
		} else {
			throw "no redirect needed";						
		}	
	},
	doHide : function(event) { 
		if (event && event.target) { 
			var id = event.target.id;
			if (id) { 
				if (id.indexOf('http')>=0) {
					this.setCookie("none");
					this.redirect(id);
					throw "redirecting";	
				} else {
					var cookie = this.setCookie(id);
					if (this.isApp() || this.requiresRedirect(id)) {
						this.redirect(id);
						throw "redirecting";
					}
				}
			} 
		} 
	},
	requiresRedirect : function(id) {
		var url = window.location.href;
		
		if (id == "advUS") {
			return url.indexOf('/gsam/advisors') < 0; 
		} else if (id == "indUS") {
			return url.indexOf('/gsam/individuals') < 0;
		} else {
			return true;
		}
	},
	isApp : function() {
		return window.location.href.indexOf("/gsam/app") > 0;
	},
	redirect : function(id) {
		var url = window.location.href;
		
		if (id == "advUS") {
			window.location = url.replace('gsam/individuals', 'gsam/advisors');
		} else if (id == "indUS") {
			window.location = url.replace('gsam/advisors', 'gsam/individuals');
		} else {
			window.location = id;
		}
	}
});
GoldmanSachs.AudienceSelector.implement(new Options);

function calcHeight(frameName)
{
  //find the height of the internal page
  var the_height=document.getElementById(frameName).contentWindow.document.body.scrollHeight;
  //change the height of the iframe
  document.getElementById(frameName).height=the_height;
}

function setDefaultView(audience)
{
    var expiredays = "91";
    var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
    document.cookie="audience=" +escape(audience)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + "; path=/";
	location.reload(true);
}

function setSiteView(audience)
{
	var exdate = new Date();
	exdate.setTime(exdate.getTime()+ 1000*60*30);
	document.cookie="audience=" +escape(audience)+";expires="+exdate.toGMTString() + "; path=/";
}

function switchSiteView(audience)
{
	if (audience == "advUS")
	{
		setSiteView("advUS");
		if (window.location.href.indexOf("pubweb.gsam") > 0)		
			window.location = "/pubweb.gsam.advisors/pages/gsam/advisors/";
		else
			window.location = "/gsam/advisors/";
	} 
	else
	{
		if (audience == "indUS")
		{
			setSiteView("indUS");
			if (window.location.href.indexOf("pubweb.gsam") > 0)		
				window.location = "/pubweb.gsam.individuals/pages/gsam/individuals/";
			else
				window.location = "/gsam/individuals/";
		}
		else
		{
			
			window.location = "/gsam/audience_selector/index.html?aud="	
		}
	}
}

function launchEconUpdate(eventURL)	
{
    window.open(eventURL,"","width=850,height=500","resizable=1,scrollbars=0");
}

window.addEvent('load', function(){ 
	if (window.location.href.indexOf('/gsam/app') > 0) {
		var c = Cookie.get('audience'); 
		if (c=='advUS') {
			document.forms[0].action="/gsam/advisors/search/search.gscgi"
		} else if (c=='indUS') {
			document.forms[0].action="/gsam/individuals/search/search.gscgi"
		}
	} 
})

GoldmanSachs.PageOverlay.prototype.synonyms = {
	'advisors' : ['individuals'],
	'individuals' : ['advisors']	
}