$(document).ready(function() {
	$('#usernameLoading').hide();
	$('#username').blur(function(){
	  $('#usernameLoading').show();
      $.post("/enter.php", {
        username: $('#username').val(), act: 'checkname'
      }, function(response){
        $('#usernameResult').fadeOut();
        setTimeout("finishAjax('usernameResult', '"+escape(response)+"')", 400);
      });
    	return false;
	});
});

function finishAjax(id, response) {
  $('#usernameLoading').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}

function minus(id,price) { document.getElementById('kolvo' + id).value--; pereschet(id,price); }
function plus(id,price) { document.getElementById('kolvo' + id).value++; pereschet(id,price); }
function minus05(id,price) { document.getElementById('kolvo' + id).value = document.getElementById('kolvo' + id).value*1 - 0.5; pereschet05(id,price); }
function plus05(id,price) { document.getElementById('kolvo' + id).value = document.getElementById('kolvo' + id).value*1 + 0.5; pereschet05(id,price); }

function pereschet(id,price) {
	if(document.getElementById('kolvo' + id).value < 1) document.getElementById('kolvo' + id).value = 1;
	if(document.getElementById('kolvo' + id).value > 999) document.getElementById('kolvo' + id).value = 999;
	document.getElementById('kolvo' + id).value = Math.round(document.getElementById('kolvo' + id).value);
	if(document.getElementById('kolvo' + id).value == "NaN") document.getElementById('kolvo' + id).value = 1;
	document.getElementById('price' + id).innerHTML = document.getElementById('kolvo' + id).value * price;
	document.getElementById('price' + id).innerHTML += ' руб.';
	$('#v' + id).fadeIn();
	$('#summa' + id).slideDown('fast');
}

function pereschet05(id,price) {
	if(document.getElementById('kolvo' + id).value < 0.5) document.getElementById('kolvo' + id).value = 0.5;
	if(document.getElementById('kolvo' + id).value > 999) document.getElementById('kolvo' + id).value = 999;
	document.getElementById('kolvo' + id).value = Math.round(document.getElementById('kolvo' + id).value * 2) / 2;
	if(document.getElementById('kolvo' + id).value == "NaN") document.getElementById('kolvo' + id).value = 1;
	var summa = Math.round(document.getElementById('kolvo' + id).value * price / 10) * 10;
	document.getElementById('price' + id).innerHTML = summa;
	document.getElementById('price' + id).innerHTML += ' руб.';
	$('#v' + id).fadeIn();
	$('#summa' + id).slideDown('fast');
}

function oppa(id,kl,tp) {
	$('#v' + id).hide();
	$('#l' + id).fadeIn();
	$.post("/enter.php",{act:"dishadd",dish:id,kolvo:kl,type:tp},function(data){$('#l' + id).hide();$('#outp' + id).slideDown();$('#outp' + id).html(data);updkorzina();});
}

function uppa(id) {
	$('#l' + id).fadeIn();
	$.post("/enter.php",{act:"dishdel",dish:id},function(data){$('#l' + id).hide();$('#outp' + id).slideUp();$('#outp' + id).html(data);updkorzina();});
}

function dostavka(tp) {
	$.post("/enter.php",{act:"dostavka",type:tp},function(data){$('#outp').html(data);document.location.reload();});
}

function updkorzina() {
	$('#korinfo').hide();
	$('#korload').show();
	$.post("/enter.php",{act:"korzina"},function(data){$('#korload').hide();$('#korzina').html(data);});
	return true;
}

function selectpaytype(tp) {
	document.getElementById('us_paytype').value=tp;
	return true;
}

function uVal(id) {
	try {
		var relchb = document.getElementById('check'+id);
		var rel = document.getElementById('kolvo'+id);
		if (relchb.checked)	{
			rel.disabled = false;
			if (rel.value == 0) rel.value = 1;
		}
		else {
			rel.disabled = true;
			rel.value = 0;
		}
	}
	catch(e) {};
}

(function (jQuery) {
	jQuery.fn.inputLabel = function(text,opts) {
		o = jQuery.extend({ color: "#888", e:"focus", force : false, keep : true}, opts || {});
		var clearInput = function (e) {
			var target = jQuery(e.target);
			var value = jQuery.trim(target.val());
			if (e.type == e.data.obj.e && value == e.data.obj.innerText) {
				jQuery(target).css("color", "").val("");
				if (!e.data.obj.keep) {
					jQuery(target).unbind(e.data.obj.e+" blur",clearInput);
				}
			} else if (e.type == "blur" && value == "" && e.data.obj.keep) {
				jQuery(this).css("color", e.data.obj.color).val(e.data.obj.innerText);
			}
		};
		return this.each(function () {
					o.innerText = (text || false);
					if (!o.innerText) {
						var id = jQuery(this).attr("id");
						o.innerText = jQuery(this).parents("form").find("label[for=" + id + "]").hide().text();
					}
					else 
						if (typeof o.innerText != "string") {
							o.innerText = jQuery(o.innerText).text();
						}
			o.innerText = jQuery.trim(o.innerText);
			if (o.force || jQuery(this).val() == "") {
				jQuery(this).css("color", o.color).val(o.innerText);
			}
				jQuery(this).bind(o.e+" blur",{obj:o},clearInput);
			
		});
	};
})(jQuery);

function getwidth() {
	if (window.innerWidth) {
    width = window.innerWidth;
  }	else if (document.documentElement && document.documentElement.clientWidth) {
    width = document.documentElement.clientWidth;
  }	else if (document.body && document.body.clientWidth) {
    width = document.body.clientWidth;
  }	else if (screen.width) {
    width = screen.width;
  }
	return width;
}

function StatusSelect() { 
	var selected = $('#us_paytype').find('option[selected]'); 
	if(selected.attr('id') == 'i1') { $('#ingr').fadeIn('slow') }; 
	if(selected.attr('id') == 'ep') { $('#ingr').slideDown('fast') }; 
};