// JavaScript Document

$(document).ready(function() {

	// Homepage slideshow
	loadImage();
	homeSlide = setInterval("loadImage()", 4000); // 4 sec
	
	
	// Pagina's
	var page = 'home';
	$('#menu:not(.block) ul li a:not(.active)').live('click', function() {
		animatePage($(this));
		page = $(this).attr('class').split(' ',1)[0];
		return false;
	});
	
	// Galleries
	var preventClick=false;
	$('.page').droppable();
	
	$('.pic a').bind('click',function(e) {
		if(preventClick)
		{
			e.stopImmediatePropagation();
			e.preventDefault();
		}
	});

	$(".pic").draggable({
		stack: '.pic',
		revert: 'invalid',
		start: function() {
			preventClick=true;
		},
		stop: function() {
			setTimeout(function(){ preventClick=false; }, 250);
		}
	});
	
	$('a.prev').live('click',function(e) {
		$.fancybox.prev();
		return false;
	});
	
	$('a.next').live('click',function(e) {
		$.fancybox.next();
		return false;
	});
	
	
	// Fancybox
	$('.fancybox.gallery').fancybox({
		type: 'ajax',
		zoomSpeedIn: 300,
		zoomSpeedOut: 300,
		overlayShow:true,
		overlayColor:'#000',
		overlayOpacity:.5,
		transitionIn:'elastic',
		transitionOut:'elastic',
		showCloseButton:true,
		opacity:true,
		cyclic:true,
		titlePosition: 'outside',
		titleFormat: function(title, currentArray, currentIndex, currentOpts) {
			return '<a href="#" class="prev"></a><span>Afbeelding ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span><a href="#" class="next"></a>';
		}
	});
	
	// Textarea maxlength
	$('textarea[maxlength]').keyup(function() {
        var max = parseInt( $(this).attr('maxlength') );
		$(this).siblings('.charsRemaining').css({'color' : '#93C'});
		if ( $(this).val().length == max ) {
			$(this).siblings('.charsRemaining').css({'color' : 'red'});
		}
		if ( $(this).val().length > max ) {
            $(this).val( $(this).val().substr(0, $(this).attr('maxlength')) );
        }
		if ( $(this).val() != '' ) {
        	$(this).siblings('.charsRemaining').html((max - $(this).val().length));
		}
		else {
			$(this).siblings('.charsRemaining').html('');
		}
    });

	
	// Contact
	$('#email').focusout(function() {  
		mailCheck();
	});
	$('#message').focusout(function() {  
		fieldCheck('message');
	}).keyup(function() {  
		fieldCheck('message');
	});
	$('#subject').focusout(function() {  
		fieldCheck('subject');
	}).keyup(function() {  
		fieldCheck('subject');
	});
	$('#submit').click(function() {  
		if ( mailCheck() && fieldCheck('subject') && fieldCheck('message') ) {
			$('#form-error').hide('slow');
			
			var postdata = new Object();
            postdata.email = $('#email').val();
            postdata.subject = $('#subject').val();
            postdata.message = $('#message').val();
            postdata.ts = $('#ts').val();
			
			$.ajax({
				url:'helpers/contact.php',
				data:postdata,
				cache:false,
				type:'post',
				success:function(data){
					if ( data == 'succes' ) {
						$('.succes').show('slow').delay(5000).hide('slow');
						$('#email, #message, #subject').val('');
					}
					else {
						console.log(data);
						$('#contact-form').before(data);
						$('#form-error').show('slow');
					}
				}
			});
		}
		return false;
	});
	
	
	// safe mailto links
	$('a.mail-corine').live('click', function() {
		mailCorine();
		return false;
	});
	$('a.mail-daniel').live('click', function() {
		mailDaniel();
		return false;
	});
	
	// Form beveiligen
	var ts = true;
	$('#email').focus(function() {
		if (ts) { 
			ts = false;
			$.get('helpers/token.php', function(data) {
				$('#contact-form').append('<input type="hidden" id="ts" name="ts" value="'+data+'" />');
			});
		}
	});
	
	// Disable image download!
	$('.page.home div, #fancybox-content div, .homeImage, .galleryImage').live("contextmenu",function(e) {
		alert('Deze foto is auteursrechtelijk beschermd!');
        return false;
    });
		
});


