$(document).ready(function() {

calc();

	$("#calc").validate({
		submitHandler: function() { calc (); },
		rules: {
			person: {
				required: true,
				minlength: 1,
				number: true
			},
			days: {
				required: true,
				minlength: 1,
				number: true
			}

		},
		messages: {
			person: "!Число!",
			days: "!Число!"
		}

	});

		$("#person").keyup( function() { calc(); } );
		$("#days").keyup( function() { calc(); } );


$("img.tobag").click(function () {

    if (div_added_inserted < 1){
	$("#content_min").before( div_added );
    }

    if (navigator.userAgent.indexOf('Opera') + 1){
    } else {
	var ts_added = setTimeout(function () { $("#added").show() }, 1000);
	var th_added = setTimeout(function () { $("#added").animate({opacity: 0}, 3500) }, 1000);
    }

    var ts_added2 = setTimeout(function () { $("#added2").show() }, 1000);
    var th_added2 = setTimeout(function () { $("#added2").hide() }, 4300);

    var re = /\d+/;
    var $tovarId = re.exec($(this).attr("id"));
    var $count = $("#inputcount" + $tovarId).attr("value");

//    $("#bag").html("Загрузка ... ");
    $.ajax({
	url: "?action=dp&vs=2/scId=6&p=bag&addPriceId=" + $tovarId + "&cnt=" + $count,
	cache: false,
	timeout: 20000,
	success: function(html){ $("#bag").html(html); spanbagdel($(this)); },
	error: function (XMLHttpRequest, textStatus, errorThrown) { $("#bag").html($loadError); }
    });

//    alert('Товар добавлен в корзину.');

if($.browser.msie && $.browser.version.substr(0,1) < "8"){
    return "";
}

//    alert($tovarId);
    var $t = $("#imgprice" + $tovarId);
    var $k = $t.next();
//    var $k = $t.next().next();
    $k.css("position", "absolute");
    var bagPos = $("#bag").offset();
    var pos = $t.offset();
    $k.css({"border": "1px solid black;"});
    $k.css({"left": (pos.left) + "px", "top": pos.top + "px" });
    $k.css({"width": $t.width() + "px", "height": $t.height() + "px" });
    $k.html($count);
    $k.css("font-size", "100px;");
    $k.fadeIn(500)
      .animate({
          width: "25px",
          height: "25px",
          opacity: 0.5,
          fontSize: "9px",
          left: bagPos.left + ($("#bag").width() / 2),
          top: bagPos.top + ($("#bag").height() / 2)
        }, 500)
        .animate({ opacity: "hide", fontSize: "150px" }, 0);


    setTimeout(function() { $t.hide(); }, 500);
    setTimeout(function() { $t.fadeIn(300); }, 1000);

});


$("span.bagdel").click(function () { spanbagdel($(this)) });

function spanbagdel(ths) {

    var re = /\d+/;
    var $tovarId = re.exec(ths.attr("id"));
//    $ths.prev().children("span").html(123);
    var $count = re.exec(ths.prev().children("span").attr("id"));

//    $("#bag").html("Загрузка ... ");
    ths.parent().slideUp();

    $.ajax({
	url: "?action=dp&vs=2/scId=6&p=bag&delPriceId=" + $tovarId + "&cnt=" + $count,
	cache: false,
	timeout: 20000,
	success: function(html){ 
//	    $("#bag").html(html);
	    clearTimeout(tmot);
	    tmot = setTimeout(function () {
		$("#bag").html(html);
		$("span.bagdel").click(function () { spanbagdel($(this)) });
	    }, 700);
	 },
	 error: function (XMLHttpRequest, textStatus, errorThrown) { $("#bag").html($loadError); }
    });
}


var tmot;
var $loadError = "Сбой соединения с сервером, повторите попытку позже.";
var div_added_inserted = 0;
var div_added = '<!-- ui-dialog --><div class="ui-overlay" id="added" style="display: none; position: fixed; z-index: 5;"><div class="ui-widget-overlay"></div><div class="ui-widget-shadow ui-corner-all" style="width: 222px; height: 72px; position: fixed; left: 40%; top: 40%;"></div></div>                                                <div id="added2" style="display: none; position: fixed; z-index: 10; width: 200px; height: 50px; left: 40%; top: 40%; padding: 10px;" class="ui-widget ui-widget-content ui-corner-all">                                                                                <div class="ui-dialog-content ui-widget-content" style="background: none; border: 0; text-align:center;"><p>Товар добавлен в корзину.</p></div></div>';

});

function calc (){
	var person = $("#person").val();
	var days = $("#days").val();
	var res = person * days;
	
	res = Math.round(((person * calc_voda_l * days)+19/2)/18.9);

//	if (res == 0){ res = 1; }
	if (days == 0 || person == 0){ res = 0; }
	
	$("#res").html(res);
}

