function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) { // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	}
	// otherwise, update 'characters left' counter
	else {
		cntfield.value = maxlimit - field.value.length;
	}
}

function smurlify(elem) {
	text = encodeURIComponent(elem.value);
	$.getJSON("http://smurl.lv/smurlify?text="+text+"&callback=?", function(data){
		elem.value = data.text;
		FitToContent(elem, document.documentElement.clientHeight);
		textCounter(elem, document.myForm.remLen2,140);
	});
}
//
//function textCounter(field,cntfield,maxlimit) {
//	if (field.value.length > maxlimit) {
//		field.value = field.value.substring(0, maxlimit);
//	}
//	// otherwise, update 'characters left' counter
//	else {
//		cntfield.value = maxlimit - field.value.length;
//	}
//}
////  End -->

function focusToEnd() {
  var to_focus = document.getElementById("textfld");
  var to_collapse_to = to_focus.value.length;
  to_focus.focus();
  to_focus.setSelectionRange(to_collapse_to, to_collapse_to);
}


function highlight(field) {
    field.focus();
    field.select();
  		}
  		
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(){

	$(".reply").click(function(){
		var $postid = this.id;
		var $comments = $(this).parents(".r").children("#comments");
		if($comments.css("display") == "none") {		
			$.ajax({
				  url: "/comment.php",
				  data: "postid=" + $postid + "&trigger=1",
				  cache: false,
				  success: function(html){
				  	$comments.html(html);
				  	$comments.slideToggle("fast");
				  }
			});
		} else {
			$comments.slideToggle("fast");
		}
		return false;
	});
	
	$(".combtn").click(function(){
		var $postid = this.id;
		var $comments = $(this).parents(".r").children("#comments");
		if($comments.css("display") == "none") {
			$.ajax({
				  url: "/comment.php",
				  data: "postid=" + $postid,
				  cache: false,
				  success: function(html){
				  	$comments.html(html);
				  	$comments.slideToggle("fast");
				  }
			});
		} else {
			$comments.slideToggle("fast");
		}
		return false;
	});
	
	$(".photozoom").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'titlePosition' : 'over',
		'padding'		: 0,
		'hideOnContentClick':	'true'
	});
	
	var twatch = function(){
	    var timer = 0;  // store the timer id
	    return function(callback, ms){
	        clearTimeout (timer);  // if the function is called before the timeout
	        timer = setTimeout(callback, ms); // clear the timer and start it over
	    }  
	}();
	
	$(".textarea_post").keyup(function(){
		// var ta = this;
		// twatch(function(){ if(ta.value.length > 140) { smurlify(ta); } },100);
		FitToContent(this, document.documentElement.clientHeight);
		textCounter(this, document.myForm.remLen2,140);
	});
	
	$(".textarea_post").keydown(function(){
		// var ta = this;
		// twatch(function(){ if(ta.value.length > 140) { smurlify(ta); } },100);
		FitToContent(this, document.documentElement.clientHeight);
		textCounter(this, document.myForm.remLen2,140);
	});
	

	
	$("#addpic").click(function(){
		$("#pievienotbildi").click();
		return false;
	});
	
});