function mailCheck() {
	$("#email").next('.error').hide();
	var hasError = false;
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
 
	var emailaddressVal = $("#email").val();
	if(emailaddressVal == '') {
		$("#email").next('.error').html('Vul uw email adres in.').show();
		hasError = true;
	}
 
	else if(!emailReg.test(emailaddressVal)) {
		$("#email").next('.error').html('Vul een geldig email adres in.').show();
		hasError = true;
	}
 
	if( hasError == true) { return false; } else { return true; }
}

function fieldCheck(field) {
	$field = $('#'+field);
	$field.next('.error').hide();
	var val = $field.val();
	if ( val == '' ) {
		$field.next('.error').show();
		return false;
	}
	else {
		return true;
	}
}

// Animate pages
function animatePage(e) {
	var page = e.attr('class');
	var active = $('#menu .active').attr('class').split(' ')[0];
	var width = $(window).width();
	var duration = 1500; // 1,5 sec
	$('#menu').addClass('block');
	$('#menu .active').removeClass('active');
	e.addClass('active');
	
	if ( page == 'home' ) {
		homeSlide = setInterval("loadImage()", 4000); // 4 sec
	}
	if ( active == 'home' ) {
		clearInterval ( homeSlide );
	}
	
	// pagina klaar zetten
	$('.page.'+page).css({
		marginLeft: width,
		opacity: 0,
		display: 'block'
	// pagina in slide
	}).animate({
		marginLeft: 0,
		opacity: 1
	}, duration, 'easeOutQuad', function() {
		$('#menu').removeClass('block');
		
	});
	
	// active weg slide
	$('.page.'+active).animate({
		marginLeft: -width,
		opacity: 0
	}, duration-(duration/3), 'easeInQuad');
	
	$('#footer').animate({
		marginTop: $('.page.'+page).height() + 30
	}, duration-(duration/3), 'easeInQuad');
}

