/* 
	### Main Javascript Functions 
	### Created On
	### Developed By Will Ayers -= dev.willayers@gmail.com
	### Additional Scripts include the information from the creator for copyrights and such
*/


function valid_e(email) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(email);
}

/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/
function slideSwitch() {
    var $active = $('#hero IMG.active');
    if ( $active.length == 0 ) $active = $('#hero IMG:last');
    var $next =  $active.next().length ? $active.next() : $('#hero IMG:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}


$(document).ready(function() {
	
	setInterval( "slideSwitch()", 4000 );
	
	$('input.focus').focus(function() {
		title = $(this).attr('title');
		
		if($(this).attr('value') == title) $(this).attr('value', '');
	});
	
	$('input.focus').blur(function() {
		title = $(this).attr('title');
		
		if($(this).attr('value') == '') $(this).attr('value', title);
	});
	
	/* Email Gathering Tool */
	$('#email_gather').submit(function() {
		
		title = $('#email_gather input').attr('title');
		value = $('#email_gather input').attr('value');
		e = false;
		msg = '';
		
		if(value == title || !valid_e(value)) { e = true; msg += 'Please enter a valid email address'; }
		
		if(e) {
			$.facebox('<p class="error_alert">' + msg + '</p>');
		} else {
			var postdata = 'email=' + value;
			
			$.ajax({
			   type: "POST",
			   url: "app/add_email.php",
			   data: postdata,
			   success: function(msg){
			     $.facebox('<p id="msg-success">Thank you for submitting your email!</p>');
				 $('#email_gather input').attr('value', title);
			   }
			 });
		}
		return false;
	})
	
	/* *** Contact Form *** */
	
	$('#contact-form').submit(function() {
		/* Variables */
		
		var v = [$('#contact_name').attr('value'), $('#contact_email').attr('value'), $('#contact_phone').attr('value'), $('#contact_questions').attr('value')];
		var e = false;
		var m = '';
		
		if(v[0] == '') { e = true; m += '<strong>Missing Field:</strong> Your name<br />' }
		if(v[1] == '') { e = true; m += '<strong>Missing Field:</strong> Email address<br />' }
		if(v[2] == '') { e = true; m += '<strong>Missing Field:</strong> Phone number<br />' }
		if(v[3] == '') { e = true; m += '<strong>Missing Field:</strong> Questions or comments<br />' }
		
		if(e) {
			$.facebox('<p class="error_alert">' + m + '</p>');
		} else {
			var postdata = 'name=' + v[0] + '&email=' + v[1] + '&phone=' + v[2] + '&questions=' + v[3] + '&action=contact';
			
			$.ajax({
				type: "POST",
				url: "app/send_mail.php",
				data: postdata,
				success: function(msg){
					$.facebox('<p id="msg-success">Thank you for submitting your questions/comments!</p>');
					$('#contact_name').attr('value', '');
					$('#contact_email').attr('value', '');
					$('#contact_phone').attr('value', '');
					$('#contact_questions').attr('value', '');
					$('#mail_success').fadeIn('slow');
				}
			});
		}
		
		return false;
	});
	
	/* *** Inquire Link *** */
	
	$('.inquire').click(function() { 
		$.get('app/inquire.html', function(data) { $.facebox(data) });
		return false;
	});
});