var billboards = '';
var news = '';

function ShowDialog (title,file,width) {
	if (!width) width = 660;
	if ($('#dialog').length > 0) {
		$('#dialog').remove();
	}
	$('body').append('<div id="dialog"></div>');
	$.get(file, function(data) {
		$('#dialog').hide().html(data);
		$('#dialog').dialog({
			modal: true,
			title: title,
			resizable: false,
			width: width,
			height: 'auto',
			minHeight: 0,
			closeOnEscape: false,
			open: function() {
			    if ($(this).parent().height() > $(window).height()) {
			        $(this).height($(window).height()*0.7);
			    }
			    $(this).dialog({position: "center"});
			},
			close: function() {
				$(this).remove();
			}
		});
	});
}

function CloseDialog () {
	$('#dialog').dialog('close');
}

function HighlightQuantityFields() {
	$('table.catering input[type="text"][value!=""]').addClass('has_qty');
	$('table.catering input[type="text"]').change(function() {
		if (parseInt($(this).val()) > 0 && !$(this).hasClass('min_qty')) {
			$(this).addClass('has_qty');
		}
		else {
			$(this).removeClass('has_qty');
		}
	});
}

function UpdateDessertQuantities() {
	$('#afternoon_section').load("/action.php?action=update_dessert_quantities",function() {
		 HighlightQuantityFields();
	});
}

function ScrollUp (id,offset) {
	var obj = $('#'+id).offset();
	window.scrollTo(0,obj.top+offset);
}

function RefreshCateringOrder () {
	$('#catering_items').load('/action.php?action=refresh_catering_order');
}

function UpdateCateringOrder (form) {
	$.post('/action.php', $(form).serialize(), function(data) {
		var new_total = data.total;

		RefreshCateringOrder();
		CloseDialog();
		UpdateCateringTotal(new_total);
		ShowDialog('Items added','/dialogs/catering_items_added.php',280);
	},'json');
}

function UpdateCateringTotal(total) {
	$('#my_order_total').html(total);
}

function DeleteCateringItem (itemid) {
	$.post('/action.php', { action: 'delete_catering_item', id: itemid }, function(data) {
		RefreshCateringOrder();
	});
	
}

function CheckForBlankCateringForm() {
	if ($('input[type="text"][value!=""]').length == 0) {
		alert('You must enter a quantity for at least one item.');
		return false;
	}
	else {
		return true;
	}
}
		
function NewsletterSignUp () {
	if (!$('#enter_name').val() || !$('#enter_email').val()) {
		alert('Please enter your name and email address');
	}
	else {
		$.post("/action.php", { action: 'newsletter_signup', name: $('#enter_name').val(), email: $('#enter_email').val() }, function(data) {
			$('#newsletter_response').html(data);
			$('#newsletter_response').slideDown(200).animate({ opacity: 1.0 },3000).fadeOut();
			$('#newsletter_form :input[type="text"]').val('');
		});
	}
}

function InitializeBillboard () {
	$.getJSON('action.php?action=initialize_billboard',function(data) {
		billboards = data;
		FlipBillboard(0);
	});
}

function FlipBillboard (num) {
	var total_images = billboards.length;
	var new_num = parseInt(num+1) % total_images;

	BillboardTimer = setTimeout('FlipBillboard('+new_num+')',3000);
	
	$('#billboard .pic img').hide(); // hide the current pic
	$('#billboard .pic img').attr('src','/pics/home_page/'+billboards[num].filename); // load the next pic
	$('#billboard .pic img').fadeIn(1000); // show the next pic

	$('#billboard .details .name').html(billboards[num].name);
	$('#billboard .details .description').html(billboards[num].description);
}

function InitializeNews () {
	$.getJSON('action.php?action=initialize_news',function(data) {
		news = data;
		SetNews(news[0]);
		setTimeout('FlipNews(1)',5000);
	});
}

function SetNews (obj) {
	$('#news .contents').html(obj.short_description);
	$('#news a.read_more').attr('href','/news/'+obj.id);
}

function FlipNews (num) {
	var total_news = news.length;
	var new_num = parseInt(num+1) % total_news;

	$('#news').fadeOut(500,function() {
		SetNews(news[num]);
		$('#news').fadeIn(1000);
	});

	setTimeout('FlipNews('+new_num+')',5000);
}

