﻿message("mes", "Установите размеры окна")
var diff = 14;
function resetinfo()
{
    $("div#mailinfo") . css("background-color", "#ffffff");
    $("div#mailinfo") . css("color", "#000000");
    $("div#mailinfo") . html("");
    $("div#mailinfo") . hide();
}
function message(type, text)
{
    resetinfo();
    if (type != "error") {
        $("div#mailinfo") . hide();
        $("div#mailinfo") . css("background-color", "#76B201");
        $("div#mailinfo") . css("color", "#ffffff");
        $("div#mailinfo") . html(text);
        $("div#mailinfo") . fadeIn("fast");
    }else {
        $("div#mailinfo") . hide();
        $("div#mailinfo") . css("background-color", "#C70000");
        $("div#mailinfo") . css("color", "#ffffff");
        $("div#mailinfo") . html(text);
        $("div#mailinfo") . fadeIn("fast"); ;
    }
}
function mainWindow(check)
{
    var w, h;
    w = parseInt($("input#mainWidth") . val());
    h = parseInt($("input#mainHeight") . val());
    if (w < 400) {
        w = 400;
        message("error", "Ширина окна не может быть менее 400 мм");
        $("input#mainWidth") . val(400);
    }
    if (w > 10000) {
        w = 10000;
        message("error", "Ширина окна не может быть более 10000 мм");
        $("input#mainWidth") . val(10000);
    }
    if (h < 400) {
        h = 400;
        message("error", "Высота окна не может быть менее 400 мм");
        $("input#mainHeight") . val(400);
    }
    if (h > 4000) {
        h = 4000;
        message("error", "Высота окна не может быть более 4000 мм");
        $("input#mainHeight") . val(4000);
    }
    if (w / h > 3) {
        if (check == "w") {
            h = Math . round(w / 3);
            message("error", "Отношение ширины к высоте не может превышать 3 (3:1).");
            $("input#mainHeight") . val(h);
        }else {
            h = 2000;
            w = 3000;
            message("error", "Отношение ширины к высоте не может превышать 3 (3:1). Значения установлены по умолчанию.");
            $("input#mainHeight") . val(2000);
            $("input#mainWidth") . val(3000);
        }
    } else {
        if (h / w > 2) {
            if (check == "h") {
                w = Math . round(h / 2);
                message("error", "Отношение высоты к ширине не может превышать 2 (2:1).");
                $("input#mainWidth") . val(w);
            }else {
                h = 2000;
                w = 3000;
                message("error", "Отношение высоты к ширине не может превышать 2 (2:1). Значения установлены по умолчанию.");
                $("input#mainHeight") . val(2000);
                $("input#mainWidth") . val(3000);
            }
        }
    }
    ratio = w / h;
    sW = 345;
    sH = Math . round(345 / ratio);
    $("div#mainWindow") . animate({
            height:sH} , 100, function()
        {
            if (check == 1) {
                manageWindow("new");}
        }
        );
	calculation();
}
function Draw(id)
{
    if (id == undefined) {
        id = "leaf_1";
    }
    mW = parseInt($("div#mainWindow") . css("width")) - (diff * 2);
    mH = parseInt($("div#mainWindow") . css("height")) - (diff * 2);
    $("div#" + id) . animate({
            width:mW, height:mH} , 1000);
}
function _select(obj)
{
	$("div#sliderWidth").hide();
	$("div#sliderW").hide();
	$("div#sliderW").slider("destroy");
	$("div#sliderHeight").hide();
	$("div#sliderH").hide();
	$("div#sliderH").slider("destroy");

	if ($("div#mainWindowInner div.ws-red-bg").size()>0) {
		message("error", "У Вас есть удаляемая створка, выберите действие нажав на стрелки или отмену.");
	}else{
		var id = obj . id;
	    var cl = $("#" + id) . attr("class");
	    if (cl == "ws-blue-bg" || cl == "ws-nets-bg") {
			$("div#mainWindowInner div.wc-blue-bg") . removeClass("wc-blue-bg") . addClass("ws-blue-bg");
			$("div#mainWindowInner div.wc-nets-bg") . removeClass("wc-nets-bg") . addClass("ws-nets-bg");
			if (cl == "ws-blue-bg") {
				$("#" + id) . removeClass("ws-blue-bg") . addClass("wc-blue-bg");
			}else{$("#" + id) . removeClass("ws-nets-bg") . addClass("wc-blue-bg");}
	        $("input#current") . val(id);
			$("div#"+id+"_draw").empty();
			$("div#"+id+" #handle_v").hide();
			$("div#"+id+" #handle").hide();
	        $("input#aWidth") . val($("#" + id) . attr("Wwidth"));
	        $("input#aHeight") . val($("#" + id) . attr("Wheight"));
	    } else {
			if (cl=='wc-blue-bg') {
				$("#" + id) . removeClass("wc-blue-bg") . addClass("ws-blue-bg");
			}else{
				$("#" + id) . removeClass("wc-nets-bg") . addClass("ws-nets-bg");
			}
	        $("input#current") . val("null");
			$("input#aWidth") . val("");
	        $("input#aHeight") . val("");
	    }
	}
}
function manageWindow(type)
{
	$("div#sliderWidth").hide();
	$("div#sliderW").hide();
	$("div#sliderW").slider("destroy");
    $("div#sliderHeight").hide();
	$("div#sliderH").hide();
	$("div#sliderH").slider("destroy");
    resetinfo();
    var id = $("input#current") . val();
	if (type!='moskit') {
		$("div#"+id+"_draw").empty();
		$("div#"+id+" #handle_v").hide();
		$("div#"+id+" #handle").hide();
	}
    if (id != "null" || type == "new") {
        switch (type) {
            case "new":
                var obj = $("div#leaf_default") . clone();
                var count = parseInt($("#count") . val()) + 1;
                $("#count") . val(count);
                obj . attr("id", "leaf_" + count);
                var mW = parseInt($("div#mainWindow") . css("width")) - (diff * 2);
                var mH = parseInt($("div#mainWindow") . css("height")) - (diff * 2);
                obj . css({
                        width:mW, height:mH, left:diff, top:diff}
                    );
                w = parseInt($("input#mainWidth") . val());
                h = parseInt($("input#mainHeight") . val());
                obj . attr("Wwidth", w);
                obj . attr("Wheight", h);
                obj . appendTo("div#mainWindowInner");
                obj . fadeIn(500);
                obj = null;
				$("div#drawdiv") . appendTo("div#mainWindowInner");
				$("div#leftArrow") . appendTo("div#mainWindowInner");
				$("div#rightArrow") . appendTo("div#mainWindowInner");
				$("div#topArrow") . appendTo("div#mainWindowInner");
				$("div#bottomArrow") . appendTo("div#mainWindowInner");
				$("div#delCancel") . appendTo("div#mainWindowInner");
                break;
            case "vertical" :
                var wW = parseInt($("div#" + id) . css("width"));
                var wH = parseInt($("div#" + id) . css("height"));
                var wL = parseInt($("div#" + id) . css("left"));
                var wT = parseInt($("div#" + id) . css("top"));
                var newW = Math . round(wW / 2);

                var oldW = $("div#" + id) . attr("Wwidth");
                var oldH = $("div#" + id) . attr("WHeight");
                var dw = Math . round(oldW / 2);
                var olnW = oldW - dw;
                if (dw >= 400 && olnW >= 400) {
                    var nW = wW - newW;
                    var nL = wL + newW;
                    var nH = wH;
                    var obj = $("div#leaf_default") . clone();
                    var count = parseInt($("#count") . val()) + 1;
                    $("#count") . val(count)
                    obj . attr("id", "leaf_" + count);
                    $("div#" + id) . attr("Wwidth", dw);
                    obj . attr("Wwidth", olnW);
                    $("input#aWidth") . val(dw);
                    obj . attr("Wheight", oldH);
                    obj . css({width:nW, height:nH, left:nL, top:wT});
                    $("div#" + id) . animate({
                            width:newW} , 1000, function()
                        {
                            obj . insertAfter("div#" + id);
                            obj . fadeIn();
                            obj = null;
                        }
                        );
                } else {
                    message("error", "Недопустим размер створки менее 400 мм.");
                }

                break;
            case "twice_vertical" :
                var wW = parseInt($("div#" + id) . css("width"));
                var wH = parseInt($("div#" + id) . css("height"));
                var wL = parseInt($("div#" + id) . css("left"));
                var wT = parseInt($("div#" + id) . css("top"));
                var newW = Math . round(wW / 3);
				var	temp=wW-newW;
				new2W=Math . round(temp/2);
				new3W=temp-new2W;
				var oldW = $("div#" + id) . attr("Wwidth");
                var oldH = $("div#" + id) . attr("WHeight");
				var dw = Math . round(oldW / 3);
				var	temp=oldW-dw;
                var oln2W = Math.round(temp/2);
				var oln3W=temp-oln2W;
                if (dw >= 400 && oln2W >= 400 &&  oln3W >= 400) {

                    var nL = wL + newW;
                    var nH = wH;
                    var obj = $("div#leaf_default") . clone();
                    var count = parseInt($("#count") . val()) + 1;
                    $("#count") . val(count)
                    obj . attr("id", "leaf_" + count);
                    $("div#" + id) . attr("Wwidth", dw);
                    obj . attr("Wwidth", oln2W);
                    $("input#aWidth") . val(dw);
                    obj . attr("Wheight", oldH);
                    obj . css({
                            width:new2W, height:nH, left:nL, top:wT}
                        );
					var obj2 = $("div#leaf_default") . clone();
                    var count = parseInt($("#count") . val()) + 1;
                    $("#count") . val(count)
                    obj2 . attr("id", "leaf_" + count);
                    obj2 . attr("Wwidth", oln3W);
                    obj2 . attr("Wheight", oldH);
					nL = nL + new2W;
                    obj2 . css({
                            width:new3W, height:nH, left:nL, top:wT}
                        );
                    $("div#" + id) . animate({
                            width:newW} , 1000, function()
                        {
                            obj . insertAfter("div#" + id);
                            obj2 . insertAfter("div#" + id);
                            obj . fadeIn();
                            obj2 . fadeIn();
                            obj = null;
                            obj2 = null;
                        }
                        );
                } else {
                    message("error", "Недопустим размер створки менее 400 мм.");
                }
				break;
			case "horizontal" :
                var wW = parseInt($("div#" + id) . css("width"));
                var wH = parseInt($("div#" + id) . css("height"));
                var wL = parseInt($("div#" + id) . css("left"));
                var wT = parseInt($("div#" + id) . css("top"));
                var newH = Math . round(wH / 2);
                var oldW = $("div#" + id) . attr("Wwidth");
                var oldH = $("div#" + id) . attr("WHeight");
                var dh = Math . round(oldH / 2);
                var olnH = oldH - dh;
                if (dh >= 400 && olnH >= 400) {
                    var nW = wW;
                    var nL = wL;
					var nT = wT + newH;
                    var nH = wH - newH;
                    var obj = $("div#leaf_default") . clone();
                    var count = parseInt($("#count") . val()) + 1;
                    $("#count") . val(count)
                    obj . attr("id", "leaf_" + count);
                    $("div#" + id) . attr("Wheight", dh);
                    obj . attr("Wheight", olnH);
                    $("input#aHeight") . val(dh);
                    obj . attr("Wwidth", oldW);
                    obj . css({
                            width:nW, height:nH, left:nL, top:nT}
                        );
                    $("div#" + id) . animate({
                            height:newH} , 1000, function()
                        {
                            obj . insertAfter("div#" + id);
                            obj . fadeIn();
                            obj = null;
                        }
                        );

                } else {
                    message("error", "Недопустим размер створки менее 400 мм.");
                }

                break;
            case "delete" :
			 	var wW = parseInt($("div#" + id) . css("width"));
                var wH = parseInt($("div#" + id) . css("height"));
                var wL = parseInt($("div#" + id) . css("left"));
                var wT = parseInt($("div#" + id) . css("top"));
				$("#" + id) . removeClass("wc-blue-bg") . addClass("ws-red-bg");
				$("div#delCancel").css({left:(wL+Math.round( wW/2 )-10),top:(wT+Math.round( wH/2 )-10)});
				$("div#delCancel").show();
				$("div#mainWindowInner div.ws-blue-bg, div#mainWindowInner div.ws-nets-bg").each(function (i) {
					/**
					 * Проверка на совпадение левого края
					 **/
					if (parseInt(this.style.top)==wT && (wT+wH)==(parseInt(this.style.top)+parseInt(this.style.height))
					&& wL==(parseInt(this.style.left)+parseInt(this.style.width))) {
						$("div#rightArrow").css({left:wL,
						top:wT+Math.round(wH/2)-4});
						$("input#leftDel").val(this.id);
						$("div#rightArrow").show();
					}
					/**
					 * Проверка на совпадение правого края
					 **/
					if (parseInt(this.style.top)==wT && (wT+wH)==(parseInt(this.style.top)+parseInt(this.style.height)) &&
					wL+wW==parseInt(this.style.left)) {
						$("div#leftArrow").css({left:((wL+wW)-8),
						top:wT+Math.round(wH/2)-4});
						$("input#rightDel").val(this.id);
						$("div#leftArrow").show();
					}
					/**
					 * Проверка на совпадение верхнего края
					 **/
					if (wT==(parseInt(this.style.top)+parseInt(this.style.height)) &&
					wW==parseInt(this.style.width) && wL==parseInt(this.style.left)) {
						$("div#bottomArrow").css({left:wL+Math.round(wW/2)-4,
						top:wT});
						$("input#topDel").val(this.id);
						$("div#bottomArrow").show();

					}
					/**
					 * Проверка на совпадение нижнего края
					 **/
					if (wL==parseInt(this.style.left) &&
						wT+wH==parseInt(this.style.top) && wW==parseInt(this.style.width)) {
						$("div#topArrow").css({left:wL+Math.round(wW/2)-4,
						top:((wT+wH)-8)});
						$("input#bottomDel").val(this.id);
						$("div#topArrow").show();
					}
			    });
                break;
            case "changeWidth":
				if ($("div#sliderWidth").css("display")=="none") {
					var mainObj=$("div#" + id);
					var wW = parseInt(mainObj . css("width"));
	                var wH = parseInt(mainObj . css("height"));
	                var wL = parseInt(mainObj . css("left"));
	                var wT = parseInt(mainObj . css("top"));
	                var maxW = parseInt(mainObj . attr("Wwidth"));
					var center =(Math.round(wW/2)+wL);
					var ratio=maxW/wW;
					var chW=0
					var chLeft=null;
					var chRight=null;
					$("div#mainWindowInner div.ws-blue-bg, div#mainWindowInner div.ws-nets-bg").each(function (i) {
						/**
						 * Проверка на совпадение левого края
						 **/
						if (parseInt(this.style.top)==wT && (wT+wH)==(parseInt(this.style.top)+parseInt(this.style.height))
						&& wL==(parseInt(this.style.left)+parseInt(this.style.width))) {
							var lW=$("div#"+this.id).attr("Wwidth");
							chW=chW+(lW-400);
							chLeft=$(this);
							$("div#"+this.id+"_draw").empty();
							$("div#"+this.id+" #handle_v").hide();
							$("div#"+this.id+" #handle").hide();

						}
						/**
						 * Проверка на совпадение правого края
						 **/
						if (parseInt(this.style.top)==wT && (wT+wH)==(parseInt(this.style.top)+parseInt(this.style.height)) &&
						wL+wW==parseInt(this.style.left)) {
							var rW=$("div#"+this.id).attr("Wwidth");
							chW=chW+(rW-400);
							chRight=$(this);
							$("div#"+this.id+"_draw").empty();
							$("div#"+this.id+" #handle_v").hide();
							$("div#"+this.id+" #handle").hide();

						}
					});
					$("div#"+id+"_draw").empty();
					$("div#"+id+" #handle_v").hide();
					$("div#"+id+" #handle").hide();

					chObj=$("input#aWidth");
					//if (chW>=0 && maxW>=400) {
					if (chLeft!=null || chRight!=null) {
						var i=chW+maxW;
							$("div#sliderW").slider({
							axis:"vertical",
							min:400,
							max:i,
							stepping:50,
							startValue:maxW,
							//handles: [{start: maxW, min: 400, max: i}],
							slide:function(e,ui){
								val=ui.value;
								chObj.val($(this).slider("value"),val);
								mainObj.attr("Wwidth",val);
								if (chLeft!=null && chRight!=null) {
									ip=Math.round((val-maxW)/2);
									ip2=(val-maxW)-ip;
									if (parseInt(chLeft.attr("Wwidth"))-(ip)<400) {
										chLeft=null;
									}
									if (parseInt(chRight.attr("Wwidth"))-(ip2)<400) {
										chRight=null;
									}
									if (chRight!=null && chLeft!=null){
									chLeft.attr("Wwidth",(parseInt(chLeft.attr("Wwidth"))-(ip)));
									chRight.attr("Wwidth",(parseInt(chRight.attr("Wwidth"))-(ip2)));
									checkL=chLeft.attr("Wwidth");
									maxW=val;
									oldW=parseInt(mainObj . css("width"));
									oldLW=parseInt(chLeft . css("width"));
									oldRW=parseInt(chRight . css("width"));
									newW=Math.ceil(val/ratio);
									cen=Math.round(newW/2);
									mainObj . css("left",center-cen);
									mainObj . css("width",(newW));
									chLeft . css("width",parseInt(mainObj . css("left"))-parseInt(chLeft . css("left")));
									//mainObj . css("left",(parseInt(chLeft . css("width"))+parseInt(chLeft . css("left"))));
									chRight . css("left",(parseInt(mainObj . css("width"))+parseInt(mainObj . css("left"))));
									chRight . css("width",(oldRW+oldLW+oldW)-(newW+parseInt(chLeft . css("width"))));
									wW=newW;
									}
								}else
								if (chLeft!=null && chRight==null) {
									chLeft.attr("Wwidth",(parseInt(chLeft.attr("Wwidth"))-(val-maxW)));
									oldW=parseInt(mainObj . css("width"));
									oldLW=parseInt(chLeft . css("width"));
									newW=Math.round(val/ratio);
									newLW=oldLW-(newW-wW);
									mainObj . css("width",newW);
									chLeft . css("width",newLW);
									mainObj . css("left",(parseInt(chLeft . css("width"))+parseInt(chLeft . css("left"))));
									wW=newW;
									maxW=val;
								}else
								if (chLeft==null && chRight!=null) {
									chRight.attr("Wwidth",(parseInt(chRight.attr("Wwidth"))-(val-maxW)));
									oldW=parseInt(mainObj . css("width"));
									oldRW=parseInt(chRight . css("width"));
									newW=Math.round(val/ratio);
									newRW=oldRW-(newW-wW);
									mainObj . css("width",newW);
									chRight . css("width",newRW);
									chRight . css("left",(parseInt(mainObj . css("width"))+parseInt(mainObj . css("left"))));
									maxW=val;
									wW=newW;
								};
							},
							change:function(){
								/*
								Анимация изменения размеров
								*/

							},
							stop:function(){
								$("div#sliderWidth").hide();
								$("div#sliderW").hide();
								$("div#sliderW").slider("destroy");
							}
						});
						$("div#sliderWidth").show();
						$("div#sliderW").show();

					}else{message("error","Ширина створки не может быть изменена");}

				}else{
				$("div#sliderWidth").hide();
				$("div#sliderW").hide();
				$("div#sliderW").slider("destroy");}
            break;
			// изменение размера по вертикали...
			 case "changeHeight":
				if ($("div#sliderW").css("display")=="none") {
					var mainObj=$("div#" + id);
					var wW = parseInt(mainObj . css("width"));
	                var wH = parseInt(mainObj . css("height"));
	                var wL = parseInt(mainObj . css("left"));
	                var wT = parseInt(mainObj . css("top"));
	                var maxH = parseInt(mainObj . attr("Wheight"));
					var center =(Math.round(wH/2)+wT);
					var ratio=maxH/wH;
					var chH=0
					var chTop=null;
					var chBottom=null;
					$("div#mainWindowInner div.ws-blue-bg, div#mainWindowInner div.ws-nets-bg").each(function (i) {
						/**
						 * Проверка на совпадение верхнего края
						 **/
						if (wT==(parseInt(this.style.top)+parseInt(this.style.height)) &&wW==parseInt(this.style.width) && wL==parseInt(this.style.left)) {
							var tH=$("div#"+this.id).attr("Wheight");
							chH=chH+(tH-400);
							chTop=$(this);
						}
						/**
						 * Проверка на совпадение нижнего края
						 **/
						if (wL==parseInt(this.style.left) && wT+wH==parseInt(this.style.top) && wW==parseInt(this.style.width)) {
							var tH=$("div#"+this.id).attr("Wheight");
							chH=chH+(tH-400);
							chBottom=$(this);
						}
					});
					chObj=$("input#aHeight");
					//if (chH>=0 && maxH>=400) {
					if (chBottom!=null || chTop!=null) {
						var i=chH+maxH;
							$("div#sliderH").slider({
							axis:"vertical",
							min:400,
							max:i,
							stepping:50,
							startValue:maxH,
							slide:function(e,ui){
								val=ui.value;
								chObj.val($(this).slider("value"),val);
								mainObj.attr("Wheight",val);
								if (chTop!=null && chBottom!=null) {
									ip=Math.round((val-maxH)/2);
									ip2=(val-maxH)-ip;
									if (parseInt(chTop.attr("Wheight"))-(ip)<400) {
										chTop=null;
									}
									if (parseInt(chBottom.attr("Wheight"))-(ip2)<400) {
										chBottom=null;
									}
									if (chBottom!=null && chTop!=null){
									chTop.attr("Wheight",(parseInt(chTop.attr("Wheight"))-(ip)));
									chBottom.attr("Wheight",(parseInt(chBottom.attr("Wheight"))-(ip2)));
									maxH=val;
									oldW=parseInt(mainObj . css("height"));
									oldTH=parseInt(chTop . css("height"));
									oldBH=parseInt(chBottom . css("height"));
									newH=Math.ceil(val/ratio);
									cen=Math.round(newH/2);
									mainObj . css("top",center-cen);
									mainObj . css("height",(newH));
									chTop . css("height",parseInt(mainObj . css("top"))-parseInt(chTop . css("top")));
									chBottom . css("top",(parseInt(mainObj . css("height"))+parseInt(mainObj . css("top"))));
									chBottom . css("height",(oldBH+oldTH+oldW)-(newH+parseInt(chTop . css("height"))));
									wW=newH;
									}
								}else
								if (chTop!=null && chBottom==null) {
									chTop.attr("Wheight",(parseInt(chTop.attr("Wheight"))-(val-maxH)));
									oldW=parseInt(mainObj . css("height"));
									oldTH=parseInt(chTop . css("height"));
									newH=Math.round(val/ratio);
									newLW=oldTH-(newH-wH);
									mainObj . css("height",newH);
									chTop . css("height",newLW);
									mainObj . css("top",(parseInt(chTop . css("height"))+parseInt(chTop . css("top"))));
									wH=newH;
									maxH=val;
								}else
								if (chTop==null && chBottom!=null) {
									chBottom.attr("Wheight",(parseInt(chBottom.attr("Wheight"))-(val-maxH)));
									oldW=parseInt(mainObj . css("height"));
									oldBH=parseInt(chBottom . css("height"));
									newH=Math.round(val/ratio);
									newBH=oldBH-(newH-wH);
									mainObj . css("height",newH);
									chBottom . css("height",newBH);
									chBottom . css("top",(parseInt(mainObj . css("height"))+parseInt(mainObj . css("top"))));
									maxH=val;
									wH=newH;
								};
							},
							change:function(){
								//
								//$("div#sliderH").hide();
								$("div#sliderHeight").hide();
								$("#sliderH").slider("destroy");
								$("#sliderH").hide();


							}
						});
						$("div#sliderHeight").show();
						$("div#sliderH").show();

					}else{message("error","Высота створки не может быть изменена");}

				}else{
				$("div#sliderHeight").hide();
				$("div#sliderH").hide();
				$("div#sliderH").slider("destroy");}
            break;
			case "rotateLeft":
				var mainObj=$("div#" + id);
				var wW = parseInt(mainObj . css("width"));
	            var wH = parseInt(mainObj . css("height"));
	            var wL = parseInt(mainObj . css("left"));
	            var wT = parseInt(mainObj . css("top"));
				$("div#drawdiv").empty();
				$("div#drawdiv").drawPolyline([(wL+wW)-8, wL+8,wL+wW-8], [wT+8, (wT+(wH/2)),wT+wH-8], {color:'#FFFFFF'});
				mainObj.attr("stype","left");
				var obj = $("div#drawdiv") . clone();
				obj . insertAfter("div#" + id);
				obj.attr("id",id+"_draw");
				$("div#"+id+" #handle").removeClass("whr").addClass("whl");
				$("div#"+id+" #handle").css("padding-top",(wH)/2-9);
				$("div#"+id+" #handle_v").hide();
				$("div#"+id+" #handle").show();
				obj.show();
				obj=null;
				$("div#drawdiv").empty();
			break;

            case "rotateRight":
				var mainObj=$("div#" + id);
				var wW = parseInt(mainObj . css("width"));
	            var wH = parseInt(mainObj . css("height"));
	            var wL = parseInt(mainObj . css("left"));
	            var wT = parseInt(mainObj . css("top"));
				$("div#drawdiv").empty();
				$("div#drawdiv").drawPolyline([wL+8, (wL+wW)-8,wL+8], [wT+8, (wT+(wH/2)),wT+wH-8], {color:'#FFFFFF'});
				mainObj.attr("stype","right");
				var obj = $("div#drawdiv") . clone();
				obj . insertAfter("div#" + id);
				obj.attr("id",id+"_draw");
				$("div#"+id+" #handle").removeClass("whl").addClass("whr");
				$("div#"+id+" #handle").css("padding-top",(wH)/2-9);
				$("div#"+id+" #handle_v").hide();
				$("div#"+id+" #handle").show();
				obj.show();
				obj=null;
				$("div#drawdiv").empty();
			break;
			case "rotateLeft2":
				var mainObj=$("div#" + id);
				var wW = parseInt(mainObj . css("width"));
	            var wH = parseInt(mainObj . css("height"));
	            var wL = parseInt(mainObj . css("left"));
	            var wT = parseInt(mainObj . css("top"));
				$("div#drawdiv").empty();
				$("div#drawdiv").drawPolyline([(wL+wW)-8, wL+8,wL+wW-8], [wT+8, (wT+(wH/2)),wT+wH-8], {color:'#FFFFFF'});
				$("div#drawdiv").drawPolyline([wL+8, wL+(wW/2),wL+wW-8], [wT+wH-8, (wT+8),wT+wH-8], {color:'#FFFFFF'});
				mainObj.attr("stype","flipleft");
				var obj = $("div#drawdiv") . clone();
				obj . insertAfter("div#" + id);
				obj.attr("id",id+"_draw");
				$("div#"+id+" #handle").removeClass("whr").addClass("whl");
				$("div#"+id+" #handle").css("padding-top",(wH)/2-9);
				$("div#"+id+" #handle_v").hide();
				$("div#"+id+" #handle").show();
				obj.show();
				obj=null;
				$("div#drawdiv").empty();
			break;
			case "rotateRight2":
				var mainObj=$("div#" + id);
				var wW = parseInt(mainObj . css("width"));
	            var wH = parseInt(mainObj . css("height"));
	            var wL = parseInt(mainObj . css("left"));
	            var wT = parseInt(mainObj . css("top"));
				$("div#drawdiv").empty();
				$("div#drawdiv").drawPolyline([wL+8, (wL+wW)-8,wL+8], [wT+8, (wT+(wH/2)),wT+wH-8], {color:'#FFFFFF'});
				$("div#drawdiv").drawPolyline([wL+8, wL+(wW/2),wL+wW-8], [wT+wH-8, (wT+8),wT+wH-8], {color:'#FFFFFF'});
				mainObj.attr("stype","flipright");
				var obj = $("div#drawdiv") . clone();
				obj . insertAfter("div#" + id);
				obj.attr("id",id+"_draw");
				$("div#"+id+" #handle").removeClass("whl").addClass("whr");
				$("div#"+id+" #handle").css("padding-top",(wH)/2-9);
				$("div#"+id+" #handle_v").hide();
				$("div#"+id+" #handle").show();
				obj.show();
				obj=null;
				$("div#drawdiv").empty();
			break;
			case "otkidnaya":
				var mainObj=$("div#" + id);
				var wW = parseInt(mainObj . css("width"));
	            var wH = parseInt(mainObj . css("height"));
	            var wL = parseInt(mainObj . css("left"));
	            var wT = parseInt(mainObj . css("top"));
				$("div#drawdiv").empty();
				$("div#drawdiv").drawPolyline([wL+8, wL+(wW/2),wL+wW-8], [wT+wH-8, (wT+8),wT+wH-8], {color:'#FFFFFF'});
				mainObj.attr("stype","flip");
				var obj = $("div#drawdiv") . clone();
				obj . insertAfter("div#" + id);
				obj.attr("id",id+"_draw");
				$("div#"+id+" #handle").hide();
				$("div#"+id+" #handle_v").css("padding-left",(wW)/2-9);
				$("div#"+id+" #handle_v").show();
				obj.show();

				obj=null;
				$("div#drawdiv").empty();
			break;
			case "gluhaya":
				var mainObj=$("div#" + id);
				if (mainObj.attr("class")=="wc-nets-bg") {
					message("error","Створка с москитной сеткой не может быть \"глухой\"");
				}else{
					var wW = parseInt(mainObj . css("width"));
		            var wH = parseInt(mainObj . css("height"));
		            var wL = parseInt(mainObj . css("left"));
		            var wT = parseInt(mainObj . css("top"));
					$("div#drawdiv").empty();
					$("div#drawdiv").drawPolyline([wL+(wW)/4,wL+(wW)/2+(wW)/4],[wT+(wH)/2+wH/4,wT+(wH)/4], {color:'#FFFFFF'});
					$("div#drawdiv").drawPolyline([wL+(wW)/4,wL+(wW)/2+(wW)/4],[wT+(wH)/4,wT+(wH)/2+wH/4], {color:'#FFFFFF'});
					mainObj.attr("stype","deaf");
					var obj = $("div#drawdiv") . clone();
					obj . insertAfter("div#" + id);
					obj.attr("id",id+"_draw");
					obj.show();
					obj=null;
					$("div#drawdiv").empty();
				}
			break;
			case "moskit":
				if ($("div#" + id).attr("stype")!="deaf") {
					$("div#" + id).removeClass("wc-blue-bg").addClass("wc-nets-bg");
				}else{message("error","Глухая створка не может быть с москитной сеткой");}

			break;
            default :
                message("error", "Нет такого действия!");
        }
    } else {
        message("error", "Не выбрана секция");
    }
	calculation();
}
function leftDelete(id){
	var Did = $("input#current") . val();
	var Nid = $("input#leftDel") . val();
	$("div#"+Nid+"_draw").empty();
	$("div#"+Nid+" #handle_v").hide();
	$("div#"+Nid+" #handle").hide();
	Dobj=$("div#"+Did);
	dW=parseInt(Dobj.css("width"));
	dH=parseInt(Dobj.css("height"));
	dL=parseInt(Dobj.css("left"));
	dT=parseInt(Dobj.css("top"));
	dUW=Dobj.attr("Wwidth");
	Dobj.fadeOut("fast",function(){Dobj.remove();});
	Nobj=$("div#"+Nid);
	nW=parseInt(Nobj.css("width"));
	nH=parseInt(Nobj.css("height"));
	nL=parseInt(Nobj.css("left"));
	nT=parseInt(Nobj.css("top"));
	nUW=Nobj.attr("Wwidth");
	Nobj.attr("Wwidth",(parseInt(nUW)+parseInt(dUW)));
	Nobj.animate({width:(dW+nW)},1000);
	finishDelete();
	$("input#leftDel") . val("null");
}
function rightDelete(){
	var Did = $("input#current") . val();
	var Nid = $("input#rightDel") . val();
	$("div#"+Nid+"_draw").empty();
	$("div#"+Nid+" #handle_v").hide();
	$("div#"+Nid+" #handle").hide();
	Dobj=$("div#"+Did);
	dW=parseInt(Dobj.css("width"));
	dH=parseInt(Dobj.css("height"));
	dL=parseInt(Dobj.css("left"));
	dT=parseInt(Dobj.css("top"));
	dUW=Dobj.attr("Wwidth");
	Dobj.fadeOut("fast",function(){Dobj.remove();});
	Nobj=$("div#"+Nid);
	nW=parseInt(Nobj.css("width"));
	nH=parseInt(Nobj.css("height"));
	nL=parseInt(Nobj.css("left"));
	nT=parseInt(Nobj.css("top"));
	nUW=Nobj.attr("Wwidth");
	Nobj.attr("Wwidth",(parseInt(nUW)+parseInt(dUW)));
	Nobj.animate({width:(dW+nW),left:dL},1000);
	finishDelete();
	$("input#rightDel") . val("null");
}
function topDelete(){
	var Did = $("input#current") . val();
	var Nid = $("input#topDel") . val();
	$("div#"+Nid+"_draw").empty();
	$("div#"+Nid+" #handle_v").hide();
	$("div#"+Nid+" #handle").hide();
	Dobj=$("div#"+Did);
	dW=parseInt(Dobj.css("width"));
	dH=parseInt(Dobj.css("height"));
	dL=parseInt(Dobj.css("left"));
	dT=parseInt(Dobj.css("top"));
	dUW=Dobj.attr("Wheight");
	Dobj.fadeOut("fast",function(){Dobj.remove();});
	Nobj=$("div#"+Nid);
	nW=parseInt(Nobj.css("width"));
	nH=parseInt(Nobj.css("height"));
	nL=parseInt(Nobj.css("left"));
	nT=parseInt(Nobj.css("top"));
	nUW=Nobj.attr("Wheight");
	Nobj.attr("Wheight",parseInt(nUW)+parseInt(dUW));
	Nobj.animate({height:(nH+dH)},1000);
	finishDelete();
	$("input#topDel") . val("null");
}
function bottomDelete(){
	var Did = $("input#current") . val();
	var Nid = $("input#bottomDel") . val();
	$("div#"+Nid+"_draw").empty();
	$("div#"+Nid+" #handle_v").hide();
	$("div#"+Nid+" #handle").hide();
	Dobj=$("div#"+Did);
	dW=parseInt(Dobj.css("width"));
	dH=parseInt(Dobj.css("height"));
	dL=parseInt(Dobj.css("left"));
	dT=parseInt(Dobj.css("top"));
	dUW=Dobj.attr("Wheight");
	Dobj.fadeOut("fast",function(){Dobj.remove();});
	Nobj=$("div#"+Nid);
	nW=parseInt(Nobj.css("width"));
	nH=parseInt(Nobj.css("height"));
	nL=parseInt(Nobj.css("left"));
	nT=parseInt(Nobj.css("top"));
	nUW=Nobj.attr("Wheight");
	Nobj.attr("Wheight",parseInt(nUW)+parseInt(dUW));
	Nobj.animate({height:(nH+dH),top:dT},1000);
	finishDelete();
	$("input#bottomDel") . val("null");
}
function finishDelete(){
	var id = $("input#current") . val();
    $("div#"+id) . removeClass("ws-red-bg") . addClass("wc-blue-bg");
	$("div#delCancel").hide();
	$("div#bottomArrow").hide();
	$("div#topArrow").hide();
	$("div#rightArrow").hide();
	$("div#leftArrow").hide();
	$("input#aWidth") . val("");
	$("input#aHeight") . val("");
}
function cancelDelete(){
	var id = $("input#current") . val();
    $("div#"+id) . removeClass("ws-red-bg") . addClass("wc-blue-bg");
	$("div#delCancel").hide();
	$("div#bottomArrow").hide();
	$("div#topArrow").hide();
	$("div#rightArrow").hide();
	$("div#leftArrow").hide();
	$("input#leftDel") . val("null");
	$("input#rightDel") . val("null");
	$("input#topDel") . val("null");
	$("input#bottomDel") . val("null");

}
function Fix()
{
    resetinfo();
    $("input#fixButton") . attr("disabled", "disabled");
    $("input#mainHeight") . attr("disabled", "disabled");
    $("input#mainWidth") . attr("disabled", "disabled");
    mainWindow(1);
	calculation();
}
function unFix()
{
    resetinfo();
    $("input#fixButton") . removeAttr("disabled");
    $("input#mainHeight") . removeAttr("disabled");
    $("input#mainWidth") . removeAttr("disabled");
	$("div#drawdiv") . appendTo("div#hiddenValues");
	$("div#leftArrow") . appendTo("div#hiddenValues");
	$("div#rightArrow") . appendTo("div#hiddenValues");
	$("div#topArrow") . appendTo("div#hiddenValues");
	$("div#bottomArrow") . appendTo("div#hiddenValues");
	$("div#delCancel") . appendTo("div#hiddenValues");
    $("div#mainWindowInner") . empty();
    $("div#doorDraw") . empty();
    $("div#mainDoorInner") . hide();
    $("#mdoor") . hide();
	$("#doorS").val("no");


	$("span#wPrice").html('0');
	message("mes", "Установите и зафиксируйте размеры окна");
}
/**
 *
 * @access public
 * @return void
 Управление балконной дверью
 **/
