var Translator = {
	
	/**
	 * Izabrani smer prevodjenja.
	 */
	direction: 2,
	
	/**
	 * Timeout flag koji sluzi za simultano prevodjenje 
	 */
	requestTimeout: null,

	/**
	 * Izabrani smer prevodjenja.
	 */
	last_query: "",
	
	/**
	 * Moguci smerovi prevodjenja
	 */
	directions: {
		0:"srpski &raquo; engleski", 
		1:"engleski &raquo; srpski", 
		2:"dvosmerno"
	},

	/**
	 * Popup objekat, trenutni dijalog, singleton instanca
	 */
	popup: null,

	/**
	 * Menja smer prevodjenja.
	 */
	changeDirection: function () {
		if (this.direction++ >= 2) {
			this.direction = 0;
		}

		$("#direction").html(this.directions[this.direction]);
		this.last_query = false;
		this.translate();

		return false;
	},
	
	/**
	 * Salje zahtev za prevodjenje aplikaciji.
	 */
	translate: function () {
		var query = $("#query").val().replace(/^\s+/gi, "").replace(/\s+$/gi, "").replace(/[^\w\s\d\žđšćč-]/gi, "").toLowerCase();
		$("#query").val(query);
		
		if ($("#query").val().length <= 0) {
			$("#results").html("");
			return false;
		}
		
		if (this.last_query == query) {
			return false;
		} else {
			this.last_query = query;
		}
	
		$("#results").html('');
		$("#progress").show();

		$.ajax({
			url: "/translate",
			type: "POST",
			cache: true,
			data: "query="+escape(query)+"&direction="+this.direction,
			success: function (data) {
				$("#progress").hide();
				if (data.length > 0) {
					$("#results").html(data);
				} else {
					$("#results").html('<div class="error">Tražena reč ne postoji u rečniku. Molimo, pokušajte ponovo.</div>');
				}
			},
			error: function () {
				$("#progress").hide();
				$("#results").html('<div class="error">Greška prilikom povezivanja. Molimo, pokušajte ponovo.</div>');
			}
		});
		return false;
	},

	/**
	 * Trazi prevod za odredjeni termin iz liste rezultata
	 */
	jumpTo: function (word) {
		if (word == "") return false;
		$("#query").val(word);
		return this.translate();
	},

	/**
	 * Prijava gresaka u prevodu
	 */
	reportError: function (word_id, word_str) {
		var form = '<div id="error-box" class="modal-box">';
		form += '<label>Kratak opis greške</label>';
		form += '<textarea id="error-desc"></textarea>';
		form += '<div>';
		form +=	'<input id="error-button" type="button" value="Pošalji" onclick="Translator.sendError()"/>';
		form +=	'</div>';
		form +=	'</div>';
		this.popup = $(form);
		this.popup.dialog({resizable:false, modal: true, title: "Greška u prevodu za: " + word_str});
		$("#error-button").data('word_id', word_id);
		return false;
	},

	/**
	 * Slanje greske koristeci AJAX
	 */
	sendError: function () {
		word_id = $("#error-button").data('word_id');
		desc = $("#error-desc").val();
		that = this;
		$.ajax({
			url: "/report_error",
			type: "POST",
			data: "word_id="+word_id+"&description="+escape(desc),
			success: function (data) {
				$("#error-box").html("<center>Greška koju ste upravo prijavili je uspešno poslata. Zahvaljujemo Vam se na saradnji.</center>");
				setTimeout(function () {
					that.popup.dialog("destroy");
					that.popup.remove();
				}, 2500);
			},
			error: function () {
				/* Do nothing */
			}
		});
		
		return false;
	},

	/**
	 * Dodavanje novih reci
	 */
	addMeaning: function (word_id, word_str) {
		var form = '<div id="meaning-box" class="modal-box">';
		form += '<label>Upisati novo znacenje</label>';
		form += '<textarea id="meaning-desc"></textarea>';
		form += '<div>';
		form += '	<input id="meaning-button" type="button" value="Pošalji" onclick="Translator.sendMeaning()"/>';
		form += '</div>';
		form += '</div>';
		this.popup = $(form);
		this.popup.dialog({resizable:false, modal: true, title: "Predlog za: " + word_str});
		$("#meaning-button").data('word_id', word_id);
		return false;
	},

	/**
	 * Slanje nove reci koristeci AJAX
	 */
	sendMeaning: function () {
		word_id = $("#meaning-button").data('word_id');
		meaning = $("#meaning-desc").val();
		that = this;

		$.ajax({
			url: "/add_meaning",
			type: "POST",
			data: "word_id="+word_id+"&meaning="+escape(meaning),
			success: function (data) {
				that.popup.html("<center>Predlog prevoda koji ste upravo dodali je uspešno poslat. Zahvaljujemo Vam se na saradnji.</center>");
				setTimeout(function () {
					that.popup.dialog("destroy");
					that.popup.remove();
				}, 2500);
			},
			error: function () {
				/* Do nothing */
			}
		});

		return false;
	},

	/**
	 * Dodavanje novih reci i njihovih prevoda
	 */
	addTranslation: function () {
		var form = '<div id="meaning-box" class="modal-box">';
		form += '<label>Rec</label>';
		form += '<input type="text" id="word" style="float:left; width: 200px"><br clear="all">';
		form += '<label>Prevod</label>';
		form += '<textarea id="meaning"></textarea>';
		form += '<div>';
		form += '	<input id="meaning-button" type="button" value="Pošalji" onclick="Translator.sendTranslation()"/>';
		form += '</div>';
		form += '</div>';
		this.popup = $(form);
		this.popup.dialog({resizable:false, modal: true, title: "Dodaj prevod"});
		return false;
	},

	/**
	 * Slanje nove reci koristeci AJAX
	 */
	sendTranslation: function () {
		word_str = $("#word").val();
		meaning = $("#meaning").val();

		if (word_str.length <=0) {
			alert("Niste definisali rec. Molimo, pokusajte ponovo.");
			$("#word")[0].focus();
			return false;
		}

		if (meaning.length <=0) {
			alert("Niste definisali znacenje reci. Molimo, pokusajte ponovo.");
			$("#meaning")[0].focus();
			return false
		}

		that = this;
		$.ajax({
			url: "/add_meaning/",
			type: "POST",
			data: "word_str="+word_str+"&meaning="+escape(meaning),
			success: function (data) {
				that.popup.html("<center>Predlog prevoda koji ste upravo dodali je uspešno poslat. Zahvaljujemo Vam se na saradnji.</center>");
				setTimeout(function () {
					that.popup.dialog("destroy");
					that.popup.remove();
				}, 2500);
			},
			error: function () {
				/* Do nothing */
			}
		});

		return false;
	}

};

/**
 * Autoload funkcija
 * Ovde pridruziti sve dogadjaje dhtml elementima (onclick, onmouseover, ...)
 */
$(function () {
	$('#query').blur(function () {
		Translator.translate();
	});

	$('#query').change(function () {
		Translator.translate();
	});

	$('#query').focus(function () {
		this.select();
	});
		
	$('#button').click(function () {
		$(this).fadeTo('fast', 0.8);
		Translator.translate(); 		
		$(this).fadeTo('fast', 1);

	});

	$('#query').keyup(function () {
		if(Translator.requestTimeout != null){
			clearTimeout(Translator.requestTimeout);
			Translator.requestTimeout = null;
		}
		Translator.requestTimeout = setTimeout("Translator.translate()", 200);
	});

	$('#query').focus();
	Translator.translate();
});

