$(document).ready(function() {
	
	//Image previews
	$(".thumbPreview").hover(
		function(){
			var hoverElem = $(this);
			var imagePreview = parseInt($(this).find("span").html());
			var pos = hoverElem.position();
			imagePreview = $("#preview-"+imagePreview);
			imagePreview.css("top",(pos.top)+"px").css("left",((pos.left-imagePreview.width())-10)+"px").show();
		},
		function(){
			var hoverElem = $(this);
			var imagePreview = parseInt($(this).find("span").html());
			imagePreview = $("#preview-"+imagePreview);
			imagePreview.hide();
		}
	);
	
	//Character limiter on additionalinfo box
	var maxCharacters = 300;
	function limitChars(textid, limit, infodiv) {
		toggleSubmitButton();
		var text = $('#'+textid).val();
		var textlength = text.length;
		if(textlength > limit) {
			$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
			$('#'+textid).val(text.substr(0,limit));
			return false;
		} else {
			$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
			return true;
		}
	}
	if ($('#additionalinfo').length > 0) {
		limitChars('additionalinfo', maxCharacters, 'chCounter');
		$('#additionalinfo').keyup(function(){limitChars('additionalinfo', maxCharacters, 'chCounter');});
	}
	
	//AOP Number
	$("#aop").click(function(){
		if ($("#aop").attr('checked') == true) {
			$(".aopNumber").stop(true,true).slideDown(400);
		} else {
			$(".aopNumber").stop(true,true).slideUp(400);
		}
	});
	
});