$(document).ready(function(){
	$.baseHref=document.location.protocol+'//'+document.location.hostname+'/';
	$('a').each(function(){
		// open external links in a new window
		if($(this).attr('href') && $(this).attr('href').match(/^https?:\/\//) && $(this).attr('href').substr(0, $.baseHref.length)!=$.baseHref){
			$(this).click(function(){
				window.open($(this).attr('href'));
				return false;
			});
		}
	});
	if(window.location.hash.match(/#a[0-9]+$/))
		showComments(window.location.hash.substring(2));
});




function showComments(id){
	$('#comment').slideUp();
	if(!$('#c'+id).length)
		return;
	if($('#c'+id).is(':hidden'))
		$('#c'+id).slideDown();
	else
		$('#c'+id).slideUp();
}
function addComment(id){
	$('#c'+id).slideUp();
	if(!$('#comment').attr('action'))
		initForm();
	$('#comment input[name=entry]').val(id);
	if($('#comment').parent().attr('id')!='i'+id){
		$('#comment').slideUp('normal', function(){
			$('#comment').appendTo('#i'+id);
			$('#comment').slideDown('normal', function(){ scrollTop(id); });
		});
	}
	else if($('#comment').is(':visible'))
		$('#comment').slideUp();
	else
		$('#comment').slideDown('normal', function(){ scrollTop(id); });
}
function initForm(){
	$('#comment').attr('action', 'comment.php');
	$('#comment').submit(function(){
		if($('#comment input[name=name]').val().length<3)
			return formError($('#comment input[name=name]'), 'Cume te cheama bai sulita bleaga?');
		if(!$('#comment input[name=email]').val().match(/^[a-z0-9_.-]+@[a-z0-9.-]+\.[a-z]+$/))
			return formError($('#comment input[name=email]'), 'Sa mori tu ca aia e adresa de mail?');
		if($('#comment textarea[name=message]').val().length<8)
			return formError($('#comment textarea[name=message]'), 'Zi ceva inainte sa apesi ca gloaba pe butoane!');
		else if($('#comment textarea[name=message]').val().length>255)
			return formError($('#comment textarea[name=message]'), 'Scrie mai putin si mai inteligent!');
	});
	$('#comment textarea[name=message]').keyup(function(){
		$('#chars').html(255-$(this).val().length);
	});
}
function formError(field, message){
	alert(message);
	field.focus();
	return false;
}
function scrollTop(id){
	$('html:not(:animated),body:not(:animated)').animate({scrollTop: $('#i'+id).offset().top}, 200, function(){
			window.location.hash='a'+id;
		});
}
