var jq = jQuery.noConflict();
var $ = jq;

var Zenzur = (function() {
	
	// config part
	var endtextes = new Array(
						"Hut ab! So gute Artikel, wie ihr sie schreibt, muss man normalerweise mit der Lupe suchen. Ihr dagegen produziert sie am Fließband. Keine Ahnung, wie ihr das macht, aber hört bloß nicht auf damit! ",
						"Toller Text mal wieder. Danke. Da zeigt sich doch, dass die ganze Diskussion um Zensursula usw. total übertrieben war. Ihr könnt immer noch schreiben, was ihr wollt. Und ich kann das immer noch toll finden. Weiter so! ",
						"Geiler Artikel. Ein Fest, das zu lesen. Wenn es nur Texte wie eure geben würde, hätte Karlsruhe die Vorratsdatenspeicherung nicht kippen müssen. Wäre doch gut, wenn all die tollen Artikel für die Nachwelt gespeichert bleiben. ",
						":) was für ein super Artikel. Danke. Ihr habt mir – mal wieder – die Augen geöffnet! ",
						"Super. Bin ganz eurer Meinung. Weiter so!"
	);
	
	// After x words, the machine is typing by itself
	var start_typing = 3;
	
	//


	function func(comment_field_id, submit_id, author_id) {
		var self = this;
		
		var comment_field = jq("#" + comment_field_id);
		var submit_btn = jq('#' + submit_id);
		this.author;
		if (author_id) {
			this.author = jq('#' + author_id);
		}
		var randValue = Math.floor(Math.random() * endtextes.length);
		
		this.endtext = endtextes[randValue];
		this.sended = false;
		this.act = 0;
		this.fake_comment;
		this.with_cookie = false;
		this.img_url = '';
		this.author_element;
		this.target_link = "http://www.reporter-ohne-grenzen.de";

		//  private vars
		var chars = this.endtext.split("");
		var words = this.endtext.split(" ");
		var words_length;
		
		
		// function for count of letters of the
		// first words, which will be changed
		var set_word_length = function() {
			var wl = 0;
			for (var y = 0; y < start_typing; y++) {
				wl += words[y].length;
			}
			// adding whitespace, to length
			wl += (start_typing - 1);					
			words_length = wl;
		};
		
		this.init = function() {
			set_word_length();
			this.act = words_length;

			// generating banner
			var fake_success_div = jq('<div id="fks_outer_layer" style="display:none">' +
										'<div id="fks_container">' +
										'<div id="fake_success">' +
										'<img src="' + this.img_url + 'success_1.gif" border="0" id="fake_success_1">' +
										'<img src="' + this.img_url + 'success_2.gif" border="0" usemap="#rog" style="display:none" id="fake_success_2">' +
										'<img src="' + this.img_url + 'success_3.gif" border="0" usemap="#rog" style="display:none" id="fake_success_3">' +
										'</div></div></div>' +
										'<map name="rog" id="rog">' +
										'<area shape="rect" coords="123,341,444,360" id="fks_close" style="cursor:pointer" alt="HIER UNZENSIERT KOMMENTIEREN" />' +
										'<area shape="rect" coords="95,145,472,256" href="' + this.target_link + '" alt="Reporter ohne Grenzen" target="_blank"/>' +
										'</map>');
	
			fake_success_div.appendTo('body');			
			
			submit_btn.attr('disabled', 'disabled');
			
			// Events
			comment_field.keypress(function() {
				self.text_replace();
			});

			comment_field.keyup(function() {
				self.set_to_censored_text();
			});
			
			jq('#fks_close').click(function() {
				self.uncensor();
			});
		}
		
		
		typing = function() {
			var t;
			comment_field.val(comment_field.val().slice(0,self.act) + chars[self.act]);
			if (self.act +1 < self.endtext.length) {
				self.act++;
				// random timing, for more natural typing
				t = setTimeout(typing, (Math.floor(Math.random()*300)));
			} else {
				clearTimeout(t);
				setTimeout(function() {self.show_submit();}, 4000);
			}
		}
		
		
		this.text_replace = function () {
			var text_input = comment_field.val();
			var text_input_arr = text_input.split("");

			if (text_input.length >= this.endtext.length) {
				//comment_field.val();
				comment_field.val(this.endtext);
			} else {
				if (text_input_arr.length <= words_length) {
					comment_field.val(this.endtext.slice(0,text_input_arr.length));
				} else {
					typing();
				}
			}
		}
		
		this.set_to_censored_text = function() {
			if (comment_field.val().length >= this.endtext.length) {
				comment_field.val(this.endtext);
			}
		}
		
		
		this.show_submit = function() {
			if (!this.sended) {
				jq('#fks_outer_layer').show();
				this.sended = true
				this.authorname = this.author ? this.author.val() : 'anonymous';
				this.author_element.html(this.authorname);
				this.set_to_censored_text();
				if (this.with_cookie) {
					var expire = new Date();
					var days = expire.getTime() + (50 * 24 * 60 * 60 * 1000);
					expire.setTime(days);				
					document.cookie = "Name=ZENSUR; expires=" + expire.toGMTString() + ';';
				}
				
				// disabling submit btn
				submit_btn.removeAttr('disabled');

				// show fake comment in commmentlist
				this.fake_comment.show();
				
				// centering banner
				var scroll_pos = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;

				var marginTop = scroll_pos - 242;
				
				jq('#fks_container').css('margin-top',marginTop);				
				jq('#fks_container').fadeIn("slow");
								
				setTimeout(function() {
					jq('#fks_container').fadeOut("slow");
					setTimeout(function() {
						jq('#fake_success_1').hide();
						jq('#fake_success_2').show();
						jq('#fks_container').fadeIn("fast");
						setTimeout(function() {
							jq('#fake_success_2').hide();
							jq('#fake_success_3').show();
						}, 5000);
					}, 2000);
				}, 4000);
			}
		}
		
		this.clone_comment = function(comment_element_to_clone, content_elm_class, author_elm_class) {
			var element = comment_element_to_clone.clone();
			jq('.' + content_elm_class, element).html(this.endtext);
			if (!this.author_element) {
				this.author_element = jq('.' + author_elm_class, element);
			}

			element.hide();
			comment_element_to_clone.after(element)
			this.fake_comment = element;
		}
		
		this.uncensor = function() {
			jq('#fks_outer_layer').remove();
			comment_field.val("");
			comment_field.focus();
			comment_field.unbind('keypress');
			comment_field.unbind('keyup');
			this.fake_comment.remove();
		}
		
	}

	return func;
})();

