// INI
$(document).ready(function(){
	// Select first item when loading the page
	//*****************************************
	$('#t1').focus();
	
	// Changing language
	//*****************************************
	$('#from').change(function(){
		$.cookie('from',this.value,{expires:365,path:'/'});
		// Hide auto-detect holder
		var qapi = $('#from').qtip('api');
		qapi.hide();
		// Automatic translation (if there is text)
		if($('#t1').val()){
			translate(true);
		}
	});
	$('#to').change(function(){
		$.cookie('to',this.value,{expires:365,path:'/'});
		// Automatic translation (if there is text)
		if($('#t1').val()){
			translate(true);
		}
	});
	
	// Key #t1
	//*****************************************
	$('#t1').keyup(function(e){
		if($('#t1').val()==''){
			// Clear #t2 if user deletes everything from #1
			$('#t2').val('');
			// Closes the auto-detect holder
			var qapi = $('#from').qtip('api');
			qapi.hide();
		} else {
			// Automatic translation when pasting data (control+V)
			if(e.ctrlKey==true && e.keyCode==86){
				// Translate clipboard data
				translate();
			} else {
				// Traduction on the fly (as-user-types)
				translate_input();
			}
		}
	});
	
	// Select all when focusing #t2
	//*****************************************
	$('#t2').focus(function(){
		this.select();
	});
	
	// Effects
	//*****************************************
	$('#content').corner('round');
	$('#footer').corner('round');
	
	//QTIP
	//*****************************************
	var qtFrom = $('#from').qtip({
		position: {
			corner: {
				target: 'leftMiddle',
				tooltip: 'rightMiddle'
			}
		},
		style: {
			width: 150,
			fontSize: 14,
			fontFamily: 'Arial',
			fontWeight: 'bold',
			textAlign: 'center',
			color: '#FFFFFF',
			background: '#214158',
			border: {
				width: 3,
				radius: 2,
				color: '#5C87A5'
			},
			tip: {
				corner: 'rightMiddle'
			},
			name: 'dark'
		},
		show: {
			when: false,
			effect: {
				type: 'slide',
				length: 140
			}
		},
		hide: {
			when: false,
			effect: {
				type: 'slide',
				length: 150
			}
		}
	});
});

// Translate
function translate(onthefly){
	// Display loader
	$('#t2').css('background-image','url(http://GAYAGA.com/ui/loading.gif)');
	// Normal translate()
	if(!onthefly){
		// Set current langs as default (with cookies)
		$.cookie('from',$('#from').val(),{expires:365,path:'/'});
		$.cookie('to',$('#to').val(),{expires:365,path:'/'});
	}
	// Auto-detect holder
	if(!$('#from').val()) {
		var qapi = $('#from').qtip('api');
	}
	// Check if t1 holds any data
	var self = $('#t1');
	if(self.val()){
		// Translate
		google.language.translate(self.val(),$('#from').val(),$('#to').val(),function(result){
			// Check if there are any errors
			if(!result.error){
				// Sanitaze string
				var s = result.translation;
				s = s.replace(/&#(\d+);/,function(m,g){return String.fromCharCode(g);});
				// Check if minimum translation has ocurred
				if(self.val()!=s){
					// Replace current traduction
					$('#t2').val(s);
					// Select traduction textarea
					if(!onthefly) $('#t2').focus();
					// Detect language
					if(!$('#from').val()){
						google.language.detect(self.val(),function(result){
							if(!result.error){
								// Get language fullname
								for(i in google.language.Languages){
									if(google.language.Languages[i] == result.language){
										var dLang = i.toLowerCase();
										break;
									}
								}
								// Information
								if(!$('#from').val()) {
									if(dLang != undefined){
										// Math.round((result.confidence)*100)
										qapi.updateContent(''+dLang+'?');
										qapi.show();
									} else {
										qapi.hide();
									}
								}
							} else {
								// Error ocurred
								alert(result.error.message);
							}
						});
					}
				} else {
					// Can't translate this... keep waiting
					if(!$('#from').val()) {
						$('#t2').val('');
						qapi.hide();
					}
				}
			} else {
				// Error ocurred
				alert(result.error.message);
			}
		});
	}
	// Timer
	if(onthefly) clearTimeout(t);
	// Remove loader
	$('#t2').css('background-image','none');
}
// Translator timer (when using on the fly)
var t;
function translate_input(){
	clearTimeout(t);
	t=setTimeout('translate(true)',1100);
}