function myPicsX(what, param) {
    if (what == "stop") {
        if (typeof(param) === "undefined") window.myPicsStop = !window.myPicsStop;
        else window.myPicsStop = param;
        if (window.myPicsStop) {
            $("#picn>a").eq(0).addClass("xactive").children("img").attr("src", "/images/tplay.gif");
        } else {
            $("#picn>a").eq(0).removeClass("xactive").children("img").attr("src", "/images/tstop.gif");
        }
    }
    if (what == "pause") {
        if (typeof(param) === "undefined") window.myPicsPause = !window.myPicsPause;
        else window.myPicsPause = param;
    }
    return false;
}

function myPics(i) {
    var $pics = $("#pics>p");
    var $picn = $("#picn>a");
    var n = $pics.length;
    if (!n) return false;
    if (!i) {
        window.setTimeout(function() { myPics(0) }, 2000);
        if (window.myPicsStop || window.myPicsPause) return false;
        i = window.myPicsN % n + 1;
    }
    window.myPicsN = i;
    $picn.removeClass("active").eq(n + 1 - i).addClass("active");
    $pics.eq(i - 1).show();
    $pics.filter(".active").fadeOut("slow", function() {
        $(this).removeClass("active");
        var href = $pics.eq(i - 1).addClass("active").children("a").attr("href");
        if (href) $("#pics-href").attr("href", href);
    });
    return false;
}

function initPics() {
    window.myPicsN = 1;
    window.myPicsStop = false;
    window.myPicsPause = false;
    var $pics = $("#pics>p");
    for (var i = 0; i < $pics.length; i ++) {
        if (i) {
            $pics.eq(i).hide();
        } else {
            var href = $pics.eq(i).addClass("active").children("a").attr("href");
            if (href) $("#pics-href").attr("href", href);
        }
    }
    window.setTimeout(function() { myPics(0) }, 2000);
}

function initCalc() {

    $("#cw").hide();
    $(".down, .cbsub a").click(function() {
        $.cookie('display-calc', $("#cw").is(":visible") ? "0" : "1", { path: "/" }); // калькулятор будет видимым
        $("#cw").slideToggle("fast");
        return false;
    });
    $(".csub").click(function() {
        $("#calc-result").html("<img src=\"/images/loading.gif\">");
        $.post(
            "/SiteNN/request/?a=calc",
            {
                dw1: $("#dw1").val(),
                sw1: $("#sw1").val(),
                df1: $("#df1").val(),
                sf1: $("#sf1").val(),
                d2: $("#d2").val(),
                d3: $("#d3").val(),
                d4: $("#d4").val(),
                s1: $("#s1").val(),
                s2: $("#s2").val(),
                s3: $("#s3").val(),
                s4: $("#s4").val(),
                h: $("#h").val(),
                door: $("#door").attr("checked") ? "Y" : "",
                window: $("#window").attr("checked") ? "Y" : ""
            },
            function(result) {
                if (result && result.OK) {
                    $("#calc-result").html(result.html);
                } else if (result && result.error) {
                    alert(result.error);
                } else {
                    alert("Ошибка");
                }
            }
        );
        return false;
    });
    var img = new Image;
    img.src = "/images/loading.gif";
}

function initFancy() {
    if (typeof($.fancybox) === "undefined") return setTimeout(arguments.callee, 100);
	$("a.fancy").fancybox({
        overlayShow: true,
        overlayOpacity: 0.5,
        zoomSpeedIn: 1000,
        zoomSpeedOut:600
    });
}

function init() {
    if (typeof($) === "undefined") return setTimeout(arguments.callee, 100);
    $(document).ready(function() {
        initCalc();
        initPics();
		initFancy();
    });
}

init();

