var moreUp = true;

$(function() {
	attachMouseOvers();
	preloadMouseOvers();
	
	// Sanne home page easter egg
	$('#page-home #sanne').mouseover(
		function() {
			this.src = this.src.replace(/home_sanne.png/, 'home_sanne_knipoog.png');
		}
	);
	$('#page-home #sanne').mouseout(
		function() {
			this.src = this.src.replace(/home_sanne_knipoog.png/, 'home_sanne.png');
		}
	);
	
	// Nice popup for portfolio etc
    $('.lightbox').lightBox();
    
    // Show/hide magnifying glass
    $('.lightbox').mouseover(
    		function() {
    			$('#magnify').show();
    		}
    );
    $('.lightbox').mouseout(
    		function() {
    			$('#magnify').hide();
    		}
    );
    
    // 'Read more' slide out text
    $('.toggle-more').click(function() {
    	if (moreUp) {
    		$('.toggle-more').text('minder');
    		$('#more').slideDown('slow');
    	}
    	else {
    		$('.toggle-more').text('meer');
    		$('#more').slideUp('slow');
    	}
    		
    	moreUp = !moreUp;
    	return false;
    });
    
    // Basic email validation check
    $('#nieuwsbrief-form').submit(function() {
    	var emailValue = $('#email').get(0).value;
    	
    	// Very basic validation before we submit
    	if (emailValue.length > 0) {
    		if (emailValue.indexOf(".") > 2 && emailValue.indexOf("@") > 0)
    			return true;
    		else
    			alert('Vul svp een geldig email address in');
    	}
    	
    	return false;
    });
});

function updateColumns() {
	// Set columns to same height
	var heightContent = parseInt($('#content').css('height'));
	var heightNavigatie = parseInt($('#navigatie').css('height'));
	
	if (heightContent < heightNavigatie) {
		$('#content').css('min-height', heightNavigatie + 25 + 'px');
		$('#ringband').css('min-height', heightNavigatie + 25 + 'px');
		$('#blz').css('min-height', heightNavigatie - 5 + 'px');
	}
	else {
		$('#navigatie').css('min-height', heightContent - 15 + 'px');
		$('#ringband').css('min-height', heightContent + 5 + 'px');
		$('#blz').css('min-height', heightContent - 30 + 'px');
	}
	
}

function attachMouseOvers() {
	// Ignore mouseover for menu item of current page to highlight
	// Tricky bit...!
	page = $('body').get(0).id.replace(/page-/, '');
	$('#page-' + page + ' #menu-' + page + ' img')
		.addClass('page-marker')
		.each(function() { this.src = this.src.replace(/_norm\./, '_over.'); });

	// Attach mouseover handlers
	$('.omo').not('.page-marker').mouseover(function() { this.src = this.src.replace(/_norm\./, '_over.'); });
	$('.omo').not('.page-marker').mouseout(function() { this.src = this.src.replace(/_over\./, '_norm.'); });
}

function preloadMouseOvers() {
	var imageObj = new Image();

	$('.omo').each(function() {
		imageObj.src = this.src.replace(/_norm\./, '_over.');
	});
}

var currentSlidePosition = 1;

function nextSlidePosition() {
	if (currentSlidePosition == $('.plaatje').length) {
		currentSlidePosition = 1;
	}
	else {
		currentSlidePosition += 1;
	}
	
	return imageSlider(currentSlidePosition);
}

function prevSlidePosition() {
	if (currentSlidePosition == 1) {
		currentSlidePosition = $('.plaatje').length;
	}
	else {
		currentSlidePosition -= 1;
	}
	
	return imageSlider(currentSlidePosition);
}

function imageSlider(position) {
	var slideDelta = 520;
	var toSlide = (position-1) * slideDelta;
	currentSlidePosition = position;
	$('#slider').animate({marginLeft: '-' + toSlide + 'px'}, 500);
	
	$('#description').text( $('#slider .plaatje img').get(position-1).title );
	$('#currentPosition').text(currentSlidePosition);
	return false;
}

