$(document).ready(function(){
	$('input.placeholder, textarea.placeholder').placeholder();
	if($.browser.msie) $('.pie, .fixradius, .fixradius2, .radius, .radius2').pie();
	
	// считаем сумму заказа
	var amount = 0;
	if ($('.amount').has('input').length){
		$('.amount input').each(function(index){
			amount = amount + ($(this).val() == "" ? 0 : parseInt($(this).val())) * 150;
		});
		$('.all span').html(amount);
		$('.amount input').keyup(function(event){
			this.value = this.value.replace(/[^0-9]/g,'');
			amount = 0;
			$('.amount input').each(function(index){
				amount = amount + ($(this).val() == "" ? 0 : parseInt($(this).val())) * 150;
			});
			$('.all span').html(amount);
		});
		
		$('input.quan').keyup(function(event){
			this.value = this.value.replace(/[^0-9]/g,'');
			var num = 0;
			catclass = $(this).attr('class').replace("quan ", "");
			$("input."+catclass).each(function(index){
				num = num + ($(this).val() == "" ? 0 : parseInt($(this).val()));
			});
			if (num > $('.amount input.day'+$(this).attr("day")).val()) {
				alert("Количество блюд в каждой категории не должно превышать количество обедов за день");
				$(this).val(0);
			}
		});
	} else {
		$('input.quan').each(function(index){
			price = parseInt($(this).parent().prev('.price').find('span').html());
			amount = amount + (this.value == "" ? 0 : this.value) * price;
		});
		$('.all span').html(amount);
		$('input.quan').keyup(function(event){
			this.value = this.value.replace(/[^0-9]/g,'');
			amount = 0;
			$('input.quan').each(function(index){
				price = parseInt($(this).parent().prev('.price').find('span').html());
				amount = amount + (this.value == "" ? 0 : this.value) * price;
			});
			$('.all span').html(amount);
		});
	}
	$('input.quan, .amount input').focusout(function(){
		this.value = this.value == "" ? 0 : this.value;
	}); 
});

$.fn.placeholder = function(){
	this.each(function(){
		$(this).attr('defaultvalue',$(this).val()).focus(function(){
			if($(this).val() == $(this).attr('defaultvalue'))
				$(this).val('');
		}).blur(function(){
			if($(this).val() == '')
				$(this).val($(this).attr('defaultvalue'));
		});
	});
}

$.fn.pie = function(a){
	if (window.PIE && $.browser.msie) {
		if(a === 'remove')
		{
			this.each(function() {
				PIE.detach(this);
			});
		} else
		{
			this.each(function() {
				PIE.attach(this);
			});
		}
	}
	return this;
}

function overlay(act){
	var overlay = $('#sm_overlay');
	if(act === 'hide'){
		overlay.hide();
		return false;
	}
	if(!overlay.length)
	{
		$('<div id="sm_overlay" class="sm_overlay"></div>').appendTo('body');
		overlay = $('#sm_overlay');
	}
	overlay.css({height:$('body').height(),opacity:0.5}).show();
}

function em(box, server, zone, img)
{
 var at = String.fromCharCode(64);
 if (img == 1)
 	return '<a href="'+'mai'+'lto'+':'+box+at+server+'.'+zone+'">';
 else
 	return '<a href="'+'mai'+'lto'+':'+box+at+server+'.'+zone+'">'+box+at+server+'.'+zone+'<'+'/a>';
}
