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

mode = 0;	//0 - normal  1 - edit  2 - move
var updateWidthTimer;

//OnLoad Event ------------------------------------------------------------------
function fridgeLoad(page_mode) {
	document.getElementById('divContent0').style.display = "block";
	document.getElementById('divContent1').style.display = "block";
	document.getElementById('divContent2').style.display = "block";

	loadThreeScrollAreas();	
	changeMode(page_mode);	
}
//Change mode --------------------------------------------------------------------
function changeMode(page_mode) {
//	alert("debug"+page_mode);
	mode = page_mode;
	
	switch (mode) {
		case 0:
			try {	//if this is the front end, none of this will work
				document.getElementById('subControlBox').innerHTML = "Customer view";
				document.getElementById('selectMode_Button').innerHTML = "Normal";						
				Sortable.destroy("shelf0");	  
				Sortable.destroy("shelf1");	  
				Sortable.destroy("shelf2");			
			} catch (e) {
				// ignore it 
			}
			changeCursor("pointer");
			break;
		case 1:
			document.getElementById('subControlBox').innerHTML = "Click on existing drinks to edit or delete, using links on bottom right of popups.  Or, add new drink: <br><div id='Add' class='formButton' onClick='addItem_Show();' onMouseOver='javascript:this.style.color=\"#e55026\"' onMouseOut='javascript:this.style.color=\"#FFFFFF\"'>Add Drink</div>";
			document.getElementById('selectMode_Button').innerHTML = "Add/Edit/Delete";						
			Sortable.destroy("shelf0");	  
			Sortable.destroy("shelf1");	  
			Sortable.destroy("shelf2");	  		
			changeCursor("pointer");
			break;
		case 2:
			document.getElementById('subControlBox').innerHTML = "Drag and drop drinks between shelves, placing them either at the far left or far right side. Then click Save Changes.<br><div id='Add' class='formButton' onClick='saveLists();' onMouseOver='javascript:this.style.color=\"#e55026\"' onMouseOut='javascript:this.style.color=\"#FFFFFF\"'>Save Changes</div>";
			document.getElementById('selectMode_Button').innerHTML = "Reorder";						

			Sortable.create("shelf0",{containment:['shelf0','shelf1','shelf2'],tag:'li',overlap:'horizontal',constraint:false,dropOnEmpty:true,onUpdate:function(){waiter();}});
			Sortable.create("shelf1",{containment:['shelf0','shelf1','shelf2'],tag:'li',overlap:'horizontal',constraint:false,dropOnEmpty:true,onUpdate:function(){waiter();}});
			Sortable.create("shelf2",{containment:['shelf0','shelf1','shelf2'],tag:'li',overlap:'horizontal',constraint:false,dropOnEmpty:true,onUpdate:function(){waiter();}});

			//showULeffects(true);
			changeCursor("move");
			break;
	}
//	alert("done change mode");
}
function showULeffects(showIt) {
	showIndividualEffects(showIt, document.getElementById('divContent0'));
	showIndividualEffects(showIt, document.getElementById('shelf1'));	
	showIndividualEffects(showIt, document.getElementById('shelf2'));	
}
function showIndividualEffects(showIt, listBox) {
	if (showIt) { 	//make visible
//		listBox.style["background"] = "white";
		listBox.onClick = function() { 
			this.style["background"] = "#FFFFFF"; 
			this.style["border"] = "solid";
			this.style["border-width"] = "1px";
			};
		listBox.onDragOut = function() {
			this.style["background"] = "none"; 
			this.style["border"] = "none"; 
			};
	} else {
		listBox.onDragOver = function() {};
		listBox.onDragOut = function() {};	
	}
}

function waiter() {
	clearTimeout(updateWidthTimer);
	updateWidthTimer = setTimeout(updateSliderWidth,400);
}
function updateSliderWidth() {
	for (var i = 0; i < 3; i++) {	
		container1 = document.getElementById('shelf'+i);
		totalItems = container1.getElementsByTagName("li").length;	

		//Make sure this part matches PHP's create event "populateFridge"----
			shelfWidth = totalItems*80 + 100;
			if (shelfWidth < 600) shelfWidth = 598;
		//-----------------------------------------------------------------
		container1.parentNode.style["width"] = shelfWidth+"px";
	
		loadScrollArea(i);
	}	
}
function saveLists() {
	$address = "fridge_backend.php?mode=1&";
	$address = $address+Sortable.serialize('shelf0');
	$address = $address+"&"+Sortable.serialize('shelf1');
	$address = $address+"&"+Sortable.serialize('shelf2');
	
	window.location.href = $address;
}
function dstatus(message) {
	//document.getElementById('debugStatus').innerHTML = message;	
}
function alert_error(formMessage) {
//	alert(formMessage);	
	document.getElementById('modalPopup_Message').innerHTML = formMessage;
	Popup.showModal('modalPopup',null,null,{'screenOpacity':.6}); //'screenColor':'#99ff99',
}