$(function() {
	
	if ($.browser.msie && $.browser.version == '6.0') {
		$('#navmain a, .check, .star').ifixpng();
		$('#stapel').attr('src', 'images/stapel2.gif');
	}

	$('#navcontent a:last').css('border', 'none');
	
	$('table').find('tr:even:has(td)').addClass('even');
	
	// $('a[href*=ondernemendnederlands.nl]').addClass('on');
	// $('a[href*=nahw.nl]').addClass('nahw');
	// $('a[href*=nederlandsonderanderen.nl]').addClass('noa');
	// $('a[href*=thuisinnederlands.nl]').addClass('tin');
	// $('a[href*=ronduitnederlands.nl]').addClass('rn');
	// $('a[href*=klaarvoordestart.org]').addClass('kvds');
	
	$('a[href=#]').click(function() { return false; });
	
	$('a[href$=".pdf"]').addClass('pdf').attr("rel","external");
	
	$("a[href^='http://']:not([href*='"+location.host+"']),area[href^='http://']:not([href*="+location.host+"])").attr("rel","external");
	$("a[rel=external],area[rel=external]").attr("target","_blank").addClass('external').each(function(i) {
		var title = $(this).attr("title") ? $(this).attr("title") : $('img', this).attr("title");
		title = $.trim(title) ? $.trim(title) : $.trim($(this).text());
		if (title) $(this).attr("title", title + " (Externe link)");
		else $(this).attr("title", "Externe link");
	}).filter(":has(img), .portal-klant, .button").removeClass('external').removeClass('pdf');
	
	$('a[rel*=lightbox]').lightBox();
	
/*	
	$('#navhead ul a:contains("Zoeken")').mouseover(function() {
		$('#zoeken').slideToggle(250,function() {
			$('#query').select();
		});
	}).click(false);
	
	$('#header').mouseleave(function() {
		$('#zoeken').slideUp(400);
	});
*/	
	$('#query').focus(function() { 
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	}).blur(function() {
		if ($.trim(this.value) == '') {
			this.value = this.defaultValue;
		}
	});
	
	$('#zoekoptions').find('select').change(function() {
		$('#zoek2').attr('method','post').submit();
	}).end().find('input:not("[type=submit]")').click(function() {
		$('#zoek2').attr('method','post').submit();
	});

	$('#zoek2').find('[type=submit]').remove().end().submit(function() {
		if (this.method.match(/post/i)) {
			$(this).find('[type=text],[type=hidden]').removeAttr('name');
			// alert('post');
		} else {
			$('#zoekoptions').find('select,input').removeAttr('name');
		}	
	});
	
	
	$('#acties').show();

	reactiesysteem = '<ul id="actiemenu">';
	reactiesysteem += '<li id="actie_reactie"><a href="#">Stuur een reactie naar ITpreneurs</a></li>';
	reactiesysteem += '<li id="actie_door"><a href="#">Doorsturen naar een collega</a></li>';
	if (!ie6()) {
		reactiesysteem += '<li id="actie_print"><a href="#">Print dit artikel</a></li>';
		reactiesysteem += '<li id="actie_abo"><a href="#">RSS feed van de laatste artikelen</a></li> </ul>';
	}
	reactiesysteem += '</ul>';
	
	$('#actiemenu').replaceWith(reactiesysteem);

	$('#actiemenu')
	.find('#actie_reactie a').click(function() {
		$('#actiemenu').hide();
		$('#reactie').slideDown(function() {
			$(this).find('input:blank:first').focus();
		});
		return false;
	}).end()
	.find('#actie_door a').click(function() {
		$('#actiemenu').hide();
		$('#doorsturen').slideDown(function() {
			$(this).find('input:blank:first').focus();
		});
		return false;
	}).end()
	.find('#actie_print a').click(function() {
		window.print();
		return false;
	}).end()
	.find('#actie_abo a').click(function() {
		window.open('http://www.itpreneurs.nl/feed/feed.xml');
		return false;
	});

	$("#acties :reset").click(function() {
		$('#acties').find('form').slideUp();
		reactieValidate.resetForm();
		doorsturenValidate.resetForm();
		// acties.resetForm();
		$('#actiemenu').show();
	});
	
	

	$('.contactform').prepend('<div class="actie-load"><div><img src="/styles/images/lightbox/lightbox-ico-loading.gif" alt="" /></div></div><input type="hidden" name="ajax" value="1" />');


	$('.actie-load').ajaxStart(function() {
		$(this).show();
	
		var winTop = $(window).scrollTop();
		var formTop = $('.contactform').offset().top-20;
		if (winTop > formTop) $('html, body').animate({scrollTop: formTop});
		
	});
	
	
	$.validator.setDefaults({
		groups: {
			pc_plaats: "pc_cijfers pc_letters plaats",
			fact_pc_plaats: "fact_pc_cijfers fact_pc_letters fact_plaats"
		},
		rules: {
			aantal_tin: {
				required: "#profiel_ogo:checked"
			},
			aantal_nahw: {
				required: "#profiel_werk:checked"
			},
			aantal_noa: {
				required: "#profiel_maat:checked"
			},
			aantal_on: {
				required: "#profiel_onder:checked"
			},
			aantal_rn: {
				required: "#profiel_kns:checked"
			},
			aantal_kvds: {
				required: "#profiel_a0-a1:checked"
			}
		},
		errorPlacement: function(error, element) {
			if (element.attr("name") == "profiel_check[]")
				error.insertAfter(element.parent().parent('ul'));
			else if (element.attr("id").match(/^aantal/))
				error.insertAfter(element.parent().nextAll(':last'));
			else if (element.attr("type") == "radio" || element.attr("type") == "checkbox")
				error.insertAfter(element.nextAll(':last'));
			else if (element.attr("name") == "pc_cijfers" || element.attr("name") == "pc_letters")
				error.insertAfter("#plaats");
			else if (element.attr("name") == "fact_pc_cijfers" || element.attr("name") == "fact_pc_letters")
				error.insertAfter("#fact_plaats");
			else
				error.insertAfter(element);
		}
	});
	
	
	var reactieValidate = $("#reactie").validate({
		submitHandler: function(form) {
			submitActie(form);
		}
	});

	var doorsturenValidate = $("#doorsturen").validate({
		submitHandler: function(form) {
			submitActie(form);
		}
	});
	
	function submitActie(form) {
		$(form).ajaxSubmit(function() {
			$(form).slideUp(2000,function() {
				$('.actie-load').hide();
				$(this).resetForm();
			});
			$('#actiemenu').show();
		});
	}


	$("#contact, #offerte").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				success: function(responseText) {
					$(form).replaceWith("<div>" + responseText + "</div>");
				}
			});
		}
	});
	
	$('#profiel_check').show().find('input:not(:checked)').each(function() {
		$('#for_'+this.id).hide();
	});



	$('#profiel_check input').click(function() {
		$('#for_'+this.id).slideToggle(200);
	});
	

	$('input[type="reset"]').click(function() {
		confirm("Weet u zeker dat u het formulier wilt resetten?");
	});
	
	
	$('input[id^="aantal"]')/*.addClass('required')*/.siblings('label').append(' <b>*</b>');

		
	$('#quick').quickNews();
	
	// $("a[href$='media/oefenboek_demo/']").popUp();
	// $("a[href$='media/kvds_ecoach/']").popUp({width:1000,height:700});
	$("a[href$='media/epg/']").popUp({width:850,height:595});
	
	// $('#player').removeClass('note').text('');
	
	$('#klantenScroll').klantenScroll();
	
});

