document.createElement('header');
document.createElement('footer');
document.createElement('nav');
document.createElement('article');
document.createElement('section');
document.createElement('aside');

$(document).ready(function(){
	// flash inladen
	// als een div aangemaakt wordt met als class "js_flash",
	// en als inhoud het absolute pad naar de flash en de variable
	// wordt deze automatisch ingeladen
	$('.js_flash').each(function(){
		slideshowvars = $(this).find('.js_flashlink').html();
		//strip HTML comment from flash vars
		slideshowvars = slideshowvars.replace(/^<!--/, "");
		slideshowvars = slideshowvars.replace(/-->$/, "");
		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');
		$(this).html("");
		$(this).flash({
	    	src: slideshowvars,
	    	width: slideshowwidth,
	    	height: slideshowheight,
			wmode: 'transparent',
			allowFullScreen: 'true'
		});
	});

	// png fix op document
	// edaarna colorboxinladen (ivm conflict)
    if (typeof $().colorbox != 'function')
    {
        // png fix op document
        // edaarna colorboxinladen (ivm conflict)
        $(document).pngFix().getScript('/public/js/jquery.colorbox-min.js', function() {


            /**
             *	-- Gebruik --
             *	Geef een anchor de class .js_colorbox en de href de locatie van de afbeelding
             *	Voor een slideshow:
             *	Geef meerdere anchor's met .js_colorbox dezelfde rel="" waarde
             *	Voorbeeld slideshow:
             *	<a class="js_colorbox" rel="testblaat" href="/public/img/video2.jpg">klik</a>
             *	<a class="js_colorbox" rel="testblaat" href="/public/img/banner.jpg">klik</a>
             */
            //set
            $('.js_colorbox').colorbox({
                transition:'elastic',
				current: "{current} / {total}"
            });

            /**
            * Html tonen in colorbox:
            * <a class="js_htmlcolorbox" rel="idvanjehtmldiv">Klik</a>
            */
            $('.js_htmlcolorbox').click(function(){
                var targetid = $(this).attr('rel');
                if (typeof targetid == 'undefined' || targetid == '') return ;

                $(this).colorbox({
                    open: true,
                    inline:true,
                    transition:'none',
                    href:"#" + targetid
                });
            });

            /**
            * Contact form tonen in colorbox:
            * Verschil met htmlcolorbox is
            * dat deze de pagina herlaadt als hij sluit
            * (om form te resetten)
            * <a class="js_contactcolorbox" rel="idvanjehtmldiv">Klik</a>
            */
            $('.js_contactcolorbox').click(function(){
                var targetid = $(this).attr('rel');
                if (typeof targetid == 'undefined' || targetid == '') return ;

                $(this).colorbox({
                    open: true,
                    inline:true,
                    transition:'none',
                    href:"#" + targetid,
                    onClosed:function(){
                        // pagina herladen
                        if(ajaxFormChanged) {
                            window.location.href = window.location.href;
                        }
                    }
                });
            });

            /**
            * Youtube movie tonen in colorbox:
             *	Voor een slideshow:
             *	Geef meerdere anchor's met .colorbox dezelfde rel="" waarde
             *	Voorbeeld slideshow:
            * <a class="js_youtubecolorbox" rel="testblaat" href="http://www.youtube.com/v/[movie-id]">Klik</a>
            */
            $('.js_youtubecolorbox').colorbox({
                iframe:true,
                innerWidth: 640,
                innerHeight:505,
                transition:'elastic'
            });
        });
    }
	
	// slideopties
	$(".js_optielink").click(function ()
	{
		holderToSlide = $(this).parent().parent().siblings('.js_optiecontent');
		if(holderToSlide.css('display')=='none')
		{
			$(this).addClass('active');
			holderToSlide.slideDown();
		}
		else
		{
			$(this).removeClass('active');
			holderToSlide.slideUp();
		}
	});

	// advanced filter funtions block enable/disable
	if(typeof $.cookie == 'function')
	{
		//## Filteropties ##
		$('.js_filteroptieslink').click(function ()
		{
			if($('.js_filteropties').css('display')=='none')
			{
				$.cookie("advanced_filter", 'true', {path: '/'});
				$('.js_adv_option').attr("disabled", false);
				$(this).addClass('active');
				$('.js_filter_uitgebreid').show();
				$('.js_filter_standaard').hide();
				$('.js_filteropties').slideDown();
				$('.js_filteropties').addClass('active');
			}
			else
			{
				$.cookie("advanced_filter", null, {path: '/'});
				$('.js_adv_option').attr("disabled", true);
				$(this).removeClass('active');
				$('.js_filter_standaard').show();
				$('.js_filter_uitgebreid').hide();
				$('.js_filteropties').slideUp();
				$('.js_filteropties').removeClass('active');
			}
			$(this).parents('form:first').submit();
		});

		if($.cookie('advanced_filter') == 'true')
		{
			$('.js_adv_option').attr("disabled", false);
			$('.js_filteroptieslink').addClass('active');
			$('.js_filter_uitgebreid').show();
			$('.js_filteropties').show();
			$('.js_filteropties').addClass('active');
		}
		else
		{
			$('.js_adv_option').attr("disabled", true);
			$('.js_filter_standaard').show();
		}
	}

	$('input.js_filteroption:checkbox').click(function(event){
		$(this).parents('form:first').submit();
	});

	$('select.js_filteroption').change(function(event){
		$(this).parents('form:first').submit();
	});


	//## Klapblokken ##
	if (0 < $('.js_uitklapblokken').length)
	{
		$('.js_uitklaplink').click(function ()
		{
			var obj = $(this).parents('.js_uitklapblok:first').find('.js_uitklapdetail:first');

			if (!$(this).hasClass('js_keepothers')) //andere blokken sluiten
			{
				$(this).parents('.js_uitklapblokken').find('.js_uitklapdetail').each(function(){
					if (!$(this).hasClass('js_dontclose')) //dit blok niet altijd open houden
					{
						$(this).slideUp();
						$(this).find('.js_uitklaplink').removeClass('open');
					}
				})
			}

			//overal active verwijderen
			$(this).parents('.js_uitklapblokken').find('.js_uitklaplink').removeClass('active');

			if (obj.css('display')=='none') //geklikt item openen
			{
				obj.slideDown('fast', function(){$(this).css('display', 'block');}); //callback zodat het in IE8 nog steeds werkt
				$(this).addClass('active');
				$(this).addClass('open');
			}
			else //geklikt item sluiten
			{
				obj.slideUp('fast');
				$(this).removeClass('open');
			}
		});
	}

	//field hints
	$(".js_hintfield").each(function(i) {
        var beginStyle = $(this).attr("style");
		$(this).addClass("js_hint");
		$(this).val($(this).attr("title"));

        $(this).bind("focus", function(e) {
            if ($(this).val() == $(this).attr("title")) {
                $(this).val("");
				$(this).removeClass("js_hint");
            }
        });
        $(this).bind("blur", function(e) {
            if ($(this).val() == "") {
				$(this).addClass("js_hint");
				$(this).val($(this).attr("title"));
            } else {
				$(this).removeClass("js_hint");
			}
        });
    });

	//submit form link
	$('.js_submitform').click(function(){
		$(this).parents('form:first').submit();
		return false;
	})
	
	
	// sliders themablokken op home
	$(".js_themaslider").sudoSlider({ 
        prevNext: false,
		fadespeed:"300",
		fade: true,
        crossFade: true
    });
	$('a.js_themasliderlink').mouseenter(function(){
		var item = $(this).attr('rel');
		$(this).closest(".themablok").find(".js_themaslider").sudoSlider(item);
	});

	// sliders navblokken
	$(".js_navblokslider").sudoSlider({ 
        prevNext: false,
		speed: "5000",
		fadespeed:"300",
		fade: true,
        crossFade: false,
		auto: true
    });
	

	//AJAX FORMS automatisch
	if (typeof $().ajaxForm == 'function')
	{
		$('form.js_ajaxform').each(function(){
			$(this).append('<input type="hidden" name="ajax_post" value="oke" />');
			var form = $(this);
			$(this).ajaxForm({
				dataType: 'json',
				beforeSubmit: function(){
					form.parent().find('.js_errormelding').remove();
					form.find('input.js_hint').removeClass('js_hint');
					form.find('input.js_hint').unbind();
					$('.js_hinttext').remove();
					form.find('.js_submitform').hide();
				},
				success: function(response){
					if (response.success)
					{
						form.html('<p>'+response.message+'</p>');
						$('.tarieven').html('&nbsp;');

						if ( typeof _gaq == 'object' )
							_gaq.push(['_trackPageview', location.pathname + '-bedankt']);
						else if ( typeof pageTracker == 'object' )
							pageTracker._trackPageview( location.pathname + '-bedankt' );
					}
					else if (response.message && response.errors)
					{
						form.find('.js_submitform').show();
						form.before($('<div class="twee_kolom errormelding margintop left js_errormelding">'+response.message+'</div>'));

						// loop trough errors and add message popup on mouseover where needed
						for(var i in response.errors)
						{
							$("#"+i).addClass('js_hint');
							$("#"+i).addClass('error');
							$('body').append("<span class='js_hinttext tooltip' id='"+"tooltip_"+i+"'>"+response.errors[i]+"</span>");

							$("#"+i).bind('mouseover', {input:i},function(e){
								$("#tooltip_"+e.data.input).show();
							});

							$("#"+i).bind('mousemove', {input:i}, function(e){
								positionTooltip(e, "tooltip_");
							});

							$("#"+i).bind('mouseout', {input:i}, function(e){
								$("#tooltip_"+e.data.input).hide();
							});

							$("#"+i).bind('keydown', {input:i}, function(e){
								$("#"+e.data.input).removeClass('js_hint');
								$("#"+e.data.input).removeClass('error');
								$("#"+e.data.input).unbind();
								$("#tooltip_"+e.data.input).remove();
							});

							$("#"+i).bind('change', {input:i}, function(e){
								$("#"+e.data.input).removeClass('js_hint');
								$("#"+e.data.input).removeClass('error');
								$("#"+e.data.input).unbind();
								$("#tooltip_"+e.data.input).remove();
							});
						}
					}
					else if (response.message)
					{
						form.find('.js_submitform').show();
						form.before($('<div class="twee_kolom errormelding margintop left js_errormelding">'+response.message+'</div>'));
					}
					form.find('.js_hint').trigger('blur');
					$('html, body').animate({scrollTop:0}, 0);
				}
			})
		});

		initializeFilterForm();
	}

	// impressions link, colorox items where already initialized
	$('.js_impressions').click(function(){
		$('.js_impressions_holder a:first-child').colorbox({open:true});
	});

	$('.js_tarieven li').click(function(){
		if(!$(this).hasClass('js_tarief_type'))
		{
			$('.js_tarieven li').removeClass('selected');
			$(this).addClass('selected');
		}

		var error = false;
		var id_parts = $('.js_tarieven li.selected').attr("id").split('_');
		var tariefgroep_id = id_parts[1];
		var prijs_type = $("input[name=tarief_type]:checked").val();
		var arrangement_id = $(".js_arrangement_id").val();
		var activiteit_id = $(".js_activiteit_id").val();
		var language = $(".js_language").val();

		var datastring = '&tariefgroep=' + tariefgroep_id + '&prijs_type=' + prijs_type + "&language=" + language;

		if(typeof activiteit_id != 'undefined')
		{
			datastring += '&type=activiteit&activiteit_id=' + activiteit_id;
		}
		else if(typeof arrangement_id != 'undefined')
		{
			datastring += '&type=arrangement&arrangement_id=' + arrangement_id;
		}
		else
			error = true;

		if(!error)
		{
			$.ajax({
				type: "POST",
				url: "/public/ajax/activiteiten/tarieven.php",
				data: datastring,
				dataType: "json",
				success: function(response){
					if(response.success)
					{
						for(var i in response.tarieven)
						{
							$('.js_tarieven li#tariefgroep_'+i + ' span.js_tariefprice').html(response.tarieven[i]);
						}
						for(var i in response.activiteiten)
						{
							for(var j in response.activiteiten[i])
							{
								$('.js_optionprice_'+i+'_'+j).html(response.activiteiten[i][j]);
							}
						}
					}
				},
				error: function(reqObject, error, ex){

				}
			});
		}
	});

	$('select[class*=js_optie_]').change(function(){
		var id_parts = $(this).attr("id").split("_");

		if(typeof id_parts[1] != 'undefined')
		{
			$('label[class*=js_optionprice_' + id_parts[1] + '_]').removeClass('active');
			$('.js_optionprice_' + id_parts[1] + '_' + $(this).val()).addClass('active');
		}
	});


	$('.js_aantal').change(function(){
		// update options, check if number of people is possible
		var arrangement_id = $(".js_arrangement_id").val();
		var language = $(".js_language").val();
		var paramstring = "&language=" + language;

		if(typeof arrangement_id != 'undefined')
		{
			paramstring += '&arrangement_id=' + arrangement_id;

			if(0<$('#aantal_volwassenen').length)
				paramstring += "&adults="+$('#aantal_volwassenen').val();
			if(0<$('#aantal_kinderen').length)
				paramstring += "&kids="+$('#aantal_kinderen').val();

			$.ajax({
				type: "POST",
				url: "/public/ajax/activiteiten/opties.php",
				data: paramstring,
				dataType: "json",
				success: function(response){
					if(response.success)
					{
						var found = false;
						for(var i in response.activiteiten)
						{

							$('.js_optie_' + i + ' option').each(function(){
								if($(this).val() != '')
								{
									// alleen verwijderen als deze niet in resultaat voorkomt.
									found = false;
									for(var k in response.activiteiten[i])
									{
										if($(this).val() == k)
											found = true;
									}
									if(!found)
										$(this).remove();
								}
							});

							for(var j in response.activiteiten[i])
							{
								// alleen toevoegen als deze er nog niet is.
								found = false;

								$('.js_optie_' + i + ' option').each(function(){
									if($(this).val() == j)
										found = true;
								});
								if(!found)
									$('.js_optie_' + i).append('<option value="'+j+'">'+response.activiteiten[i][j]+'</option>');
							}
						}
					}
				},
				error: function(reqObject, error, ex){
				}
			});
		}
	});

	$('.js_totalen').change(function(){
		var error = false;
		var arrangement_id = $(".js_arrangement_id").val();
		var activiteit_id = $(".js_activiteit_id").val();
		var language = $(".js_language").val();
		var paramstring = "&language=" + language;

		if(typeof activiteit_id != 'undefined')
		{
			paramstring += '&type=activiteit&activiteit_id=' + activiteit_id;
		}
		else if(typeof arrangement_id != 'undefined')
		{
			paramstring += '&type=arrangement&arrangement_id=' + arrangement_id;
		}
		else
			error = true;

		if(0<$('#aantal_volwassenen').length)
			paramstring += "&adults="+$('#aantal_volwassenen').val();
		if(0<$('#aantal_kinderen').length)
			paramstring += "&kids="+$('#aantal_kinderen').val();
		
		$('select[class*=js_optie_]').each(function(){
			if($(this).val() != '')
			{
				var parts = $(this).attr('id').split('_');
				paramstring += "&optie["+parts[1]+"]="+$(this).val();
			}
		});

		if(!error)
		{
			$.ajax({
				type: "POST",
				url: "/public/ajax/activiteiten/totalen.php",
				data: paramstring,
				dataType: "html",
				success: function(data){
					$('.js_totalen_blok').html(data);
				},
				error: function(reqObject, error, ex){
				}
			});
		}
	});

	$(".js_calendar").one("mouseover", function(){
		Calendar.setup({
				inputField     :    "datum",   //"datum",     // id of the input field
				ifFormat       :    "%d-%m-%Y",  //"%d-%m-%Y",      // format of the input field
				button         :    "calendar",  //"i_datum",  // trigger for the calendar (button ID)
				showsTime	   :	false,
				singleClick    :    true,
				dateStatusFunc :	getDateStatus,
				onSelect	   :    customSelect
		});
	});

	if(0<$('#calendarholder').length)
	{
		Calendar.setup({
				flat           :    "calendarholder",
				ifFormat       :    "%d-%m-%Y",  //"%d-%m-%Y",      // format of the input field
				showsTime	   :	false,
				singleClick    :    true,
				dateStatusFunc :	getDateStatus,
				onSelect	   :    customSelect

		});
	}

	// links in "_blank" i.p.v. via HTML i.v.m. valid XHTML
	$('.js_blankwindow').attr('target','_blank');
});

