/* Author: 
 * 
 * Steffen Kraft
 * 
*/
var resized = false;
var t = 0;
$(document).ready(function(){
	//JPARRALLAX 
	//must reload the page so jparallax knows the new images sizes
	window.onresize = function(){
		if($('#welcome').offset().top >= 0){
			location.replace(location.href);
		} else {
			resized = true;
		}
	}
	$('#parallax').jparallax({yparallax: false});
	
	//NAVIGATION
	$('.scrollMe').click(function(){
		var elementClicked = $(this).attr('href');
		var topScroll = '';
		if(elementClicked == '#welcome') {
			if (resized) {
				location.replace(location.href);
				resized = false;
			}
			//fadeout info message 
			if (t > 0) fadeOutInfo();
			//aktivate current link
			handleLinks(this);
			//go to page welcome
			topScroll = '0';
		}
		if(elementClicked == '#about') {
			//fadeout info message
			if (t > 0) fadeOutInfo();
			//aktivate current link
			handleLinks(this);
			//go to page vita
			topScroll = '-820';
		}
		if(elementClicked == '#contact') {
			//fadeout info message
			if (t > 0) fadeOutInfo();
			//aktivate current link
			handleLinks(this);
			//go to page contact
			topScroll = '-1660';
		}
		$('#welcome,#about,#contact').animate({top: topScroll},900);
		return false;
	});
	//FORM
	//if submit button is clicked
    $('#submit').click(function () {       
        //Get the data from all the fields
        var name = $('input[name=name]');
        var email = $('input[name=email]');
        var comment = $('textarea[name=comment]');
        var emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var error = false;
        //validation
        if (name.val() == '') {
            name.next().text("Dieses Feld bitte ausfüllen.");
            name.addClass('inputError');
            error = true;
        } else {
        	name.removeClass('inputError');
        	name.next().text("");
        }
        if (email.val() == '') {
            email.next().text('Dieses Feld bitte ausfüllen.');
            email.addClass('inputError');
            error = true;
        } else if (!emailReg.test(email.val())) {
        	email.next().text('Ihre Eingabe ist leider ungültig.');
        	email.addClass('inputError');
            error = true;
        } else {
        	email.removeClass('inputError');
        	email.next().text("");
        }
        if (comment.val() == '') {
        	comment.next().text("Dieses Feld bitte ausfüllen.");
        	comment.addClass('inputError');
            error = true;
        } else {
        	comment.removeClass('inputError');
        	comment.next().text("");
        } 

        
        //if one input is invalide or missing return false
        if (error) {
        	return false;
        }
        
        //data properly
        var data = 'name=' + name.val() + '&email=' + email.val() + 
        '&comment='  + encodeURIComponent(comment.val());
         
        //start the ajax
        $.ajax({
            url: "process.php",
            type: "GET",
            data: data,    
            cache: false,
            //success
            success: function (html) {             
                //if process.php returned 1/true (send mail success)
                if (html==1) {                 
                	$('form').fadeOut(600);
                    $('.formSubmited').fadeIn(200);
                } else alert('Sorry, unexpected error. Please try again later.');              
            }      
        });
        return false;
    }); 
    
    //toggle vita info
    $('.headline').click(function(){
    	if($(window).width() > 400) {
    		clearTimeout(t);
    		t = setTimeout("fadeOutInfo()",7000);
    		if(!$(this).next().hasClass('open')){
    			$('.vita').find('.open').fadeOut('fast').removeClass('open').css('height','0');
    			$(this).next().fadeIn('slow').addClass('open');
    		}
    	}
    });
    
});

//Hande the Info Message
function fadeOutInfo() {
	$('.vita').find('.open').fadeOut('fast').removeClass('open');
	t = 0;
}


//handle navigationlinks states: aktive inaktive
function handleLinks (e) {
	if (!$(e).hasClass('active')){
		$('nav').find('.active').removeClass('active');
		$(e).addClass('active');
	}
}
