///**
//* Discussion class
//*
//* Handles discussion for the news items.
//* Contains virtual keyboard as well.
//*
//* @category   Discussion
//* @package    Armtown
//* @author     Serguei Hovsepian <shovsepian@childerensoncologygroup.org>
//* @version    SVN: $Id$
//* @copyright  2009
//* @see        Mootools
//*/

var Discussion = new Class({

	//initialize: function(){
		//this.attach();
	//},

	showReplies: function(el){
		var el = $(el);
		var box = el.getNext('div');
		if (el.retrieve('state') !== 1)
		{ //closed, lets open
			el.store('state', 1);
			el.removeClass('lnkreplies').addClass('lnkreplies0');
			box.show();
			if (box.get('html') == '')
			{
				this.getReplies(box);
			}
		} else {
			el.store('state', 0);
			el.removeClass('lnkreplies0').addClass('lnkreplies');
			box.hide();
		}
		//prevent click action
		return false;
	},

	getReplies: function(box){
		var req = new Request.HTML({
			'method': 'post',
			'url': '/ajax/getreplies.php',
			'data': { 'id' : box.get('id').substring(3) },
			'update': box,
			'onRequest': function() {sp = new Spinner(box.getPrevious('a')); sp.show()},
			'onComplete': function(response) {sp.destroy();}
		}).send();
	},

	rateReply: function(el, act, author, islogin){
		var id = $(el).get('id').substring(6);
		var box = $('fldrating'+id);
		if (islogin)
		{
			if (box.retrieve('state') !== 1)
			{
				var req = new Request({
					'method': 'post',
					'url': '/ajax/ratereply.php',
					'data': {'id': id, 'author': author, 'act': act},
					'onRequest': function() {sp = new Spinner(box); sp.show();},
					'onFailure': function(xhr) {sp.destroy();},
					'onComplete': function(response) {
							sp.destroy();
							if (!$chk(response.toInt())){
								box.empty().set('html', 'X').store('state', 1);
								if ($chk($('loginform'))){
									this.showForm(el,'rate',0);
									return false;
								}
							}else{
								box.empty().set('html', response).store('state', 1);
								if (act===1)
								{
									box.getParent().getParent().highlight('#FFCCCC');
								} else {
									box.getParent().getParent().highlight('#CCFFCC');
								}
							};
						}.bind(this)
				}).send();
			}
		} else {
			this.showForm(el,'rate',0);
		}
	},

	revileReply: function(el){
		var el = $(el);
		var menu = el.getParent('li').getParent('ul').getPrevious('ul');
		var reply = menu.getParent('li').getLast('span');
		el.getParent('ul').destroy();
		menu.show();
		reply.show();
		//prevent click action
		return false;
	},

	showForm: function(el, type, islogin){
		var el = $(el);
		switch (type)
		{
			case 'rate': 
			case 'reply': 
				var box = el.getParent('ul').getNext('div').getNext('div');
				var form = $('replyform');
				var id = el.get('id').substring(6);
				break;
			case 'replytop': 
				var box = el.getParent('div').getNext('div').empty();
				var form = $('replyform');
				var id = 0;
				break;
			case 'replybottom': 
				break;
			case 'email': 
				var box = el.getParent('div').getNext('div').empty();
				var form = $('emailform');
				break;
			case 'translate': 
				var box = el.getParent('div').getNext('div').empty();
				var form = $('translateform');
				break;
			default: 
				return false;
		}
		// not authenticated
		if (!islogin)
		{
			var form = $('loginform');
		}

		if (box.retrieve('state') !== 1)
		{ //closed, lets open
			if (box.get('text')=="")
			{
				box.adopt(form.clone());
				kbd = new VirtKey();
				if ($chk(id))
				{
					box.getFirst().getFirst().getFirst('input').set('value', id);
				}
			}
			box.store('state', 1).show();
		} else {
			box.store('state', 0).hide();
		}
		//prevent click action
		return false;
	},

	closeForm: function(el){
		var el = $(el);
		var box = el.getParent('form').getParent('div');
		box.store('state', 0).hide();
		box.hide();
		//prevent click action
		return false;
	}
});

