var preview_enabled = true;

$(document).ready(function() {
	$('.remove').click(function() {
		return confirm('Weet u zeker dat u dit item wilt verwijderen?');
	});
	
	$('.hide').hide();
	
	$('form.autosubmit').each(function() {
		form = this;
		$(this).find('input[@type="submit"]').hide();
		$(this).find('select').change(function () {
			$(form).submit();
		});
	});
	
	$('.clickable').each(function() {
		var link = $(this).find('a:first').attr('href');
		if (link != '') {
			$(this).children().not('.not-clickable').css('cursor', 'pointer');
			$(this).children().not('.not-clickable').click(function() {
				document.location.href = link;
			});
		}
	});
	
	init_show_on_hover();
	
	init_image_preview();
});

function init_image_preview(container) {
	$(container).find('.image_preview').mouseout(function() {
		preview_enabled = true
		$(this).fadeOut('fast');
		return false;
	});
	
	$(container).find('.small_image').click(function() {
		if (preview_enabled) {
			preview_enabled = false;
			$(this).parents('.container').find('.image_preview').fadeIn('slow');
		}
		return false;
	});
}

function show_on_hover_or_mouseover(trigger_element) {
	if (preview_enabled) {
		preview_enabled = false;
		var container = $('#'+$(trigger_element).attr('id').replace('show_on_hover_', ''));
		if ($(container).length) {
			$(container).fadeIn('slow');
		}
	}
}

function init_show_on_hover(selector) {
	$(selector).find('.show_on_hover').each(function() {
	
		if ($(this).is('a')) {
			$(this).mouseover(function() {
				show_on_hover_or_mouseover($(this));
				return true;
			});
			$(this).click(function() {
				return false;
			});
		}
		else {
			$(this).click(function() {
				show_on_hover_or_mouseover($(this));
				return false;
			});
		}
	});
	
	$(selector).find('.preview_data').mouseout(function() {
		if (!preview_enabled) {
			preview_enabled = true;
			$(this).fadeOut('fast');
		}
		return true;
	});
}

function number_format(number, number_of_decimals) {
	var absolute_value = Math.floor(number);
	var decimals = number - absolute_value;
	var decimals = Math.round(decimals*100).toString();
	for (var i = decimals.length; i < number_of_decimals; i++) {
		decimals = decimals + '0';
	}
	return absolute_value + ',' + decimals;
}
