var _Packages = {
    package1: {
        banners: [{
            width: 468,
            height: 60
        }],
        is_default: true
    },
    package2: {
        banners: [{
            width: 125,
            height: 125
        },
        {
            width: 300,
            height: 250
        },
        {
            width: 468,
            height: 60
        },
        {
            width: 728,
            height: 90
        }]
    },
    package3: {
        banners: [{
            width: 88,
            height: 31
        },
        {
            width: 120,
            height: 240
        },
        {
            width: 120,
            height: 60
        },
        {
            width: 120,
            height: 90
        },
        {
            width: 120,
            height: 600
        },
        {
            width: 160,
            height: 600
        },
        {
            width: 180,
            height: 150
        },
        {
            width: 200,
            height: 200
        },
        {
            width: 234,
            height: 60
        },
        {
            width: 240,
            height: 400
        },
        {
            width: 250,
            height: 250
        },
        {
            width: 300,
            height: 100
        },
        {
            width: 300,
            height: 250
        },
        {
            width: 300,
            height: 600
        },
        {
            width: 336,
            height: 280
        },
        {
            width: 380,
            height: 200
        },
        {
            width: 468,
            height: 60
        },
        {
            width: 728,
            height: 90
        }]
    }
};
var _default_package = false;
var total = 1;

function initForm() {
    var D = __$("banners");
    var B = __$("add-another-banner");
    var E = __$("first-banner");
    if (B && E) {
        E.style.zIndex = 1000;
        B.href = "#";
        B.onclick = function () {
            addBanner();
            return false
        };
        var A = __$$(D, "div", "banner");
        if (A.length) {
            for (var C = 0; C < A.length; C++) {
                initBanner(A[C])
            }
            calcPrice()
        }
    }
    initPackages();
    initDropDownDoubleInput(__$("package"));
    if (_default_package && !A.length) {
        _default_package._notshow = true;
        _default_package.onclick();
        _default_package._notshow = false
    }
}
var open_dd = false;
var _timeout = false;

