/*dishes requests*/
isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;

var current_scroll = 0;

var basket_current_scrollto = 0;
var assortment_current_scrollto = 0;

$(document).ready(function(){
	$().ajaxStart(showProgressBar);
	$().ajaxStop(hideProgressBar);
	$().ajaxError(hideProgressBar);

	$(window).scroll(updateScrollingDivPositions);
	updateScrollingDivPositions();
	
	showBasket();
});


function updateScrollingDivPositions(){	
	if(isIE6){
		updateBarPosition();
	}	
	
	//baseheight (header+menu+mainL01
	var base = $("#header").height() + $("#menu").height() + $(".mainL01").height();
	
	var scroll_top = $(window).scrollTop();
	
	var scrolling_up = false;
	if(current_scroll > scroll_top){
		scrolling_up = true;	
	}
	current_scroll = scroll_top;
	
	updateBasketPosition(base, scroll_top, scrolling_up);
	updateCategoriesPosition(base, scroll_top, scrolling_up);
	//
	refreshDOM();
	//
}

//set basket within view
function updateBasketPosition(base, scroll_top, scrolling_up){
	var window_height = $(window).height();
	var basket_height = $(".mainL09").height();
		
	var scroll_to = calculateScrollTo(base, scroll_top, scrolling_up, window_height, basket_height);
	var do_scroll = doScroll(base, scroll_top, scrolling_up, window_height, basket_height, basket_current_scrollto);
	
	if(scroll_to > 0){
		if(do_scroll){
			$(".mainL09").css("margin-top",scroll_to);
			basket_current_scrollto = scroll_to;
		}
	}else{
		$(".mainL09").css("margin-top",0);
		basket_current_scrollto = 0;
	}	
}

//set categories within view
function updateCategoriesPosition(base, scroll_top, scrolling_up){
	var window_height = $(window).height();
	var assortment_height = $(".mainL09").height();
		
	var scroll_to = calculateScrollTo(base, scroll_top, scrolling_up, window_height, assortment_height);
	var do_scroll = doScroll(base, scroll_top, scrolling_up, window_height, assortment_height, assortment_current_scrollto);
	
	if(scroll_to > 0){
		if(do_scroll){
			$(".category_selector").css("margin-top",scroll_to);
			assortment_current_scrollto = scroll_to;
		}
	}else{
		$(".category_selector").css("margin-top",0);
		assortment_current_scrollto = 0;
	}	
}

function doScroll(base, scroll_top, scrolling_up, window_height, item_height, current_scrollto){
	if(item_height > window_height){
		var difference = item_height - window_height;
		
		if(!scrolling_up){
			if((scroll_top + window_height) <= (base + current_scrollto + item_height)){	
				return false;
			}
		}else{
			if((scroll_top + window_height) >= (base + current_scrollto + item_height - difference)){
				return false;	
			}
		}
	}
	return true;
}

function calculateScrollTo(base, scroll_top, scrolling_up, window_height, item_height){
	//
	var scroll_to = scroll_top-base-10;
	var difference = item_height - window_height;
	
	if(item_height > window_height){
		if(!scrolling_up){
			scroll_to -= difference;	
		}
	}

	if((scroll_to+item_height) > $(".mainL07").height()){
		scroll_to = $(".mainL07").height() - item_height;
	}	
	return scroll_to;
}

//ie6 progressbar position
function updateBarPosition(){
	$("#ajax_progressbar").css("top",$(window).scrollTop() + 10);
}

function showDishWindow(dish_id){ 
	 $("#add_dish_window div").load("../Controller/AjaxController.php?class=ShowDishHandler&method=showDishWindow&dish_id="+dish_id, "", refreshDOM);
}
function hideDishWindow(){
	 $("#add_dish_window div").load("../Controller/AjaxController.php?class=ShowDishHandler&method=hideDishWindow", "", refreshDOM); 
	 $('.mainL09 select').show();
}
function addToBasket(dish_id){
	var params="";
	var param;
	for(i=0; i<document.form.elements.length; i++)
	{
		if(document.form[i].type=='select-one' || document.form[i].type=='checkbox'){
			name=document.form[i].name;
			value=document.form[i].value;
			//checkbox
			if(document.form[i].type=='checkbox' && !document.form[i].checked){
				value="";	
			}
			param=name+"="+value;
			params+="&"+param;
		}
	}
	var url="../Controller/AjaxController.php?class=BasketHandler&method=addToBasket&dish_id="+dish_id+params;
	$("#basket div").load(url,"",refreshDOM);
	$("#add_dish_window div").text("");	
}
function showBasket(){
	$("#basket div").load("../Controller/AjaxController.php?class=BasketHandler&method=showBasket", "", refreshDOM);
}
function updateTel(){
	var tel;
	var telfld=document.getElementById('tel');
	if(telfld){
		tel=telfld.value;
	}
	dest="../Controller/AjaxController.php?class=BasketHandler&method=formatTel&tel="+tel;
	$.ajax({
 		url: dest,
		cache: false,
  		success: function(result){
   		telfld.value=result;
		
		dest="../Controller/AjaxController.php?class=BasketHandler&method=updateTel&tel="+result;
		$("#basket div").load(dest, "", refreshDOM);
  	}
	});
//	$("#tel input").load(, "", refreshDOM);
}
function deleteDish(dish_id)
{
	$("#basket div").load("../Controller/AjaxController.php?class=BasketHandler&method=deleteDish&dish_id="+dish_id, "",refreshDOM);
}
function savePromo(promo_id,rest_id)
{
	var dish_id;
	var dish=document.getElementById(promo_id);
	if(dish){
		dish_id=dish.value;
	}
	$("#dummy div").load("../Controller/AjaxController.php?class=BasketHandler&method=savePromo&promo_id="+promo_id+"&rest_id="+rest_id+"&dish_id="+dish_id, "");
}
function clearBasket(rest_id)
{
	$("#basket div").load("../Controller/AjaxController.php?class=BasketHandler&method=clearBasket&rest_id="+rest_id, "",refreshDOM);	
}

function refreshDOM(){
	$("#site").hide();
	$("#site").show();
}

function showProgressBar(){
	$("#ajax_progressbar").show();
}

function hideProgressBar(){
	$("#ajax_progressbar").hide();
	updateScrollingDivPositions();
}

