$(document).ready(function(){
	
	/** Contact Google Maps loader **/
	$('div.content.contact div.googlemaps').each(function() {
		var locator = $(this).find('ul.meta-list li.c-map-location a'),
			gmap = $('div.content.contact div.left-col').find('iframe'),
			mapTwo = $('div.content.contact div.left-col').find('iframe.map-2');
			
		var mapOne = gmap.attr('src'),
			mapTwo = mapTwo.attr('src');
			
		$('ul.meta-list li.c-map-location a.active').addClass('gccolori');
		locator.not('.active').addClass('inactive gccolor');
			
		locator.bind('click', function() {
			locator.toggleClass('active inactive gccolor gccolori');
			if($(this).attr('href') == 'berlin') {
				gmap.attr('src', mapOne);
			} else {
				gmap.attr('src', mapTwo);
			}
			
			return false;
		});
	
	});

	$('#submit').click(function(e){

		//stop the form from being submitted
		e.preventDefault();
		var error = false;
		
		var name = $('input.name').val(),
			mail = $('input.mail').val(),
			msg = $('textarea.message').val();
		
		if(name.length == 0 || name.indexOf('..') == '9') {
			var error = true;
			$('input.name').addClass('error', 400);
		}else {
			$('input.name').removeClass('error', 600);
		}
		if(mail.length == 0){
			var error = true;
			$('input.mail').addClass('error', 400);
		} else if(mail.indexOf('@') == '-1') {
			var error = true;
			$('input.mail').addClass('alert', 400);
		} else {
			if($('input.mail').hasClass('alert')) {
				$('input.mail').removeClass('alert', 600);
			} else {
				$('input.mail').removeClass('error', 600);
			}
		}
		if(msg.length == 0){
			var error = true;
			$('textarea.message').addClass('error', 400);
		}else{
			$('textarea.message').removeClass('error', 600);
		}

		//now when the validation is done we check if the error variable is false (no errors)
		if(error == false){
			$('#submit').attr({'disabled' : 'true', 'value' : 'Sending ...' });
			$.post("assets/send_email.php", $("#replyform").serialize(),function(result){
				//and after the ajax request ends we check the text returned
				if(result == 'sent'){
					//if the mail is sent remove the submit paragraph
					 $('#submit').remove();
					//and show the mail success div with fadeIn
					$('.contact-success').fadeIn(500);
				}else{
					//show the mail failed div
					$('.contact-fail').fadeIn(500);
					//reenable the submit button by removing attribute disabled and change the text back to Send
					$('#submit').removeAttr('disabled').attr('value', 'Submit');
				}
			});
		}
	});
});
