var SForum = new Class ({
	
	Implements: [Options],
	
	options: {		
		loading:false,
		from_id:0,
		iterations:3
	},
	
	initialize: function(options){
		this.setOptions(options);
	},
	
	show_posts: function(data) {
		var foo = Json.evaluate(data);
		if(foo.error) alert(foo.error);
		else {
			$('postscontainer').adopt(new Element('div').setHTML(foo.text));
			$('lastpost').value = this.options.from_id = foo.post_id;
			$('message').setHTML('');
			$('message').value = '';
		}
		return false;
	},
	transition: function (data) {

		//var myFx = new Fx.Style('postsbetween', 'opacity', {duration: 300, wait: false}).start(0, 1);
		var foo = Json.evaluate(data);
		var div = new Element('div');
		div.setHTML(foo.text);
		$('postscontainer').adopt(div);
		this.options.loading = false;
		this.options.from_id=foo.from_id;
		return this;
	},
	getMorePosts: function() {
		//console.log($('lastpost').value+">"+this.from_id+" - "+this.loading+" > "+($('lastpost').value>this.from_id));
		if(!this.options.loading && parseInt($('lastpost').value)>parseInt(this.options.from_id)) {
			this.options.loading = true;
			//console.log("nachladen!");
			jslink('/'+this.options.LANG+'/forum/'+this.options.forum_id+'/'+this.options.forum_name+'/'+this.options.topic_id+'/'+this.options.topic_title+'/'+this.options.from_id+'/null/10/', {foo:0}, 'Forum::ajaxGetPosts', this.transition.bind(this));
		}
	},
	toBottom: function(nologinto) {
		if(!this.isLoggedIn(nologinto)) return false;
		this.options.loading = true;
		this.scrollFx = new Fx.Scroll(window, {duration:1500, transition:Fx.Transitions.Expo.easeIn}).toBottom();
		jslink('/'+this.options.LANG+'/forum/'+this.options.forum_id+'/'+this.options.forum_name+'/'+this.options.topic_id+'/'+this.options.topic_title+'/'+this.options.from_id+'/null/-15/', {foo:0}, 'Forum::ajaxGetPosts', 
			function(data) {
				this.transition.attempt(data, this);
				this.scrollFx.stop();
				this.scrollFx = new Fx.Scroll(window).toBottom();
	 		}.bind(this)		
		);
		return false;
	},
	doScroll: function() {
		if((window.getScrollHeight()/10)>=(window.getScrollHeight()-(window.getScrollTop()+window.getHeight()))) {
			this.getMorePosts.attempt(null, this);
		}
	},
	quote: function(postID) {
		if(!this.isLoggedIn()) return false;
		var username = $$('#post_'+postID+' .username a').getLast().innerHTML;
		var posttext = revertp($$('#post_'+postID+' .post_content').getLast().innerHTML);
		var posttime = $$('#post_'+postID+' h5').getLast().innerHTML;
		var quotetext = "[quote]\n[b]"+username+" schrieb am "+posttime+":[/b]\n"+posttext+"\n[/quote]\n";
		if(window.ie) $('message').innerText = quotetext;
		else		$('message').setHTML(quotetext);
		this.toBottom();
		return false;
	},
	
	
	editText:Array(), //needed for cancel button
	edit: function(id) {
		if($('post_content_'+id).get('html').test("<textarea")) return false;
		height = heightEl('post_content_'+id) + 22;
		if(height < 200) height = 200;
		
		this.editText[id] = $('post_content_'+id).innerHTML;
		$('post_content_'+id).innerHTML = "<textarea rows=\"15\" class=\"writeentry\" style=\"width:500px;height: "+height+"px;\" id=\"savethistext_"+id+"\">"+revertp(this.editText[id])+"</textarea>"+"<br/>"+"<br/>"; //+getBBBar("savethistext_"+id)
		
		$('post_content_'+id).adopt(
			new Element("button").set('id', 'save_button_'+id).set('html', 'speichern').addEvent('click', 
				function() {return mySForum.saveEntry(id)}.pass(id)
			)
		).adopt(new Element("span").set('html', "&nbsp;")).adopt(
			new Element("button").set('id', 'cancel_button_'+id).set('html', 'abbrechen').addEvent('click', 
				function() {return mySForum.cancelEdit(id)}.pass(id)
			)
		);
		return false;
	},
	
	cancelEdit: function(id) {
		$('post_content_'+id).innerHTML = this.editText[id];
		//restoreEditButtons(id);
		return false;
	},
	
	saveEntry: function(id) {
		this.editText[id] = $('savethistext_'+id).value;
		toShow = wpautop(this.editText[id]); //get the html code
		$('post_content_'+id).set('html', toShow);
		
		jslink('/'+LANG.LANG+'/forum/edit/'+id+'/', {'text':this.editText[id]}, 'Forum::edit', function(theText) {$('post_edit_button_'+id).setHTML("&rarr; &auml;ndern"); var myObject = Json.evaluate(theText); $('post_content_'+id).setHTML(wpautop(myObject.text))});
		
		//disable the buttons and say "wird gespeichert..."
		if($('save_button_'+id))$('save_button_'+id).onclick=function() {return false;};
		$('post_edit_button_'+id).set('html', "wird gespeichert...");
		if($('cancel_button_'+id))$('cancel_button_'+id).onclick=function() {return false;};		
		
		return false;
	},
	
	isLoggedIn: function(nologinto) {
		if(this.options.user_id>0) return true;
		else {location.href = '/'+this.options.LANG+'/nologin'+nologinto; return false; }
	},
	
	vote: function(theForm) {
		if(!$(theForm).toQueryString()) return false;
		if(this.voted) return false;
		this.myElements = $$('.voteFields li div');
		var effectOptions = new Object();
		this.myElements.each(function(el){
			var tmp = Json.evaluate("{"+el.id.replace(/voteResult/,'')+":{'opacity': [0,1],'width': [0,"+$random(0,300)+"]}}");
			$extend(effectOptions, tmp);
		});
		this.myElementsEffects = new Fx.Elements(this.myElements);
		this.myElementsEffects.start(effectOptions).chain(function(){
			this.doVoteEffect();
		}.bind(this));
		
		jslink('/'+this.options.LANG+'/forum/'+this.options.forum_id+'/'+this.options.forum_name+'/'+this.options.topic_id+'/'+this.options.topic_title+'/vote.html', {query:$(theForm).toQueryString()}, 'Forum::vote', this.showVoteResult.bind(this));
		$$('.voteFields input').each(function(el){el.disabled=true;});
		$('voteSubmitButton').remove();
		this.voted=true;
		return false;
	},
	
	showVoteResult: function(re) {
		this.resultComplete = Json.evaluate(re);
	},
	
	doVoteEffect: function() {
		this.options.iterations--;
		var effectOptions  = new Object();
		if(this.options.iterations<=0 && this.resultComplete) {
			this.myElements.each(function(el){
				var myID = el.id.replace(/voteResult/,'');
				var voteResultForID = (this.resultComplete[myID]>0?this.resultComplete[myID]/this.resultComplete.sum:0);
				el.setHTML(this.resultComplete[myID]+" ("+(100*voteResultForID).round()+"%)");
				var tmp  = Json.evaluate("{"+myID+":{'width': "+(300*voteResultForID).round()+"}}");
				$extend(effectOptions, tmp);
			}.bind(this));
			this.myElementsEffects.start(effectOptions);
			return;
		}else{
			this.myElements.each(function(el){
				var myID = el.id.replace(/voteResult/,'');
				var tmp  = Json.evaluate("{"+myID+":{'width': "+$random(0,300)+"}}");
				$extend(effectOptions, tmp);
			}.bind(this));
			this.myElementsEffects.start(effectOptions).chain(function(){
				this.doVoteEffect();
			}.bind(this));
		}
	}
	
});

