(function($) {
    $.showMemoryPage = function(options) {
        var defaults = {
            participationRequestText: "Request sent"
        };
        var opts = $.extend(defaults, options);

        var popups = $("#popup-templates").bitPopup();

        $(".memo-lane-content").click(function() {
            popups.show($(this));
            return false;
        });

        var hidden_fullsize_links = $("body").find("a.hidden-fullsize-link");
        hidden_fullsize_links.fancybox({
            "hideOnContentClick": true
        });

        if (document.getElementById("map_canvas") != null){
            var map = new GMap2(document.getElementById("map_canvas"));
            var start_location = GLatLng.fromUrlValue($('#latlng').val());
            var zoom_level = 13;
            map.setCenter(start_location, zoom_level);
            //map.setUIToDefault();
            var edit_marker = new GMarker(start_location, {draggable: false});
            map.addOverlay(edit_marker);
        }

        $("a.favorite").favorite({
            toggleListener: function(favorited) {
                var counters = $(".fancount");
                var value = parseInt($(".fancount:first").text());
                counters.text(favorited ? value+1 : value-1);
            }
        });
        $("#show-more-bit").scrollable({
            items: "#show-more-bit-items",
            prevPage: ".prev-bit",
            nextPage: ".next-bit",
            keyboard: false,
            size: 1
        });
        
        //The layout needs to be done before the height of the container is computed on webkit browsers
        var layout = $("#memo-collage").verticalLayoutRects({
            marginTop: 0,
            containerWidth: null,
            handler: null
        });
        layout.update($(".memo-lane-content", "#memo-collage"));
        
        var childheight = $("#memo-collage").height();
        var parentheight =$(".show-memo-wrap").height();

        if (childheight > parentheight){
            $("a.more-memo").show();
            $("#hor-shade-b").show();
        }       
        $("a.more-memo").click(function(){
           $("this").hide();
           $(".show-memo-wrap").animate({
               height: $("#memo-collage").height()
             },{
                 duration: 400,
                 easing: "easeOutBounce"
             });
            $("a.more-memo").hide();
            $("#hor-shade-b").hide();
            
            return false;
       });

        //when the delete button was clicked
        $("a#delete-memo").click(function(e){
            //Cancel the link behavior
            e.preventDefault();
            //Get the screen height and width
            var maskHeight = $(document).height();
            var maskWidth = $(window).width();
            //Set height and width to mask to fill up the whole screen
            $('#mask').css({'width':maskWidth,'height':maskHeight});
            //transition effect
            $('#mask').fadeIn(1000);
            $("#confirm").fadeIn("fast");
        });
        $("a#cancel").click(function(e){
            e.preventDefault();
            $("#confirm, #mask").hide();
        });
        //show share panel
        $("#share-memo").click(function(){
            $("#share-panel").toggle();
        });

        $("embed", ".videolink-bit").attr({"width":"320", "height":"268"});
       
        $("a#in-memo").click(function(){
            if($('#choose-media').is(':hidden')){
                $('#choose-media').show();
            }else{
                $('#choose-media').hide();
            }
            return false;
        });
        $('#choose-media').click(function(e){
            e.stopPropagation();
        });
        $(document).click(function(){
            $('#choose-media').hide();
        });

        $("div#share-box a.remove").click(function() {
            $("div#share-box").slideUp("slow");
        });

        $("a.share-f").click(function(){
            $(this).siblings("div.facebook-share-dialog").show();
            return false;
        });

        $("a.close", "div.facebook-share-dialog").click(function(){
            $("div.facebook-share-dialog").hide();
            return false;
        });
        
        $("div#facebook-share-dialog .righty").click(function(){
            $.post(
                $("div#facebook-share-dialog").attr("data-share_link"),
                {"data":"true"}
            ), "json";
        });

        $("a.make-public-link").click(function(){
            var href = $("a.make-public-link").attr("href");
            var element = $(this);
            $.post(
                href, 
                {"privacy":"public"},
                function(data){
                    if (data.success){
                        element.hide();
                        element.siblings(".righty").show();
                    }
                }, "json");
            return false;
        });

        $("a.share-email").click(function(){
            $(this).siblings("div.email-share-dialog").show();
            return false;
        });

        $("a.close", "div.email-share-dialog").click(function(){
            $(this).parent().hide();
        });

        $("a#participation-request").participated({
            text: opts.participationRequestText
        });

        $("#email-share-form").ajaxForm({
            dataType: 'json',
            success: shareJsonHandler
        });

        function shareJsonHandler(json) {
            $.handleJSON(json);
            $("#email-share-dialog").hide();
        }

        //Fixes for vimeo video
        $.each($("embed"), function(n, val){
            var source = $(val).attr("outerHTML");
            if (!source || source == undefined){
                $(val).attr("wmode", "transparent");
            } else {
                if(source.search("wmode") == -1){
                    $(val).attr("outerHTML", source.replace(">", " wmode=transparent>"));
                }
            }
        });
    };
})(jQuery);

