//-----------------------------------------------------------------------------------------
//THE FRIDGE
//  created by Dave Coleman 10/06/2007
//	of Bottlecap Development, Inc.
//	for Fizz Corp.
//-----------------------------------------------------------------------------------------

popupOpen = false; //is the popup open?
overPopup = false; //when the mouse clicks, is it over the popup?

document.onmousedown  = mouseSelect;

//--BEGIN ADD/EDIT FORM-------------------------------------------
	var add_html = '';
	add_html = add_html+'<div id="fridgePopup_SubTitle"></div>';
	add_html = add_html+'	<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="fridge_backend.php?mode=1">';
	add_html = add_html+'		<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>">';
	add_html = add_html+'		<input type="hidden" name="drinkID" id="drinkID" value="">';
	add_html = add_html+'		Name:<br/><input type="text" size="29" name="drinkName" id="drinkName"/><br/>';
	add_html = add_html+'		Upload Image:<br/><input type="file" size="15" name="drinkImage" id="drinkImage"/><br/>';
	add_html = add_html+'			Description:<br/><textarea rows="10" cols="22" name="drinkDesc" id="drinkDesc"></textarea><br/>';
	add_html = add_html+'		<input type="submit" id="drinkSubmit" value="Create New Item" />';
	add_html = add_html+'	</form>';
	add_html = add_html+'</div>';
	
//-- BEGIN DELETE CONFIRM PAGE ----------------------------------
	var delete_html = '';
	delete_html = delete_html+'		<br /><div id="confirmMessage"></div><br />';
	delete_html = delete_html+'		<form method="POST" action="fridge_backend.php?mode=1">';
	delete_html = delete_html+'			<input type="hidden" name="deleteID" id="deleteID" value="">';
	delete_html = delete_html+'			<input type="submit" value="OK" />';
	delete_html = delete_html+'			<input type="button" value="Cancel" onClick="popup_Hide(); return false;" />';
	delete_html = delete_html+'		</form>';
	
