(function ($) {
	// VERTICALLY ALIGN FUNCTION
	$.fn.vAlign = function() {
		return this.each(function(i){
			var ah = $(this).height();
			var ph = $(this).parent().height();
			var mh = (ph - ah) / 2;
			$(this).css('margin-top', mh);
		});
	};
	})(jQuery);

	//(function ($) {
		// VERTICALLY ALIGN FUNCTION
		/*$.fn.resizeToViewport = function() {
			return this.each(function(i) {
				// GET VIEWPORT SIZE
				var vw = $(window).width();
				var w = vw - 30;
				$(this).css('width', w);
			});
		};
		})(jQuery);*/

		jQuery(document).ready(function($) {
			// SET PAGESIZE TO VIEWPORT ALSO ON RESIZE
			/*$('#wrapper #inner-wrapper div.page-wrapper').resizeToViewport();
			$(window).smartresize(function () {
				$('#wrapper #inner-wrapper div.page-wrapper').resizeToViewport();
				$('#wrapper').scrollTo($('#wrapper #inner-wrapper div.current'), 100);
			});
			// Set Current page to locations hash
			hash = window.location.hash;
			// If we hash at start hijack the motherbitch
			if(hash != '') {
				$(hash).addClass('current');
				$('#wrapper').scrollTo($(hash), 2500);
			}
			$('a.scrollto').each(function() {
				if($(this).attr('href') == hash) $(this).parent().addClass('current');
				// SCROLL TO INIT
				$(this).click(function(e) {
					e.preventDefault();
					$('#menu li.current').removeClass('current');
					$(this).parent().addClass('current');
					// add current class to div too
					$('#inner-wrapper div.current').removeClass('current');
					$($(this).attr('href')).addClass('current');
					$('#wrapper').scrollTo($($(this).attr('href')), 2500);
				});
			});*/
			/* ------------------------------------------------- */
			/* KWIS SECTIE
			/* ------------------------------------------------- */
			if($('div.scrollable-kwis').size() > 0) {
				$('div.scrollable-kwis').scrollable({size:1,clickable:false,api:true});
				$('div.scrollable-kwis').scrollable().onSeek(function() {
					var total = $('div.scrollable-kwis').scrollable().getPageAmount() - 2;
					if($('div.scrollable-kwis').scrollable().getIndex() == 0){
						$('div.kwis-footer span.current-page').text('start de kwis');
					}else if($('div.scrollable-kwis').scrollable().getIndex() == total + 1){
						$('div.kwis-footer span.current-page').text('einde van de kwis');
					}else{
						$('div.kwis-footer span.current-page').text('vraag ' + $('div.scrollable-kwis').scrollable().getIndex() + ' van de ' + total);
					}
				});
			}
			/* ------------------------------------------------- */
			/* AJAX SUBMIT KWIS
			/* ------------------------------------------------- */
			if($('#form-kwis').size() > 0) {
				$("#form-kwis").submit(function() {
					$("#form-kwis").ajaxSubmit({
						target: "#kwis-response",
						success:    function() {
						} 
					});
					return false;
				});
			}
			/* ------------------------------------------------- */
			/* AJAX GET KWIS
			/* ------------------------------------------------- */
			$('a.call_kwis').unbind('click').click(function(e) {
				e.preventDefault();
				var url = $(this).attr('href');
				$('#kwis-response').fadeOut(200,function(){
					$('#kwis-response').load(
						url,
						null,
						function(){
							$('#kwis-response').fadeIn();
						}
					);
				});
			});

			// VERTICAL ALIGN ELEMENTS SECTION!
			$('.valign').vAlign();
			// INIT SCROLLPANES
			//$('.scrollpane-again').jScrollPane();
			/* ------------------------------------------------- */
			/* NIEUWS/KALENDER SECTIE JAVASCRIPT
			/* ------------------------------------------------- */
			//$("#nieuws-list li").quickpaginate({perpage:4, showcounter:true, pager:$("#nieuws-list-counter") });
			//$("#kalender-list li").quickpaginate({perpage:4, showcounter:true, pager:$("#kalender-list-counter") });
			//$("#nav-list li").quickpaginate({perpage:4, showcounter:true, pager:$("#nav-list-counter") });
			/* ------------------------------------------------- */
			/* AJAX NIEUWS/KALENDER
			/* ------------------------------------------------- */
			$('a.call_news').unbind('click').click(function(e) {
				e.preventDefault();
				var url = $(this).attr('href');
				$('#news-infobox').fadeOut(200,function(){
					$('#news-infobox').load(
						url,
						null,
						function(){
							$('#news-infobox').fadeIn();
						}
					);
				});
			});
			$('a.call_pinned').unbind('click').click(function(e) {
				e.preventDefault();
				$('#wrapper').scrollTo($('#nieuws'), 1000);
				var url = $(this).attr('href');
				$('#news-infobox').fadeOut(200,function(){
					$('#news-infobox').load(
						url,
						null,
						function(){
							$('#news-infobox').fadeIn();
						}
					);
				});
			});
			$('a.call_agenda').unbind('click').click(function(e) {
				e.preventDefault();
				var url = $(this).attr('href');
				$('#agenda-infobox').fadeOut(200,function(){
					$('#agenda-infobox').load(
						url,
						null,
						function(){
							$('#agenda-infobox').fadeIn();
						}
					);
				});
			});
			
			$('a.call_photoalbums').unbind('click').click(function(e) {
				e.preventDefault();
				var url = $(this).attr('href');
				$('#photoalbum').fadeOut(200,function() {
					$('#photoalbum').addClass('loading');
					$('#photoalbum').load(
						url,
						null,
						function(){
							$('#photoalbum').fadeIn();
							$('#photoalbum a.colorbox').colorbox({current:"{current} / {total}", previous:'< vorige', next:'volgende >', close:'sluit', width:'80%', height:'80%'});
							$('#photoalbum').removeClass('loading');
						}
					);
				});
			});
			/* ------------------------------------------------- */
			/* PLATTEGROND SECTIE
			/* ------------------------------------------------- */
			if($('#map-items').size() > 0) {

				function hideAllMapItems(){
					$('#map-items li.map-item').each(function() {
						$(this).hide();
					});
				}

				$('#map-items li.map-item div.slideshow').scrollable({size:1, loop: true}).autoscroll().mousewheel();

				$('#map-items li.map-item a.btn-sluit').each(function() {
					$(this).click(function(e) {
						e.preventDefault();
						hideAllMapItems();
					});
				});

				hideAllMapItems();

				$('#m-plattegrond area').each(function() {

					$(this).tooltip({
						track: true,
						delay: 0,
						showURL: false
					});

					$(this).click(function(){
						hideAllMapItems();
						var item = $(this).attr('href');
						$('#map-items li' + item).show();
					});
				});
			}
			/* ------------------------------------------------- */
			/* VALIDATION FORMS
			/* ------------------------------------------------- */
			if($('#form-contact').size() > 0) {
				$("#form-contact").validate({
					submitHandler: function(form) {
						jQuery(form).ajaxSubmit({
							target: "#form-contact",
							success:    function() {
							}
						});
					}
				});
			}
			if($('#form-subscribe').size() > 0) {
				$("#form-subscribe").validate({
					submitHandler: function(form) {
						jQuery(form).ajaxSubmit({
							target: "#form-subscribe",
							success:    function() {
							} 
						});
					}
				});
			}
			/* ------------------------------------------------- */
			/* EMPTY FIELDS ON FOCUS
			/* ------------------------------------------------- */
			if($('#geboortedatum-input').size() > 0) {
				$("#geboortedatum-input").focus(function() {
					if( this.value == this.defaultValue ) {
						this.value = "";
					}
				}).blur(function() {
					if( !this.value.length ) {
						this.value = this.defaultValue;
					}
				});
			}
			/* ------------------------------------------------- */
			/* Colorbox init
			/* ------------------------------------------------- */
			$('a.colorbox').colorbox({current:"{current} / {total}", previous:'< vorige', next:'volgende >', close:'sluit', width:'80%', height:'80%'});
			// SCROLLABLE INITS
			if($('div.scrollable').size() > 0) {
				$('a.nav').click(function(e) { e.preventDefault(); return false; })
				$('a.navi').click(function(e) { e.preventDefault(); return false; })
				$('div.scrollable').scrollable({size:1,loop:true,speed:1500}).mousewheel({api:true});
				$('div.scrollable').scrollable().onSeek(function() {
					var item = $($('div.scrollable img.item')[$('div.scrollable').scrollable().getIndex()]);
					var caption = item.attr('alt');
					$('p.caption span').html(caption);
				});
			}
			
		});
