BX.ready(function() {
    if (!window.jQuery) {
        BX.loadScript("/bitrix/js/main/jquery/jquery-1.8.3.min.js", function() {
            jQueryLoad();
        });
    } else {
        jQueryLoad();
    }

    function jQueryLoad() {
        BX.loadScript("/bitrix/js/cetacs.propertyimage/jquery.prettyPhoto.js", function() {
            cetacsPropertyImageInit();
        });
    }
});

function cetacsPropertyImageInit() {
    var DOWNLOAD_STR = BX.message("CPI_DOWNLOAD_STR") ? BX.message("CPI_DOWNLOAD_STR") : "Download";

    var ppStyles = [
        "pp_default",
        "light_rounded",
        "dark_rounded",
        "dark_square",
        "light_square",
        "facebook"
    ];
    for (i in ppStyles) {
        var params = {
            show_title: true,
            theme: ppStyles[i],
            animation_speed: 'fast',
            social_tools: ""
        };
        $("a[rel^='prettyPhoto'].cetacs_pp." + ppStyles[i]).prettyPhoto(params);

        params.social_tools = '<a target="_blank" id="pp_download" href="#">' + DOWNLOAD_STR + '</a>';
        params.changepicturecallback = function() {
            $("#pp_download").attr("href", $("#pp_full_res #fullResImage").attr("src"));
        };

        $("a[rel^='prettyPhoto'].cetacs_pp.show_dl." + ppStyles[i]).prettyPhoto(params);
    }

    $("a[rel^='prettyPhoto'].cetacs_pp").hover(function() {
        $(this).find("span").fadeTo(200, 0.5);
    }, function() {
        $(this).find("span").fadeTo(200, 0);
    });
}