//Info box popup------------------------------------------------------------------------
function popup_Show(id, drinkName, drinkItem) {
	//id - the database id of each drink item
	//drinkName - string of drink name
	//drinkItem - the 'this' object that the popup centers to	
	if (mode == 2) return;
	viewItem(id, drinkName);	
	move_popup(drinkItem,document.getElementById('fridgePopup'),document.getElementById('fridgePopup_Tail'),276, 0);
	if(document.getElementById('fridgePopup_Text').offsetHeight <  document.getElementById('divContainer3').offsetHeight)
		document.getElementById('fridgePopup_Arrows').style.display='none';
	else
		document.getElementById('fridgePopup_Arrows').style.display='block';
	popupOpen = true;
}
//Popup Hide ------------------------------------------------------------------------
function popup_Hide() {
	document.getElementById('fridgePopup').style.display = "none";
	popupOpen = false;
}
//Add Popup--------------------------------------------------------------------------
function addItem_Show() {
	document.getElementById('fridgePopup_Title').innerHTML = "ADD DRINK";
	document.getElementById('fridgePopup_Edit').innerHTML = "<a href='javascript:void(0)' onClick='popup_Hide(); return false;'>Cancel</a>";
	document.getElementById('fridgePopup_Arrows').style.display = "none";	//hide arrows
	document.getElementById('fridgePopup_Text').innerHTML = add_html;
	document.getElementById('fridgePopup').style.display = "block";
		//decided against this since the tail would be pointing at the add box, and the popup would be off screen
		//move_popup(document.getElementById('addButton'),document.getElementById('fridgePopup'),document.getElementById('fridgePopup_Tail'),276, 20);
	popupOpen = true;
	//instead:
	document.getElementById('fridgePopup').style.left = '200px';
	document.getElementById('fridgePopup').style.top = 120 + 'px';	
	document.getElementById('fridgePopup_Tail').style.display = "none";
	//PerformScroll(0,-200,3);	//moves the slider to the top so that none of the form is cut off
}
//View Info Popup--------------------------------------------------------------------------
function viewItem(id, drinkName) {
	document.getElementById('fridgePopup_Title').innerHTML = drinkName;
	if (mode == 1) {
		document.getElementById('fridgePopup_Edit').innerHTML = "<a href='javascript:void(0)' onClick=\"editItem("+id+",'"+drinkName+"'); return false;\">Edit</a> | <a href='javascript:void(0)' onClick=\"deleteItem("+id+",'"+drinkName+"'); return false;\">Delete</a>";
	} else {
		document.getElementById('fridgePopup_Edit').innerHTML = "";		
	}
	document.getElementById('fridgePopup_Arrows').style.display = "block";
	document.getElementById('fridgePopup_Text').innerHTML = document.getElementById('drinkDescription'+id).innerHTML;
	document.getElementById('fridgePopup_Tail').style.display = "block";
	document.getElementById('fridgePopup').style.display = "block";	
	InitialiseScrollableArea(3);	//turns on verticle scrolling for popup boxes
}
//Edit Popup--------------------------------------------------------------------------
function editItem(id, drinkName) {
	drinkDesc = document.getElementById('fridgePopup_Text').innerHTML;
	document.getElementById('fridgePopup_Title').innerHTML = "EDIT DRINK";
	document.getElementById('fridgePopup_Edit').innerHTML = 
		"<a href='javascript:void(0)' onClick=\"viewItem("+id+",'"+drinkName+"'); return false;\">Cancel</a>";
	document.getElementById('fridgePopup_Text').innerHTML = add_html		//create form content
		document.getElementById('drinkName').value = drinkName;
		document.getElementById('drinkSubmit').value = "Update";
		document.getElementById('drinkID').value = id;
		document.getElementById('drinkDesc').innerHTML = drinkDesc;
	PerformScroll(0,-200,3);	//moves the slider to the top so that none of the form is cut off
}
//Confirm Delete Popup--------------------------------------------------------------------------
function deleteItem(id, drinkName) {
	document.getElementById('fridgePopup_Title').innerHTML = "DELETE DRINK";
	document.getElementById('fridgePopup_Edit').innerHTML = 
		"<a href='javascript:void(0)' onClick=\"viewItem("+id+",'"+drinkName+"'); return false;\">Cancel</a>";
	document.getElementById('fridgePopup_Arrows').style.display = "none";	//hide arrows
	document.getElementById('fridgePopup_Text').innerHTML = delete_html;
	document.getElementById('confirmMessage').innerHTML = "Are you sure you want to delete <b>"+drinkName+"</b>?";
	document.getElementById('deleteID').value =	id;
	document.getElementById('drinkName').value = drinkName;
}
//Move box --------------------------------------------------------------------------
function move_popup(CR, box, tail, width, topCompensate) {
	//width is width of popup box
	//an aka "this" - click region where the click event happened
	//box is also the popup box
	//CR stands for Click Region
	
	var CR_left = 0;
	var ctop = 0;
	var obj = CR;
	while (obj.offsetParent) {
		CR_left += obj.offsetLeft;	//add up where click region was
		ctop += obj.offsetTop;
		obj = obj.offsetParent;
	}
	//Get top position of popup box
	var obj2 = box;
	var boxTop = 0;
	while (obj2.offsetParent) {
		boxTop += obj2.offsetTop;
		obj2 = obj2.offsetParent;
	}
	
	box.style.left = (CR_left - width + 5) + 'px';		//set the left position of box
	
	ctop += CR.offsetHeight;

	if (document.body.currentStyle && document.body.currentStyle['marginTop']) { //compensate for something?
		//alert("body margin top current style is true");
		ctop += parseInt(document.body.currentStyle['marginTop']);
	}

	tail.style.top = (ctop - boxTop - 70 + topCompensate) + 'px';
	box.style.top = 120 + 'px';
}

function mouseSelect(e)
{
//  var obj = ns6 ? e.target.parentNode : event.srcElement.parentElement;
	if (popupOpen & !overPopup) {
			document.getElementById('fridgePopup').style.display = "none" ;
			popupOpen = false;
	} else {
    	return true ;
	}
  return false;
}
function changeCursor(type) {	//changes all the drink cursors on mouse over
	for (var i = 0; i < document.getElementsByName("drinkItems").length; i++) {
		document.getElementsByName("drinkItems")[i].style.cursor = type;			
	}
}