var customSelect = function(calendar, date){
    if (calendar.dateClicked) {
		$(calendar.params.inputField).val(date);
		calendar.callCloseHandler(); // this calls "onClose" (see above)
	}
}


function getDateStatus(date, y, m, d)
{
	var today = new Date();
	var checkdate = new Date(y, m, d, 23, 59, 59);

	// disable past dates
	if(checkdate<today)
	{
		return "disabled";
	}
	else
	{
		if(dateDeelsVol(y,m,d))
		{
			return "deels";
		}
		else
		{
			if(dateVol(y,m,d))
				return "vol";
			else
				return false;
		}
	}
}

function dateDeelsVol(year, month, day)
{
	var y = deels_vol[year];

	if(!y) return false;

	var m = y[month];

	if(!m) return false;

	for(var i in m)
	{
		if(m[i] == day)	
			return true;
	}

	return false;
}

function dateVol(year, month, day)
{
	var y = vol[year];

	if(!y) return false;

	var m = y[month];

	if(!m) return false;

	for(var i in m)
	{
		if(m[i] == day)
			return true;
	}

	return false;
}

// Post filter form and filter results with quicksand
var initializeFilterForm = function(){
	$('form.js_filterform').each(function(){
		$(this).ajaxForm({
			dataType: 'html',
			success: function(data){
				if(0<$(data).find('li.js_arrangement').length)
					$('.js_arrangementen_blok').show();
				else
					$('.js_arrangementen_blok').hide();

				if(0<$(data).find('li.js_arrangement').length)
					$('.js_arrangementen_noresult').hide();
				else
					$('.js_arrangementen_noresult').show();

				if(0<$(data).find('li.js_activiteit').length)
					$('.js_activiteiten_blok').show();
				else
					$('.js_activiteiten_blok').hide();

				if(0<$(data).find('li.js_activiteit').length)
					$('.js_activiteiten_noresult').hide();
				else
					$('.js_activiteiten_noresult').show();

				$('.js_arrangementen').quicksand($(data).find('li.js_arrangement'), {adjustHeight: 'dynamic', attribute: 'id', easing: 'easeOutCubic', duration: 200});
				$('.js_activiteiten').quicksand($(data).find('li.js_activiteit'), {adjustHeight: 'dynamic', attribute: 'id', easing: 'easeOutCubic', duration: 200});

			}
		})
	});
}


