var duration = '800';
var duration_fast = '400';
var easing = 'jswing';

$(document).ready( function () {

	// Initialize Shadowbox
	Shadowbox.init();

	/**
	 * Team
	 */

	$('.member_content').each( function () {

		$(this).mouseenter(function () {

			$(this).find('.member_info').stop().animate({opacity: 1},200,easing);
		});

		$(this).mouseleave(function () {

			$(this).find('.member_info').stop().animate({opacity: 0},200,easing);
		});
	});

	/**
	 * Project filter
	 */

	var filter_display = false;

	$('#show_filter').click(function(){

		if (filter_display == false) {

			$('#project_filter').stop().fadeIn();

			$('#show_filter').toggleClass('active');
			$('#show_all').toggleClass('active');

			filter_display = true;

		} else {

			var n = $('.checkbox:checked').length;

			if (n == 0) {

				$('#project_filter').stop().fadeOut();

				$('#show_filter').toggleClass('active');
				$('#show_all').toggleClass('active');

				filter_display = false;
			}
		}
	});

	/* Branche filter deselector */
	$('#branche_deselect').click(function () {
		$('.checkbox_branche').each(function () {
			$(this).attr('checked',false);
			set_cases ();
		});
	});

	/* Service filter deselector */
	$('#service_deselect').click(function () {
		$('.checkbox_service').each(function () {
			$(this).attr('checked',false);
			set_cases ();
		});
	});

	$('.checkbox').each(function () {
		$(this).change(function () {
			set_cases ();
		});
	});

	set_cases ();

	/**
	 * Case image
	 */

	// Set action for each thumb
	$('.case_images .thumbs img').each(

		function(index){

			$(this).bind("click",

				function(){

					// Thumb ID
					var thumb_id = $(this).attr('id');

					// Large ID
					var large_id = thumb_id.replace('thumb_','');

					// Determine case ID
					var str_parts = thumb_id.split("_");
					var case_id = str_parts[3];

					// Display selected large image
					display_large_case_img (case_id, large_id);
				}
			);
		}
	);

	/**
	 *	Hover action on case filter
	 */
	$('#action_buttons li#show_filter').mouseenter(function () {

		$(this).css('text-decoration', 'underline');

	}).mouseleave(function () {
		$(this).css('text-decoration', 'none');

	});

	// Display first case images automatically
	$('.large_img').each(

		function(index){

			// Determine case ID
			var case_id = $(this).attr('id');
			var case_id = case_id.replace('img_c_con_','');

			// Large ID
			var large_id = 'img_c_'+case_id+'_'+1;

			// Display first large image of this case
			display_large_case_img (case_id, large_id);
		}
	);

	/**
	 * Apps
	 */

	$('#app_icons li').mouseover(function(){

		$(this).find(".hover").css('display','block');
	});

	$('#app_icons li').mouseout(function(){

		$(this).find(".hover").css('display','none');
	});

	/**
	 * Add target blank to Twitter links
	 */

	$('#twitter_update_list a').each( function () {

		$(this).attr('target', '_blank');
	});
});

/**
 * Display large case image
 */

function display_large_case_img (case_id, large_id) {

	// First hide current images
	$('#img_c_con_'+case_id+' img').each(

		function(index){

			$(this).css('display','none');
		}
	);

	// Display selected img
	$('img#'+large_id).fadeIn('fast');
}

/**
 * Set cases
 */

function set_cases () {

	// Fetch selected categories
	selected_categories = '';

	$('.checkbox').each(function () {

		if ($(this).attr('checked') == true) {

			selected_categories += $(this).attr('value')+',';
		}
	});

	// Remove last char
	selected_categories = selected_categories.substring(0, selected_categories.length-1)

	// Get new content
	$.get(
		'http://www.merkmeester.nl/data/cases/items.php', { selected_categories:selected_categories },
		function(result) {
			$('#project_overview').find("div.project").remove();
			$('#project_overview').append( $(result).find('div.project'));
			set_project_info ();
		}
	);
}

/**
 * Set project info
 */

function set_project_info () {

	$('.project_content').each( function () {

		$(this).mouseenter(function () {

			$(this).find('.project_info').stop().animate({opacity: 1},200,easing);
		});

		$(this).mouseleave(function () {

			$(this).find('.project_info').stop().animate({opacity: 0},200,easing);
		});
	});
}

