$(function() {
	if ($('#Linkerkant_CCIPlaceHolder_CPB_Linkerkant:has(ul)').length > 0) {
		$('body').addClass('content_smal');
	}
	$("#Linkerkant_CCIPlaceHolder_CPB_Linkerkant #linkerkant .mid:empty").parent().remove();
	$(".showdate").each(updateMeetingDate);
	$('input:checkbox').addClass('checkbox')
	$('input:radio').addClass('radio')
	$(".google:empty").hide(function() {
		$(".tekst").removeClass("gmaps");
	});
	var showGotoMyProfile = function(id) {
		if (!document.getElementById(id))
			return;
		var el = document.getElementById(id);
		el.innerHTML = 'U bevindt zich nu op de website. Klik <a id="text_loggedin_vervanging" href="\/ledennet\/homepage">hier</a> om terug te keren naar het ledennet.';
		el.style.visibility = "visible";
	}
	showGotoMyProfile('text_loggedin');
	initShowCase.goto(0);
	/* Zoekformulier */
	$(".zoekformulier").each(function() {
		var minCharLength = 2;

		var searchInputText = $(this).find('input:text');
		var InputMessage = $(searchInputText).val();
		var searchInputButton = $(this).find('input:button');
		var searchInputHidden = $(this).find('input:hidden');
		var searchError = $(this).find('.error');
		var edited = false;
		var searchSubmit = function() {

			var searchTerm = $.trim(searchInputText.val());
			if (edited == true && (minCharLength == 0 || searchTerm.length > (minCharLength - 1))) {
				$("form").submit(function() { return false });
				window.location = searchInputHidden.val() + encodeURIComponent(searchTerm);
			} else {
				showError();
				return false;
			}
		}

		var showError = function() {
			//	window.alert(searchError.text());
			$(searchError).fadeIn()
			$("body").bind('click keydown', removeError);

		}
		var removeError = function() {
			$(searchError).fadeOut();
			$("body").unbind('click keydown', removeError);
		}

		$(searchInputText).keydown(function(e) {
			if (e.keyCode == 13) {
				return searchSubmit($(this));
			} else {
				edited = true;
			}
		}).focus(function() {
			if (edited == false) {
				$(this).val('').parent().addClass('active');
			}
		}).blur(function() {
			if (edited == false) {
				$(this).val(InputMessage).parent().removeClass("active");
			}
		}).click(function() { removeError() });

		$(searchInputButton).click(function() {
			return searchSubmit($(this));
		});


	});
	/*
	functie  removeEmptyOptionalElements:
	- zoek elementen met class minstens een van .blok, .tekst, .mid, .extra, .afbeelding, voor ieder van deze elementen:
	- start daarin removeEmptyOptionalElements, dus recursief.
	- bevat daarna het element geen tekst, en geen img, input,textarea,option of object, verwijder het dan.
	*/

	var removeEmptyOptionalElements = function(s) {
		if (typeof s == "undefined") s = $("body");
		/*
		s.find(".optieParent > .optieChild").each(
		function(){
		var t = "";
		removeEmptyOptionalElements($(this));
		t = $.trim( $(this).text().replace(/&nbsp;/g,' ') );
		if (t === "" && $(this).find("img,input,object").length == 0 )
		{
		$(this).parent.remove();
		}
		}
		);*/


		s.find(".blok, .tekst, .mid, .extra, .afbeelding, h2, h3").each(
		function() {
			var t = "";

			removeEmptyOptionalElements($(this));
			t = $.trim($(this).text().replace(/&nbsp;/g, ' '));
			if (t === "" && $(this).find("img,input,textarea,option,object,embed").length == 0) {
				$(this).remove();
			}
		}
	);
	}
	removeEmptyOptionalElements();
});
Date.prototype.showDate = function(endDate) {
	var d = this;

	var monthArray = ['jan', 'feb', 'mrt', 'apr', 'mei', 'jun', 'jul', 'aug', 'sept', 'okt', 'nov', 'dec'];
	var y = this.getYear();

	var t = "";
	t += this.getDate() + " ";
	t += monthArray[this.getMonth()] + "  ";
	t += y > 1900 ? y : (y + 1900);
	t += ", ";

	var beginTime = "";
	var hours = this.getHours();
	if (hours < 10) {
		beginTime += "0";
	}
	beginTime += hours + ":";
	var minutes = this.getMinutes();
	if (minutes < 10) {
		beginTime += "0";
	}
	beginTime += minutes;

	t += beginTime;

	if (typeof endDate != "undefined") {
		var endTime = "";
		var hours = endDate.getHours();
		if (hours < 10) {
			endTime += "0";
		}
		endTime += hours + ":";
		var minutes = endDate.getMinutes();
		if (minutes < 10) {
			endTime += "0";
		}
		endTime += minutes;

		if (endTime != beginTime) {
			t += " - ";
			t += endTime;
		}
	}

	t += " uur";

	return t;
}
function updateMeetingDate() {
	var parse = function(iso8601) {
		var s = $.trim(iso8601);
		s = s.replace(/-/, "/").replace(/-/, "/");
		s = s.replace(/T/, " ").replace(/Z/, " UTC");
		s = s.replace(/([\+-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400
		return new Date(s);
	}
	var begin = parse($(this).find(".begin").html());
	var einde = parse($(this).find(".einde").html());

	if (begin.toString().substring(0, 10) == einde.toString().substring(0, 10))// zelfde dag
	{
		$(this).find(".begin").html(begin.showDate(einde));

	} else {
		$(this).find(".begin").html(begin.showDate());
		$(this).find(".einde").html(einde.showDate());
		$(this).find(".eindeSpan").css("display", "inline");
	}
}


var initShowCase = function() {
	/* Aanpasbaar */
	/* html div */
	var container = "#showCase";
	/*  snelheid van overgang */
	var speed = 400;
	/* */
	var sec = 2;

	/* Animatie kan gestopt worden, door deze waarde op false zetten */
	var runShowCase = false;

	/* Bepaal cases en items */
	var cases = $(container).find(".titeltekst");
	var items = $(container).find(".afb");

	var hoverNavItem = function() {
		initShowCase.goto(cases.index($(this)));
	}
	var clickNavItem = function() { };

	var current = cases.filter(":eq(0)");
	var currentItem = items.filter(":eq(0)");
	var xm;
	current.addClass("active");
	items.fadeOut(0).removeClass("active");
	currentItem.fadeIn(speed).addClass("active");

	var show = function(n) {
		current.removeClass("active");
		current = cases.filter(":eq(" + n + ")");
		current.addClass("active");
		currentItem = items.filter(":eq(" + n + ")");

		if (!currentItem.hasClass("active")) {
			items.stop(true, true).fadeOut(speed).removeClass("active");
			currentItem.fadeIn(speed).addClass("active");
		}
		if (runShowCase == true || sec == 0) {

			xm = setTimeout('initShowCase.goto(' + parseInt((n + 1) > cases.length - 1 ? 0 : n + 1) + ')', sec * 1000.0);
		}

	}
	cases.each(function() {
		$(this).mouseenter(hoverNavItem).click(clickNavItem);
	});

	var _goto = function(x) {
		window.clearTimeout(xm);
		show(x);
	};

	return (
	{
		goto: function(x) { _goto(x); }
	});
} ();