function positionTooltip(event, tooltip){
	var posX = event.pageX+10;
	var posY = event.pageY+10;

	$('#'+tooltip+event.data.input).css({left: posX, top: posY});
}
/*
//slide an element up without causing blinking flash content
 
function slideUp(object, speed, slideTime)
{
    if (parseInt(speed) == NaN) return false;
    if (object.jquery == undefined) object = $(object);
	if (object.height() <= 0) return true;
    object.css('overflow', 'hidden');
    var ppf = (object.height() / speed * 20);
    var orgh = object.height();
    var timeout = setInterval(function(){
        object.height(object.height() - ppf);
        if (object.height() <= 0){
            object.hide();
            object.height(orgh);
            clearInterval(timeout);
        }
    }, 10);
    return true;
}

//slide an element down without causing blinking flash content
function slideDown(object, speed)
{
    if (parseInt(speed) == NaN) return false;
    if (object.jquery == undefined) object = $(object);
    object.css('overflow', 'hidden');
    var ppf = (object.height() / speed * 20);
    var orgh = object.height();
    object.height(0);
    object.show();
    var timeout = setInterval(function(){
        object.height(object.height() + ppf);
        if (object.height() >= orgh){
            object.height(orgh);
            clearInterval(timeout);
        }
    }, 10);
    return true;
}
 */
