var DisqusOverlay = function () {

	this.environment = '';
	this.instanceName = '';
	this.forumShortName = '';
	this.siteNameOverride = null;
	this.useDeveloperMode = false;
	
	this.popUpWidth = 625;
	this.popUpHeight = null;
	this.topOffset = 0;
	this.leftOffset = 0;
	
	this._currentThreadIdentifier = null;
	
	this.close = function () {
		$.unblockUI({
			onUnblock: jQuery.proxy(this.resetContainer, this)
		});
		
//		if (jQuery.browser.msie) {
//			$('html').css('overflow', 'auto');
//		} else {
//			$('body').css('overflow', 'auto');
//		}
		
		return false;
	};
	
	this.resetContainer = function () {
		$(window).unbind('resize', this.resizeHandler);
		$('#disqusOverlayContent').empty();
	};
	
	
	this.open = function (threadIdentifier) {
		this._currentThreadIdentifier = threadIdentifier;
		
		var winWidth;
		var winHeight;

		if ($.browser.msie) {
			winWidth = document.documentElement.clientWidth;
			winHeight = document.documentElement.clientHeight;
		} else {
			winWidth = window.innerWidth;
			winHeight = window.innerHeight;
		}

		
		this.topOffset = Math.round(winHeight*0.05);
		this.popUpHeight = winHeight - this.topOffset*2;
		this.leftOffset = Math.round((winWidth - this.popUpWidth)/2);
		
		$('#disqusOverlayContainer').height(this.popUpHeight);
		$(window).resize(jQuery.proxy(this.resizeHandler, this));
		this.resizeHandler(null);
		
		$.blockUI({
			message: $('#disqusOverlayContainer'),
			overlayCSS: {
				cursor: "default"
			},
			css: {
				cursor: "auto",
				backgroundColor: "transparent",
				border: "none",
		        width:  this.popUpWidth+'px',
//		        height: this.popUpHeight+'px',
		        top: this.topOffset+"px",
		        left: this.leftOffset+"px",
		        textAlign: "left"
			},
			fadeIn: 400,
			fadeOut: 400,
			bindEvents: false,
			onBlock: jQuery.proxy(this.resizeHandler, this)
		});
		
		$('.blockOverlay').click(jQuery.proxy(this.close, this)); 
		
//		if (jQuery.browser.msie) {
//			$('html').css('overflow', 'hidden');
//		} else {
//			$('body').css('overflow', 'hidden');
//		}
		
		
		var qs = jQuery.param({
			shortname: this.forumShortName,
			developer: this.useDeveloperMode ? '1' : '0',
			identifier: this._currentThreadIdentifier,
			environment: this.environment
		});
		
		var html = "<iframe id=\"disqusIframe\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" src=\""+this.environment+"data/modules/DisqusComments/iframe_content.php?"+qs+"\"></iframe>";
		
		$('#disqusOverlayContent').html(html);
//		alert(disqus_url);
		
		return false;
	};
	
	this.resizeHandler = function (event) {
		var winWidth;
		var winHeight;

		if ($.browser.msie) {
			winWidth = document.documentElement.clientWidth;
			winHeight = document.documentElement.clientHeight;
		} else {
			winWidth = window.innerWidth;
			winHeight = window.innerHeight;
		}
		
		this.popUpHeight = winHeight - this.topOffset*2;
		
		this.leftOffset = Math.round((winWidth - this.popUpWidth)/2);
		
		$('#disqusOverlayContainer').height(this.popUpHeight);
		
		$('#disqusOverlayContentContainer').height(this.popUpHeight - $('#disqusOverlayHeader').outerHeight());
		
		$('#disqusIframe').height($('#disqusOverlayContentContainer').height());
		$('#disqusIframe').width($('#disqusOverlayContentContainer').width());
		
		$('div.blockUI.blockMsg').css('left', this.leftOffset);
	};
	
	this.applyHandlers = function () {
		//console.log('--- applying handler! ---');
		//jQuery(".dsq-post-as button.dsq-button").click(jQuery.proxy(this.sendButtonClickedHandler, this));
	};
	
	this.sendButtonClickedHandler = function (ev) {
		//console.log('send button clicked!');
	};
	
	
	this.commitComment = function () {
		AjaxUtils.loadJSON(this.environment+'data/rpc_wrapper.php?module=modules/DisqusComments/commit_comment', {
			instance_name: this.instanceName,
			site: this.siteNameOverride ? this.siteNameOverride : Globals.currentSite,
			vid: this._currentThreadIdentifier
		}, function (data) {
			
		}, this);
	};
};

DisqusOverlay.instance = null;
DisqusOverlay.getInstance = function () {
	if (!DisqusOverlay.instance) {
		DisqusOverlay.instance = new DisqusOverlay();
	}

	return DisqusOverlay.instance;
};
