var AjaxQuery1v1 = Class.create();
AjaxQuery1v1.prototype = {
	interval: null,
	request: false,
	url: '',
	window: false,
	
	commitUrl: '',
	rejectUrl: '',
	userName: '',
	
	initialize: function(url) {
		this.url = url;
		this.queryChatStart();
		this.interval = setInterval(this.queryChatStart.bind(this), 10000);
	},
	
	queryChatStart: function() {
		if(this.request || this.window)
			return;
		
		this.request = true;
		new Ajax.Request(this.url, {
			method: 'post',
			onSuccess: this.onSuccess.bind(this)
		});		
		
		
	},
	
	onSuccess: function(transport) {
		if(transport.status == 200) {
			var response = transport.responseJSON;
			if(response != null) {
				if(response.showInvite && myLightWindow && !myLightWindow.windowActive) {
//					myLightWindow = new lightwindow();  
//					myLightWindow.activateWindow({
//						href: response.inviteUrl,
//						height: 501,
//						width: 833
//					});
					
					this.commitUrl = response.commitUrl;
					this.rejectUrl = response.rejectUrl;
					this.userName = response.userName;
					
					this.displayInvitation();
				}
				
				this.request = false;
				return;
			}
		}
		
		clearInterval(this.interval);
	},
	
	displayInvitation: function() {
		var so1294 = new SWFObject("fileadmin/templates/swf/chatPopup.swf", "flash-pozvanka-embed", "390", "170", "9.0.115", "0", "FFFFFF");
		so1294.addParam("allowFullScreen", "true");
		so1294.addParam("allowScriptAccess", "always");
		so1294.addParam('wmode', 'transparent');
		so1294.addParam('userName', this.userName);
		so1294.useExpressInstall("");
		so1294.write("flash-pozvanka");
		
		
		$('flash-pozvanka').show();
		this.window = true;
	},
	
	commitChat: function() {
		window.open(this.commitUrl ,'_blank','width=700,height=600');
		this._closePopup();
	},
	
	_closePopup: function() {
		$('flash-pozvanka').innerHTML = '';
		$('flash-pozvanka').hide();
		this.window = false;
	},
	
	closePopup: function() {
		this.rejectChat();
	},
	
	rejectChat: function() {
		new Ajax.Request(this.rejectUrl);
		this._closePopup();
	}
	
}

function commitChat() {
	ajaxQuery1v1.commitChat();
}

function closePopup() {
	ajaxQuery1v1.closePopup();
}

function rejectChat() {
	ajaxQuery1v1.rejectChat();
}

function getUserName()
{
	return ajaxQuery1v1.userName;
}
