GoldmanSachs.OverlayLink = new Class({
	options: {
		type: 'standard'
	},
	hardTop: null,
	initialize: function (elem, options) {
		this.setOptions(options);
		this.loadUrl = elem.href
		elem.addEvent('click', this.doOverlay.bind(this));
		elem.onclick = function (event) { var evt = new Event(event);evt.preventDefault();}
		if (this.options.name) {
			this.name = options.name;
			GoldmanSachs.OverlayLink.links[this.name] = this;
		}
	},
	doOverlay: function () {
		if (this.options.type == 'video') {
			var ol = new GoldmanSachs.VideoOverlay(this.loadUrl);
		} else {
			var ol = new GoldmanSachs.PageOverlay(this.loadUrl);	
		}	
	}
});
GoldmanSachs.OverlayLink.implement(new Options);
GoldmanSachs.OverlayLink.links = {}

GoldmanSachs.OverlayLink.init = function() {
	try {
		var href = window.location.href;
		var overlay = href.match(/overlay=([^&]*)(&?)(.*)$/)[1];
		if (GoldmanSachs.OverlayLink.links[overlay])
			GoldmanSachs.OverlayLink.links[overlay].doOverlay();
	} catch (e) {}
}

GoldmanSachs.PageOverlay = new Class({
	options: {
		topOffset: 150,
		fadeContentIn: true,
		targetHeight: 200
	},
	presets : {
		CONTAINER_ID: 'gs_doc',
		OVERLAY_BG_CLASSNAME : 'gs_ol_bg',
		CONTENT_OUTER_CONTAINER_CLASSNAME : 'gs_ol_contain',
		CONTENT_INNER_CONTAINER_CLASSNAME : 'gs_ol_content',
		CLOSE_BUTTON_CLASSNAME : 'gs_ol_close',
		LOADING_IMG_CLASSNAME : 'gs_loading',
		LOADING_IMG_URL : 'img/overlay/loadingImg.gif'
	},
	initialize: function (url, redirecturl) {
		if ($defined(url)) {
			this.url = url;
			var bod = $$('body')[0];
			this.overlayBG = new Element('div', {'class': this.presets.OVERLAY_BG_CLASSNAME});;
			this.overlayBGEffect = new Fx.Style(this.overlayBG, 'opacity', {duration: 400});			
			this.overlayBGEffect.addEvent('onComplete', this.loadContent.bind(this));
			this.overlayBGEffect.set(0);
			this.overlayBG.addEvent('click', this.destroy.bind(this));
			bod.adopt(this.overlayBG);			
			
			this.overlayContainer= new Element('div', {'class': this.presets.CONTENT_OUTER_CONTAINER_CLASSNAME});
			this.overlayContainerFadeEffect = new Fx.Style(this.overlayContainer, 'opacity', {duration: 250});	
			this.overlayContainerFadeEffect.set(0);
			bod.adopt(this.overlayContainer);
	
			this.closeButton = new Element('div', {'class': this.presets.CLOSE_BUTTON_CLASSNAME});
			if(redirecturl == undefined){
				this.closeButton.addEvent('click', this.destroy.bind(this));
			 }
			else{
				this.closeButton.addEvent('click', function(){window.location.href=redirecturl});	
			}
			this.overlayContainer.adopt(this.closeButton);
			this.overlayContent= new Element('div', {'class': this.presets.CONTENT_INNER_CONTAINER_CLASSNAME});
			this.overlayContainer.adopt(this.overlayContent);
			
			if (this.doPosition() == true) {
				window.addEvent('resize', this.doPosition.bind(this));	
			}
			this.overlayBGEffect.start(0,0.6);
		} 
	},
	loadContent: function () {
		this.contentLoader = new GoldmanSachs.TextFileLoader({'method':'get'});
		this.contentLoader.addEvent('onFileLoad', this.handleLoadContent.bind(this));
		this.contentLoader.send(this.url);
	},
	handleLoadContent : function () {
		//this.contentLoader.evalStyles();
		this.overlayContent.setHTML(this.contentLoader.fileContent);
		//this.contentLoader.evalScripts();
		this.overlayBG.setStyle('background-image', 'none')
		if (this.options.fadeContentIn == false || window.ie6 == true) {
			this.overlayContainerFadeEffect.set(1);	
		} else {
			this.overlayContainerFadeEffect.start(0,1);
		}
		this.contentLoader.evalScripts();
	},
	doPosition : function () {
		var ww = window.getWidth()
		var wh = window.getHeight();
		var sos = window.getScrollTop();
		var bh = $(this.presets.CONTAINER_ID).getCoordinates().height;
		
		var contTarg = (ww <= 984)? '984px' : '100%';
		var bgTarg = (ww <= 984)? '984px' : '100%';
		
		if (this.hardTop == null) {
			if (sos <= this.options.topOffset) {
				this.hardTop = (sos + this.options.topOffset) + 'px'; 
			} else {
				this.hardTop = ((wh - this.options.topOffset) < (this.options.targetHeight + this.options.topOffset)) ? sos + 'px' : (sos + this.options.topOffset) + 'px';			
			}	
		}
		
		this.overlayBG.setStyles({height: bh +'px', width: bgTarg, position: 'absolute', top: '0', left: '0', 'background-position' : 'center ' + (sos + this.options.topOffset + 50) + 'px'});
		this.overlayContainer.setStyles({width: contTarg, position: 'absolute', top: this.hardTop , left: '0'});
		return true;
	},
	destroy: function () {
		this.overlayContainer.innerHTML = ' removing ';
		window.removeEvent('resize', this.doPosition.bind(this));
		this.overlayBG.remove();
		this.overlayContainer.remove();
	}	
});
GoldmanSachs.PageOverlay.implement(new Options);

GoldmanSachs.VideoOverlay = GoldmanSachs.PageOverlay.extend({
	presets : {
		CONTAINER_ID: 'gs_doc',
		OVERLAY_BG_CLASSNAME : 'gs_ol_bg',
		CONTENT_OUTER_CONTAINER_CLASSNAME : 'gs_ol_contain_vid',
		CONTENT_INNER_CONTAINER_CLASSNAME : 'gs_ol_content',
		CLOSE_BUTTON_CLASSNAME : 'gs_ol_close',
		LOADING_IMG_CLASSNAME : 'gs_loading',
		LOADING_IMG_URL : 'img/overlay/loadingImg_bk.gif'
	},
	options : {
		topOffset: 150,
		fadeContentIn: false,
		targetHeight: 400
	}
});