// Load new home slides
function loadImage() {
	
	$gallery = $('div.page.home');
	
	$.ajax({
		url:'getHomeImage.php',
		cache:false,
		success:function(data){
			$gallery.append(data);
			var $image = $gallery.find('.homeImage').last();
			
			var zoom = 150;
			var duration = 1500; // 1,5 sec
			var degrees = randomFromTo(-5, 5);
			var rotations = "rotate(" + degrees + "deg)";
			var height = $image.height();
			var width = $image.width();
			
            $image.css({
                height : height + zoom  + "px",
                width : width + zoom  + "px",
			});
			
			if ( Modernizr.csstransforms ) {
				$image.css({
					"transform" : rotations,
					"-moz-transform" : rotations,
					"msTransform" : rotations,
					"-webkit-transform" : rotations
				});
			}
			else if ( $.browser.msie ) {
				
				ieRotateArray = new Array();
				ieRotateArray[-10] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.984807753012208, M12=0.17364817766693033, M21=-0.17364817766693033, M22=0.984807753012208, sizingMethod='auto expand');";
				ieRotateArray[-9] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9876883405951378, M12=0.15643446504023087, M21=-0.15643446504023087, M22=0.9876883405951378, sizingMethod='auto expand');";
				ieRotateArray[-8] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9902680687415704, M12=0.13917310096006544, M21=-0.13917310096006544, M22=0.9902680687415704, sizingMethod='auto expand');";
				ieRotateArray[-7] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.992546151641322, M12=0.12186934340514748, M21=-0.12186934340514748, M22=0.992546151641322, sizingMethod='auto expand');";
				ieRotateArray[-6] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9945218953682733, M12=0.10452846326765346, M21=-0.10452846326765346, M22=0.9945218953682733, sizingMethod='auto expand');";
				ieRotateArray[-5] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9961946980917455, M12=0.08715574274765817, M21=-0.08715574274765817, M22=0.9961946980917455, sizingMethod='auto expand');";
				ieRotateArray[-4] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9975640502598242, M12=0.0697564737441253, M21=-0.0697564737441253, M22=0.9975640502598242, sizingMethod='auto expand');";
				ieRotateArray[-3] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9986295347545738, M12=0.05233595624294383, M21=-0.05233595624294383, M22=0.9986295347545738, sizingMethod='auto expand');";
				ieRotateArray[-2] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9993908270190958, M12=0.03489949670250097, M21=-0.03489949670250097, M22=0.9993908270190958, sizingMethod='auto expand');";
				ieRotateArray[-1] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9998476951563913, M12=0.01745240643728351, M21=-0.01745240643728351, M22=0.9998476951563913, sizingMethod='auto expand');";
				
				ieRotateArray[0] = "progid:DXImageTransform.Microsoft.Matrix(M11=1, M12=0, M21=0, M22=1, sizingMethod='auto expand');";

				ieRotateArray[1] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9998476951563913, M12=-0.01745240643728351, M21=0.01745240643728351, M22=0.9998476951563913, sizingMethod='auto expand');";
				ieRotateArray[2] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9993908270190958, M12=-0.03489949670250097, M21=0.03489949670250097, M22=0.9993908270190958, sizingMethod='auto expand');";
				ieRotateArray[3] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9986295347545738, M12=-0.05233595624294383, M21=0.05233595624294383, M22=0.9986295347545738, sizingMethod='auto expand');";
				ieRotateArray[4] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9975640502598242, M12=-0.0697564737441253, M21=0.0697564737441253, M22=0.9975640502598242, sizingMethod='auto expand');";
				ieRotateArray[5] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9961946980917455, M12=-0.08715574274765817, M21=0.08715574274765817, M22=0.9961946980917455, sizingMethod='auto expand');";
				ieRotateArray[6] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9945218953682733, M12=-0.10452846326765346, M21=0.10452846326765346, M22=0.9945218953682733, sizingMethod='auto expand');";
				ieRotateArray[7] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.992546151641322, M12=-0.12186934340514748, M21=0.12186934340514748, M22=0.992546151641322, sizingMethod='auto expand');";
				ieRotateArray[8] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9902680687415704, M12=-0.13917310096006544, M21=0.13917310096006544, M22=0.9902680687415704, sizingMethod='auto expand');";
				ieRotateArray[9] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.9876883405951378, M12=-0.15643446504023087, M21=0.15643446504023087, M22=0.9876883405951378, sizingMethod='auto expand');";
				ieRotateArray[10] = "progid:DXImageTransform.Microsoft.Matrix(M11=0.984807753012208, M12=-0.17364817766693033, M21=0.17364817766693033, M22=0.984807753012208, sizingMethod='auto expand');";
				$image.css({
					"filter" : ieRotateArray[degrees],
                	"height" : height + ( zoom / 2 )  + "px",
                	"width" : width  + ( zoom / 2 ) + "px",
                	"bottom" : offsetBottom + "px"
				});
				
				if ( parseInt($.browser.version) == 8 ) {
					$image.css({
						"bottom" : "+=30px"
					});
				}
				
			}
			
			if ( $gallery.find('.homeImage').length >= 7 ) {
				$gallery.find('.homeImage').first().fadeOut(500, function() {
					$(this).remove();
				});
			}
			
			setTimeout(function() {
				$image.stop(true, true).animate({
					height: "-=" + zoom + "px",
					width: "-=" + zoom + "px",
					opacity: 1
				}, duration);
			}, 500);
		}
	});
	
}

function randomFromTo(from, to) {
   return Math.floor(Math.random() * (to - from + 1) + from);
}
