var MenuCal = new WheelItems('AreaMenus');
var TotalPrice = 0;
var PriceDCRatio = 0;

// 메뉴계산기 초기화
function initFoodMenuCal() {
	if(!MENU_CAL_VIEW || MENU_CAL_VIEW != '1') return;
	result = JSON.decode(dynamic.loadText('../common/MenuCalJS.aspx?act=MenuList'));
	// if(result.Msg != 'OK' || result.Count < 1) return; // 계산할 메뉴가 없을경우 리턴
	visibleMenuArea(true);
	PriceDCRatio = Number(result.PriceDCRatio);
	changeDCBtn(PriceDCRatio);
	if (result.Count > 0){
		for(i=0;i<result.Count;i++) {
			row = result.MenuList[i];
			_addFoodMenuCal(row.food_cd, row.food_name, row.price, true);
		}
	} else {
		showPrice();
	}
}

// 메뉴 추가/제거
function addFoodMenuCal(el, food_cd, price_gb) {
	if(typeof price_gb == 'undefined') price_gb = 1;
	result = JSON.decode(dynamic.loadText('../common/MenuCalJS.aspx?act=Add&food_cd=' + food_cd + '&price_gb=' + price_gb));
	if(result.Msg != 'OK') return;
	if(!isVisibleMenuArea()) visibleMenuArea(true);
	_addFoodMenuCal(result.food_cd, result.food_name, result.price, false);
}
function _addFoodMenuCal(food_cd,food_name,price,not_dynamic) {
	var html =	'<table width="84" border="0" cellspacing="0" cellpadding="0"><tr>' +
				'<td class="TD_SUB_01" style="line-height:120%;">' + food_name + '</td>' +
				'<td width="2"></td>' +
				'<td width="10" valign="top"><a href="javascript:removeFoodMenuCal(\'' + food_cd + '\');"><img src="../../image/bt_adder_x.gif" align="absmiddle" border="0"></a></td>' +
				'<td width="2"></td>' +
				'</tr></table>' +
				'<div><img src="../../image/adder_img_040.gif" align="absmiddle" border="0"></div>';
	if(MenuCal.addItem(food_cd,html,not_dynamic)) {
		displayMenusNone(false);
		TotalPrice += Number(price);
		showPrice();
	}
}
function removeFoodMenuCal(food_cd) {
	result = JSON.decode(dynamic.loadText('../common/MenuCalJS.aspx?act=Remove&food_cd=' + food_cd));
	if(result.Msg != 'OK') return;
	if(MenuCal.removeItem(food_cd)) {
		TotalPrice -= Number(result.price);
		showPrice();
	}
	if(MenuCal.items.length == 0) {
		//visibleMenuArea(false);	// 계산할 메뉴가 없을경우 계산기 숨기기
		displayMenusNone(true);
		changeDCRatio(0);
	}
}

// 가격표시
function showPrice() {
	dc_price = Math.floor(!PriceDCRatio ? TotalPrice : TotalPrice -  (TotalPrice * (PriceDCRatio / 100)));
	dc_price = Math.max(dc_price, 0);
	document.getElementById('text_TotalPriceDC').value = formatPrice(dc_price);
}
function formatPrice(price) {
	var str = String(price).replace(/,/gi,'');
	var regx = new RegExp(/(-?\d+)(\d{3})/); 
	var bExists = str.indexOf(".",0); 
	var strArr = str.split('.'); 
	while (regx.test(strArr[0])) {
		strArr[0] = strArr[0].replace(regx,"$1,$2");
	}
	var ret = '';
	if (bExists > -1) 
		ret = strArr[0] + '.' + strArr[1]; 
	else 
		ret = strArr[0];
	return ret;
}

// 할인율 변경
function changeDCRatio(ratio) {
	result = JSON.decode(dynamic.loadText('../common/MenuCalJS.aspx?act=ChangeDC&PriceDCRatio=' + ratio));
	if(result.Msg == 'OK') {
		PriceDCRatio = result.PriceDCRatio;
		changeDCBtn(PriceDCRatio);
		showPrice();
	}
}
function changeDCBtn(ratio) {
	if(ratio != 10) document.getElementById('PriceDCRatio_10').src = '../../image/bt_adder_10.gif';
	if(ratio != 20) document.getElementById('PriceDCRatio_20').src = '../../image/bt_adder_20.gif';
	if(ratio != 30) document.getElementById('PriceDCRatio_30').src = '../../image/bt_adder_30.gif';
	if(ratio == 10 || ratio == 20 || ratio == 30) document.getElementById('PriceDCRatio_' + ratio).src = '../../image/bt_adder_' + ratio + '_over.gif';
}


// 메뉴계산기 보이기/숨기기
function visibleMenuArea(visible) {
	document.getElementById('AreaMenuCal').style.display = visible ? 'block' : 'none';
}
function isVisibleMenuArea() {
	return (document.getElementById('AreaMenuCal').style.display == 'block' ? true : false);
}


// 계산할 메뉴가 없을때 메시지 보이기
function displayMenusNone(flag) {
	if(typeof document.getElementById('AreaMenusNone') == 'undefined') return;
	document.getElementById('AreaMenusNone').style.display = flag ? 'block' : 'none';
}