function select_door(){
	if ($("div#door_default").attr("class")=="ws-blue-bg") {
		$("div#door_default").removeClass("ws-blue-bg").addClass("wc-blue-bg");
	}else{
		$("div#door_default").removeClass("wc-blue-bg").addClass("ws-blue-bg");
	}
}
function mainDoor(check)
{
    var w, h;
    w = parseInt($("input#doorWidth") . val());
    h = parseInt($("input#doorHeight") . val());
    if (w < 400) {
        w = 400;
        message("error", "Ширина двери не может быть менее 400 мм");
        $("input#doorWidth") . val(400);
    }
    if (w > 3000) {
        w = 3000;
        message("error", "Ширина двери не может быть более 3000 мм");
        $("input#doorWidth") . val(3000);
    }
    if (h < 400) {
        h = 400;
        message("error", "Высота двери не может быть менее 400 мм");
        $("input#doorHeight") . val(400);
    }
    if (h > 4000) {
        h = 4000;
        message("error", "Высота двери не может быть более 4000 мм");
        $("input#doorHeight") . val(4000);
    }
    if (w / h > 1) {
        if (check == "w") {
            h = Math . round(w / 1);
            message("error", "Отношение ширины к высоте не может превышать 1 (1:1).");
            $("input#doorHeight") . val(h);
        }else {
            h = 2500;
            w = 1000;
            message("error", "Отношение ширины к высоте не может превышать 1 (1:1). Значения установлены по умолчанию.");
            $("input#doorHeight") . val(2500);
            $("input#doorWidth") . val(1000);
        }
    } else {
        if (h / w > 3) {
            if (check == "h") {
                w = Math . round(h / 3);
                message("error", "Отношение высоты к ширине не может превышать 3 (3:1).");
                $("input#doorWidth") . val(w);
            }else {
                h = 2000;
                w = 3000;
                message("error", "Отношение высоты к ширине не может превышать 3 (3:1). Значения установлены по умолчанию.");
                $("input#doorHeight") . val(2500);
                $("input#doorWidth") . val(1000);
            }
        }
    }
    ratio = w / h;
    sW = 140;
    sH = Math . round(140 / ratio);
    $("div#door_default") . animate({
            height:sH} , 100);
	calculation();
}
/**
 *
 * @access public
 * @return void
 **/
