/*
**     JavaScript Source Code
**     Created by Kalin Ganev
**     <KalinGanev [AT] Gmail (DOT) com>
**     Date Created:  2008-06-15
**     Last Modified: 2011-12-30
*/





var  gstr_navigatorAppVersionLower = navigator.appVersion.toLowerCase();
var  gbool_isBrowserIe5 = -1 != gstr_navigatorAppVersionLower.indexOf('msie 5');
var  gbool_isBrowserIe6 = -1 != gstr_navigatorAppVersionLower.indexOf('msie 6');
var  gbool_isBrowserChrome = -1 != gstr_navigatorAppVersionLower.indexOf('chrome');
var  gbool_isBrowserSafari = -1 != gstr_navigatorAppVersionLower.indexOf('safari');




function /*void*/ toggleProductTypeOptions (int_idProductType) {
	if (document.getElementById && (obj_layerProductTypeOptions = document.getElementById('ProductTypeOptions' + int_idProductType))
			&& (obj_layerProductType = document.getElementById('ProductType' + int_idProductType))) {
		if ('block' == obj_layerProductTypeOptions.style.display) {
			obj_layerProductType       .style.display = 'block';
			obj_layerProductTypeOptions.style.display = 'none';
		} else {
			obj_layerProductType       .style.display = 'none';
			obj_layerProductTypeOptions.style.display = 'block';
		} // else
		positionPanelOrderAmount();
	} // if
} // toggleProductTypeOptions() function


function /*float*/ calculatePricePrefinal (int_idProductType) {
	var  obj_form = document.getElementById('FormMenu_producttype_id' + int_idProductType);
	var  fl_pricePrefinal = 0;
	var  int_quantity = window.parseInt(document.getElementById('quantity_producttype_id' + int_idProductType).value);

	// Looping through all form fields:
	for (i=0; i<obj_form.length; i++) {
		if (0 == obj_form[i].id.indexOf('productitem_id') && obj_form[i].checked) {
			// OK: Found a checked "productitem" radio button within current form.
			fl_pricePrefinal += window.parseFloat(document.getElementById('price_productitem_id' + obj_form[i].value).value) * int_quantity;
		} else {
			if (0 == obj_form[i].id.indexOf('mealaddition_productitem_id') && obj_form[i].checked) {
				// OK: Found a checked "meal addition productitem" checkbox within current form.
				fl_pricePrefinal += int_quantity * window.parseFloat(
							document.getElementById('price_mealaddition_productitem_id' + obj_form[i].value + '_pt_id' + int_idProductType).value);
			}
		}
	} // for - looping through all form fields

	return fl_pricePrefinal;
} // calculatePricePrefinal() function


function /*void*/ printPricePrefinal (int_idProductType) {
	if (obj_spanPricePrefinal = document.getElementById('price_prefinal_producttype_id' + int_idProductType)) {
		obj_spanPricePrefinal.innerHTML = getPrecision2FloatSi(calculatePricePrefinal(int_idProductType));
	}
}




function /*void*/ setVisibilityImgLoading (int_idProductType, bool_isVisible) {
	if (obj_img = document.getElementById('ImgLoading' + int_idProductType)) {
		// OK: Current "loading" image exists.
		obj_img.style.visibility = bool_isVisible ? 'visible' : 'hidden';
	}
}


function /*void*/ handleResponseAddProductsToCart () {
	if (4 == gobj_httpRequest.readyState) {
		try {
			window.eval(gobj_httpRequest.responseText);
		} catch (e) {
			// A server error arose.
			if (DEBUG) {
				$.msgbox('An error arose while contacting server!' + '<br/>Debug Information:<br/><br/>'
						+ e.message + '<br/><br/>' + gobj_httpRequest.responseText, {type: 'error'});
			}
		}
	}
}


function /*void*/ addProductsToCart (int_idProductType) {
	// Showing "loading" icon:
	window.setVisibilityImgLoading(int_idProductType, true);

	// Getting positive quantities for all product items associated with current product type:
	var  obj_form = document.getElementById('FormMenu_producttype_id' + int_idProductType);
	var  str_parametersProductItems = '';

	// Looping through all form fields:
	for (i=0; i<obj_form.length; i++) {
		if (0 == obj_form[i].id.indexOf('productitem_id') && obj_form[i].checked) {
			// OK: Found a checked "productitem" radio button within current form.
			str_parametersProductItems += '' == str_parametersProductItems ? '?' : '&';
			str_parametersProductItems += 'productitem_id=' + obj_form[i].value
					+ '&quantity=' + document.getElementById('quantity_producttype_id' + int_idProductType).value;
		} else {
			if (0 == obj_form[i].id.indexOf('mealaddition_productitem_id') && obj_form[i].checked) {
				// OK: Found a checked "meal addition productitem" checkbox within current form.
				str_parametersProductItems += '' == str_parametersProductItems ? '?' : '&';
				str_parametersProductItems += 'mealaddition_productitem_ids[]=' + obj_form[i].value;
			}
		}
	} // for - looping through all form fields

	if ('' == str_parametersProductItems) {
		// Error.
		$.msgbox('System error!', {type: 'error'}, function(buttonPressed) {
			// Hiding "loading" icon:
			window.setVisibilityImgLoading(int_idProductType, false);
		});
	} else {
		// OK: Some product item quantities are selected.
		// Resetting form:
		obj_form.reset();

		// Resetting prefinal price caption:
		printPricePrefinal(int_idProductType);

		// Adding language parameter to get parameters:
		str_parametersProductItems += LANG_PAR_EFFECTIVE_AMP_SIMPLE;

		// Sending a request to server using AJAX technology:
		gobj_httpRequest.open('get', BASE + 'ajax/cart-add/' + str_parametersProductItems, true);
		gobj_httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		gobj_httpRequest.onreadystatechange = handleResponseAddProductsToCart;
		gobj_httpRequest.send(null);
	}
} // addProductsToCart() function


