var submit = false;

$(document).ready(function() {
    if ($('#tefProductId').val() > 0)
        tb_show("Share with a friend", "/#TB_inline?height=780&amp;width=708&amp;inlineId=shareWithFriend");
    if ($('#askProductId').val() > 0) {
        tb_show("Ask a question", "/#TB_inline?height=745&amp;width=708&amp;inlineId=askQuestion");
        submit = true;
        updateAskAQuestion();
    }
});

function addItem(id) {
    $.get("productBasketAddProduct.do?productId=" + id);
    tb_show("Item added to cart", "/#TB_inline?height=200&amp;width=708&amp;inlineId=addToCartWarn");
}

function addItemPP() {
    var id = $('#ppProductId').val();
    var pp = $("input[name='paymentPlan']:checked").val();
    $.get("productBasketAddProduct.do?productId=" + id + "&paymentPlan=" + pp);
    tb_remove();
    setTimeout(function() {
        tb_show("Item added to cart", "/#TB_inline?height=200&amp;width=708&amp;inlineId=addToCartWarn");
    }, 250);
}

function updateAskAQuestion() {
    if ($('#TB_window #shippingQuestion').attr('checked')) {
        $('#TB_ajaxContent').css('height', submit?930:830);
        $('#TB_window #askAQuestionAddress').show();
        $('#TB_window').css('margin-top', submit?'-490px':'-440px');
    } else {
        $('#TB_ajaxContent').css('height', submit?745:645);
        $('#TB_window #askAQuestionAddress').hide();
        $('#TB_window').css('margin-top', submit?'-395px':'-345px');
    }
}

function paymentPlanUpdate(id) {
    if (id == null)
        id = $('#ppProductId').val();
    else
        $("#paymentPlan2").attr("checked", "true");
    var payments = $("input[name='paymentPlan']:checked").val();
    if (!(payments > 1)) payments = 2;
    $.getJSON("ajaxPaymentPlan.do?productId=" + id + "&paymentPlan=" + payments, function(data) {
        $("#ppPrice").html(data.price);
        $("#ppTitle").html(data.title);
        $("#ppProductId").val(id);
        for (var i=1; i<=6; i++)
            $("#ppMonth" + i).css("display", "none");
        $("#ppDeposit").html(data.deposit);
        for (var i=1; i <= payments; i++) {
            $("#ppMonth" + i).css("display", "inline");
            if (i < payments)
                $("#ppPayment" + i).html(data.next);
            else
                $("#ppPayment" + i).html(data.last);
        }
    });
}

function tefUpdate(id) {
    $('#tefProductId').val(id);
}

function askUpdate(id) {
    $('#askProductId').val(id);
    $('#askSubject').val($('#productTitle' + id).html() + " / " + id);
    $('#askSubject').blur();
}

function showImage(productId, imageId) {
    $('#mainImage' + productId).attr('src', $('#image' + imageId).attr('src'));
    $('#mainImage' + productId).attr('imageId', imageId);
}

function mainImageClick(productId) {
    Shadowbox.open(document.getElementById('imageLink' + $('#mainImage' + productId).attr('imageId')));
}

function locationUpdate() {
    var $office = $('#officeFilterForm input:checked');
    if ($office.length == 1) {
        document.location = 'filterSearch.do?pageNum=1&office=' + $office.val();
    } else {
        document.location = 'filterSearch.do?pageNum=1&office=0';
    }
}

