var openref = 0;
var referenties = new Array();
var slideTime = 1600;
var slideId = 1;

$(document).ready(function(){
	if ($('#paginanummer').html())
	{
		paginanummer = $('#paginanummer').val;
	}
	
	// flash inladen
	// als een div aangemaakt wordt met als class "flash",
	// en als inhoud het absolute pad naar de flash en de variable
	// wordt deze automatisch ingeladen
	$('.flash').each(function(){
		slideshowvars = $(this).find('.flashlink').html();
		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');
		$(this).html("");
		$(this).flash({
	    	src: slideshowvars,
	    	width: slideshowwidth,
	    	height: slideshowheight,
			wmode: 'transparent'
		});
	});
	
	
	$('.videoplayer').each(function(){
		slideshowvars = $(this).find('.flashlink').html();
		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');
		$(this).html("");
		$(this).flash({
	    	src: slideshowvars,
	    	width: slideshowwidth,
	    	height: slideshowheight,
			allowFullScreen: 'true'
		});
	});
	
	
	// h3 smal automatisch omzetten in flash
	$('h3.header_smal').each(function(){
		title = $(this).html();
		title = title.replace('&amp;','%26');
		$(this).html("");
		$(this).flash({
	    	src: '/public/flash/h3_smal.swf?titel=' + title,
	    	width: '300',
	    	height: '30',
			wmode: "transparent"
		});
	});
	
	// h3 breed automatisch omzetten in flash
	$('h3.header_breed').each(function(){
		title = $(this).html();
		title = title.replace('&amp;','%26');
		$(this).html("");
		$(this).flash({
	    	src: '/public/flash/h3_breed.swf?titel=' + title,
	    	width: '620',
	    	height: '30',
			wmode: "transparent"
		});
	});
	
	
	// subs in/uitklappen
	
	referenties = $('#referenties.loop .referentieitem'); //array
	if (referenties.length > 0)
	{
		slider();
		var referentieInterval = setInterval(slider,6000);
	}
	
	$('.referentieheader').click(function () {
		clearInterval(referentieInterval);
		$('.referentieitem').removeClass('active');
		$(this).parent().addClass('active');
		$('#referenties').find('.referentiecontent').each(function(){
			if (!$(this).parents('.referentieitem').hasClass('active')) slideUp($(this),slideTime);
		});
		slideDown($(this).parent().find('.referentiecontent'),slideTime);
		openref = 0;
	});


	//## Klapblokken ##

	// subs in/uitklappen
	$('.uitklaplink').click(function () {
		var obj = $(this).parents('.uitklapblok:first').find('.uitklapdetail:first');

		if (!$(this).hasClass('keepothers')) //andere blokken sluiten
		{
			$(this).parents('.uitklapblokken').find('.uitklapdetail').each(function(){
				if (!$(this).hasClass('dontclose')) //dit blok niet altijd open houden
				{
					$(this).slideUp();
					$(this).find('a.uitklaplink').removeClass('open');
				}
			})
		}

		//overal active verwijderen
		$(this).parents('.uitklapblokken').find('a.uitklaplink').removeClass('active');

		if (obj.css('display')=='none') //geklikt item openen
		{
			obj.slideDown();
			$(this).addClass('active');
			$(this).addClass('open');
		}
		else //geklikt item sluiten
		{
			obj.slideUp();
			$(this).removeClass('open');
		}
	});
	
	
	//cookies opslaan bij verlaten van pagina
	$(window).unload(function(){
		$('.uitklapblokken').each(function(){
			if ($(this).hasClass('enablecookie'))
			{
				$(this).find('.uitklaplink').each(function()
				{
					var id = $(this).attr('id');
					if (id != undefined && id)
					{
						if ($(this).hasClass('open'))
							$.cookie(id, 1);
						else
							$.cookie(id, 0);
					}
				});
			}
		});
	});
	//cookies uitlezen
	$('.uitklapblokken').each(function(){
		if ($(this).hasClass('enablecookie'))
		{
			$(this).find('.uitklaplink').each(function()
			{
				var id = $(this).attr('id');
				if (id != undefined && id)
				{
					if ($.cookie(id) == 1) //was uitgeklapt
					{
						if (!$(this).hasClass('open'))
							$(this).trigger('click'); //uitklappen
					}
				}
			});
		}
	});

	// links in "_blank" i.p.v. via HTML i.v.m. valid XHTML
	$('.blankwindow').attr('target','_blank');

	var menutimeout = null;
	$('#menu > ul > li').mouseover(function(){
		if (menutimeout) clearTimeout(menutimeout);
		$('#menu ul li').removeClass('hover');
		$(this).addClass('hover');
		$('#menu ul li ul.submenu').hide();
		$(this).find('ul.submenu').show();
	});
	$('#menu > ul > li').mouseout(function(){
		var hovered = this;
		if (!$(hovered).hasClass('current'))
		{
			menutimeout = setTimeout(function(){ //over 1 seconde het geopende menu terugzetten
				$("ul.submenu").hide('slow', function(){$('#menu ul li').removeClass('hover');});
				$('#menu > ul > li.current > ul.submenu').show('slow', function(){$('#menu ul li.current').addClass('hover');});
			}, 1000);
		}
	});

	$('.bedrijfsgegevens .routestart').click(function(){
		var div = $(this).parents('.bedrijfsgegevens:first');
		window.open('http://maps.google.nl/maps?f=d&source=s_d&saddr='+ encodeURI($(div).find('input[name=postcode]').val())+'&daddr=' + encodeURI($(div).find('.itsnadres').html() + ',' + $(div).find('.itsnpcplaats').html()) + '&hl=nl')
	})

	//field hints
	$(".hintfield").each(function(i) {
        var beginStyle = $(this).attr("style");
		$(this).addClass("hint");
		$(this).val($(this).attr("title"));

        $(this).bind("focus", function(e) {
            if ($(this).val() == $(this).attr("title")) {
                $(this).val("");
				$(this).removeClass("hint");
            }
        });
        $(this).bind("blur", function(e) {
            if ($(this).val() == "") {
				$(this).addClass("hint");
				$(this).val($(this).attr("title"));
            } else {
				$(this).css("border-color", "");
				$(this).removeClass("hint");
			}
        });
    });

	//submit form link
	$('.submitform').click(function(){
		$(this).parents('form:first').submit();
	})

	//AJAX FORMS automatisch
	$('form.ajaxform').each(function(){
		var form = $(this);
		$(this).ajaxForm({
			dataType: 'json',
			beforeSubmit: function(){
				form.parent().find('.errormelding').remove();
				
				//vereiste velden
				var ok = true;
				form.find('.required').each(function(){
					if ($(this).val().replace(' ', '') == '' || $(this).hasClass('hint')) //leeg of bevat nog de hint
					{
						ok = false;
						$(this).addClass('formerror');
					}
					else
						$(this).removeClass('formerror');
				});
				form.find('.required.email').each(function(){
					var regex = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
					if (!regex.test($(this).val()))
					{
						ok = false;
						$(this).addClass('formerror');
					}
				});
			
				if (ok)
				{
					//de hints niet meesturen
					form.find('input.hint').each(function(){
						$(this).val('');
					});
				}
				else
					return false;
			},
			success: function(response){
				if (response.success)
				{
					form.html('<p>'+response.melding+'</p>');
				}
				else if (response.melding){
					form.before($('<p class="errormelding">'+response.melding+'</p>'));
				}
			}
		})
	})

	//USP hover's
	//preloaden
	$('.usphover').each(function(){
		$('<img src="/public/img/middle/usp_slogan_'+ $(this).attr('rel')+'.gif"/>').hide().remove();
	});

	$('.usphover').hover(function(){
		$(this).parents('.uspinfo:first').find('.usptoelichting').css('background-image', 'url(/public/img/middle/usp_slogan_'+ $(this).attr('rel')+'.gif)')
	});

	//Referentie filters
	$('#referentiefilter select').change(function(){
		$(this).parents('form:first').submit();
	});

	//vacatureform stijl
//	$('#reactie .file').uniform();

});

function slider(){
	$('.referentieitem').removeClass('active');
	$(referenties[openref]).addClass('active');
	$('#referenties').find('.referentiecontent').each(function(){
		if (!$(this).parents('.referentieitem').hasClass('active')) slideUp($(this),slideTime);
	});
	slideDown($(referenties[openref]).find('.referentiecontent'),slideTime);
	if(openref < referenties.length - 1){
		openref++;
	} else {
		openref = 0;
	}
};


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);
	ppf *= 500;
    var orgh = object.height();
    var timeout = setInterval(function(){
		var newh = object.height() - ppf;
		if (newh < 0)
		{
			object.hide();
            object.height(orgh);
            clearInterval(timeout);
		}
		else
			object.height(newh);
    }, 50);
    return true;
}

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);
	ppf *= 500;
    var orgh = object.height();
    object.height(0);
    object.show();
    var timeout = setInterval(function(){
		var newh = object.height() + ppf;
		if (newh > orgh)
		{
			object.height(orgh);
            clearInterval(timeout);
		}
		else
			object.height(object.height() + ppf);
//        }
    }, 50);
    return true;
}