/*
 * Translated default messages for the jQuery validation plugin.
 * Language: NL
 */
jQuery.extend(jQuery.validator.messages, {
        required: "Dit is een verplicht veld.",
        remote: "Controleer dit veld.",
        email: "Vul hier een geldig email adres in.",
        url: "Vul hier een geldige URL in.",
        date: "Vul hier een geldige datum in.",
        dateISO: "Vul hier een geldige datum in (ISO).",
        number: "Vul hier een geldig nummer in.",
        digits: "Vul hier alleen nummers in.",
        creditcard: "Vul hier een geldig credit card nummer in.",
        equalTo: "Vul hier dezelfde waarde in.",
        accept: "Vul hier een waarde in met een geldige extensie.",
        maxlength: jQuery.format("Vul hier maximaal {0} tekens in."),
        minlength: jQuery.format("Vul hier minimaal {0} tekens in."),
        rangelength: jQuery.format("Vul hier een waarde in van minimaal {0} en maximaal {1} tekens."),
        range: jQuery.format("Vul hier een waarde in van minimaal {0} en maximaal {1}."),
        max: jQuery.format("Vul hier een waarde in kleiner dan of gelijk aan {0}."),
        min: jQuery.format("Vul hier een waarde in groter dan of gelijk aan {0}.")
});

