function createCookie(a, e, d) {
    if (d) {
        var c = new Date;
        c.setTime(c.getTime() + d * 864E5);
        d = "; expires=" + c.toGMTString()
    } else
        d = "";
    document.cookie = a + "=" + e + d + "; path=/"
}
function readCookie(a) {
    a += "=";
    for (var e = document.cookie.split(";"), d = 0; d < e.length; d++) {
        for (var c = e[d]; c.charAt(0) == " "; )
            c = c.substring(1, c.length);
        if (c.indexOf(a) == 0)
            return c.substring(a.length, c.length)
    }
    return null
}
function setSizeVars() {
    width = $(window).width() - 12;
    var a = Math.max(readCookie("imagesize") * 1, 140);
    sideNavw = 160;
    gutterw = 10;
    b = width - sideNavw;
    browsew = b - b % (a + 2 * gutterw);
    mainw = browsew + sideNavw;
    ie6 == 1 && (mainw += 30, browsew += 5)
}
function setRecentlyViewed(a) {
    $.post("/ajax/set-recently-viewed-product", {id: a})
}
function getRecentlyViewed() {
    $.getJSON("/ajax/get-recently-viewed-products", function(a) {
        var e = '<div id="rh"><h4>Recently Viewed</h4> <a class="r" href="#" id="deleteRecent">[Clear list]</a></div>', d = !1, c = (Math.max($("#nav1").width(), $(".fp3").width()) - 30) / 100 - 1, f;
        for (f in a)
            d = !0, f < c && (e += '<div class="rv"><a href="' + a[f].url + '"><img src="' + a[f].img + '" width="90" height="90" alt=""></a></div>');
        d && ($("#recent").html(e), $("#recentContainer").show(), $("a#deleteRecent").live("click", function() {
            $.post("/ajax/delete-recently-viewed-products");
            $("#recentContainer").fadeOut();
            return !1
        }))
    })
}
function doResize() {
}
function updateTotal() {
    var a = 0;
    $(".lineItem").each(function() {
        a += parseFloat($(this).text())
    });
    $(".lineDiscount").each(function() {
        a -= parseFloat($(this).text())
    });
    $("#total").text(a.toFixed(2))
}
var closeMenuClick = function(a) {
    $(a.target).parents("#login").size() == 0 && $("#login>a").click();
    a.stopPropagation()
};
$("form.autosubmit").submit();
$(document).ready(function() {
    $("a.new-window").click(function() {
        window.open(this.href);
        return !1
    });
    var a = !0;
    $("#login>a").click(function() {
        a ? ($("#loginBox").show(), $("#login").css("background-color", "#eee"), $(document).bind("click", closeMenuClick), a = !1) : ($("#loginBox").hide(), $("#login").css("background-color", "#fff"), $(document).unbind("click", closeMenuClick), a = !0);
        return !1
    });
    $("#updater").hide();
    $("#discountCode").parent().append(' <input type="submit" name="update" id="dcUpdate" value="Update">');
    $("#dcUpdate").click(function() {
        value = $(this).parent().next();
        dc = $(this).parent().find("[name='discountCode']").val();
        $.ajax({type: "POST",url: "/basket/update",data: "ajax=1&discountCode=" + dc,success: function(a) {
                value.html('-&pound;<span id="discount" class="lineDiscount">' + a + "</span>");
                updateTotal()
            }});
        return !1
    });
    $(".basketDel").click(function() {
        link = $(this);
        url = link.attr("href");
        $.ajax({type: "GET",url: url,success: function() {
                link.parent().parent().remove();
                $("#country_code").change()
            }});
        return !1
    });
    $("#country_code").change(function() {
        country = $(this).parent().find("[name='country_code']").val();
        $.ajax({type: "POST",url: "/basket/update",data: "ajax=1&country_code=" + country,success: function(a) {
                $("#postageCost").html(a);
                updateTotal()
            }});
        return !1
    });
    var e = [, 146, 190, 258];
    $("#size").change(function() {
        var a = $("#size").val();
        $(".product").removeClass("p150 p190 p258").addClass("p" + e[a]);
        $(".im>a>img").each(function() {
            $(this).attr("height", e[a]).attr("width", e[a])
        });
        $(".im>a>img").each(function() {
            var c = 
            [], c = $(this).attr("src").split("/");
            c[3] == "static" ? c[4] = e[a] : c[3] = e[a];
            $(this).attr("src", c.join("/"))
        });
        createCookie("imagesize", e[a], 30);
        doResize()
    });
    $(".quantity").keyup(function() {
        var a = $(this), c = parseFloat($(this).val());
        if (c > 0) {
            var d = $(this).attr("id").substring(12), e = $(this).parent().parent();
            $.ajax({type: "POST",url: "/basket/update",data: "ajax=1&id=" + d + "&qty=" + c,success: function(d) {
                    a.val(d);
                    c > d && alert("Sorry, only " + d + " available");
                    var g = parseFloat(e.find(".itemPrice").html());
                    g *= d;
                    e.find(".lineItem").html(g.toFixed(2));
                    $("#country_code").change()
                }})
        }
        return !1
    });
    $("#size").val(imageSize);
    $(window).resize(function() {
        doResize()
    });
    doResize();
    readCookie("hz") != null && $("#recent").length && getRecentlyViewed();
    $("#PostageContainer").show();

    $('input[name="ShipTo_CustomerAddressId"]').length && ($('input[name="ShipTo_CustomerAddressId"]:checked').val() !== "0" && $("#fieldset-ShipToAddress").hide(), $('input[name="ShipTo_CustomerAddressId"]').change(function() {
        $(this).val() == 0 ? $("#fieldset-ShipToAddress").show() : $("#fieldset-ShipToAddress").hide()
    })//,

//    $('input[name="BillTo_CustomerAddressId"]:checked').val() !== "0" && $("#fieldset-BillTo > .element").hide(), $('input[name="BillTo_CustomerAddressId"]').change(function() {
//        $(this).val() == 0 ? $("#fieldset-BillTo > .element").show() : $("#fieldset-BillTo > .element").hide()
//    })
    );

    $('input[name="BillTo_CustomerAddressId"]').length && ($('input[name="BillTo_CustomerAddressId"]:checked').val() !== "0" && $("#fieldset-BillTo > .element").hide(), $('input[name="BillTo_CustomerAddressId"]').change(function() {
        $(this).val() == 0 ? $("#fieldset-BillTo > .element").show() : ($("#fieldset-BillTo > .element").hide())
    })
    );

    var d = $("#productId").val();
    typeof d != "undefined" && setRecentlyViewed(d);
    $("#BillTo>div.element").toggleClass("disabled");
    if ($("#BillTo_UseShipToAddress").length) {
        var c = 
        function() {
            $("#BillTo_UseShipToAddress").is(":checked") ? ($("#BillToAddress>div.element>input,#BillToAddress>div.element>select").attr("disabled", "disabled"), $("#BillToAddress>div.element").toggleClass("disabled"), $("#BillToAddress>ul.errors").hide(), $("#BillToAddress input").removeClass("error")) : ($("#BillToAddress>div.element>input,#BillToAddress>div.element>select").attr("disabled", ""), $("#BillToAddress>div.element").toggleClass("disabled"))
        };
        c();
        $("#BillTo_UseShipToAddress").click(function() {
            c()
        })
    }
});

