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

$(document).ready(function(){
	// 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
	});

	$("#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');

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

		update_freeform_params();
	});
	// Need to go ahead and do this for the Catering page on load
	update_freeform_params();

});

// 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()
		});
	}
}