/* $Id$ */

$(document).ready(function(){
	
	function checkEmail (field) {
		var reEmail = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
		if (!reEmail.test(field.val())) field.addClass('invalid');
		else field.removeClass('invalid');
		return !field.hasClass('invalid');
	}
	
	function checkPhone (field) {
		var rePhone = /^((\+\d{1,3}(-|\.| )?\(?\d\)?(-|\.| )?\d{1,3})|(\(?\d{2,3}\)?))(-|\.| )?(\d{3,4})(-|\.| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/i;
		if (!rePhone.test(field.val())) field.addClass('invalid');
		else field.removeClass('invalid');
		return !field.hasClass('invalid');
	}
	
	$('#nav > li').mouseover(function(){
		$(this).addClass('hover');
	}).mouseout(function(){
		$(this).removeClass('hover');
	});
	$('#nav > li > ul > li').mouseover(function(){
		$(this).addClass('hover');
	}).mouseout(function(){
		$(this).removeClass('hover');
	});
	
	if (SELF.match(/projects?\.php$/)) $('#nav-projects').addClass('sel');
	else if (SELF.match(/services\.php$/)) $('#nav-services').addClass('sel');
	else if (SELF.match(/about\.php$/)) $('#nav-about').addClass('sel');
	else if (SELF.match(/contact\.php$/)) $('#nav-contact').addClass('sel');
	
	if (DOC_LOC.match(/\/projects\/current\/?/)) $('#nav-projects-current').addClass('sel');
	else if (DOC_LOC.match(/\/projects\/buy\/?/)) $('#nav-projects-buy').addClass('sel');
	else if (DOC_LOC.match(/\/projects\/rent\/?/)) $('#nav-projects-rent').addClass('sel');
	else if (DOC_LOC.match(/\/services\/demo\/?/)) $('#nav-services-demo').addClass('sel');
	else if (DOC_LOC.match(/\/services\/customhomes\/?/)) $('#nav-services-homes').addClass('sel');
	else if (DOC_LOC.match(/\/services\/medical\/?/)) $('#nav-services-medical').addClass('sel');
	else if (DOC_LOC.match(/\/services\/commercial\/?/)) $('#nav-services-commercial').addClass('sel');
	else if (DOC_LOC.match(/\/services\/multifamily\/?/)) $('#nav-services-multi').addClass('sel');
	else if (DOC_LOC.match(/\/about\/partners\/?/)) $('#nav-about-partners').addClass('sel');
	else if (DOC_LOC.match(/\/about\/testimonials\/?/)) $('#nav-about-testimonials').addClass('sel');
	else if (DOC_LOC.match(/\/about\/careers\/?/)) $('#nav-about-careers').addClass('sel');
	
	// $('#nav-home').click(function(){
	// 	document.location = ROOT_URL;
	// });
	// $('#nav-projects').click(function(){
	// 	if (SELF.match(/index\.php$/)) document.location = ROOT_URL + 'projects';
	// });
	// $('#nav-services').click(function(){
	// 	if (SELF.match(/index\.php$/)) document.location = ROOT_URL + 'services.php';
	// });
	// $('#nav-about').click(function(){
	// 	document.location = ROOT_URL + 'about.php';
	// });
	// $('#nav-contact').click(function(){
	// 	document.location = ROOT_URL + 'contact.php';
	// });
	// 
	// // subnav
	// $('#nav-projects-current').click(function(){
	// 	document.location = ROOT_URL + 'projects/current';
	// });
	// $('#nav-projects-buy').click(function(){
	// 	document.location = ROOT_URL + 'projects/buy';
	// });
	// $('#nav-projects-rent').click(function(){
	// 	document.location = ROOT_URL + 'projects/rent';
	// });
	// 
	// $('#nav-services-demo').click(function(){
	// 	document.location = ROOT_URL + 'services/demo';
	// });
	// $('#nav-services-homes').click(function(){
	// 	document.location = ROOT_URL + 'services/customhomes';
	// });
	// $('#nav-services-medical').click(function(){
	// 	document.location = ROOT_URL + 'services/medical';
	// });
	// $('#nav-services-commercial').click(function(){
	// 	document.location = ROOT_URL + 'services/commercial';
	// });
	// $('#nav-services-multi').click(function(){
	// 	document.location = ROOT_URL + 'services/multifamily';
	// });
	
	// photo gallery
	Galleria.loadTheme(ROOT_URL + 'js/jquery/galleria/themes/classic/galleria.classic.js');
	if ($('#project-gallery').is('*')) {
		$('#project-gallery').galleria('classic', {
			autoplay: 4000,
			height: 480
		});
	}
	if ($('#front-slider').is('*')) {
		$('#front-slider').galleria('classic', {
			autoplay: 4000,
			thumbnails: false,
			carousel: false,
			transition: 'fade',
			height: 300,
			image_crop: true
		});
	}
	if ($('.project-gallery-sm').is('*')) {
		$('.project-gallery-sm').galleria('classic', {
			autoplay: 2000,
			thumbnails: false,
			carousel: false,
			transition: 'fade',
			height: 200,
			image_crop: true
		});
	}
	
	// form stuff
	$('#scl-contact input').blur(function(){
		if ($(this).val() == '') $(this).addClass('invalid');
		else $(this).removeClass('invalid');
	});
	$('#scl-contact input[name=email]').blur(function(){
		checkEmail($(this));
	});
	$('#scl-contact input[name=phone]').blur(function(){
		checkPhone($(this));
	});
	
	// contact form submission and validation
	$('#scl-contact-submit').click(function(){
		var fn = $('#firstname');
		var ln = $('#lastname');
		var email = $('#email');
		var phone = $('#phone');
		var msg = $('#message');
		var errors = [];
		
		if (fn.val() == '') {
			errors.push('First name is required');
			fn.addClass('invalid');
		}
		if (ln.val() == '') {
			errors.push('Last name is required');
			ln.addClass('invalid');
		}
		
		if (!checkEmail(email)) errors.push('An invalid e-mail was provided');
		if (!checkPhone(phone)) errors.push('An invalid phone number was provided');
		
		if (errors.length == 0) {
			var dataStr = '';
			dataStr = 'firstname=' + fn.val();
			dataStr += '&lastname=' + ln.val();
			dataStr += '&email=' + email.val();
			dataStr += '&phone=' + phone.val();
			dataStr += '&message=' + msg.val();
			
			$.ajax({  
				type: 'POST',  
				url: ROOT_URL + 'includes/scripts/formsubmit.php',  
				data: dataStr,
				dataType: 'html',
				success: function(data, textStatus, XMLHttpRequest) {
					if (data == '') {
						// success
						$('#scl-contact').fadeOut(150, function(){
							$(this).html('<div class="success">Your request was successfully sent. Expect to hear back from us soon!</div>');
							$(this).fadeIn(150);
						});
					}
					else {
						// error returned
						$('#scl-contact > div.errors').html(data).fadeIn(150);
					}
				}  
			});
		}
		else {
			$('#scl-contact > div.errors').html(errors.join('<br/>')).fadeIn(150);
		}
		
		return false;
	});
	
});