function manageDoor(type)
	{
		switch(type){
			case "left":
				var mainObj=$("div#door_default");
				var wW = parseInt(mainObj . css("width"));
	            var wH = parseInt(mainObj . css("height"));
	            var wL = parseInt(mainObj . css("left"));
	            var wT = parseInt(mainObj . css("top"));
				$("div#doorDraw").empty();
				$("div#doorDraw").drawPolyline([(wL+wW)-8, wL+8,wL+wW-8], [wT+8, (wT+(wH/2)),wT+wH-8], {color:'#FFFFFF'});
				$("div#doorHandle").css({top:(wH)/2-9,left:0});
				$("div#doorHandle").show();
				mainObj.attr("stype","left");
				break;
			case "right":
				var mainObj=$("div#door_default");
				var wW = parseInt(mainObj . css("width"));
	            var wH = parseInt(mainObj . css("height"));
	            var wL = parseInt(mainObj . css("left"));
	            var wT = parseInt(mainObj . css("top"));
				$("div#doorDraw").empty();
				$("div#doorDraw").drawPolyline([wL+8, (wL+wW)-8,wL+8], [wT+8, (wT+(wH/2)),wT+wH-8], {color:'#FFFFFF'});
				$("div#doorHandle").css({top:(wH)/2-9,left:(wL+wW)-8});
				$("div#doorHandle").show();
				mainObj.attr("stype","right");
				break;
			case "topright":
				var mainObj=$("div#door_default");
				var wW = parseInt(mainObj . css("width"));
	            var wH = parseInt(mainObj . css("height"));
	            var wL = parseInt(mainObj . css("left"));
	            var wT = parseInt(mainObj . css("top"));
				$("div#doorDraw").empty();
				$("div#doorDraw").drawPolyline([wL+8, (wL+wW)-8,wL+8], [wT+8, (wT+(wH/2)),wT+wH-8], {color:'#FFFFFF'});
				$("div#doorDraw").drawPolyline([wL+8, wL+(wW/2),wL+wW-8], [wT+wH-8, (wT+8),wT+wH-8], {color:'#FFFFFF'});
				$("div#doorHandle").css({top:(wH)/2-9,left:(wL+wW)-8});
				$("div#doorHandle").show();
				mainObj.attr("stype","flipright");
				break;
			case "topleft":
				var mainObj=$("div#door_default");
				var wW = parseInt(mainObj . css("width"));
	            var wH = parseInt(mainObj . css("height"));
	            var wL = parseInt(mainObj . css("left"));
	            var wT = parseInt(mainObj . css("top"));
				$("div#doorDraw").empty();
				$("div#doorDraw").drawPolyline([(wL+wW)-8, wL+8,wL+wW-8], [wT+8, (wT+(wH/2)),wT+wH-8], {color:'#FFFFFF'});
				$("div#doorDraw").drawPolyline([wL+8, wL+(wW/2),wL+wW-8], [wT+wH-8, (wT+8),wT+wH-8], {color:'#FFFFFF'});
				$("div#doorHandle").css({top:(wH)/2-9,left:0});
				$("div#doorHandle").show();
				mainObj.attr("stype","flipleft");
				break;
				break;
			default:
				;
		} // switch
		calculation();
	}
	function workspace(type){
	unFix();
		switch(type){
			case "plastic":
				$("div#mainDoorInner").hide();
				$("tr#doorSelect").show();
				$("td#windowConfig").show();
				$("td#windowConfig2").show();
				$("#doptions").show();
				$("#mdoor").hide();
				$("#actstv").show();
				$("#showAll").show();
				$("#doptionsresh").hide();
				break;
			case "wood":
				$("div#mainDoorInner").hide();
				$("tr#doorSelect").hide();
				$("td#windowConfig").show();
				$("td#windowConfig2").hide();
				$("#doptions").show();
				$("#mdoor").hide();
				$("#actstv").show();
				$("#showAll").show();
				$("#doptionsresh").hide();
				break;
			case "grid":
				$("div#mainDoorInner").hide();
				$("td#windowConfig2").hide();
				$("td#windowConfig").hide();
				$("#doptions").hide();
				$("#mdoor").hide();
				$("#actstv").hide();
				$("#showAll").hide();
				$("#doptionsresh").show();
				break;
			default:
				;
		}
	}
	function showDoor(val){
		if (val=="ok") {
			$("div#mainDoorInner").fadeIn();
			$("#mdoor").fadeIn();
		}else{
			$("div#mainDoorInner").fadeOut();
			$("#mdoor").fadeOut();
		}
		calculation();
	}
	/**
	 Венец всего труда, функция калькуляции!
	 **/
	function calculation(){
		var val=$("#wType").val();
		w = parseInt($("input#mainWidth") . val());
        h = parseInt($("input#mainHeight") . val());
		$("#sendText").empty();
		var text='';
		switch(val){
			case "plastic":
				// окно
				text+="Тип окна: Пластиковое размер: "+w+"x"+h+"<br>\n";
				s=w*h/1000000*cena_za_kvmp;
				// створки
				text+="Створки:<br>\n";
				$("div#mainWindowInner div.ws-blue-bg, div#mainWindowInner div.ws-nets-bg,div#mainWindowInner div.wc-blue-bg, div#mainWindowInner div.wc-nets-bg").each(function (i) {
					s1=$(this).attr("Wwidth");
					s2=$(this).attr("Wheight");
					stype=$(this).attr("stype");

					switch(stype){
						case "left":
							text+="Тип створки: Поворотная створка размер: "+s1+"x"+s2+"<br>\n";
							s=s+s1*s2/1000000*cena_za_kvmp_ps;
							break;
						case "right":
							text+="Тип створки: Поворотная створка размер: "+s1+"x"+s2+"<br>\n";
							s=s+s1*s2/1000000*cena_za_kvmp_ps;
							break;
						case "flipright":
							text+="Тип створки: Наклонно-поворотная створка размер: "+s1+"x"+s2+"<br>\n";
							s=s+s1*s2/1000000*cena_za_kvmp_ps+cena_za_kvmp_nps;
							break;
						case "flipleft":
							text+="Тип створки: Наклонно-поворотная створка размер: "+s1+"x"+s2+"<br>\n";
							s=s+s1*s2/1000000*cena_za_kvmp_ps+cena_za_kvmp_nps;
							break;
						case "flip":
							text+="Тип створки: Откидная створка размер: "+s1+"x"+s2+"<br>\n";
							s=s+s1*s2/1000000*cena_za_kvmp_os;
							break;
						case "deaf":
							text+="Тип створки: глухая размер: "+s1+"x"+s2+"<br>\n";
							break;
						default:
							;
					} // switch
					if ($(this).attr("class")=="ws-nets-bg" || $(this).attr("class")=="wc-nets-bg") {
						s=s+cena_za_kvmms;
						text+="Москитная сетка размер: "+s1+"x"+s2+"<br>\n";
					}
				});
				//	балконная дверь
				s1 = parseInt($("input#doorWidth") . val());
   				s2 = parseInt($("input#doorHeight") . val());
  				stype=$("#door_default").attr("stype");
				if ($("#doorS").val()=="ok") {
					text+="Балконная дверь размер: "+s1+"x"+s2+"<br>\n";
					s=s+s1*s2/1000000*cena_za_kvd;
				}

				// подоконник
				shp=$("#podok").val();
				if (shp>0) {
					text+="Подоконник размер: "+shp+" (см)<br>\n";
					eval("s=s+(w+200)/1000*cena_za_pp"+shp);
				}
				// решётка
				www=$("#reshtype").val();
				if (www>0) {
					text+="Решётка, тип "+$("#reshtype option[value='"+$("#reshtype").val()+"']").html()+"<br>\n";
					s=s+w*h/1000000*$("#reshtype").val();
				}// монтаж
				if ($("#wMon").val()==1) {
				text+="Монтаж: Да<br>\n";
					s=s+w*h/1000000*cena_za_mkvmp;
					//s=s+w*h/1000000*cena_za_mkvmr;
				}else{text+="Монтаж: Нет<br>\n";}
				break;
			case "wood":
			   text+="Тип окна: Деревянное размер: "+w+"x"+h+"<br>\n";
				shp=$("#podok").val();
				// окно
				s=w*h/1000000*cena_za_kvmd;
				// подоконник
				if (shp>0) {
					text+="Подоконник размер: "+shp+" (см)<br>\n";
					s=s+(w+200)*(shp*10)/1000000*cena_za_kvmd_p;
				}

				// монтаж
				if ($("#wMon").val()==1) {
					text+="Монтаж: Да<br>\n";
					s=s+(s/100*cena_za_mkvmd);
					//s=s+w*h/1000000*;
					//s=s+w*h/1000000*cena_za_mkvmr;
				}else{text+="Монтаж: Нет<br>\n";}

				// решётка
				www=$("#reshtype").val();
				if (www>0) {
					text+="Решётка, тип "+$("#reshtype option[value='"+$("#reshtype").val()+"']").html()+"<br>\n";
					s=s+w*h/1000000*$("#reshtype").val();
				}
				break;
			case "grid":
				// решётка
				s=w*h/1000000*$("#reshtype2").val();
				text+="Решётка, тип "+$("#reshtype2 option[value='"+$("#reshtype2").val()+"']").html()+"<br>\n";
				// монтаж
				if ($("#wMon").val()==1) {
					text+="Монтаж: Да<br>\n";
					s=s+w*h/1000000*cena_za_mkvmr;
				}else{text+="Монтаж: Нет<br>\n";}
				break;
			default:
				;
		} // switch
		$("span#wPrice").html(Math.round(parseInt(s)));
		$("#sendText").html(text);
	}
$(document) . ready(function()
    {
        mainWindow(0); // не зыбыть убрать 1
        mainDoor(); // не зыбыть убрать 1
        message("mes", "Установите и зафиксируйте размеры окна");
    }
);
