/* Author: The Atom Group 

*/
// #######################
// Modal
// #######################

var modal = function(reference){
	if(reference != null) {
  		$(reference).colorbox({ inline: true, href: '#modal-close-wrapper', opacity: 0.5});
	} else {
		$.colorbox({ inline: true, href: '#modal-close-wrapper', opacity: 0.5});
	}
};

var mobile_modal = function () {
	$('#container').hide();
	$('#modal-wrapper').show();
	$('#modal-close').click(function(){ $('#container').show(); $('#modal-wrapper').hide(); });
};


$(document).ready(function(){
	

if($.cookie('frm_cookie') !== 'submitted') {
	if( current_page !== 'home' && jQuery.browser.mobile === false){
		setTimeout('modal()', 1500);
	} else if ( current_page !== 'home' && jQuery.browser.mobile === true ) { // be explicit about mobile
		mobile_modal();
	};

	$('#modal-close').click(function(){
		$.colorbox.close();
	});

	$('.promo').click(function(){
		modal('.promo');
	});
} else if ($.cookie('frm_cookie') === 'submitted') {
	$('.promo').remove();
}



// #######################
// Recipe slider
// #######################

$('#recipes').cycle({
	fx: 'scrollHorz',
	speed: 'slow',
	timeout: 5000,
	next: '#cabot-next',
	prev: '#cabot-prev'
});

// #######################
// Skii conditions
// #######################
if($('#current-conditions-list').length > 0){
$.getJSON('includes/ski-conditions-json.php', function(data){

	var conditions = data.items;

	for( var i = 0; i < data.totalItems; i += 1)
	{
		// helper vars
		var entry = conditions[i],
		number = i + 1,

		title = entry.resortName,

		temperature = entry.forecastBaseTemp,

		new_snow = entry.snowLast48Hours,
		
		base_min = entry.avgBaseDepthMin,
		base_max = entry.avgBaseDepthMax,
		base = 0,

		surface = entry.primarySurfaceCondition,

		$newCondition = null;

		if (temperature != '') {
			temperature = temperature + '&deg;';
		}

		if(new_snow === '' ) {
			new_snow = 0;
		}

		if(base_min !== "" && base_max !== "" ){
			base = (parseInt(base_min) + parseInt(base_max)) / 2;
		} else if (base_min === "" && base_max !== "") {
			base = base_max;
		} else if ( base_min !== "" && base_max === "") {
			base = base_min;
		}

		// set values
		
		$newCondition = '<li id="' + number + '" class="clearfix"><ul><li class="condition-info"><ul><li class="condition-text"><div class="condition-location"><a href="' + entry.webSiteLink + '" target="_blank">' + title + '</a></div><div class="condition-measurement">'+ new_snow +'&lsquo; New Snow, ' + base +'&rdquo; base</div><div class="condition-description">' + surface + '</div></li><li class="condition-temp">' + temperature + '</li></ul></li>';

		// append to list

		$('#current-conditions-list').append($newCondition);
		$('#remove-on-load').remove();

	};

	// #######################
	// Cycle current conditions
	// #######################

	$('#current-conditions-list').cycle({
		fx: 'scrollHorz',
		speed: 'slow',
		pause: 1,
		timeout: 5000,
		next: '#current-conditions-next a'
	});

});
};

// #######################
// Events-Deals-Links
// #######################

(function(){

	$('.events-content').hide();
	$('.links-content').hide();

	// helper functions
	var remove_hide = function(element, button){
		var $element = $(element),
			$button = $(button);

		$button.parent().removeClass('current');
		$element.hide();
	},

	current_show = function(elem_this, element){
		var $this = $(elem_this),
			$element= $(element);

		$this.parent().addClass('current');
		$element.show();
	},

	// core functions

	deals_hide = function(){
		remove_hide('.deals-content', '#deals');
	},

	links_hide = function(){
		remove_hide('.links-content', '#links');
	},

	events_hide = function(){
		remove_hide('.events-content', '#events');
	};

	$('#events').click(function(e){
		e.preventDefault();

		current_show(this, '.events-content');

		deals_hide();

		links_hide();
	});

	$('#deals').click(function(e){
		e.preventDefault();

		current_show(this, '.deals-content');

		events_hide();

		links_hide();
	});

	$('#links').click(function(e){
		e.preventDefault();

		current_show(this, '.links-content');

		events_hide();

		deals_hide();
	});

}());

$('#full-rules').click(function(e){
	e.preventDefault();
	$('#modal-form-container').hide();
	$('#modal-legal-info').show();
});
$('#close-terms').click(function(e){
	e.preventDefault();
	$('#modal-form-container').show();
	$('#modal-legal-info').hide();
});


$('#footer-nav-terms').colorbox({ inline: true, href: '#modal-close-wrapper', opacity: 0.5, onLoad: function(){$('#modal-form-container').remove(); $('#modal-legal-info').show(); $('#close-terms').click(function(){$.colorbox.close()})} });

$('#modal-form').submit(function(){
	var check_fields = function(){

		var $name =  $('#name').val(),
		$phone = $('#phone').val(),
		$zip = $('#zip').val(),
		$email = $('#email').val(),
		$confirm_email = $('#confirm-email').val(),
		phone_match = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/,
		zip_match = /^\d{5}$|^\d{5}-\d{4}$/,
		email_match = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
		error = function(field, message){ 
			var error_element =	'<div id="' + field +'-error" class="error">' + message + '</div>';
			return error_element;
		};

		function check_name()
		{
			if($name == '' || $name > 50)
				{
					if($('#name-error').length == 0){
						$('#name').after(error('name', 'Please enter a valid name.'));
						return false;
					} 
				} else if ($name !== '' || $name <= 50)
				{
					$('#name-error').remove();
					return true;
				}
		}; 

		function check_phone()
		{
			if ( !$phone.match(phone_match) || $phone === '' ) 
			{
				if($('#phone-error').length == 0)
				{
					$('#phone').after(error('phone', 'Please enter a valid phone number.'));
					return false;
				} 

			}	else if ( $phone.match(phone_match) )
				{ 
					$('#phone-error').remove();
					return true;
				}
		};

		function check_zip()
		{
			if(!$zip.match(zip_match) || $zip === '')
			{
				if($('#zip-error').length == 0)
				{
					$('#zip').after(error('zip', 'Please enter a valid zip code.'));
					return false;
				}
			} else if ( $zip.match(zip_match) )
			{
				$('#zip-error').remove();
				return true;
			}
		}

		function check_email()
		{
			if( $email === '')
			{
				if($('#email-error').length == 0)
				{
					$('#email').after(error('email', 'Please enter a valid email address.'));
					return false;
				}
			} else if ( $email !== '' )
				{
					$('#email-error').remove();
					return true;
				}
		}

		function confirm_email()
		{
			if($confirm_email !== $email || $confirm_email === '')
			{
				if ($('#confirm-email-error').length == 0)
				{
					$('#confirm-email').after(error('confirm-email', 'Please confirm your email address.'));
					return false;
				}
			} else if ( $confirm_email === $email )
			{
				$('#confirm-email-error').remove();
				return true;
			}
		}

		check_name();
		check_phone();
		check_zip();
		check_email();
		confirm_email();
	
		if(check_name() && check_phone() && check_zip() && check_email() && confirm_email())
		{
			var return_val = true;
			return return_val;
		}
	};

	function ga_conversion() {
		var google_conversion_id = 1055443929,
			google_conversion_language = 'en',
			google_conversion_format = '2',
			google_conversion_color = 'ffffff',
			google_conversion_label,
			google_conversion_value = 0;

		if (current_page === 'skiing') {
			google_conversion_label = "NOVFCKePuQIQ2Zej9wM";
		}
		else if (current_page === 'family') {
			google_conversion_label = "GPu3CK-OuQIQ2Zej9wM";
		}
		else if (current_page === 'romance') {
			google_conversion_label = "RVQTCL-MuQIQ2Zej9wM";
		}
		else if (current_page === 'food') {
			google_conversion_label = "tevnCLeNuQIQ2Zej9wM";
		}
		$.getScript( "http://www.googleadservices.com/pagead/conversion.js" );	
	}

	function ga_tracker() {
		if (window._gaq) {
			_gaq.push([ '_trackPageview', '/thank_you.php' ]);
			log('success');
		}
	}
	function submitFinished() { 
		$('#modal-form').hide();
		$('#modal-thankyou').show();
		$.cookie('frm_cookie', 'submitted', { expires: 365, path: '/' });
		ga_conversion();
		ga_tracker();
	};

	if(check_fields() ===  true)
	{
		var contactForm = $(this);	
		$('#submitting-notice').show();
		$.ajax( {
          url: contactForm.attr( 'action' ) + "?ajax=true",
          type: contactForm.attr( 'method' ),
      	  data: contactForm.serialize(),
      	  success: submitFinished
    	} ); 
	} 
	return false; // prevent double submission

});


}); // END DOC READY

