/* Author: Jon Gerlach

*/


$(document).ready(function(){
	
	var randomRGBAval = randomRGBA();
	var grayRBGAval = "rgba(200, 200, 200, .25)";
	$('.overlay').animate({backgroundColor:randomRGBAval}, 1000);
	$('.overlay-gray').animate({backgroundColor:grayRBGAval}, 1000);
	
	$('#mail-form').validate();
	
	init();	
	
});


// ===== FUNCTIONS ===== //


function bindEventListeners(){
		
}


function init(){
	
	// Tween between random background colors
	
	
	$('.overlay').everyTime(4000, 'tween', function(){
		var randomRGBAval = randomRGBA();
		$('.overlay').animate({backgroundColor:randomRGBAval}, 4000);
	});	
	
	
	// Parse URL and set navigation
	navArray = ['work', 'blog', 'about', 'contact'];
	testurl = 'http://jongerla.ch/blog';
	testurl = window.location.pathname;
	testurl = testurl.substr(1, testurl.length);
		
	if(testurl === ''){
		$('#nav-list #work').attr('id', 'work-active');
	}
	else {
		testurl += '/';
		testurl = testurl.substr(0, testurl.indexOf('/'));
		
		for( var i=0; i<navArray.length; i++){
			if( testurl.indexOf(navArray[i]) != -1 ){
				$('#nav-list #' + navArray[i]).attr('id', navArray[i] + '-active');
				break;
			}
		}
	}
	
	
	
	// ===== BIND EVENT LISTENERS ===== //
	
	$('li.nav-item a').bind('mouseover', function(){
		if( $(this).parent().attr('id').indexOf('active') == -1){
			$(this).parent().css('backgroundPosition', '0 -35px');
		} else {
			$(this).parent().css('backgroundPosition', '0 -70px');
		}
	});
	
	$('li.nav-item a').bind('mouseout', function(){
		if( $(this).parent().attr('id').indexOf('active') == -1){
			$(this).parent().css('backgroundPosition', '0 -0');
		} else {
			$(this).parent().css('backgroundPosition', '0 -70px');
		}
	});	
}


// ===== SUPPORTING FUNCTIONS ===== //

// Generate and return random RGB color
function randomColor(){
	RGB = '';
	for( var i=0; i<3; i++){
		num = Math.round( 255 * Math.random() );
		num = num.toString(16);
		RGB += num;
	}	
	return RGB;
}


function randomRGBA(){
	
	var RGBA = 'rgba(';
	
	for( var i=0; i<3; i++){
		num = Math.round( 255 * Math.random() );
		RGBA += num + ', ';
	}	
	
	RGBA += ' .25)';
	
	return RGBA;
}

function validateform(){
	
	if($("#mail-form").valid()){
		submitform();
	}
	else{
		$("mail-form").validate();	
	}

}


function submitform(){
	
	var captcha = $('input[name=human-test]').val();
	captcha = captcha.toLowerCase();
	
	if( captcha === 'no' || captcha === 'nope' || captcha === 'negative' || captcha === 'no way' ){
	
		var data = 'your-name=' + $('input[name=your-name]').val() + '&your-email=' + $('input[name=your-email]').val() + '&your-website=' + $('input[name=your-website]').val() + '&your-words-name='  + $('input[name=your-words-name]').val() + '&your-message=' + $('textarea[name=your-message]').val();
		
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "http://jongerla.ch/_resources/php/sendMail.php", 
			 
			//GET method is used
			type: "POST",
	
			//pass the data         
			data: data,     
			 
			//Do not cache the page
			cache: false,
			 
			//success
			success: function (html) {              
				//if process.php returned 1/true (send mail success)
				
				//console.log("result " + html);
				
				//if (html==1) {                  
					
					$('#mail-form').hide();
					$('#success').removeClass('hidden');
					
					 
				//if process.php returned 0/false (send mail failed)
				//} else {
					//alert("Oops! Something went wrong. Let's give it another try.");  
				//}
				
			}       
		});
	}
	
};