function addBanner() {
    var A = null;
    var D = __$("first-banner");
    var B = __$("banners");
    if (D && B) {
        var C = D.innerHTML;
        _div = document.createElement("DIV");
        _div.className = "banner";
        _div.innerHTML = C;
        var A = B.appendChild(_div);
        initBanner(A)
    }
    calcPrice();
    return A
}
function initBanner(C) {
    var D = __$("first-banner");
    total++;
    C.style.zIndex = D.style.zIndex - total + 1;
    initDropDownDoubleInput(C);
    var B = __$$(C, "label", "banner-cnt");
    if (B.length) {
        if (B[0]) {
            B[0].htmlFor = "l-banner" + (total)
        }
    }
    var E = __$$(C, "label", "hidden");
    if (E.length) {
        if (E[0]) {
            E[0].htmlFor = "r-banner" + (total)
        }
    }
    var A = __$$(C, "a", "delete");
    if (A[0]) {
        A[0].href = "javascript:;";
        A[0].onclick = function () {
            var F = this.parentNode;
            F.parentNode.removeChild(F);
            if (!this._notcalc) {
                calcPrice()
            }
            return false
        }
    }
}
function initDropDownDoubleInput(L) {
    if (L) {
        var D = __$$(L, "input");
        if (L.id != "package") {
            if (D[0]) {
                D[0].name = "width[" + total + "]"
            }
            if (D[1]) {
                D[1].name = "height[" + total + "]"
            }
            if (D[0]) {
                D[0].id = "l-banner" + total
            }
            if (D[1]) {
                D[1].id = "r-banner" + total
            }
            if (D[4]) {
                D[4].name = "frames[" + total + "]"
            }
            if (D[2]) {
                D[2].name = "is_rounded[" + total + "]";
                D[2].parentNode.htmlFor = D[2].id = "rounded-" + total
            }
            if (D[3]) {
                D[3].name = "is_animated[" + total + "]";
                D[3].parentNode.htmlFor = D[3].id = "animated-" + total
            }
        }
        var A = __$$(L, "div", "select");
        if (A[0]) {
            var G = __$$(A[0], "ul");
            if (G[0]) {
                G[0].onmouseover = function () {
                    open_dd = this;
                    if (_timeout) {
                        clearTimeout(_timeout)
                    }
                };
                G[0].onmouseout = function () {
                    _timeout = setTimeout("closeDiv()", 200)
                }
            }
            var M = __$$(A[0], "a");
            for (var F = 0; F < M.length; F++) {
                M[F].href = "#";
                if (F == 0) {
                    M[F].removeAttribute("href");
                    if (G[0]) {
                        M[F]._div = G[0]
                    }
                    M[F].onclick = function () {
                        if (this._div) {
                            if (this._div.style.display != "block") {
                                this._div.style.display = "block"
                            } else {
                                this._div.style.display = "none"
                            }
                        }
                        return false
                    };
                    M[F].onmouseover = function () {
                        if (this._div) {
                            this._div.onmouseover()
                        }
                    };
                    M[F].onmouseout = function () {
                        if (this._div) {
                            this._div.onmouseout()
                        }
                    }
                } else {
                    if (A[0].parentNode.id == "package") {
                        M[F]._close_a = M[0];
                        var C = __$$(A[0], "em");
                        if (C[0]) {
                            M[F]._em = C[0];
                            C[0]._a = M[0];
                            C[0].onclick = function () {
                                this._a.onclick();
                                return false
                            };
                            C[0].onmouseover = function () {
                                this._a.onmouseover()
                            };
                            C[0].onmouseout = function () {
                                this._a.onmouseout()
                            }
                        }
                        if (D[0]) {
                            M[F]._inp = D[0]
                        }
                        M[F].onclick = function () {
                            var O = __$$(this, "strong");
                            if (O[0]) {
                                this._em.innerHTML = O[0].innerHTML;
                                if (this.rel) {
                                    var Q = __$("order-package");
                                    if (Q) {
                                        Q.value = _Packages[this.rel]["title"]
                                    }
                                }
                            }
                            if (!this._notshow) {
                                this._close_a.onclick()
                            }
                            var S = __$$(document, "a", "delete");
                            for (var P = 0; P < S.length; P++) {
                                if (S[P].onclick) {
                                    S[P]._notcalc = true;
                                    S[P].onclick()
                                }
                            }
                            total = 1;
                            if (this.rel) {
                                var R = _Packages[this.rel].banners;
                                for (var P = 0; P < R.length; P++) {
                                    fillBannerLine(addBanner(), R[P])
                                }
                            }
                            calcPrice();
                            return false
                        }
                    } else {
                        if (D[0]) {
                            M[F]._winput = D[0]
                        }
                        if (D[1]) {
                            M[F]._hinput = D[1]
                        }
                        M[F]._close_a = M[0];
                        M[F].onclick = function () {
                            var O = __$$(this, "em");
                            if (O.length > 1) {
                                if (O[0]) {
                                    this._winput.value = O[0].innerHTML
                                }
                                if (O[1]) {
                                    this._hinput.value = O[1].innerHTML
                                }
                            } else {
                                this._winput.value = "";
                                this._hinput.value = "";
                                try {
                                    this._winput.focus()
                                } catch (P) {}
                            }
                            this._close_a.onclick();
                            return false
                        }
                    }
                }
            }
        }
        if (A[1]) {
            var B = document.createElement("ul");
            for (var F = 2; F <= 12; F++) {
                var I = document.createElement("li");
                var J = document.createElement("a");
                J.rel = F;
                J.innerHTML = F + " frames";
                var E = I.appendChild(J);
                B.appendChild(I)
            }
            var H = A[1].appendChild(B);
            if (H) {
                H.onmouseover = function () {
                    open_dd = this;
                    if (_timeout) {
                        clearTimeout(_timeout)
                    }
                };
                H.onmouseout = function () {
                    _timeout = setTimeout("closeDiv()", 100)
                }
            }
            var M = __$$(A[1], "a");
            for (var F = 0; F < M.length; F++) {
                M[F].href = "javascript:;";
                if (F == 0) {
                    M[F].removeAttribute("href");
                    if (H) {
                        M[F]._div = H
                    }
                    M[F].onclick = function () {
                        if (this._div) {
                            if (this._div.style.display != "block") {
                                this._div.style.display = "block"
                            } else {
                                this._div.style.display = "none"
                            }
                        }
                        return false
                    };
                    M[F].onmouseover = function () {
                        if (this._div) {
                            this._div.onmouseover()
                        }
                    };
                    M[F].onmouseout = function () {
                        if (this._div) {
                            this._div.onmouseout()
                        }
                    }
                } else {
                    var K = __$$(A[1], "em", "value");
                    var N = __$$(A[1], "input");
                    if (K[0]) {
                        M[F]._em = K[0]
                    }
                    if (N[0]) {
                        M[F]._input = N[0]
                    }
                    M[F]._close_a = M[0];
                    M[F].onclick = function () {
                        this._em.innerHTML = this.innerHTML;
                        this._input.value = this.rel;
                        this._close_a.onclick();
                        calcPrice();
                        return false
                    }
                }
            }
        }
        if (A[1]) {
            var B = document.createElement("ul");
            for (var F = 2; F <= 12; F++) {
                var I = document.createElement("li");
                var J = document.createElement("a");
                J.rel = F;
                J.innerHTML = F + " frames";
                var E = I.appendChild(J);
                B.appendChild(I)
            }
            var H = A[1].appendChild(B);
            if (H) {
                H.onmouseover = function () {
                    open_dd = this;
                    if (_timeout) {
                        clearTimeout(_timeout)
                    }
                };
                H.onmouseout = function () {
                    _timeout = setTimeout("closeDiv()", 100)
                }
            }
            var M = __$$(A[1], "a");
            for (var F = 0; F < M.length; F++) {
                M[F].href = "javascript:;";
                if (F == 0) {
                    M[F].removeAttribute("href");
                    if (H) {
                        M[F]._div = H
                    }
                    M[F].onclick = function () {
                        if (this._div) {
                            if (this._div.style.display != "block") {
                                this._div.style.display = "block"
                            } else {
                                this._div.style.display = "none"
                            }
                        }
                        return false
                    };
                    M[F].onmouseover = function () {
                        if (this._div) {
                            this._div.onmouseover()
                        }
                    };
                    M[F].onmouseout = function () {
                        if (this._div) {
                            this._div.onmouseout()
                        }
                    }
                } else {
                    var K = __$$(A[1], "em", "value");
                    var N = __$$(A[1], "input");
                    if (K[0]) {
                        M[F]._em = K[0]
                    }
                    if (N[0]) {
                        M[F]._input = N[0]
                    }
                    M[F]._close_a = M[0];
                    M[F].onclick = function () {
                        this._em.innerHTML = this.innerHTML;
                        this._input.value = this.rel;
                        this._close_a.onclick();
                        calcPrice();
                        return false
                    }
                }
            }
        }
        initAnimatedChecked(L)
    }
}
function initLabels() {
    var C = __$("banners");
    var B = __$$(C, "label", "banner-cnt");
    for (var A = 0; A < B.length; A++) {
        if (B.length == 1) {
            B[A].innerHTML = "Banner"
        } else {
            if (A < 9) {
                B[A].innerHTML = "Banner <span>0" + (A + 1) + "</span>"
            } else {
                B[A].innerHTML = "<span>" + (A + 1) + "</span> Banner"
            }
        }
    }
    var D = __$$(C, "a", "delete");
    if (D.length) {
        if (D.length > 1) {
            D[0].style.display = "block"
        } else {
            D[0].style.display = "none"
        }
    }
}
function initPackages() {
    var G = __$("package");
    if (_Packages && G) {
        var F = __$$(G, "div", "select");
        var D = __$("order-package");
        var I = __$$(G, "em");
        if (D && D.value && I) {
            I[0].innerHTML = D.value
        }
    }
}
function initAnimatedChecked(C) {
    var A = __$$(C, "input");
    var B = __$$(C, "div", "select");
    if (A[3]) {
        if (B[1]) {
            A[3]._select = B[1]
        }
        A[3].onchange = A[3].onclick = function () {
            if (this._select) {
                if (this.checked) {
                    this._select.style.visibility = "visible"
                } else {
                    this._select.style.visibility = "hidden"
                }
            }
            calcPrice()
        }
    }
}
function fillBannerLine(E, D) {
    var A = __$$(E, "input");
    var C = __$$(E, "div", "select");
    if (A[0] && D.width) {
        A[0].value = D.width
    }
    if (A[1] && D.height) {
        A[1].value = D.height
    }
    if (A[2]) {
        if (D.is_rounded) {
            A[2].checked = true
        } else {
            A[2].checked = false
        }
    }
    if (A[3]) {
        if (D.is_animated) {
            A[3].checked = true;
            if (C[1]) {
                C[1].style.visibility = "visible"
            }
        } else {
            A[3].checked = false;
            if (C[1]) {
                C[1].style.visibility = "hidden"
            }
        }
        if (D.frames && A[4] && C[1]) {
            A[4].value = D.frames;
            var B = __$$(C[1], "em", "value");
            if (B[0]) {
                B[0].innerHTML = D.frames + " frames"
            }
        }
    }
}
function calcPrice() {
    var F = __$("banners");
    var E = __$$(F, "span", "price");
    var C = __$("total-price");
    var PRICE = __$("total_price");
    var B = 0;
    if (E.length > 0 && C) {
        for (var D = 0; D < E.length; D++) {
            var A = __$$(E[D].parentNode, "input");
            if (A[3] && A[3].checked && A[4]) {
                B += (parseInt(_Price["default"]) + (parseInt(A[4].value) - 1) * parseInt(_Price.perframe));
                E[D].innerHTML = "$" + (parseInt(_Price["default"]) + (parseInt(A[4].value) - 1) * parseInt(_Price.perframe))
            } else {
                B += parseInt(_Price["default"]);
                E[D].innerHTML = "$" + parseInt(_Price["default"])
            }
        }
        C.innerHTML = "$" + B
        PRICE.value = B;
    }
    initLabels()
}
function closeDiv() {
    if (open_dd) {
        open_dd.style.display = "none"
    }
}
function __$(A) {
    return document.getElementById(A)
}
function __$$(D, A, B) {
    var F = [];
    if (D) {
        var E = D.getElementsByTagName(A);
        for (var C = 0; C < E.length; C++) {
            if (B) {
                if (E[C].className.indexOf("" + B) != -1) {
                    F[F.length] = E[C]
                }
            } else {
                F[F.length] = E[C]
            }
        }
    }
    return F
}
function initOrderFile() {
    var A = $("order-file");
    var B = $("order-source");
    if (A) {
        A.addEvent("change", function () {
            var C = A.getProperty("value");
            B.setProperty("value", C.substring(C.lastIndexOf("\\") + 1, C.length))
        })
    }
}
window.addEvent("domready", function () {
    initForm();
});
