scj.overlay = {};

scj.OverlayLink = new Class({
	Implements : [Options, Events],
	options: {
		type: 'standard',
		eventTrigger: 'click'
	},
	hardTop: null,
	initialize: function (elem, options) {
		this.setOptions(options);
		var reference = this;
		this.source = elem.getProperty('href') || elem.getProperty('source') || false;
		elem.addEvent(this.options.eventTrigger, function(evt) {
			evt.stop();
			reference.triggerOverlay(evt);
		});
	},
	triggerOverlay: function (event) {
		$(document.body).addClass(this.options.type);
		var type = this.options.type.indexOf('Overlay') !== -1 ? this.options.type : this.options.type + 'Overlay';
		if (scj.overlay[this.options.type] && scj.overlay[this.options.type].trash) scj.overlay[this.options.type].trash();
		scj.overlay[this.options.type] = new scj[type](this.source, this.options);
	}
});

scj.PageOverlay = new Class({
	Implements : [Options, Events],
	options: {
		topOffset: 150,
		fadeContentIn: true,
		targetHeight: 200,
		overlay_bg_opacity: 0.6
	},
	presets : {
		CONTAINER_ID: 'wrapper',
		OVERLAY_BG_CLASSNAME : 'overlay',
		CONTENT_OUTER_CONTAINER_CLASSNAME : 'ol_contain overlay',
		CONTENT_INNER_CONTAINER_CLASSNAME : 'ol_content',
		CLOSE_BUTTON_CLASSNAME : 'ol_close',
		LOADING_IMG_CLASSNAME : 'loading',
		LOADING_IMG_URL : 'img/overlay/loadingImg.gif'
	},
	initialize: function (source, options) {
		this.setOptions(options);
		if ($defined(options.source)) {
			this.source = source;
		}
		this.build();
	},
	build: function() {
		var body = document.body;
		this.overlayHolder = $(this.presets.CONTAINER_ID) || body;
		this.overlayBG = new Element('div', {'class': this.presets.OVERLAY_BG_CLASSNAME});;
		this.overlayBGEffect = new Fx.Morph(this.overlayBG, {duration: 400});			
		if (this.source) {
			this.overlayBGEffect.addEvent('onComplete', this.loadContent.bind(this));
		}
		if (window.Browser.Platform.mac && window.Browser.Engine.gecko) {
		    this.hider = {'visibility': 'hidden'};
		    this.showme = {'visibility': 'visible'};
		} else {
		    this.hider = {'opacity': 0}
		    this.showme = {'opacity': 1};
		}
		
		this.overlayBGEffect.set(this.hider);
		this.overlayBG.addEvent('click', this.trash.bind(this));
		this.overlayHolder.adopt(this.overlayBG);			
		
		this.overlayContainer= new Element('div', {'class': this.presets.CONTENT_OUTER_CONTAINER_CLASSNAME});
		this.overlayContainerFadeEffect = new Fx.Morph(this.overlayContainer, {duration: 'short'});	
		this.overlayContainerFadeEffect.set(this.hider);
		this.overlayHolder.adopt(this.overlayContainer);

		this.closeButton = new Element('div', {'class': this.presets.CLOSE_BUTTON_CLASSNAME});
		this.closeButton.addEvent('click', this.trash.bind(this));
		this.overlayContainer.adopt(this.closeButton);
		this.overlayContent= new Element('div', {'class': this.presets.CONTENT_INNER_CONTAINER_CLASSNAME});
		if (this.options.eventTrigger == 'mouseenter') {
			var ref = this;
			this.overlayContent.addEvent('mouseleave', function() {
				ref.destroy();
			});
		}
		this.overlayContainer.adopt(this.overlayContent);
		
		if (this.doPosition() == true) {
			window.addEvent('resize', this.doPosition.bind(this));	
		}
		
		if (window.Browser.Platform.name == 'win') {
		    this.overlayBGEffect.start({'opacity': this.options.overlay_bg_opacity});
		} else {
		    this.overlayBG.setStyles(this.showme);
		    this.loadContent();
		}
	},
	loadContent: function () {
		if (this.source.indexOf('.swf') !== -1) {
			this.handleLoadContent();
			this.createSwf();
		} else if (this.options.type == 'iframe'){
			this.createiframe();
			this.handleLoadContent();
		} else if (this.source.indexOf('#') == 0){
			var hashValue = this.source.split('#')[1];
			this.overlayContent.adopt($(hashValue).clone());
			this.handleLoadContent();
		} else {
			this.overlayContent.load(this.source);
			this.handleLoadContent();
		}
		
	},
	createiframe: function() {
		if ($('spotlightContent') && $('scrollbar-content').getElement('.active a')) {
			var active = $('scrollbar-content').getElement('.active a').getProperty('onclick').toString().clean();
			var swftrigger = '';
			if (window.Browser.Engine.name == 'trident') {
				var last = active.search('}}');
				var sub = active.substring(active.search('scj'), last+2);
				swftrigger = sub + ', true);';
			} else {
				swftrigger = active.substring(0, (active.length-2)) + ', true)';
			}
			if (window.Browser.Engine.name == 'trident') {
				eval.delay(250, swftrigger);
			} else {
				eval(swftrigger);
			}
		}

		this.iframe = new Element('iframe', {'src': this.source, 'height': this.options.targetHeight, 'width': this.options.targetWidth, 'frameborder': 0, 'scrolling': 'no'});
		this.overlayContent.adopt(this.iframe);
	},
	handleLoadContent : function () {
		this.fadeOverlay();
	},
	fadeOverlay: function() {
		if (!this.options.fadeContentIn || window.Browser.trident4 || (window.Browser.Platform.name !== 'mac')) {
			this.overlayContainerFadeEffect.set(this.showme);	
		} else {
    		this.overlayBG.setStyle('background-image', 'none');
			this.overlayContainer.setStyles(this.showme);
		}
	},
	trash: function() {
		this.overlayContainer.innerHTML = ' removing ';
		window.removeEvent('resize', this.doPosition.bind(this));
		$(document.body).removeClass(this.options.type);
		this.overlayBG.dispose();
		this.overlayContainer.dispose();
		if (scj.overlay[this.options.type]) delete scj.overlay[this.options.type];
		if ($('spotlightContent') && $('scrollbar-content').getElement('.active a')) {
			scj.utility.loadVideo('videoImg',  {playerName: 'videoplayer.swf', flashvarsObj: {video: '/videos/world'}});
			var active = $('scrollbar-content').getElement('.active a').getProperty('onclick').toString();
			eval(active);
		}

	},
	doPosition : function () {
		var ww = window.getWidth()
		var wh = window.getHeight();
		var sos = window.getScrollTop();
		var bh = '100%'; //$(this.presets.CONTAINER_ID).getCoordinates().height;
		var bw = $(this.presets.CONTAINER_ID).getCoordinates().width;
		
		var contTarg = this.options.targetWidth ? this.options.targetWidth : this.overlayContent.getCoordinates().width;
		var bgTarg = '100%';
		var contHeight = this.options.targetHeight ? this.options.targetHeight : 'auto';
		var contLeft = (bw/2) - (contTarg/2);
		if (this.options.leftOffset || this.options.leftOffset == '') {
			contLeft = this.options.leftOffset;
		}
		
		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, width: bgTarg, position: 'absolute', top: '0', left: '0', 'background-position' : 'center ' + (sos + this.options.topOffset + 50) + 'px'});
		this.overlayContainer.setStyles({position: 'absolute', top: this.hardTop, left: contLeft, width: contTarg, height: contHeight});
		return true;
	},
	destroy: function () {
		this.trash();
	}	
});

scj.iframeOverlay = new Class({
	Extends: scj.PageOverlay,
	presets : {
		CONTAINER_ID: 'wrapper',
		OVERLAY_BG_CLASSNAME : 'overlay',
		CONTENT_OUTER_CONTAINER_CLASSNAME : 'ol_contain iframe orange overlay z-index999',
		CONTENT_INNER_CONTAINER_CLASSNAME : 'ol_content',
		CLOSE_BUTTON_CLASSNAME : 'ol_close',
		LOADING_IMG_CLASSNAME : 'loading',
		LOADING_IMG_URL : '' +
                          '\css/RadSkins/Window/loading.gif'
	},
	options : {
		type: 'iframeOverlay',
		topOffset: 150,
		fadeContentIn: false,
		targetHeight: 400,
		targetWidth: 710,
		overlay_bg_opacity: 0.1
	}
});