var VirtKey = new Class({

	isNum: false,

	initialize: function()
	{
		this.u = [];
		this.dc = [96,49,50,51,52,53,54,55,56,57,48,45,61,8,
				9,113,119,101,114,116,121,117,105,111,112,91,93,92,
				0,97,115,100,102,103,104,106,107,108,59,39,13,
				16,122,120,99,118,98,110,109,44,46,47,16,
				17,18,32,18,17,
				126,33,64,35,36,37,94,38,42,40,41,95,43,8,
				9,81,87,69,82,84,89,85,73,79,80,123,125,124,
				0,65,83,68,70,71,72,74,75,76,58,34,13,
				16,90,88,67,86,66,78,77,60,62,63,16,
				17,18,32,18,17];
	},

	show: function(el, lng)
	{
		var el = $(el);
		var fldset = el.getParent('fieldset');
		var txt = fldset.getFirst('div').getNext('div').getFirst('textarea');
		var wrp = fldset.getFirst('div').getNext('div').getNext('div');
		this.setLanguage(lng);
		this.renderKeyboard(wrp);
		this.initTextField(txt);
	},

	setLanguage: function(lng)
	{
		this.lng = lng;
		switch (this.lng)
		{
		case 1:
			this.u = ['՝','է','թ','փ','ձ','ջ',')','և','ր','չ','ճ','-','ժ','Back',
				'Tab','ք','ո','ե','ռ','տ','ը','ւ','ի','օ','պ','խ','ծ','շ',
				'Caps','ա','ս','դ','ֆ','գ','հ','յ','կ','լ',';','՛','Enter',
				'Shift','զ','ղ','ց','վ','բ','ն','մ',',','.','/','Sift',
				'Ctrl','Alt','Space','Alt','Ctrl',
				'՜','Է','Թ','Փ','Ձ','Ջ','(','%','Ռ','Չ','Ճ','—','Ժ','Back',
				'Tab','Ք','Ո','Ե','Ր','Տ','Ը','Ւ','Ի','Օ','Պ','Խ','Ծ','Շ',
				'Caps','Ա','Ս','Դ','Ֆ','Գ','Հ','Յ','Կ','Լ',':','\"','Enter',
				'Shift','Զ','Ղ','Ց','Վ','Բ','Ն','Մ','«','»','՞','Sift',
				'Ctrl','Alt','Space','Alt','Ctrl'];
		break;
		case 2:
			this.u = ['ю','1','2','3','4','5','6','7','8','9','0','-','ь','Back',
				'Tab','я','ж','е','р','т','ы','у','и','о','п','ш','щ','э',
				'Caps','а','с','д','ф','г','ч','й','к','л',';','\'','Enter',
				'Shift','з','х','ц','в','б','н','м',',','.','/','Sift',
				'Ctrl','Alt','Space','Alt','Ctrl',
				'Ю','!','@','#','$','%','Ё','ё','*','(',')','_','Ь','Back',
				'Tab','Я','Ж','Е','Р','Т','Ы','У','И','О','П','Ш','Щ','Э',
				'Caps','А','С','Д','Ф','Г','Ч','Й','К','Л',':','"','Enter',
				'Shift','З','Х','Ц','В','Б','Н','М','<','>','?','Sift',
				'Ctrl','Alt','Space','Alt','Ctrl'];

		}
	},

	renderKeyboard: function(placeholder)
	{
		var kbd = $(placeholder);
		if (this.lng)
		{
			for (i=0,k='<ul>'; i < 58; i++)
			{
				k = k + '<li id="k' + this.dc[i] + '"' + (i==13 || i==14 || i == 40 ? 'class="long"' : (i == 41 ? 'class="longer2"' : (i == 28 || i == 52 || i == 53 || i == 54 || i == 56 || i == 57 ? 'class="longer"' : (i == 55 ? 'class="longest"' : '')))) + '>' + this.u[i] + '</li>';
				k = k + (i == 13 || i == 27 || i == 40 || i == 52 ? '<br class="clear"/>' : '');
			}
			kbd.set('html', k + '</ul><br class="clear"/>');

			//var keys = $$('#keyboard li');
			var keys = kbd.getElements('li');
			this.fxs = [];
			keys.each(function(el, i)
			{
				this.fxs[i] = new Fx.Morph(el, {
					duration: 1500,
					wait: false,
					transition: Fx.Transitions.Quad.easeOut
				});
			}, this);
		} else {
			kbd.set('html', '');
			return true;
		}
	},

	initTextField: function(textarea)
	{
		var txt = $(textarea);
		txt.removeEvents();
		//txt.focus();

		txt.addEvent("keydown", function(e)
		{
			var ev = new Event(e);
			this.isNum = ev.code != 107 && ev.code > 95 && ev.code < 112 ? true : false;
			this.isDel = ev.code == 46 ? true : false;//alert(ev.code);
			this.isArrow = ev.code > 36 && ev.code < 41 ? true : false;
			this.isF = ev.code > 111 && ev.code < 124 ? true : false;
			this.isMenu = ev.code > 90 && ev.code < 94 ? true : false;
			this.isBackspace = ev.code == 8 ? true : false;
			this.isTab = ev.code == 9 ? true : false;
			this.isSpace = ev.code == 32 ? true : false;
			this.isEnter = ev.code == 13 ? true : false;
			//alert(ev.code + ' :: ' + ev.key);
		}.bindWithEvent(this));

		txt.addEvent("keypress", function(e)
		{
			var ev = new Event(e);
			var c = ev.code;
			var k = ev.key;
			var pos = this.dc.indexOf(c);
			var n = pos > 57 && pos < 71 ? pos - 58 : ( pos > 72 && pos < 86 ? pos - 58 : ( pos > 86 && pos < 98 ? pos - 58 : ( pos > 99 && pos < 110 ? pos - 58 : pos )));
			//alert(c + ' :: ' + pos + ' :: ' + k + ' :: ' + u[pos]);
			//alert(ev.control + ' :: ' + ev.alt + ' :: ' + ev.meta + ' :: ' + this.isArrow + ' :: ' + this.isNum + ' :: ' + this.isDel + ' :: ' + this.isF + ' :: ' + this.isMenu + ' :: ' + this.isEnter + ' :: ' + this.isBackspace + ' :: ' + this.isTab);

			if(ev.control || ev.alt || ev.meta || this.isArrow || this.isNum || this.isDel || this.isF || this.isMenu || this.isEnter || this.isBackspace || this.isTab) return true;

			if (c > 32 && c < 127)
			{
				var ch = this.u[pos];
				this.fxs[n].start({
					'background-color': ['#ffff00', '#fff'],
					'opacity': [0, 1]
				});
			} else {
				var ch = k;
			}

			if(this.isSpace) return true;

			if (this.lng && ch)
			{
				if (Browser.Engine.trident)
				{
					window.event.keyCode = ch.charCodeAt(0);
				} else {
					if('scrollTop' in txt) var scrollTop = txt.scrollTop;
					m = txt.selectionStart;
					if(m != null)
					{
						txt.value=txt.value.substring(0, m) + ch + txt.value.substring(txt.selectionEnd);
						txt.setSelectionRange(++m, m);
						if('scrollTop' in txt) txt.scrollTop=scrollTop;
						ev.stop();
					}
				}
			}
		}.bindWithEvent(this));

	},

	textareaCounter: function(textarea, counter, maxchar){
		var txt = $(textarea);
		var cnt = $(counter);
		if (cnt == null)
		{
			cnt = txt.getParent('div').getPrevious('div').getFirst('var');
		}
		if (maxchar == null)
		{
			maxchar = 1500;
		}
		if (txt.get("value").length > maxchar)
		{
			txt.set('value', txt.get("value").substring(0, maxchar).trim());
		}
		cnt.set('text', maxchar - txt.get("value").length)
	}

});

window.addEvent('domready', function(){
	this.disc = new Discussion();
});

function googleTranslateElementInit() {
	new google.translate.TranslateElement({
		pageLanguage: 'en', 
		includedLanguages: 'ar,hy,az,zh-CN,zh-TW,cs,da,nl,en,et,fi,fr,ka,de,el,iw,hi,hu,it,ja,ko,lv,lt,no,fa,pl,pt,ro,ru,sr,sk,es,sv,tr,uk,vi,yi'
	}, 'google_translate_element');
};
