$(document).ready(function(){
	
	
	$("#addtob").click(function () {
		$('#tobasket').ajaxSubmit(options);
		return false;
	});
	
	$(".changefoto").click(function () {
		if ($(this).parent().hasClass('active')){
			return false;
		}
		
		$('#boxviewfoto').block({ message: null });
		
		$("#boxviewfoto img").attr('src',$(this).attr('href'));
		$(this).parent().parent().find('li').removeClass('active');
		$('div.podlogka div.center li').find('img').css('opacity',1);
		$(this).parent().addClass('active');
		$("#boxviewfoto img").load(function () {
			$('#boxviewfoto').unblock(); 
			$('div.podlogka div.center li.active').find('img').css('opacity',0.4);
		});
		
		
		return false;
	});
	
	$(".profils_order h4 input").click(function () {
		if ($(this).parent().parent().hasClass('profils_order')){
			$(".profils_order fieldset").hide();
			$(this).parent().parent().children('fieldset').show();
		}
	});
	
	$(".product").keyup(function () {
		if (!(/^[0-9]{1,9}$/.test($(this).val()))){
			$(this).val('')
		} else if (($(this).val()*1) > ($(this).attr('maxvalue')*1)){
			$(this).val($(this).attr('maxvalue'));
		}
		calc_summ();
	});

	$(".btnedit").click(function () {
		if ($(this).children('em').html() == 'Редактировать'){
			$(this).parent().children('div.show').hide();
			$(this).parent().children('div.edit').show();
			$(this).children('em').html('Пересчитать');
		} else {
			$(this).parent().children('div.edit').children('form').ajaxSubmit(options2);
		}
		return false;
	});
	
	$(".delete").click(function () {
		if(confirm("Вы уверены, что хотите удалить данный товар ?")){
			return true;
		} else {
			return false;
		}
	});
	
	calc_summ();
	
	$("a[class^='comgal']").prettyPhoto();
});

function calc_summ(){
	var summa = 0;
	var count = 0;
	$(".product").each(function(){
		if ($(this).val() != ''){
			summa += $(this).val()*1*$(this).attr('price');
			count += 1;
		}
	});
	$('#summ_names').html(summa);
	$('#count_names').html(count);
}

var options = { 
		success: showResponse 
};
var options2 = { 
		success: showResponse2
};

function showResponse(responseText, statusText)  {
	showsave("Товар успешно добавлен в лист заказа");
	$('#countb').html(responseText);
}
function showResponse2(responseText, statusText)  {
	showsave('Лист заказа обновлен');
	document.location.href='/basket/';
}

