﻿function roundTo(value, digits) {
    return parseFloat(Math.round(value * Math.pow(10, -digits)) / Math.pow(10, -digits));
}

$(function () {
    //initializeFancyBox();
    //initializeMbContainers();
    initializeShoppingCart();
    initializeShowCompatible();
    searchElement($("#Query"));
    initializeRegister();
    initializeOrganizationInfoType();
    initializeHiddenUrls();
});

function initializeRegister() {
    function switchRegistrationType() {
        var value = $("input[name=RegistrationType]:radio:checked");
        if (value == "Person") {
            $(".register.view fieldset:gt(0)").attr("style", "display: none;");
            $(".register.view fieldset:eq(3)").removeAttr("style");
        }
        else {
            $(".register.view fieldset").removeAttr("style");
        }
    }
    $(".register.view fieldset:gt(0)").attr("style", "display: none;");
    switchRegistrationType();
    $("input[name=RegistrationType]:radio").click(switchRegistrationType());
}

function initializeOrganizationInfoType() {
    function switchRegistrationType() {
        $(".register.view fieldset.Organization > p").attr("style", "display: none;");
        var value = $("input[name=OrganizationType]:radio:checked").val();

        if (value == "Form") {
            $(".register.view  fieldset.Organization > p:lt(12)").removeAttr("style");
        } else if (value == "Document") {
            $(".register.view fieldset.Organization > p:last").removeAttr("style");
        }
    }
    switchRegistrationType();
    $("input[name=OrganizationType]:radio").click(switchRegistrationType);
}

function searchElement(element) {
    var placeholder = element.attr("placeholder");
    if (!element.val()) {
        element.addClass("off").val(placeholder);
    }
    element
        .focus(function () {
            if ($(this).hasClass("off")) {
                $(this).removeClass("off").val("");
            }
        })
        .blur(function () {
            if (!$(this).val().replace(/\s+/g, '')) {
                $(this).addClass("off").val(placeholder);
            }
        });
}

function initializeShowCompatible() {
    $("tr.Product > td.Compatible > a").click(function (e) {
        var id = $(this).attr("href").match(/\d+$/);
        window.open('/products/cartridges/compatibleinfo/' + id, 'Compatible', 'width=200, height=360, scrollbars=no');
        e.preventDefault();
    });
}

function initializeShoppingCart() {
    $(".shopping-cart").shoppingCart({
            maxValue: 99,
            spinButtonWidth: 34,
            spinButtonHeight: 15
        });
}

function initializeHiddenUrls() {
    //alert($("a[data-href]").length);

    $("a[data-href]").each(function (index) {
        $(this).attr("href", $(this).attr("data-href"));
    });
    
    
}

function liveInternetCounter() {
    document.write(
        "<a href='http://www.liveinternet.ru/click' "
        + "target=_blank><img src='http://counter.yadro.ru/hit?t52.6;r"
        + escape(document.referrer)
        + ((typeof (screen) == "undefined") ? "" : ";s"
        + screen.width
        + "*"
        + screen.height
        + "*"
        + (screen.colorDepth ? screen.colorDepth : screen.pixelDepth))
        + ";u"
        + escape(document.URL)
        + ";" + Math.random()
        + "' alt='' title='LiveInternet: показано число просмотров и посетителей за 24 часа' "
        + "border='0' width='88' height='31'><\/a>");
}