/** Called upon Ajax response after adding product(s) to shopping cart. */
function /*void*/ updatePanelOrderPreview (str_htmlContent) {
	document.getElementById('OrderContainer').innerHTML = str_htmlContent;
}


/** Called upon Ajax response after adding product(s) to shopping cart. */
function /*void*/ updatePanelOrderAmount (str_htmlContent) {
	document.getElementById('OrderAmount').innerHTML = str_htmlContent;
	positionPanelOrderAmount();
}




var  gobj_imageAdded;
if (document.images) {
	gobj_imageAdded = new Image();
	gobj_imageAdded.src = BASE + 'images/added.png';
}


function /*void*/ highlightProductTypeAdded (int_idProductType) {
	if (document.getElementById) {
		// Hiding "loading" icon:
		window.setVisibilityImgLoading(int_idProductType, false);
		if (gobj_imageAdded) {
			document.getElementById('ImgAdded' + int_idProductType).src = gobj_imageAdded.src;
		}
		document.getElementById('ProductType' + int_idProductType).className = 'ProductType ProductTypeAdded';
	}
}




function /*int*/ getBodyScrollLeft () {
	var  int_pos = 0;

	if (window.pageXOffset) {
		int_pos = window.pageXOffset;
	} else {
		if (document.documentElement && document.documentElement.scrollLeft) {
			int_pos = document.documentElement.scrollLeft;
		} else {
			if (document.body) {
				int_pos = document.body.scrollLeft;
			}
		}
	}
	return int_pos;
}


function getDocHeight () {
	var D = document;
	return Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}


function /*void*/ positionPanelOrderAmount () {
	if ((obj_panelOrderAmount = document.getElementById('OrderAmount')) && '' != obj_panelOrderAmount.innerHTML) {
		// Order Amount panel (OA) has some content.

		// Setting and calculating dimensions of some document elements:
		var  int_heightDocument = getDocHeight();
		var  MENU_PADDING_TOP = 59;
		var  MENU_HEIGHT_MIN = 580;
		var  int_heightMenu = Math.max(MENU_HEIGHT_MIN, document.getElementById('Menu').offsetHeight) - MENU_PADDING_TOP;
		var  FOOTER_MARGIN_TOP = 20;
		var  int_heightFooter = FOOTER_MARGIN_TOP
				+ document.getElementById('Footer').offsetHeight + document.getElementById('bottom_wrapper').offsetHeight;
		var  int_heightHeader = int_heightDocument - int_heightMenu - int_heightFooter;
		var  int_heightOa = obj_panelOrderAmount.offsetHeight;

		var  CONTENT_WIDTH = 992;
		var  OA_MARGIN_LEFT = window.opera || gbool_isBrowserChrome || gbool_isBrowserSafari ? 592 : 593;

		// Calculating position of OA where it should be according to dimensions of document elements:
		var  int_posOaXRel = Math.floor((Math.max(CONTENT_WIDTH, document.body.clientWidth) - CONTENT_WIDTH) / 2) + OA_MARGIN_LEFT;
		var  int_posOaYRel = Math.floor((getScreenHeight() - int_heightOa) / 2);
		var  int_posOaXAbs = int_posOaXRel - getBodyScrollLeft();
		var  int_posOaYAbs = int_posOaYRel + getBodyScrollTop();


		if (int_posOaYAbs < int_heightHeader || gbool_isBrowserIe5 || gbool_isBrowserIe6) {
			// Page is scrolled too up (where header is) or browser doesn't support 'fixed' position.
			obj_panelOrderAmount.style.position = 'absolute';
			obj_panelOrderAmount.style.left     = int_posOaXRel + 'px';
			obj_panelOrderAmount.style.top      = int_heightHeader + 'px';
		} else {
			if (int_posOaYAbs + int_heightOa > int_heightDocument - int_heightFooter) {
				// Page is scrolled too down (where footer is).
				obj_panelOrderAmount.style.position = 'absolute';
				obj_panelOrderAmount.style.left     = int_posOaXRel + 'px';
				obj_panelOrderAmount.style.top      = (int_heightDocument - int_heightFooter - int_heightOa) + 'px';
			} else {
				// Page is scrolled to the middle (where Menu is).
				obj_panelOrderAmount.style.position = 'fixed';
				obj_panelOrderAmount.style.left     = int_posOaXAbs + 'px';
				obj_panelOrderAmount.style.top      = int_posOaYRel + 'px';
			}
		}

		obj_panelOrderAmount.style.visibility = 'visible';
	}
} // positionPanelOrderAmount() function


window.onresize = positionPanelOrderAmount;
window.onscroll = positionPanelOrderAmount;
window.onload   = positionPanelOrderAmount;




function /*void*/ collapseDropContentProductCategories () {
	var  obj_dropcontent;
	if (document.getElementById && (obj_dropcontent = document.getElementById('LiProductCategories'))) {
		obj_dropcontent.className = '';
	}
}


function /*void*/ toggleDropContentProductCategories () {
	var  obj_dropcontent;
	gbool_isClickedDropContentProductCategories = true;
	if (document.getElementById && (obj_dropcontent = document.getElementById('LiProductCategories'))) {
		collapseDropContentLogin();
		collapseDropContentHowToUse();
		obj_dropcontent.className = 'hover' == obj_dropcontent.className ? '' : 'hover';
	}
}


