var HIGHLIGHT = '#f5f5df';
var HIGHLIGHT_ERROR = '#f0dddd';


var ForumPost = new Class({
	Implements: [Options],
	options: {
	},
	
	initialize: function(options) {
		this.setOptions(options);
		this.postModel = new Model({url: this.options.updateUrl,'peer':'IwForumPost'});
		this.topicModel = new Model({url: this.options.updateUrl,'peer':'IwForumTopic'});

        /*$('choosenTopic').getElements('.edit-post-body').each(function(body) {
			var id = body.get('id').replace('post-body-','');
			var ipe_body = new InPlaceEditor(body ,body.getParent(),{
				rows: 10,
				loadUrl: this.options.loadPostUrl.replace('POST_ID',id),
				onSave: function(a, txt) {
					body.addClass('none');
					var ind = new Element('img',{'src': '/img/common/ajax-loader.gif'}).injectBefore(body);
					var url = this.options.savePostUrl.replace('POST_ID',id);
					new Request.HTML({
						url: url,
						data: 'post='+txt,
						onComplete: function(tree,elems,html) {
							body.set('html',html);
							ind.destroy();
							body.removeClass('none');
							body.highlight(HIGHLIGHT);
						}
					}).send();
				}.bind(this)
			});
		}.bind(this));
        */
		this.initThanks();
		this.initTip();		
	},
	
	initThanks: function() {
		$('choosenTopic').getElements('.thanx').each(function(el) {
			el.removeEvents('click');
			el.addEvent('click',function(e) {
				new Event(e).stop();
				var url = el.get('href');
				var id = el.get('id').replace('post-thanx-','').replace('remove-','');
				var where = $('post-'+id).getElement('.interaction');
				var req = new Request.HTML({
						url: url,
						onComplete: function(tree,elems,html) {
							var thx = $('post-thanks-'+id);
							
							if (tree && tree.item(0)) {
								if(thx) {
									tree.item(0).injectAfter(thx);
									thx.destroy();
								} else {
									tree.item(0).inject(where,'bottom');
								}
								thx = $('post-thanks-'+id);
							
								if (thx && tree.item(0)) {
									if ($('tip-post-thanks-'+id)) {
										$('tip-post-thanks-'+id).destroy();
									}
									tree.item(0).injectAfter(thx);
								}
															
								this.initTip();
								
								if (!thx.hasClass('open')) {
									thx.fireEvent('click');
								}
							} else {
								
								if ($('tip-post-thanks-'+id)) {
									$('tip-post-thanks-'+id).destroy();
								}
								if (thx) thx.destroy();
							}

                            var user = JSON.decode(req.getHeader('X-JSON'));
                            if (user && user.user_id) {
                                $$('.userThanks-'+user.user_id).each(function(el) {
                                   el.set('html',user.thanks);
                                });
                            }
							this.initThanks();
						}.bind(this)
					}).send();
			}.bindWithEvent(this));
		}.bind(this));
	},
	
	initTip: function() {
		$$('.thanxFrom').each(function(el) {
			new Tip(el);
		});
	},
	
	log: function(text, args) {
		if (window.console) console.log(text);
	}
});
