window.addEvent('domready',function() { 
	new SmoothScroll({ duration:700 }, window); 
});

function nice_textarea(item, height) {
	window.addEvent('domready', function() {
		var h = $(item).getStyle('height').toInt();
		var w = $(item).getStyle('width').toInt();
		$(item).addEvents({
			focus: function() {
				if (h < height) this.morph({ 'height': height+'px'});
			},
			blur: function() {
				if (this.value.trim() == '') this.morph({ 'height': h+'px', 'width': w+'px' });
			}
		});
	});
}


function load_comments() {
	var window_buffer = 600;
	var windowVH;
	var window_size = $('all').getDimensions(true);
	
	setVisibleHeight();
	var position = window.getScroll();
	if (window_size.height - windowVH - position.y <= window_buffer) {
		loadMore();
	}
	
	window.addEvent('scroll', function() { 
		var position = window.getScroll();
		window_size = $('all').getDimensions(true);
		if (window_size.height - windowVH - position.y <= window_buffer) {
			loadMore();
		}
	});
	window.addEvent('resize', function() { 
		setVisibleHeight();
	});

	function setVisibleHeight() {
		var windowC = window.getCoordinates();
		windowVH = windowC.height;
	}
	function loadMore() {
		if ($('loadingc').get('value') == 1 && $('loadm').get('value') == 1) {
			$('loadingc').set('value', 0);
			loadMoreComments();
		}
	}
	function loadMoreComments(){
		var commentsLoad = $('comentarii_load')
		var req = new Request.HTML({url: '/alte-comentarii/', evalScripts:true, method: 'post', data: 'modul='+$('commentsmodul').get('value')+'&mid='+$('commentsmid').get('value')+'&commentsp='+$('commentsp').get('value'),
			onRequest: function() {
				commentsLoad.empty().addClass('spin');
			},
			onSuccess: function(html) {
				commentsLoad.empty().removeClass('spin');
				var newElement  = new Element('div').adopt(html);
				newElement.inject(commentsLoad, 'before');
			},
			onFailure: function() {
				commentsLoad.adopt('Eroare: Va rugam sa reincarcati pagina.');
			}
		});
		req.send();
	}
}
function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
	return true;
}
function postRequest(my_url, post_data, divid){
	var req = new Request.HTML({url: my_url, evalScripts:true, method: 'post', data: post_data,
		onRequest: function() { $(divid).empty().addClass('spin'); },
		onSuccess: function(html) { $(divid).empty().removeClass('spin').adopt(html); },
		onFailure: function() { $(divid).empty().adopt('Eroare: Va rugam sa reincarcati pagina.'); }
	});
	req.send();
}
