// default.js

//
$.postJSON = function (url, data, callback) {
	$.post(url, data, callback, 'json');
};

$(function(){
	
	$('#locationMap').hide();
	$('#locationLarger').hide();
	
	$('#photos').cycle({ 
		fx:    'fade', 
		speed:  4000
	});
	
});

function viewLocation(url){
		
	// link
	linkURL = url;
	linkURL = linkURL.replace(/&source=s_q/i,'&source=embed').replace(/&output=embed/i, '');
	

	$('#locationLarger').attr('href', linkURL);
	
	$('#locationMap').slideDown( 'slow', function(){$(this).attr('src', url);});
	$('#locationLarger').show();
	
	return false;
}

function regIsEmail(fData){
	var reg = new RegExp("^[0-9a-zA-Z\.]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");
	
	return reg.test(fData);
}

function checkForm() {
	var hold = false;
	var err	= '';
	
	$('#submit').fadeOut();
	
	$('input').not('#submit').each(function (){
	    
		if(!$(this).val()){
			$(this).css({'border':'1px solid #ff0000', 'background': '#ffcccc'});
			err += '- ' + $(this).siblings('label').text() + ' is a required field  ';
			hold = true;
		} else {
			$(this).css({'border':'1px solid #cccccc', 'background': '#ffffff'});
		}
		
	})
	
	if(!regIsEmail($('#email').val())){
		$('#email').css({'border':'1px solid #ff0000', 'background': '#ffcccc'});
		err += '- ' + $('#email').siblings('label').text() + ' is not valid  ';
		hold = true;
	} else {
		$('#email').css({'border':'1px solid #cccccc', 'background': '#ffffff'});
	}
	
	if(hold == true){
		$('#error').html(err);
		$('#submit').fadeIn();
		
		return false;
	} else {
	
		$('#error').html('Sending Information ...');
	
		$('#submitBtn').fadeOut();
		
		$.postJSON('/ajax.php',{'action':'newsletter', 'firstName':$('#firstName').val(), 'lastName':$('#lastName').val(), 'email':$('#email').val(), 'ajax':'y' },function(data){
			if(data.success == true){
				$('#signUp').fadeOut();
				$('#thankYou').fadeIn();
			}else{
				$('#error').html('Error Sending Information');
				$('#submit').fadeIn();
			}
		});
		
	}
}