function LoadTab (id) {
	$('#tabs li').removeClass('on');
	$('#tabs #'+id+'_tab').addClass('on');
	$('#belt .tab_panel').hide();
	$('#belt #'+id).show();
}

function Validate (form) {
	var req_fields = $(form).find(".required");
	var valid = true;

	$(form).find("input,select,textarea").removeClass('left_blank');
	
	for (i=0; i<req_fields.length; i++) {
		var field_name = $(req_fields[i]).attr('name');

		if ($(req_fields[i]).val() == '') {
			$(req_fields[i]).addClass('left_blank');
			valid = false;
		}
	}
	
	if (valid == false) {
		DisplayError("You left a required field blank");
	}
	
	return valid;
}

function DisplayError (text) {
	if (text) $("#error").html(text);
	$("#error").animate({ opacity: 1.0 }, 0).slideDown(200); // .animate({ opacity: 1.0 }, 5000).animate({ opacity: 0 }, 1000).slideUp()
}

function DisplayMessage (text) {
	if (text) $("#error").html(text);
	$("#message").animate({ opacity: 1.0 }, 0).slideDown(200); // .animate({ opacity: 1.0 }, 5000).animate({ opacity: 0 }, 1000).slideUp()
}

function ToggleNavMenu (id) {
	$('#'+id).toggleClass('on');
	$('#'+id+'_menu').slideToggle(100);
	$('.nav_button').filter('[id!="'+id+'"]').removeClass('on');
	$('.nav_menu').filter('[id!="'+id+'_menu"]').slideUp(100);
}

function CheckDeliveryDate () {
	var current_date = new Date();
	var delivery_date = new Date($('#delivery_date').val()+' '+$('#delivery_time').val());
	var prep_time = (delivery_date.getTime()-current_date.getTime())/3600000;
	
	if (delivery_date.getDay() == 6 && delivery_date.getHours() < 10) {
		DisplayError("Hannah's isn't open until 10:00am on Saturdays.<br />Please select a later delivery time.");
		return false;
	}
	else if (prep_time < 8) {
		DisplayError("You've selected a delivery date/time that is less than<br />eight hours away. Please call us at (312) 621-1111.");
		return false;
	}
	else {
		return true;
	}
}

$(document).ready(function() {
	// hide the drop-down menus if the user clicks outside them
	$(document).click(function() {
		$('#our_locations_menu').slideUp(100, function() {
			$('#our_locations').removeClass('on');
		});
		$('#mailing_list_menu').slideUp(100, function() {
			$('#mailing_list').removeClass('on');
		});
		$('#contact_us_menu').slideUp(100, function() {
			$('#contact_us').removeClass('on');
		});
		$('#jobs_menu').slideUp(100, function() {
			$('#jobs').removeClass('on');
		});
	});
	$("#our_locations, #mailing_list, #contact_us, #jobs, #our_locations_menu, #mailing_list_menu, #contact_us_menu, #jobs_menu").click(function(e) {
	    e.stopPropagation();
	});
	
	// hook up datepickers
	$(".datepicker").datepicker({
		minDate: 0
	});

	// highlight catering quantity fields w/ values
	HighlightQuantityFields();
	
	// hook up catering thumbnail zoom dialog
	if(jQuery().fancybox) {
		$('a.preview').fancybox({speedIn: 200});
	}
	
	// hook up min_qty check on individual catering items
	$('input.min_qty').change(function() {
		var add_more = $(this).parent().parent().children().find('.add_more');
		if ($(this).val() == '' || $(this).val() == 0) {
			$(this).removeClass('left_blank');
			$(add_more).hide();
			$('#update_order_button').show();
		}
		else if ($(this).val() < $(this).attr('min_qty')) {
			$(this).addClass('left_blank');
			$(add_more).show();
			$('#update_order_button').hide();
		}
		else {
			$(this).removeClass('left_blank');
			$(this).addClass('has_qty');
			$(add_more).hide();
			$('#update_order_button').show();
		}
	});
	
	/*
	Block Sundays
		beforeShowDay: function(date) {
		    var day = date.getDay();
		    return [(day != 0),''];
		}
	*/
});

