var spot_count 	  = 1;
var spot_position = 1;
var num_spots	  = 0;
var btn_clicked   = '';

// update news form: store the value of the news image 
function store_image_field() {
	$('input.news_image_field').each(function(){
		$(this).attr('savedValue', $(this).val());
	});
}

// update news form: restore the value of the news image
function restore_image_field() {
	$('input.news_image_field').each(function(){
		$(this).val($(this).attr('savedValue'));
	});
}

// update menu form: update field ids
function update_field_ids() {			

	$('input.hidden_update_field').val('');

    $('input.update_field').each(function() {

		var id_raw = this.id;
		var id_ex = id_raw.split('_');
		var field_id = id_ex[2];
		var id = id_ex[3];

        if($(this).attr('checked') == true) {
        	$('#field_id_'+field_id).val($('#field_id_'+field_id).val() + id + '|');
        }

    });

	// Go through all the hidden id fields and clean up the last pipe
	$('input.hidden_update_field').each(function() {
		var val_raw = $(this).val();
		var new_val = val_raw.substring(0, (val_raw.length - 1));
		$(this).val(new_val);
	});

}

$(document).ready(function(){
	
	// update menu form: run update field ids
	update_field_ids();
	$('input.update_field').click(function() {
		update_field_ids();
	});
	
	// update news form: delete image
	$('span.deleted-message').hide();
	store_image_field();
	restore_image_field();
	$('a.delete-img-btn').click(function(){
		$(this).next('input.news_image_field').val('')
		.parent('span.current-image-message').hide()
		.siblings('span.deleted-message').show();
		return false;
	});
	
	// update news form: undo delete image
	$('a.undo-delete-btn').click(function(){
		restore_image_field();
		$(this).next('input.news_image_upload').val('');
		$(this).next('input.news_image_upload')
		.parent('span.deleted-message').hide()
		.siblings('span.current-image-message').show();
		return false;
	});
	
	// Get the number of feature items from the spots ul rel
	num_spots = $('#spots > li').size();

	$("#homecarousel").jCarouselLite({
		// auto: 5000, 
		speed: 1000,
	    btnNext: "#ss-next",
	    btnPrev: "#ss-prev",
		easing: "easeOutQuart",
		afterEnd: function() {
			if(btn_clicked == 'prev') {
				$('#spots li').removeClass('active');
				spot_position--;
				if(spot_position == 0) spot_position = num_spots;
				$('#spot-feature-item-'+spot_position).addClass('active');
			} else if(btn_clicked == 'next') {
				$('#spots li').removeClass('active');
				spot_position++;
				if(spot_position > num_spots) spot_position = 1;
				$('#spot-feature-item-'+spot_position).addClass('active');
			}
		}
	});

	$('.tooltip-parent').simpletip({
		onShow: function() {
			this.update(this.getParent().attr('rel'));
		}
	});

	$('#ss-prev').click(function() {
		btn_clicked = 'prev';
	});

	$('#ss-next').click(function() {
		btn_clicked = 'next';
	});

	$("a[rel='external']").click( function(){ window.open($(this).attr('href')); return false; });
	$("a[rel='nofollow']").click( function(){ window.open($(this).attr('href')); return false; });

	// contact form validation
	$('#feedback').validate({
		rules: {
			comment_type: "required",
			location_id: "required",
			comment: "required",
			prefix: "required",
			first_name: "required",
			last_name: "required",
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			comment_type: "Please choose a type of comment",
			location_id: "Please enter the store location",
			comment: "Please submit a comment",
			prefix: "Please tell us how to address you",
			first_name: "Please provide your first name",
			last_name: "Please provide your last name",
			email: "Please enter a valid email address"
		},
		errorElement: "em",
		errorPlacement: function(error, element) {
			var id_is = element.attr("id");
			// alert(id_is);
			error.appendTo( $('#fs-'+id_is+' label') );
	   	}
		// debug: true
	});
	
	// registration form validation
	$('#register_member_form').validate({
		rules: {
			username: {
				required: true,
				minlength: 4
				//remote: "users.php"
			},
			password: {
				required: true,
				minlength: 5
			},
			password_confirm: {
				required: true,
				equalTo: "#password"
			},
			screen_name: {
				required: true
				//remote: "users.php"
			},
			email: {
				required: true,
				email: true
				//remote: "users.php"
			},
			m_field_id_1: "required",
			accept_terms: "required"
		},
		messages: {
			username: {
				required: "Please enter a username.",
				minlength: "Your username is too short."
				//remote: "That username is already in use. Please choose another."
			},
			password: {
				required: "Please enter a password.",
				minlength: "Your password is too short."
			},
			password_confirm: {
				required: "Please confirm your password.",
				equalTo: "Your passwords do not match."
			},
			screen_name: {
				required: "Please tell us your first and last name."
				//remote: "That name is already in use. Please enter another."
			},
			email: {
				required: "Please enter your email address.",
				email: "Please enter a valid email address."
				//remote: "That email address is already in use. Please enter another."
			},
			m_field_id_1: "Please select your restaurant location.",
			accept_terms: "Please accept the Terms of Use."
		},
		errorElement: "em",
		errorPlacement: function(error, element) {
			var id_is = element.attr("id");
			// alert(id_is);
			error.appendTo( $('#fs-'+id_is+' label') );
	   	}
		// debug: true
	});
	
	// registration form validation
	$('#login_member_form').validate({
		rules: {
			username: {
				required: true
				//remote: "users.php"
			},
			password: {
				required: true
				//remote: "users.php"
			}
		},
		messages: {
			username: {
				required: "Please enter your username."
				//remote: "Your username was not found."
			},
			password: {
				required: "Please enter your password."
				//remote: "Your password is incorrect."
			}
		},
		errorElement: "em",
		errorPlacement: function(error, element) {
			var id_is = element.attr("id");
			// alert(id_is);
			error.appendTo( $('#fs-'+id_is+' label') );
	   	}
		// debug: true
	});
	
	// forgot password form validation
	$('#forgot_password_form').validate({
		rules: {
			email: {
				required: true,
				email: true
				//remote: "users.php"
			}
		},
		messages: {
			password: {
				required: "Please enter your email address.",
				email: "Please enter a valid email address."
				//remote: "That email address was not found."
			}
		},
		errorElement: "em",
		errorPlacement: function(error, element) {
			var id_is = element.attr("id");
			// alert(id_is);
			error.appendTo( $('#fs-'+id_is+' label') );
	   	}
		// debug: true
	});
	
	// restaurant update form validation
	$('#entryform').validate({
		rules: {
			// store name
			title: "required",
			// street name (address)
			field_id_16: "required",
			// city
			field_id_18: "required",
			// state
			field_id_19: "required",
			// zip
			field_id_20: "required",
			// phone
			field_id_21: "required",
			// latitude
			field_id_22: "required",
			// longitude
			field_id_23: "required",
			// store number
			field_id_31: "required",
			// news headline 1
			field_id_61: {
				required: "#field_id_62:filled"
			},
			// news story 1
			field_id_62: {
				required: "#field_id_61:filled"
			},
			// news headline 2
			field_id_64: {
				required: "#field_id_65:filled"
			},
			// news story 2
			field_id_65: {
				required: "#field_id_64:filled"
			},
			// news headline 3
			field_id_67: {
				required: "#field_id_68:filled"
			},
			// news story 3
			field_id_68: {
				required: "#field_id_67:filled"
			}
		},
		messages: {
			// store name
			title: "Please enter the store name.",
			// street name (address)
			field_id_16: "Please enter the street address.",
			// city
			field_id_18: "Please enter the city.",
			// state
			field_id_19: "Please enter the state.",
			// zip
			field_id_20: "Please enter the zip.",
			// phone
			field_id_21: "Please enter the phone number.",
			// latitude
			field_id_22: "Please enter the latitude.",
			// longitude
			field_id_23: "Please enter the longitude.",
			// store number
			field_id_31: "Please enter the store number.",
			// news headline 1
			field_id_61: {
				required: "Please enter a headline."
			},
			// news story 1
			field_id_62: {
				required: "Please enter a story."
			},
			// news headline 2
			field_id_64: {
				required: "Please enter a headline."
			},
			// news story 2
			field_id_65: {
				required: "Please enter a story."
			},
			// news headline 3
			field_id_67: {
				required: "Please enter a headline."
			},
			// news story 3
			field_id_68: {
				required: "Please enter a story."
			}
		},
		errorElement: "em",
		errorPlacement: function(error, element) {
			var id_is = element.attr("id");
			// alert(id_is);
			error.appendTo( $('#fs-'+id_is+' label') );
	   	}
		// debug: true
	});

	$("#search_string").focus(function(){
		if(this.value == this.defaultValue){
			this.value = "";
		}
	}).blur(function(){
		if(!this.value.length){
			this.value = this.defaultValue;
		}
	});

	// Contact & Careers form
	$('form#feedback select#location_id, form#careers select#location_id, form#feedback #comment_type').change(function(){
		// Format some values based on the selection
		var loc_name  = $('select#location_id option:selected').text();
		var loc_city  = $('select#location_id option:selected').parents('optgroup').attr('label');
		var loc_store_id  = $('select#location_id option:selected').attr('class');

		// Set a readable literal value for freeform
		$('#location').val(loc_city+' ('+loc_name+')');
		$('#store_id').val(loc_store_id);

		update_freeform_params();
	});

	// Contact & Careers form
	$('form#feedback_beta select#location_id, form#feedback_beta #comment_type').change(function(){
		// Format some values based on the selection
		var loc_name  = $('select#location_id option:selected').text();
		var loc_city  = $('select#location_id option:selected').parents('optgroup').attr('label');
		var loc_store_id  = $('select#location_id option:selected').attr('class');

		// Set a readable literal value for freeform
		$('#location').val(loc_city+' ('+loc_name+')');
		$('#location_name').val(loc_name);
		$('#store_id').val(loc_store_id);

		update_freeform_params_beta();
	});

	// Need to go ahead and do this for the Catering page on load
	update_freeform_params();
	
	// make block clickable on /restaurant/locations/
	$(".user-location-wrap, .user-location-wrap-first").click(function(){
		window.location=$(this).find("a").attr("href"); return false;
	});
	$(".user-location-wrap, .user-location-wrap-first").css("cursor","pointer");
	
	$("optgroup:empty").remove();
	
});

// Update the freeform params
function update_freeform_params(){
	if($('select#location_id option:selected').val()){
		$.ajax({
			type: "POST",
			url: "/inc/emailer-utility.php",
			data: "li="+$('select#location_id option:selected').val()+"&fn="+$('#segment_1').val()+"&fp="+$('input[name=params_id]').val()+"&it="+$('select#comment_type option:selected').val()
		});
	}
}


function update_freeform_params_beta(){
	if($('select#location_id option:selected').val()){
		$.ajax({
			type: "POST",
			url: "/inc/beta.emailer-utility.php",
			data: "li="+$('select#location_id option:selected').val()+"&fn="+$('#segment_1').val()+"&fp="+$('input[name=params_id]').val()+"&it="+$('select#comment_type option:selected').val()+"&ln="+$('select#location_id option:selected').text()
		});
	}
}