SForum.implement(new Chain, new Events, new Options);



window.addEvent("domready", function(){
	var cmntArea = $('message');
	if ( cmntArea ) cmntArea.addEvent('keyup', function(){
		$('commentPreview').innerHTML = '<div>' + wpautop(($('message').value)) + '</div>';
	});
	var userForm = new Element('form').setProperties({
		'action': 'box.html',
		'method': 'post',
		'id': 'reportForm'
	});
	userForm.innerHTML = '<textarea style="width:290px;" rows="5" cols="50" name="reason" id="reason"></textarea>';
	if($$('.postdenunciationbutton')) {
		$$('.postdenunciationbutton').each(function(el) {
			el.addEvent('click', function(el) {
				var boxHtml = new MooPrompt(LANG.forum_posts.denunciate, userForm, {
					buttons: 2,
					horizontal: 'center', vertical: 'middle', effect:'slide', 
					button1: LANG.forum_posts.denunciate_send,
					button2: LANG.forum_posts.cancel,
					overlay: true,
					onButton1: function() {
						console.log(jslink(el.href, {'reason':$('reason').value}, 'Denunciation::user', function(data){$('denunciationreturn').setHTML('<a href=\'#\' onclick="return false;">&rarr; '+LANG.forum_posts.denunciat_request_denunciat+'</a>')}));
						return false;
					}
				});
				return false;
			}.pass(el));
		});
	}
});
