var guideNotes;
var winePairingPageNo;
var tabColourOn = '#ffeeee';
var tabColourOff = '#eebbcc';

function setUp() {
  if (validBrowser() == true) {
    guideDisplay();
    document.getElementById("tabContainer").style.display = 'none';
    toggleSplash("show");

  } else {
    toggleSplash("hide");
    document.location.href = "javascript:document.write ('Browser is not supported.');window.stop()";
  }
}

function changeTab(tabNo) {
  var tabName = "tab" + tabNo;
  hideAllTabs();
  toggleSplash("hide");

  document.getElementById("tabContainer").style.visibility = 'visible';
  document.getElementById("tabContainer").style.display = 'block';

  document.getElementById(tabName).style.display = 'block';
  toggleNavTab("navLink" + tabNo, "on")
}

function toggleNavTab(tabName, toggle) {
  if (toggle == "on") {
    document.getElementById(tabName).style.background = tabColourOn;
  } else {
    document.getElementById(tabName).style.background = tabColourOff;
  }
}

function hideAllTabs() {
  document.getElementById("tab1").style.display = 'none';
  document.getElementById("tab2").style.display = 'none';
  document.getElementById("tab3").style.display = 'none'; 

  toggleNavTab("navLink1", "off")  
  toggleNavTab("navLink2", "off")  
  toggleNavTab("navLink3", "off")
}

function toggleSplash(showOrHide) {
  if (showOrHide == "show") { document.getElementById("splash").style.display = 'block'; } 
  else { document.getElementById("splash").style.display = 'none'; }
}

function validBrowser() {
  var browserAgent = navigator.userAgent;
  var indexiPod = browserAgent.indexOf("iPod");
  var indexiPhone = browserAgent.indexOf("iPhone");
  
  if ((indexiPod > 0) || (indexiPhone > 0)) {
    return true;
  } else {
    return false;
  } 
}


// ----- TAB ONE -----

function guideDisplay() {
  winePairingPageNo = 0;

  guideNotes =  [ 
 "<h1 align='center'><a href='#' onClick='guidePageForward();'/>Begin...</a></h1>",  
 "<b>1. Consider the flavour intensity of your meal</b><p>Consider the type of food that you will be having in advance. The old rule says that white wines go well with foods such as chicken and fish and that red wines go well with steaks, pizzas and the like; however, this rule isn't always your best bet.</p><p>Instead, it is much better to match light-bodied wines with foods that are lighter in flavour intensity. Similarly, it is ideal to match full-bodied wines with more flavourful, full-bodied dishes.</p><p>Foods that possess the least amount of flavour intensity (beginning with the least intense) include: scallop, shrimp, trout, chicken, and halibut; whereas, foods that contain more flavour intensity (beginning with the most intense) include:  venison, steak, beef tenderloin, pork, and salmon.</p><p>Continue below...</p>" + wrapNavInDiv(showNextLink()), 
 "<p>On a flavour intensity scale ranging from the least intense to the most intense, wines would be ranked as follows:  Riesling (white), Pinot Grigio (white), Chenin Blanc (white), Sauvignon Blanc (white), Chardonnay (white), Pinot Noir (red), Sangiovese (red), Merlot (red), Zinfandel (red), Cabernet Sauvignon (red), and Syrah/Shiraz (red).  Consider choosing a wine whose flavour intensity aligns nicely with that of your meal.  When considering food intensity, be sure to also take note of how the food is prepared, and what type of sauce it is served with.</p><p>Continue below...</p>" + wrapNavInDiv(showBackLink() + " | " + showNextLink()),  
"<b>2. Balance out spicy foods with sweet wines</b><p>Now having said that,  it can sometimes be more beneficial to contrast the effects of your meal with the wine.  For instance, when eating foods that are a bit spicy, it is best to choose a sweeter wine that will balance out the heat from the food.  Wines such as Riesling and Gewurztraminer do a great job of contrasting spicy Indian foods such as beef vindaloo and curry chicken as well as many Thai dishes.  Blush wines such as White Shiraz, and White Zinfandel are also good options here.</p><p>Continue below...</p>" + wrapNavInDiv(showBackLink() + " | " + showNextLink()),  
"<b>3. Pair an off-dry wine with foods that are a bit sweet</b><p>Foods prepared using sweet and sour sauces, glazes, and teriyaki - sauces which tend to be on the sweet side - go best with wines that are off dry, or in other words, slightly sweet.   For these types of foods consider wines such as Riesling, Pinot Grigio, and Chenin Blanc as per the flavour intensity rankings for wine mentioned earlier.</p><p>Continue below...</p>" + wrapNavInDiv(showBackLink() + " | " + showNextLink()),  
"<b>4. Pair a slightly acidic wine with foods that contain high acidity</b><p>For acidic foods such as salads containing Italian dressing or vinaigrette, it is usually difficult to pair these with wine.  However, if natural citrus juices such as lemon, or somewhat sweeter vinegars such as balsamic are used, a wine with high acidity can work out quite nicely.  In a case such as this, consider choosing a wine such as Sauvignon Blanc, Pinot Grigio, or even Pinot Noir.</p><p>Continue below...</p>" + wrapNavInDiv(showBackLink() + " | " + showNextLink()),  
"<b>5. Read and experiment!</b><p>The tips outlined here have been proven to work quite nicely for wine drinkers all over the world.  But, as with most anything else, choosing a wine all comes down to personal preference.  Do some research: take a look at the many wine books that are currently out to get a feel for what else is out there.  If you're taking a vacation to wine-producing areas in France, Italy, Australia, or California for instance, try touring the vineyards there to see how the wines are produced.  Check your local newspaper for upcoming wine tasting events and festivals;  you'd be surprised at the kinds of hidden treasures that are out there that no one talks about!</p><p>Continue below...</p>" + wrapNavInDiv(showBackLink() + " | " + showNextLink()),  
"<b>6. Take note of your findings</b><p>As always, take note of the wines you've tried.  Write down how they tasted initially (the <i>attack</i>), how they tasted in mid-palette, and how they <i>finished</i>.   What foods or flavours can you compare them to?  What did you have with the wine?  By writing this down, you can save yourself some grief the next time you visit the local wine shop.  As an added bonus, you can impress your friends by recommending terrific wines that they may not have ever heard of!</p><p>Cheers!</p>" + wrapNavInDiv(showBackLink())
]

  document.getElementById("tab1Table").innerHTML = guideNotes[winePairingPageNo]; 
}

