$(function() {



    $('#saveBrochure').click(function() {
        if (isAuthenticated) {
            $.post("/user/saveuserbrochure",
                    { brochureId: $(this).children('img').attr('ID').split('_')[1] },
                    function() {
                        $('#saveBrochure').hide();
                        $('#removeBrochure').show();
                        OmnitureAction("event8", "eVar21", "Brochure Saved");
                    }
                );
        } else {
            ShowLogin();
        }
    });

    $('#removeBrochure').click(function() {
        if (isAuthenticated) {
            $.post("/user/deleteuserbrochure",
                    { brochureId: $(this).children('img').attr('ID').split('_')[1] },
                    function() {
                        $('#saveBrochure').show();
                        $('#removeBrochure').hide();
                    }
                );
        } else {
            ShowLogin();
        }
    });

    $('#printBrochure').click(function() {
        OmnitureAction("event8", "eVar21", "Brochure Printed");
        return true;
    });

    $('.list > li').click(function() {
        if (this.hasClass("hidden")) {
            this.removeClass("hidden");
        }
    });

    $('.contact-agent').click(function() {
        goToAnchor("Contact");
        $("#SenderName").focus();
        return false;
    });

    $('.multi-contact-agent').click(function() {
        var agentID = $(this).attr('name');
        $("#AgentID").val(agentID);
        goToAnchor("Contact");
        $("#SenderName").focus();
        return false;
    });

    $('#sendToFriendLink').click(function() {
        var properties = $(this).attr('name').split("_");
        var propertyClass = properties[0];
        var propertyID = properties[1];
        var AgentID = properties[2]; 
        $.fn.colorbox(
                { href: '/sendtoafriend.cfm?pid=' + AgentID + '&aid=' + propertyID, open: true, opacity: .15, title: ' ' },
                function() { $("#RecipientEmail").focus(); }
            );
    });

    $("#openGallery").click(function() {
        $(".brochureSlideshow").eq(0).trigger("click");
    });

    $('.brochureSlideshow').click(function() {
        var index = $('.brochureSlideshow').index(this);

        if ($('a[rel="gallery"]').size() == 0) {
            
            var elements;
            for (var i = 0; i < images.length; i++) {
                elements += "<a style='display:none' rel='gallery' href='" + images[i][0] + "' />";
            }

            $(elements).appendTo('body').colorbox({ transition: 'elastic', speed: 500, opacity: .25, close: 'close', innerHeight: "315px", innerWidth: "420px", scalePhotos: true });
        }

        $('a[rel="gallery"]').eq(index).click();
    });
});
