$(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'
		});
	});

    
	
	// Staqndaard image colorbox
	// rel is voor als er genavigeerd moet kunnen worden
	// <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>
	$('.js_colorbox').each(function(){
		$('.js_colorbox').colorbox({
			transition:'elastic'
		});
	});

	// Html tonen in colorbox:
	// <a class="js_htmlcolorbox" rel="idvanjehtmldiv">Klik</a>
	
	$('.js_htmlcolorbox').each(function(){
		$('.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').each(function(){
		$('.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:
	// rel is voor als er genavigeerd moet kunnen worden
	// <a class="js_youtubecolorbox" rel="testblaat" href="http://www.youtube.com/v/[movie-id]">Klik</a>
	// <a class="js_youtubecolorbox" rel="testblaat" href="http://www.youtube.com/v/[movie-id]">Klik</a>

	$('.js_youtubecolorbox').each(function(){
		$('.js_youtubecolorbox').colorbox({
			iframe:true,
			innerWidth: 640,
			innerHeight:505,
			transition:'elastic'
		});
   });
	

	// 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');
			}
		});
	}

	// slideshow (functie voor extern bedienen onderaan document)
	$('.js_slideshow').each(function()
	{
		$(this).cycle({
			fx:     'scrollRight',
			speed:  400,
			timeout: 5000,
			pager:  '.js_nav'
		});
	});
	
	// laatste x pagina's opslaan
    var cLastPages = $.cookie('lastpages');
    var lastPages = new Array();
    if (cLastPages) {
        lastPages = cLastPages.split("\\");
    }
    lastPages.push(document.location);
    while(10 < lastPages.length) {
        lastPages.shift();
    }
    var joined = lastPages.join("\\");
	var date = new Date();
    date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000));
    $.cookie('lastpages', joined, {path: '/', expires: date});

    $('.js_lastpagesfield').each( function() {
        $(this).attr('value',joined);
    });
	
	//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;
	})

	//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',
				beforeSerialize: function(){
					form.find('.js_hint').val('');
				},
				beforeSubmit: function(){
					form.parent().find('.js_errormelding').remove();
				},
				success: function(response){
					if (response.success)
					{
						form.html('<p>'+response.message+'</p>');
						
						if ( typeof _gaq == 'object' )
							_gaq.push(['_trackPageview', location.pathname + '-bedankt']);
						else if ( typeof pageTracker == 'object' )
							pageTracker._trackPageview( location.pathname + '-bedankt' );
					}
					else if (response.message)
					{
						form.before($('<p class="foutmelding js_errormelding">'+response.message+'</p>'));
					}
					form.find('.js_hint').trigger('blur');
				}
			})
		})
	}
	
	if ( 0 < $('.js_twitterfeed').length )
	{
		$.ajax({
			url: "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=venloonice&count=3&include_rts=true",
			success: function(data) {
				for(var i=0;i<data.length;i++)
				{
					data[i].text = data[i].text.replace(/(http:\/\/\S+)/g, "<a target='_blank' href='$1'>$1</a>")
					$('.js_twitterfeed').append("<li>"+data[i].text+'<span>'+relative_time(data[i].created_at)+"</span></li>");
				}
			},
			dataType: 'jsonp'
		});
	}
	
	
	
});

function relative_time(time_value) {
	var values = time_value.split(" ");
	time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	var parsed_date = Date.parse(time_value);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	delta = delta + (relative_to.getTimezoneOffset() * 60);
	if (delta < 60) {
		return 'minder dan 1 minuut geleden';
	} else if(delta < 120) {
		return 'ongeveer 1 minuut geleden';
	} else if(delta < (60*60)) {
		return 'ongeveer ' + (parseInt(delta / 60)).toString() + ' minuten geleden';
	} else if(delta < (120*60)) {
		return 'ongeveer 1 uur geleden';
	} else if(delta < (24*60*60)) {
		return 'ongeveer ' + (parseInt(delta / 3600)).toString() + ' uur geleden';
	} else if(delta < (48*60*60)) {
		return '1 dag geleden';
	} else {
		return (parseInt(delta / 86400)).toString() + ' dagen geleden';
	}
}
