// JavaScript Document

$(function() {
	// Bludgeon IE into behaving...
	if ( $.browser.msie ) {
		document.createElement("section");
		document.createElement("header");
		document.createElement("article");
		document.createElement("nav");
		document.createElement("aside");
	}
});

$(document).ready(function(){ // Things to do when the page has loaded

  $(".selectedDrinkIngredsList").click(function(event) { // selected cocktail pages
	$(this).children("span").toggle(); // toggle glossary visibility
	x = $(".pubcrawlButton").position(); // work out the height of the content
	contentHeight = Math.floor(x.top)+240;// ...
	getScreenData(); // Reset the scrollbar
	event.preventDefault(); // stop page refresh
  });
  
  $(".ingredientLabel").click(function(event) { // ingredient page
	$(this).next(".glossary").toggle(); // toggle glossary visibility
	x = $(".pubcrawlButton").position(); // work out the height of the content
	contentHeight = Math.floor(x.top)+240; // ...
	getScreenData(); // Reset the scrollbar
	event.preventDefault(); // stop page refresh
  });

  $("#clear").click(function(event) { // untick all checkboxes
	$('input:checkbox').each(function() {
	  this.checked=false;
	});
	isChecked = 1; // prevent users leaving the page without saving
	event.preventDefault(); // stop page refresh
	return false;
  });
  
  $("#undo,#save").click(function(event) { // undo button on ingredients page
	isChecked = 0; //allow users to leave page without warnings
  });
  
	if(window.location.pathname == "http://baboonsaloon.com/ingredients.php"){ // Ensure users don't browse away from the ingredients page without saving changes
		var isChecked = 0;
		window.onbeforeunload = function(){
			if (isChecked != 0) { 
				return 'You haven\'t saved your changes so you\'ll lose them if you leave this page. Press Cancel to stay on this page';
			}
		}
	}
	
	$("#ingredientsButtons input").click(function(event) { // save button on ingredients page
		if ($('input[type="checkbox"]:checked').val() == undefined) { // If they try saving without ticking any ingredients
			if(confirm("You haven't gibbon us anything to work with!\n\nAs you haven't ticked any ingredients, we can't give you\na customised list of all the cocktails you can make.\n\nIf you don't want a customised list, click on the All Cocktails\nlink on the left to view our full list of drinks.\n\nIf you want, click on OK to save the blank ingredient list.")){
				return true;
			} else {
				return false;
			}
		}
/*		gotIngs = "";
		$('input[type="checkbox"]:checked').each(function() {
			gotIngs += $(this).val(); 
		})
		$.cookie('baboonsaloon', 'gotIngs', { expires: 3650 }); // write cookie
		alert($.cookie('the_cookie'));
*/	});
	
/*function saveError() {
	newx = 0
	for (i = 0; i < document.drinks.elements.length; i++) {
		if (document.drinks.elements[i].checked == true) {newx++}
	}
	if (newx < 1) {
		sure = confirm("You haven't gibbon us anything to work with!\n\nAs you haven't ticked any ingredients, we can't give you\na customised list of all the cocktails you can make.\n\nIf you don't want a customised list, click on the All Cocktails\nlink on the left to view our full list of drinks.\n\nIf you want, click on OK to save the blank ingredient list.")
		if (sure == true) {
			return true
			}
		else {
			return false
		}
	}
}
*/
});

function getdrink() {
	if (randomdrink.length > 0) {
		var randomnumber = Math.floor(Math.random() * randomdrink.length)
		window.location.href = 'drinkDetail.php?id='+randomdrink[randomnumber];
	}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized - does anyone still use it?
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
