function send_order(form) {
	var v = $("input[name='basket_person_count']").val();
	if(!v || isNaN(v)) {
		alert("Укажите количество персон");
		return false;	
	}
	return true;
}

var curChangedId = -1;
var curChangeTimeout;
var curChangeValue;
var curChangeOriginalValue;

function click_action(domInput,id,action){
   switch(action)
    {
        case 'inc' : domInput.val(parseInt(domInput.val())+1);break; 
        case 'dec': if(domInput.val()>0){domInput.val(parseInt(domInput.val())-1)};break;         
    } 
          
    bskChCount(domInput,id,action);      
}
function bskChCount(domInput, id,action,original) {
   
	if(id == curChangedId) {
		clearTimeout(curChangeTimeout);
	} else {
		curChangedId = id;
	}
	curChangeOriginalValue = original;
    if(action)
    { 
        curChangeValue=parseInt(domInput.val());  
    }else{    
        curChangeValue = domInput.value;        
    }      
	
    if(curChangeValue==0){
        bskRemove(id);
    } else 
        curChangeTimeout = setTimeout(bskChange, 1000);    
   
}

function bskChange() {
	if(curChangeOriginalValue!=curChangeValue && 
		(curChangedId=='time' || !isNaN(curChangeValue))) {
		
		$.post("/ajax/basket/add",{
			basket_action: 'count',
			item_index: curChangedId,
			item_value: curChangeValue
		}, onLoadBasket);
	}
}

function bskRemove(removeId) {
	$.post("/ajax/basket/add",{
		basket_action: 'remove',
		item_index: removeId	
	}, onLoadBasket);
	return false;    
}

function onLoadBasket(res) {
	$("#basket_cont").html(res);   
	initFlyBasked();
}

//полёты во сне и наяву
var domFly;
var flyTop = 0;

function initFlyBasked() {
	domFly = document.getElementById('basket_fly');      
	if(domFly) {      
		domFly.className = '';
		
		var o = domFly;
		flyTop = domFly.offsetTop;
		while(o = o.offsetParent) {
			flyTop += o.offsetTop;
		}
		if($.browser.msie &&navigator.appVersion.match(/MSIE\s*(\d+)/)[1] < 8) {
			flyTop -= 309;
		}			
		onScrollWindow();
	}
}

function onScrollWindow() {
	if(!domFly)
		return;
	var scrollTop = self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
	if(flyTop - scrollTop > 15) {
		if(domFly.className == 'basket-fly') {
			domFly.className = '';
			if(!$.browser.msie || navigator.appVersion.match(/MSIE\s*(\d+)/)[1] != 7)
				domFly.style.width = 'auto';
		}
	} else {
		if(domFly.className != 'basket-fly') {
			domFly.className = 'basket-fly';	
			domFly.style.width = domFly.parentNode.offsetWidth + 'px';
		}			
	}
}	

$().ready(function(){
	var defaultValue = "Как можно скорее";
	
	$("input[name='basket_time']")
	.keyup(function(){
		document.cookie = "bsk_time="+encodeURIComponent(this.value)+"; path=/";
	})
	.change(function(){
		document.cookie = "bsk_time="+encodeURIComponent(this.value)+"; path=/";
	})
	.focus(function(){
		if(this.value == defaultValue)
			this.value = '';
	})
	.blur(function(){
		if(this.value.length == 0)
			this.value = defaultValue;
	});
	
	$("input[name='basket_person_count']")
	.keyup(function(){
		document.cookie = "bsk_person="+encodeURIComponent(this.value)+"; path=/";
	})
	.change(function(){
		document.cookie = "bsk_person="+encodeURIComponent(this.value)+"; path=/";
	})
	
	$("select[name='basket_day']").change(function(){
		document.cookie = "bsk_day="+this.options[this.selectedIndex].value+"; path=/";
	});
	
	onscroll = onScrollWindow;
	initFlyBasked();
});

