
var ignoreHashChange = false;

function sendContactForm(){
	// validate submission
	if(		$("#contactUsForm input[name='name']").val().length < 1
		|| 	$("#contactUsForm input[name='email']").val().length < 1
		||	$("#contactUsForm textarea[name='comments']").val().length < 1){
		alert("Please complete all form fields.");
		return false;
	}
	$('#contactUsMessage').text('');
	$("#contactUsForm .contactLoader").css('display', 'inline');
	$.post("ajax/proxy.cfm?action=contactUs",
		   $('#contactUsForm').serialize(),
		   function(data){
			   if(data.trim() == 'true'){
					$('#contactUsForm').slideUp('slow');
				   	$('#contactUsMessage').html('<br/>Thank you, your comments have been received. We appreciate your interest in South Street!');
				}else{
					$('#contactUsMessage').html('<div class="errorBox>We are sorry, and error has occurred and your message could not be sent.</div>');	
				}
				$("#contactUsForm .contactLoader").css('display', 'none');
		   },
		   "html"
	);
}

function sendSuggestionForm(){
	// validate submission
	if($("#suggestionForm input[name='location']").val().length < 1){
		alert("Please enter a suggested location");
		return false;
	}
	
	$('#suggestionMessage').text('');
	$("#suggestionForm .contactLoader").css('display', 'inline');
	$.post("ajax/proxy.cfm?action=suggestLocation",
		   $('#suggestionForm').serialize(),
		   function(data){
			   if(data.trim() == 'true'){
					$('#suggestionForm').slideUp('slow');
				   	$('#suggestionMessage').html('<br/>Thank you! We have received your suggestion.');
				}else{
					$('#suggestionMessage').html('<div class="errorBox>We are sorry, and error has occurred and your message could not be sent.</div>');	
				}
				$("#suggestionForm .contactLoader").css('display', 'none');
		   },
		   "html"
	);
}

function getCategoryProducts(_category){
	currentCategory = _category;
	$('#ajaxLoader').fadeIn("fast");
	$('#productsDisplay').fadeTo(100,0, function(){
		$.get('ajax/products.cfm?category='+_category, function(data){
			//open modal with data returned
			$('#productsDisplay').fadeTo(1500,1).html(data);
			$('#ajaxLoader').fadeOut("slow");
			SWFAddress.setTitle('Menu - ' + _category + ' : South Street Cheese Steaks');
		});										 
	});
}

function getMap(_map){
	//make an AJAX call to get the correct form
	$.get("ajax/mapWrapper.cfm?map="+_map, function(data){
		//open modal with data returned
		$(data).modal({
			close: 			true,
			position: 		["15%",],
			overlayId: 		'mapOverlay',
			containerId: 	'mapContainer',
			onOpen: 		mapModal.open,
			onClose: 		mapModal.close
		});
	});
}

var mapModal = {
	message: null,
	//open modal function
	open: function(dialog){
		// dynamically determine height
		var h = 385;

		dialog.overlay.fadeIn(200, function() {
			dialog.container.fadeIn(200, function() {
				dialog.data.fadeIn(200, function() {
					$('#mapContainer .modalContent').animate({
						height: h
					}, function() {});								 
				});								  
			});									
		});
	},//end option function
	
	close: function(dialog){
		$('#mapContainer .mapMessage').fadeOut();
		$('#mapContainer .modalContent').animate({
			height: 40													 
		}, function(){
			dialog.data.fadeOut(200, function() {
				dialog.container.fadeOut(200, function() {
					dialog.overlay.fadeOut(200, function() {
						$.modal.close();
					});									   
				});								  
			});
		});
	},//end close function
	
	error: function(){
		alert('An error has occurred! Process failed.');
	}//end error function
};//end productForm object

function display(id, wrapper){
	//first, find the div in the wrapper (display:block) and fade out
	//then fade in the div specified
	$('#ajaxLoader').fadeIn("fast");
	$("#"+wrapper).children("div").filter( 
			function(index){ return $(this).css("display") == "block";
		}).fadeOut("normal", function(){
			$("#"+id).fadeIn("normal"); 
			$('#ajaxLoader').fadeOut("slow");
		}
	);
}

function initiateModals(){
	//preload the images for the modal box
	var img = ['productModal_top.gif', 'productModal_bottom.gif', 'productModal_loading.gif'];
	$(img).each( function() {
		var i = new Image();
		i.src = 'assets/images/' + this;
	});//end image preloader
}//end initiateModals function

function init(){
	$("a:not(.external, .silent, .lightBox)").click( function(){
		$("#ajaxLoader").css('display', 'block');					   
	});
	$("a").focus( function(){
		$(this).blur();	   
	});
}

function initiateSwfAddress(type){
	if(type == "menu"){
		SWFAddress.addEventListener("change", menuChangeHandler);
	}else{
		SWFAddress.addEventListener("change", cateringChangeHandler);
	}
}

function menuChangeHandler(event){
	if(ignoreHashChange){
		ignoreHashChange = false;
		return;
	}
	var category = event.value.replace(/^\//, '').toLowerCase();
	if(category.length < 2)
		category = "sandwiches";
	getCategoryProducts(category);	
}

function cateringChangeHandler(event){
	var catering = event.value.replace(/^(#|\/)+/, '').toLowerCase();
	if(catering != "sss" && catering != "pizzas")
		catering = "experience";
	display(catering, 'cateringWrapper');
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
}


