(function($) {
    $(document).delegate('body', 'keyup', function(){
        $('input, button, a, [tabindex]').keynav('keynav','keynav');
    });
    $(document).delegate('body', 'keydown', function(){
        $('input, button, a, [tabindex]').keynav('keynav','keynav');
    });
    $(document).delegate('body', 'keyleft', function(){
        $('input, button, a, [tabindex]').keynav('keynav','keynav');
    });
    $(document).delegate('body', 'keyright', function(){
        $('input, button, a, [tabindex]').keynav('keynav','keynav');
    });

     $(document)
        .delegate(
            '[data-role="page"]:has(.gallery)',
            {
                "pageshow" : function(e){
                    var currentPage = $(e.target),
                        options = {
                            captionAndToolbarShowEmptyCaptions : false
                        },
                        items = $('.gallery a')
                            .find('img')
                            .attr('alt','')
                            .end(),
                        photoSwipeInstance = $(items, e.target).photoSwipe(options,  currentPage.attr('id'));

                    return true;
                },
                "pagehide" : function(e){
                    var currentPage = $(e.target),
                        photoSwipeInstance = Code.PhotoSwipe.getInstance(currentPage.attr('id'));

                    if (typeof photoSwipeInstance != "undefined" && photoSwipeInstance != null) {
                        Code.PhotoSwipe.detatch(photoSwipeInstance);
                    }

                    return true;

                }
            }
        );




})(jQuery);

