function FitToContent(id, maxHeight)
{
   var text = id && id.style ? id : document.getElementById(id);
   if ( !text )
      return;

   var adjustedHeight = text.clientHeight;
   if ( !maxHeight || maxHeight > adjustedHeight )
   {
      adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
      if ( maxHeight )
         adjustedHeight = Math.min(maxHeight, adjustedHeight);
      if ( adjustedHeight > text.clientHeight )
         text.style.height = adjustedHeight + "px";
   }
}
$(function(){
	var defTeksts = "paste your long url here";
	$("#saite").click(function(){
		if (this.value == defTeksts){  
			this.value = '';  
 		}
 		if(this.value != defTeksts){
 			this.focus();  
 			this.select();  
		} 
	});
	$.getJSON("/stats.json",function(data) {
		var sm = data.skaits;
		var cl = data.clicks;
		$("#papildinfo").append(" &bull; <b>"+sm+"</b> smurls & <b>"+cl+"</b> clicks");
  	});
	$("#saite").blur(function() {  
		if ($.trim(this.value) == ''){  
			this.value = (this.defaultValue ? this.defaultValue : '');  
		}  
  	});
	$("#saite").keyup(function(){
		FitToContent(this, document.documentElement.clientHeight)
	});
	$("#aiziet").click(function(){
		var longurl = $("#saite").val();
		longurl = longurl.replace(/(\n\r|\n|\r)/gm,"");
		var kluda = false;
		if (longurl == "") {
			kluda = true;
		} else { kluda = false; }
		if (kluda == false) {
			$.get("/c?url="+encodeURIComponent(longurl), function(smurl) {
				$("#saite").val(smurl);
				$("#saite").css("height","18px");
				$("#aiziet").hide();
				$("#atpakal").show();
			});
		}
		return false;
  	});
	$("#atpakal").click(function(){
		$("#saite").val(defTeksts);
		$("#atpakal").hide();
		$("#aiziet").show();
	});
});