jQuery.validator.addMethod("telefoon", function(value, element) {
	return this.optional(element) || /^[\d-\+()\s]+$/i.test(value);
}, "Vul hier een geldige telefoonnummer in.");

jQuery.validator.addMethod("pc_cijfers", function(value, element) {
	return this.optional(element) || /^[\d]{4}$/i.test(value);
}, "Vul hier een geldige postcode in.");

jQuery.validator.addMethod("pc_letters", function(value, element) {
	return this.optional(element) || /^[a-z]{2}$/i.test(value);
}, "Vul hier een geldige postcode in.");


function ie6() {
	if($.browser.msie && $.browser.version == '6.0') {
		return true;
	}
}


jQuery.fn.quickNews = function(settings) {
	settings = jQuery.extend({
		delay: 10000,
		transition: 1000
	}, settings);
	
	return this.each(function() {		
		var $this = $(this);
		var $list = $this.find('ul');
		var $quick = $(this).find('li');
		var current = 0;
		var over = false;
		$this.show();
		
		function slide() {
			current = current < $quick.length ? current : 0;
			if (!over) {
				$list.empty().html($quick[current]).find('li').show().delayAnimation(settings.transition/2).css({left:$this.width()}).animate({left:0},settings.transition);
				current++;
			}
			
			$this.delayAnimation(settings.delay,function() {
				if (!over) $this.find('li').fadeOut(settings.transition/2,slide);
				else slide();
			});
		}
		slide();
		
		$this.hover(function() { over = true; },function() { over = false; });	
		
	});
};

jQuery.fn.popUp = function(settings) {
	var screenWidth = screen.availWidth;
	var screenHeight = screen.availHeight;
	
	settings = jQuery.extend({
		name: 'popup',
		width: screenWidth * .95,
		height: screenHeight * .9,
		scrollbars: 0,
		resizable: 0,
		toolbar: 0,
		location: 0,
		statusbar: 0,
		menubar: 0	
	}, settings);
	
	return this.each(function() {
		$(this).click(function() {
			var url = this.href;
			var left = (screenWidth - settings.width)/2;
			var top = (screenHeight - settings.height)/2;
	
			var props = 'width='+settings.width+',height='+settings.height+',left='+left+',top='+top+',toolbar='+settings.toolbar+',scrollbars='+settings.scrollbars+',location='+settings.location+',statusbar='+settings.statusbar+',menubar='+settings.menubar+',resizable='+settings.resizable+',directories=0';
	

			if (url) {
				window.open(url, settings.name, props).focus();
			} else {
				alert(url);
			}

			return false;
		});
	});
};


jQuery.fn.klantenScroll = function() {
	var $this = $(this);
	var klant = $this.find('ul li');
	var width = klant.width();
	var totalWidth = width * klant.length;
	var menu = $this.find('div').width();
	var count = width-menu;
	var $ul = $this.find('ul');
	
	var move = 8;
	var start = Math.round(Math.random() * (klant.length - move)) * width;
	
	$ul.css({width:totalWidth,right:start});

	$this.find('div').before('<a href="#" id="scroll_left">&laquo;</a>').after('<a href="#" id="scroll_right">&raquo;</a>');

	$('#scroll_left, #scroll_right').click(function() {
		if (this.id == 'scroll_left' && !$ul.is(':animated')) {
			var count = parseInt($ul.css('right')) - width * move;
		
			var jump = count > 0 ? count : 0;
		
			$ul.animate({right:jump}, (parseInt($ul.css('right'))-jump)*1.5);
		}
		else if (this.id == 'scroll_right' && !$ul.is(':animated')) {
			var row = width * move;
			var max = totalWidth - row;
			var count = parseInt($ul.css('right')) + row;
		
			var jump = count < max ? count : max;
		
			$ul.animate({right:jump}, (jump-parseInt($ul.css('right')))*1.5);
		}
	
		return false;
	});
}
