﻿var Dictionary = {

	caretpos: 0,

    mouseOver: function(component) {
    	var mail = "ma";
    	mail += "il";
    	mail += "@";
    	mail += "orosz-";
    	mail += "szotar";
    	mail += ".hu";
    	component.href="mailto:" + mail;		
    },        
	
	appendLetter: function(button) {		
		var letter = button.value;
		var word = $("word");				
		var shiftkey = $("shiftkey");
		if (letter == "^") {
			if (button.name=="capital") {
				button.name="little";
				button.className="little";
			}
			else {
				button.name="capital";
				button.className="capital";
			}
		}
		else {
		
			if (letter == "CR") {
				word.value="";
				Dictionary.caretpos = 0;
			}		
			else if (letter == "<") {
				if (word.value.length > 0 && Dictionary.caretpos > 0) {
					var firstpart=word.value.substring( 0, Dictionary.caretpos - 1 );
					var secondpart="";
					if (word.value.length > Dictionary.caretpos) {
						secondpart = word.value.substring( Dictionary.caretpos, word.value.length );
					}
					word.value = firstpart + secondpart;
					Dictionary.caretpos--;
				}
			}
			else if (letter == "_") {
				var firstpart=word.value.substring( 0, Dictionary.caretpos );
				var secondpart="";
				if (word.value.length > Dictionary.caretpos) {
					secondpart = word.value.substring( Dictionary.caretpos, word.value.length );
				}
				word.value = firstpart + " " + secondpart;				
				Dictionary.caretpos++;
			}
			else {
				if (shiftkey != null && shiftkey.name=="capital") {
					letter = letter.toUpperCase();
				}				
				var firstpart=word.value.substring( 0, Dictionary.caretpos );
				var secondpart="";
				if (word.value.length > Dictionary.caretpos) {
					secondpart = word.value.substring( Dictionary.caretpos, word.value.length );
				}
				word.value = firstpart + letter + secondpart;
				Dictionary.caretpos++;
			}
			
			//set the caret position						
			Dictionary.setCaretPosition(word);			
		}		
	},
	
	setCaretPosition: function(word, pos) {		
		if (pos) {
			Dictionary.caretpos = pos;
		}
		else {
			pos = Dictionary.caretpos;
		}		
		if (document.selection && navigator.userAgent.indexOf("Opera") == -1) {
			//IE
			word.focus();
			var selrange = document.selection.createRange();
			selrange.moveStart ('character', -word.value.length);
			selrange.moveEnd ('character', -word.value.length);
			selrange.moveStart ('character', pos);
			selrange.select();
		}
		else if (word.selectionStart) {	
			//Firefox, Opera, Safari
			word.select();
			word.selectionStart = pos;
			word.selectionEnd = word.selectionStart;
		}
	},
	
	getCaretPosIE: function() {
		var word = $("word");
		var i=word.value.length + 1;
		if (word.createTextRange){
			theCaret = document.selection.createRange().duplicate();
			while ( theCaret.parentElement() == word
				&& theCaret.move("character",1)==1 ) --i;
		}
		return i==word.value.length+1?-1:i;
	},
	
	storeCaretPosition: function() {
				
		var word = $("word");
		if (document.selection && navigator.userAgent.indexOf("Opera") == -1) {			
			//IE
			var word = $("word");
			Dictionary.caretpos = Dictionary.getCaretPosIE() - 1;		
			if (Dictionary.caretpos < 0) {						
				Dictionary.caretpos = word.value.length;			
			}			
		}
		else if (word.selectionStart || word.selectionStart == 0) {
			//Firefox, Opera, Safari
			Dictionary.caretpos = word.selectionStart;
			//word.select();
		}				
		
	},

    observeEnter: function() {
        Event.observe('word','keydown',function(event){			
			if (event.keyCode==13) {
				Dictionary.search();
			}
        });
    },
	
	search: function() {
		Dictionary.showSearchText();
		var word = Dictionary.encode($("word").value);
		var lang = $("language").value;
		var strictness = $("strictness").value;
		var resultdiv = $("result");
		var randomnumber = Math.random();
		var url = "/search.php?word="+word+"&language="+lang+"&strictness="+strictness+"&id="+randomnumber;			
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(transport) {
			resultdiv.innerHTML=transport.responseText;
			//reset caret position
			var wordfield = $("word");
			var pos = wordfield.value.length;
			Dictionary.setCaretPosition(wordfield, pos);			
		  }
		});
	},
	
	searchFromResults: function(string) {
		Dictionary.showSearchText();
		$("word").value = string;
		var word = Dictionary.encode(string);
		var lang = $("language").value;
		//select the opposite language
		if (lang == "russian") {
			lang = "hungarian";
		}
		else if (lang == "hungarian") {
			lang = "russian";
		}
		$(lang).selected = true;
		var strictness = $("strictness").value;
		var resultdiv = $("result");		
		var randomnumber = Math.random();
		var url = "/search.php?word="+word+"&language="+lang+"&strictness="+strictness+"&id="+randomnumber;			
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(transport) {
			resultdiv.innerHTML=transport.responseText;
			//reset caret position			
			var wordfield = $("word");
			Dictionary.caretpos = wordfield.textLength;
			Dictionary.setCaretPosition(wordfield);
		  }
		});
	},
	
	showSearchText: function() {
		var resultdiv = $("result");
		resultdiv.innerHTML="<table class='noresult'><tr class='rowcolor2'><td colspan='2' class='resultheader'>Kis türelmet kérek...</td></tr></table>";
	},
		
	encode: function(string) {
		return escape(Dictionary.utf8_encode(string));
	},		
    
	utf8_encode: function(string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
	
	utf8_decode : function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }

};

