var rotationUrls;
var rotationImgs;
var timer;
var currentImage = 1;
var maxImage = 12;

function startSpinner()
{
	rotationUrls = [];
	rotationImgs = [];
	
	rotationUrls[1] = "Menu.aspx";
	rotationUrls[2] = "Coupons.aspx";
	rotationUrls[3] = "Menu.aspx";
	rotationUrls[4] = "Coupons.aspx";
	rotationUrls[5] = "Menu.aspx";
	rotationUrls[6] = "Coupons.aspx";
	rotationUrls[7] = "Menu.aspx";
	rotationUrls[8] = "Coupons.aspx";
	rotationUrls[9] = "Menu.aspx";
	rotationUrls[10] = "Coupons.aspx";
	rotationUrls[11] = "Menu.aspx";
	rotationUrls[12] = "Coupons.aspx";
	
	rotationImgs[1] = "images/rotator/8pc-Meal.jpg";
	rotationImgs[2] = "images/coupons/15PctOff.png";
	rotationImgs[3] = "images/rotator/chickenpotpie.jpg";
	rotationImgs[4] = "images/coupons/BonelessWingsCup.png";
	rotationImgs[5] = "images/rotator/Wingettes.jpg";
	rotationImgs[6] = "images/coupons/CountryFriedSteakMeal.png";
	rotationImgs[7] = "images/rotator/8piecefamilyfeast.jpg";
	rotationImgs[8] = "images/coupons/BreastMeal.png";
	rotationImgs[9] = "images/rotator/Ribs.jpg";
	rotationImgs[10] = "images/coupons/WingSnack.png";
	rotationImgs[11] = "images/rotator/Catering.jpg";
	rotationImgs[12] = "images/coupons/15PctOff.png";
	
	timer = setTimeout(imgSwitch, 3000);
}

function imgSwitch()
{
	currentImage++;
	if (currentImage > maxImage) currentImage = 1;
	
	var linky = document.getElementById("imgRotLnk");
	var picky = document.getElementById("imgRotPic");
	
	linky.href = rotationUrls[currentImage];
	picky.src = rotationImgs[currentImage];

	timer = setTimeout(imgSwitch, 5000);
}

// Menu

function hideMenu()
{
	hideAllShowOne('menuFront');
}

function hideAllShowOne(divName)
{
	document.getElementById("menuFront").style.display = 'none';
	document.getElementById("menuDinners").style.display = 'none';
	document.getElementById("menuBoxes").style.display = 'none';
	document.getElementById("menuFamily").style.display = 'none';
	document.getElementById("menuSnacks").style.display = 'none';
	document.getElementById("menuChildren").style.display = 'none';
	document.getElementById("menuLunch").style.display = 'none';
	document.getElementById("menuSpecialty").style.display = 'none';
	document.getElementById("menuDessert").style.display = 'none';
	document.getElementById("menuDrinks").style.display = 'none';
	document.getElementById(divName).style.display = 'block';
}

// Catering Calculator

var orders = new Array();
var totals = new Array();

function hideAllShowCatering(divName)
{
	document.getElementById("bulk").style.display = 'none';
	document.getElementById("breakfast").style.display = 'none';
	document.getElementById("dessert").style.display = 'none';
	document.getElementById("drinks").style.display = 'none';
	document.getElementById("other").style.display = 'none';
	document.getElementById("full").style.display = 'none';
	document.getElementById(divName).style.display = 'block';
}

function startCatering()
{
	hideAllShowCatering('full');
}

function updateRunningTotal()
{
	var txt;
	var tgt = document.getElementById('runningTotal');
	txt = "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">";
	var total = 0;
	for (keyVar in orders)
	{
		if (totals[keyVar]>0) {
			txt += orders[keyVar];
			total += totals[keyVar];
		}
	}

	txt += "</table><p>TOTAL: " + formatCurrency(total) + "</p>";
	tgt.innerHTML = txt;
}

function calcPrice(itemId, resultId, unitPrice, minimum, niceName) {
	var itemBox = document.getElementById(itemId);
	var resultSpan = document.getElementById(resultId);
	var number = itemBox.value;
	var tmp = formatCurrency(number * unitPrice);

	if (number >= minimum || number == 0) {
		resultSpan.innerHTML = tmp
		totals[niceName] = number * unitPrice;
		orders[niceName] = "<tr><td>" +number + "x " + niceName + "</td><td>" + tmp + "</td></tr>";
	} else {
		alert("There is a minimum order of "+minimum+" on orders of "+niceName);
	}

	updateRunningTotal();
}

function requestOrder()
{
	// Fill in data
	var txt;
	txt = "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">";
	var total = 0;
	for (keyVar in orders)
	{
		if (totals[keyVar]>0) {
			txt += orders[keyVar];
			total += totals[keyVar];
		}
	}

	txt += "</table><p>TOTAL: " + formatCurrency(total) + "</p>";

	if (total < 50) {
		alert('Our minimum catering order is $50. Please add more items to your order.');
	} else {
		document.getElementById('ordertxt').value = txt;
		document.getElementById('ordertotal').value = total;
	
		// Submit the form
		document.forms[0].submit();
	}
}

// Utils
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
