
// When the DOM is ready
$('document').ready(function(){

	/* -- Fancybox -- */

	// Set Fancybox defaults
	$.fn.fancybox.defaults.overlayColor = '#000';
	$.fn.fancybox.defaults.overlayOpacity = 0.7;
	$.fn.fancybox.defaults.hideOnContentClick = false;
	$.fn.fancybox.defaults.padding = 0;
	$.fn.fancybox.defaults.titlePosition = "inside";
	$.fn.fancybox.defaults.centerOnScroll = true;
	
	// Activate Fancybox links
	$('a.fancybox').fancybox();
	
	/* -- Input hints -- */

	$('input[title], textarea[title]').inputHint({});
	
	/* -- Thumbnails -- */

	// Fade in thumbnail images
	$('.tile a, .fancybox, .thumbnail').children('img').FadeInImageOnLoad();
	
	/* -- Custom checkboxes -- */
	
	$('input[type="checkbox"]').each(function() {
		// Was checked?
		var check = ($(this).attr('checked')) ? true : false;
		
		// Prepare the ID for the custom checkbox:
		var id = $(this).attr('name');
		
		// Replace with the custom checkbox
		$(this).replaceWith('<div class="custom-checkbox" id="'+ id +'"><!-- --></div>');
	
		$('#' + id).click(function() {
			doCustomCheckboxClick($(this));
		});
		
		$('label[for="' + id + '"]').click(function() {
			doCustomCheckboxClick($('#' + id));
			$(this).removeAttr('for');
		});
		
		// If the checkbox was selected
		if(check) {
			$('#' + id).trigger('click');
		}
	});
	
	/* -- Prevent dragging of links and images, and prevent right click -- */
	
	$('a, img').each(function() {
		this.draggable = false;
	}).mousedown(function(event) {
		event.preventDefault();
		return false;
	});
	
	$('img').bind("contextmenu", function(event) {
		event.preventDefault();
		return false;
	});
	
});

/**
 * A custom checkbox click
 */
function doCustomCheckboxClick($customCheckbox) {
	// If selected
	if($customCheckbox.hasClass('on')) {
		// Then, deselect now:
		setNewCustomCheckboxState($customCheckbox, false);
	}
	// If not selected
	else {
		// Then, select now:
		setNewCustomCheckboxState($customCheckbox, true);
	}
}

/**
 * Set new state of checkbox
 */
function setNewCustomCheckboxState($customCheckbox, newStateFlag) {
	// If selected
	if(! newStateFlag) {
		// Then, deselect now:
		$customCheckbox.removeClass('on').html('');
	}
	// If not selected
	else {
		// Then, select now:
		$customCheckbox.removeClass('on').addClass('on').html('<input type="hidden" name="'+ $customCheckbox.attr('id') +'" value="1" />');
	}
}

/**
 * Toggle a html-element
 * 
 * This function will show or hide (animated) a element
 *
 * @param element jQuery element
 * @param bool TRUE to show, FALSE to hide
 * @return void
 */
function toggleElement(element, displayFlag, speed) {
	if (!$(element).is(':animated')) {
		(displayFlag) ? $(element).stop().slideDown(speed) : $(element).stop().slideUp(speed);
	}
}
