// default.js

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

$(function(){
	
	$('#locationMap').hide();
	$('#locationLarger').hide();
	
	$('#photos').cycle({ 
		fx:    'fade', 
		speed:	4000,
		timeout: 3000,
		pause:	0
	});
	
	 var options = { 
	 	url:		'/ajax.php?action=survey&ajax=y',
        beforeSubmit:  function(){ if($('#locationID').val() == 0){ $('#surveyError').fadeIn(); return false; }else{$('#surveyBtn,#surveyError').fadeOut(); return true;} },
        success:    function(responseText, statusText){ if(responseText.success == true){ $('#surveyThankYou').fadeIn(); $('#survey').slideUp(); $('html, body').animate({ scrollTop: 0 }, 1000); }else{ $('#surveyBtn').fadeIn(); } },
        type:      'post',        
        dataType:  'json'
    }; 
 	
	
    $('#survey').ajaxForm(options);
	
	if($('#date_of_visit').val()){
		$('#date_of_visit').datepicker();
	}
	
	$('.panorama').panorama({tone: 16});
	
	//windowshade
	$('.windowshade').each(function(){
		$(this).find('.string').bind('click', function(){
			$(this).parent('p').siblings('.shade').slideToggle();
			return false;
		});
	});
	
	//
	$('.hide-me').hide();
	
});

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();
	
    $('html,body').animate({scrollTop: $("#locationMap").offset().top},'slow');
	
	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();
			}
		});
		
	}
}

function checkSurvey() {
	
}
