
var clip = null;

var lang = {
	COPY_OK: 'A URL foi copiada para a área de transferência!',
	OOPS: 'Oops! Infelizmente nossos servidores agiram de forma inesperada. Tente novamente mais tarde.',
	OK: 'Pronto! A sua URL foi encurtada!',
	EMPTY_URL: 'A URL não pode estar vazia.',
	INVALID_URL: 'A URL não é válida. Ela deve ter no máximo 20 caracteres e conter somente números ou letras.',
	INVALID_URL_EXTENSION: 'A URL não é válida.',
	EXCEEDED_URL: 'A URL deve ter no máximo 4.096 caracteres.',
	EXISTING_URL: 'Esta URL encurtada já está relacionada com outro link.'
};

var block_ext = ['.exe','.bat','.msi','.pif','.src','.vbs'];

$(document).ready(function(){
	$('#url').focus();
	clipBoard();
});

$('#shorterUrl').focus(function(){
    this.select();
});

function verifyURL(url){
	var _return = true;
	for(var i = 0; i < block_ext.length; i++){
		if(url.indexOf(block_ext[i]) > -1){
			_return = false;
			break;
		}
	}
	return _return;
}

function clipBoard() {
	clip = new ZeroClipboard.Client();
	clip.setHandCursor(true);
	
	$('#btCopy').mouseover(function(){
		clip.setText($('#shorterUrl').val());

		if (clip.div) {
			clip.receiveEvent('mouseout', null);
			clip.reposition(this);
		}
		else clip.glue(this);

		clip.receiveEvent('mouseover', null);
	});
	
	clip.addEventListener('mouseOver', function (client) {
		clip.setText( $('#shorterUrl').val());
	});
	
	clip.addEventListener('mouseDown', function (client) {
		$('.notify').hide();
		showMessage(lang.COPY_OK);
	});
}

function create() {
	$('.urlShortened').slideUp('slow');
	$('.notify').hide();
	
	var inputUrl = $('#url').val();
	var dados = 'url=' + escape(inputUrl) + '&output=json&rnd=' + Math.floor(Math.random()*10000000);
	
	if(verifyURL(inputUrl)){
		$.ajax({
			async: false,
			type: 'POST',
			dataType: 'html',
			url: 'create.html',
			data: dados,
			timeout: 3000,
			success: function(retorno){
				var obj = $.parseJSON(retorno);
				if (obj.linkId != 0) {
					$('#originalUrl').html(obj.linkUrl);
					$('#infoUrl').html(obj.linkNiceUrl+'+').attr('href', obj.linkNiceUrl+'+');
					$('#shorterUrl').val(obj.linkNiceUrl);
	
					$('#facebookUrl').attr("href", "http://www.facebook.com/share.php?u=" + obj.linkNiceUrl);
					$('#twitterUrl').attr("href", "http://twitter.com/home?status=" + obj.linkNiceUrl);
					$('#linkedinUrl').attr("href", "http://www.linkedin.com/shareArticle?mini=true&url=" + obj.linkNiceUrl);
					$('#qrCodeImage').html('<img src="https://chart.googleapis.com/chart?chs=58x58&cht=qr&chl='+  escape(obj.linkNiceUrl) +'&chld=L|0&choe=UTF-8" alt="'+obj.linkNiceUrl+'" />');
					
					$('.urlShortened').slideDown(1000, function() { $('#shorterUrl').focus(); });
				}
				showMessage(lang[obj.message]);
			},
			error: function(retorno){
				showMessage(lang.OOPS);
			}
		});
		$('#url').val('');
	}else{
		showMessage(lang.INVALID_URL_EXTENSION);
	}
	return false;
}

var g_time = 0;
function showMessage(message) {
	if (message != '') {
		$('.notify').html(message);
		$('.notify').slideDown('slow');

		clearTimeout(g_time);

		g_time = setTimeout("$('.notify').slideUp('slow')", 5000);
	}
}