function showBackLink() { return "<a href='#' id='pagination' onClick='guidePageBack();'>< Previous</a>"; }
function showNextLink() { return "<a href='#' id='pagination' onClick='guidePageForward();'>Next ></a>"; }
function wrapNavInDiv(linkNavs) { return "<div align='center'>" + linkNavs + "</div>" }

function guidePageForward() {
  winePairingPageNo++;
  document.getElementById("tab1Table").innerHTML = guideNotes[winePairingPageNo];
}

function guidePageBack() {
  winePairingPageNo = winePairingPageNo - 1;
  document.getElementById("tab1Table").innerHTML = guideNotes[winePairingPageNo];
}


// ----- TAB TWO -----

function wSel(choice) {
  document.getElementById("q").value = "which foods with " + choice;
  document.getElementById("tab2Table").innerHTML = ""; 

  if (choice != "") {
    document.getElementById("tab2Table").innerHTML = "<h3>Matching foods</h3>Foods which pair well with " + choice + ": <br/>" + findFood(choice);
  } else {
   document.getElementById("tab2Table").innerHTML = "";
  }
}

function findFood(wine) {
  var foods;
  var foodsHtml;

  switch(wine) {
    case "barbera":  foods = [ "Pizza", "Game meats", "Wild boar", "Reggiano cheese" ];  break;
    case "cabernet franc":  foods = [ "Steak", "Ham", "Pork", "Veal", "Pizza" ];  break;
    case "cabernet sauvignon":  foods = [ "Steak", "Beef dishes", "Lamb chops", "Strong cheeses", "Chocolate" ];  break;
    case "carmenere":  foods = [ "Chicken", "Veal", "Pork", "Vegetable paté" ];  break;
    case "dessert (red)":  foods = [ "Spicy curry dishes", "Dark chocolate", "Desserts less sweet than the wine itself" ];  break;
    case "gamay":  foods = [ "Turkey", "Poultry dishes" ];  break;
    case "generic red blend":  foods = [ "Pasta (tomato sauce)", "Pizza", "Steak" ];  break;
    case "grenache":  foods = [ "BBQ pork", "BBQ chicken wings", "Cheddar cheese (mild)", "Vegetable quiche", "Ham", "Pork chops" ];  break;
    case "malbec":  foods = [ "Beef dishes", "Steak" ];  break;
    case "merlot":  foods = [ "Steak", "Beef dishes", "Lamb dishes", "Peking duck" ];  break;
    case "nebbiolo":  foods = [ "Strong, flavourful meat dishes", "Spicy Italian meats", "Strong cheeses" ];  break;
    case "pinot noir":  foods = [ "Lamb", "Peking duck", "Veal", "Turkey" ];  break;
    case "pinotage":  foods = [ "Beef carpaccio", "Pasta with vegetables", "Quiche" ];  break;
    case "port":  foods = [ "Stilton cheese" ];  break;
    case "sangiovese":  foods = [ "Pizza", "Pasta (tomato sauce)" ];  break;
    case "shiraz/syrah":  foods = [ "Peppercorn steak", "Spicy red meats", "Grilled pork", "Chorizo", "Chili", "Paiella", "Chocolate" ];  break;
    case "tempranillo":  foods = [ "Tapas", "Lamb", "Chicken dishes", "Chorizo" ];  break;
    case "zinfandel":  foods = [ "Pasta with tomato-based sauce", "Beef dishes", "Lamb dishes", "Chocolate" ];  break;
    case "chardonnay":  foods = [ "Pasta (cream sauce)", "Chicken/poultry dishes", "Seafood", "Veal", "Turkey", "Ham" ];  break;
    case "chenin blanc":  foods = [ "Sweet and sour pork or chicken", "Sushi", "Braised chicken", "Oriental dishes" ];  break;
    case "dessert (white)":  foods = [ "Stilton cheese", "Roquefort cheese", "Desserts less sweet than the wine itself" ];  break;
    case "generic white blend":  foods = [ "Chicken", " Pasta (cream sauce)" ];  break;
    case "gewurztraminer":  foods = [ "Indian food", "Spicy curry dishes", "Chinese/Szechuen dishes", "Pasta with seafood", "Smoked salmon" ];  break;
    case "icewine":  foods = [ "Fruit sorbet", "Tortes" ];  break;
    case "muscat":  foods = [ "Indian dishes", "Mexican dishes" ];  break;
    case "pinot blanc":  foods = [ "Spicy foods", "White meats", "Chicken" ];  break;
    case "pinot grigio":  foods = [ "Ham", "Soup (chicken or veal stock based)", "Sweet and sour pork or chicken" ];  break;
    case "pinot noir":  foods = [ "Chicken", "Veal", "Turkey", "Beef dishes", "Lamb", "Pork" ];  break;
    case "riesling":  foods = [ "Spicy foods", "Cantonese food", "Pasta with seafood", "Sweet and sour pork or chicken", "Tandoori chicken" ];  break;
    case "sauvignon blanc":  foods = [ "Pasta (cream sauce)", "Poultry dishes", "Risotto", "Mushrooms", "Camembert cheese" ];  break;
    case "semillon":  foods = [ "Grilled fish", "Pasta (cream sauce)", "Patés" ];  break;
    case "viognier":  foods = [ "Fruit salad" ];  break;
  }

  foodsHtml = "<ul>";

  for (var i=0;i<foods.length;i++) {
    foodsHtml = foodsHtml + "<li>" + foods[i] + "</li>";
  }
  foodsHtml = foodsHtml + "</ul>";
  return foodsHtml;
}


