Array.prototype.append = function(elems){
	if( elems instanceof Array ){
		for( var i=0, n = elems.length; i < n; i++){
			this.push( elems[i] );
		}
	}else{
		this.push( elems );	
	}
}

// http://biasecurities.com/blogs/jim/archive/2006/08/08/4655.aspx 
if (!String.prototype.lTrim) {
    String.prototype.lTrim = function() { return this.replace(/^\s*/, ''); }
}
if (!String.prototype.rTrim) {
    String.prototype.rTrim = function() { return this.replace(/\s*$/, ''); }
}
if (!String.prototype.trim) {
    String.prototype.trim = function() { return this.lTrim().rTrim(); }
}


var Translator = {
	
	// http://www.codetoad.com/javascript_get_selected_text.asp
	getSelectedText : function(){
    	var txt = '';
 		if (window.getSelection){
    		txt = window.getSelection();
         }else if (document.getSelection){
    		txt = document.getSelection();
        }else if (document.selection){
    		txt = document.selection.createRange().text;
        }else 
        	return '';
        
        return (txt+'').trim();
	},
	
	openTranslateWindow : function(e){
		var txt = this.getSelectedText();
		if( !txt || txt.length < 2 )
			return;
		
		var strLangs = '';
		for( var i=0, n=this.langs.length; i < n; i++ ){
			strLangs += '&db=' + this.defLang + '-' + this.langs[i];
		}
		
		var URL = 'http://dictionary.todayszaman.com/dictionary/Dict.do?word=' + txt + strLangs;
		var wnd = window.open(URL, 'translator', 'toolbar=no, location=no, directories=no, status=no, menubar=no, \
		scrollbars=yes, resizable=yes, copyhistory=yes, width=300, height=300, left=750 , top=500');
		wnd.focus();
		/*
	   // Since we cannot make requests to the sites other than sender, we cannot use ajax.
	   var x = Event.pointerX(e);
	   var y = Event.pointerY(e)
	   	
	   var dictParams = 'word=' + txt + strLangs;	
	   var URL = this.localDictURL;
	   
	   var _translator = this;
	   new Ajax.Request(URL,
		{asynchronous: true, method: 'get', parameters : dictParams
		, onSuccess:
			function(transport){
				_translator.showDiv(transport.responseText, x , y );
			}			
	}) ; */
		
	},
	
	showDiv : function(msg, pointerX, pointerY){
		var hintDiv = $('dictHintDiv'); 
		this.removeDiv();
		
		hintDiv = document.createElement('div');
//	    var text = document.createTextNode(msg);
//	    hintDiv.appendChild(text);
    	hintDiv.innerHTML = msg;
    	
    	hintDiv.setAttribute('id','dictHintDiv');       
		hintDiv.className="dictionaryDiv";                   

		//set the inner styling of the div tag
		hintDiv.style.overflow = 'auto';
		hintDiv.style.width = '350px'; 
		hintDiv.style.position="absolute";       
		hintDiv.style.left= pointerX + 10 ;
		hintDiv.style.top= pointerY + 10 ;
		//hintDiv.style.backgroundColor = 'red';
		
		document.body.appendChild(hintDiv);
	}, 
	
	bindTranslationOnDblClick : function(defLang, langs, htmlElem){
		
		this.defLang = defLang;
		if( !this.defLang || this.defLang.length == 0){
			this.defLang = 'eng';
		}
		
		this.langs = langs;
		if( !this.langs || this.langs.length == 0){
			this.langs = ['tur'];
		}
		 var translator = this;
		 htmlElem.dblclick([translator], function() {
			translator.openTranslateWindow(this);
		});
		//this.localDictURL = localDictURL;
	},
	
	removeDiv : function(){
		var hintDiv = $('dictHintDiv'); 
		if( hintDiv ){
			document.body.removeChild(hintDiv);
		}
	},
	
	onKeyDownDoc : function(e){
		if( e.keyCode == Event.KEY_ESC ){
			this.removeDiv();
		}
	}
};

//Event.observe(document, 'keydown', Translator.onKeyDownDoc.bindAsEventListener(Translator));


