(function(context) {

    describe("searcher.js tests", function() {

        beforeEach(function() {
            JKTestUtils.loadFixtures('/spec/javascripts/fixtures/searcher.htm');
        });

        describe("Empty Search", function() {

            // See the markup in fixtures/searcher.htm

            var searcher;
            var ajaxSpy;

            var fakeApp = {
                ajaxError: function() {
                    console.debug("ajaxError");
                }
            };

            beforeEach(function() {
                spyOn($, "ajax").andCallFake(function(opts) {
                    opts.success(TestResponses.emptySearch);
                });
                searcher = new JK.Searcher(fakeApp);
                searcher.initialize();
            });

            it("No Results message shown", function() {
                // Workaround for key events not being reflected in val() calls
                $('.searchtextinput').val('AA');
                var e = jQuery.Event("keyup");
                e.which = 65;   // "a"
                $('.searchtextinput').focus();
                $('.searchtextinput').trigger(e);
                $('.searchtextinput').trigger(e);

                expect($('.searchresults .emptyresult').length).toEqual(1);
            });
        });

        describe("Full Search", function() {

            // See the markup in fixtures/searcher.htm

            var searcher;
            var ajaxSpy;

            var fakeApp = {
                ajaxError: function() {
                    console.debug("ajaxError");
                }
            };

            beforeEach(function() {
                spyOn($, "ajax").andCallFake(function(opts) {
                    opts.success(TestResponses.fullSearch);
                });
                searcher = new JK.Searcher(fakeApp);
                searcher.initialize();
            });

            it("No Results message shown", function() {
                // Workaround for key events not being reflected in val() calls
                $('.searchtextinput').val('AA');
                var e = jQuery.Event("keyup");
                e.which = 65;   // "a"
                $('.searchtextinput').focus();
                $('.searchtextinput').trigger(e);
                $('.searchtextinput').trigger(e);

                expect($('.searchresults h2').length).toEqual(4);
            });
        });

        describe("Search Tests", function() {

            // See the markup in fixtures/searcher.htm

            var searcher;
            var ajaxSpy;

            var fakeApp = {
                ajaxError: function() {
                    console.debug("ajaxError");
                }
            };

            beforeEach(function() {
                spyOn($, "ajax").andCallFake(function(opts) {
                    opts.success(TestResponses.search);
                });
                searcher = new JK.Searcher(fakeApp);
                searcher.initialize();
            });

            it("first keypress should not search", function() {
                // Workaround for key events not being reflected in val() calls
                $('.searchtextinput').val('A');
                var e = jQuery.Event("keyup");
                e.which = 65;   // "a"
                $('.searchtextinput').focus();
                $('.searchtextinput').trigger(e);
                expect($.ajax.wasCalled).toBe(false);
            });

            it("second keypress should search", function() {
                $('.searchtextinput').val('AA');
                $('.searchtextinput').focus();
                var e = jQuery.Event("keyup");
                e.which = 65;   // "a"
                $('.searchtextinput').trigger(e);
                // trigger again
                $('.searchtextinput').trigger(e);

                expect($.ajax).toHaveBeenCalled();
            });

            it("response div is absolute position", function() {
                $('.searchtextinput').val('AA');
                $('.searchtextinput').focus();
                var e = jQuery.Event("keyup");
                e.which = 65;   // "a"
                $('.searchtextinput').trigger(e);
                $('.searchtextinput').trigger(e);
                expect($('.searchresults').css('position')).toEqual('absolute');
            });

            it("response displayed in results", function() {
                $('.searchtextinput').val('AA');
                $('.searchtextinput').focus();
                var e = jQuery.Event("keyup");
                e.which = 65;   // "a"
                $('.searchtextinput').trigger(e);
                $('.searchtextinput').trigger(e);

                expect($('.searchresults').length).toEqual(1);
                expect($('.searchresults h2').length).toEqual(1);
                expect($('.searchresults li').length).toEqual(1);
                expect($('.searchresults li img').length).toEqual(1);
                expect($('.searchresults li span.text').length).toEqual(1);
                expect($('.searchresults li span.subtext').length).toEqual(1);
            });


            it("response positioned under input", function() {
                $('.searchtextinput').val('AA');
                $('.searchtextinput').focus();
                var e = jQuery.Event("keyup");
                e.which = 65;   // "a"
                $('.searchtextinput').trigger(e);
                $('.searchtextinput').trigger(e);

                expect($('.searchresults').length).toEqual(1);
                var bodyOffset = $('body').offset();
                var inputOffset = $('.searchtextinput').offset();
                var inputHeight = $('.searchtextinput').outerHeight();
                var expectedTop = bodyOffset.top + inputOffset.top + inputHeight;
                var expectedLeft = bodyOffset.left + inputOffset.left;

                var searchResultOffset = $('.searchresults').offset();
                expect(searchResultOffset.top).toEqual(expectedTop);
                expect(searchResultOffset.left).toEqual(expectedLeft);
            });

            it("search results are visible", function() {
                $('.searchtextinput').val('AA');
                $('.searchtextinput').focus();
                var e = jQuery.Event("keyup");
                e.which = 65;   // "a"
                $('.searchtextinput').trigger(e);
                $('.searchtextinput').trigger(e);

                var visible = $('.searchresults').is(':visible');
                expect(visible).toBe(true);
            });

            it("escape key hides search results", function() {
                $('.searchtextinput').val('AA');
                $('.searchtextinput').focus();
                var e = jQuery.Event("keyup");
                e.which = 65;   // "a"
                $('.searchtextinput').trigger(e);
                $('.searchtextinput').trigger(e);

                e = jQuery.Event("keyup");
                e.which = 27; // ESCAPE
                $('.searchtextinput').trigger(e);

                var visible = $('.searchresults').is(':visible');
                expect(visible).toBe(false);
            });
        });

    });
})(window);