// ----- TAB THREE -----

function fSel(choice) {
  document.getElementById("q").value = "which wines with " + choice;
  document.getElementById("tab3Table").innerHTML = "";

  if (choice != "") {
    document.getElementById("tab3Table").innerHTML = "<h3>Matching wines</h3>Wines which pair well with " + choice + ": <br/>" + findWine(choice);
  } else {
   document.getElementById("tab3Table").innerHTML = "";
  }
}

function findWine(food) {
  var wines;
  var winesHtml;

  switch(food) {
    case "beef dishes":  wines = [ "Shiraz/Syrah", "Cabernet Sauvignon", "Merlot" ];  break;
    case "chicken":  wines = [ "Pinot Grigio", "Riesling", "Chardonnay", "Merlot" ];  break;    
    case "cheese (goat)":  wines = [ "Pinotage", "Merlot", "Shiraz", "Cabernet Sauvignon" ];  break;
    case "cheese (mild)":  wines = [ "Sauvignon Blanc", "Cabernet Sauvignon", "Merlot", "Pinotage", "Riesling" ];  break;
    case "cheese (strong)":  wines = [ "Barbera", "Cabernet Sauvignon", "Shiraz", "Chenin Blanc", "Pinotage", "Sauvignon Blanc" ];  break;
    case "chocolate":  wines = [ "Cabernet Sauvignon", "Shiraz/Syrah", "Zinfandel" ];  break;
    case "curry dishes":  wines = [ "Chenin Blanc", "Sauvignon Blanc", "Pinotage Rosé", "Gewurztraminer" ];  break;
    case "spicy chorizo":  wines = [ "Shiraz/Syrah" ];  break;
    case "fruit salad":  wines = [ "Viognier" ];  break;
    case "ham":  wines = [ "Shiraz/Syrah", "Merlot", "Pinotage", "Malbec", "Pinotage Rosé", "Chardonnay", "Pinot Grigio" ];  break;
    case "hamburger":  wines = [ "Cabernet Sauvignon", "Pinotage", "Shiraz", "Malbec" ];  break;
    case "indian food":  wines = [ "Merlot", "Riesling", "Gewurztraminer" ];  break;
    case "lamb":  wines = [ "Pinot Noir", "Cabernet Sauvignon" ];  break;
    case "mushrooms":  wines = [ "Sauvignon Blanc" ];  break;
    case "nuts":  wines = [ "Chardonnay", "Sauvignon Blanc" ];  break;
    case "pasta salad":  wines = [ "Sauvignon Blanc", "Chenin Blanc" ];  break;
    case "pork":  wines = [ "Cabernet Franc", "Chardonnay", "Chianti", "Merlot" ];  break;
    case "risotto":  wines = [ "Sauvignon Blanc" ];  break;
    case "crab":  wines = [ "Sauvignon Blanc", "Chardonnay", "Pinot Grigio" ];  break;
    case "lobster":  wines = [ "Chardonnay", "Pinot Grigio", "Sauvignon Blanc" ];  break;
    case "mussels":  wines = [ "Chenin Blanc", "Pinot Grigio", "Sauvignon Blanc" ];  break;
    case "pizza":  wines = [ "Sangiovese", "Merlot", "Pinotage Rosé", "Pinotage", "Chenin Blanc", "Malbec", "Barbera" ];  break;
    case "spicy chinese":  wines = [ "Savignon Blanc", "Pino Grigio", "Merlot", "Pinotage Rosé" ];  break;
    case "spicy mexican":  wines = [ "Chenin Blanc", "Shiraz/Syrah", "Malbec" ];  break;
    case "steak":  wines = [ "Shiraz/Syrah", "Cabernet Sauvignon", "Merlot", "Malbec", "Pinotage" ];  break;
    case "sushi":  wines = [ "Chenin Blanc", "Pinot Grigio", "Sauvignon Blanc" ];  break;
    case "shrimp":  wines = [ "Chenin Blanc", "Chardonnay", "Pinot Grigio", "Sauvignon Blanc" ];  break;
    case "thai food":  wines = [ "Chenin Blanc", "Pinotage", "Sauvignon Blanc", "Pinot Grigio" ];  break;
    case "tuna":  wines = [ "Sauvignon Blanc", "Pinotage", "Merlot", "Shiraz", "Chardonnay" ];  break;
    case "turkey":  wines = [ "Merlot", "Chardonnay", "Pinotage Rosé" ];  break;
    case "veal":  wines = [ "Carmenere", "Chardonnay", "Merlot" ];  break;
    case "vegetable paté":  wines = [ "Carmenere" ];  break;
  }

  winesHtml = "<ul>";

  for (var i=0;i<wines.length;i++) {
    winesHtml = winesHtml + "<li>" + wines[i] + "</li>";
  }
  winesHtml = winesHtml + "</ul>";
  return winesHtml;
}