﻿(function ($) {

    //wait until document is ready
    $(document).ready(function () {

        //find the searchForm div. Because of "runat=server" its necessary to find it this way.
        var searchForm = $('div[id*="searchForm"]');

        //find the hidden field
        var searchAddress = $('input[id*="searchUrl"]');

        //find the input with type=submit, and execute the code when clicked.
        searchForm.find('input[type="submit"]').click(function (e) {
            e.preventDefault();

            doSearch();

            return false;
        });

        //find the text input by name, and execute code on keypress. Only if keyCode 13 (enter) is pressed,
        //will doSearch() be called.
        searchForm.find('input[name="search"]').keypress(function (e) {
            if (e.keyCode === 13) {
                e.preventDefault();

                doSearch();

                return false;
            }
            return true;
        });


        var doSearch = function () {

            var searchUrl = searchAddress.attr('value'), //look at the searchurl attribute we sat on the div, save it in a variable
        input = searchForm.find('input[name="search"]').val();


            /*
            var searchUrl = searchForm.attr('searchurl'), //look at the searchurl attribute we sat on the div, save it in a variable
        input = searchForm.find('input[name="search"]').val(); //find the value in the textfield*/

            //link to the url sat on the div, with the querystring included.
            window.location.href = searchUrl + '?search=' + encodeURIComponent(input) + "&page=1";
        };
    });







    $(document).on('click.newWin', 'a[rel="newWin"]', function (e) {
        e.preventDefault();

        var link = $(this);

        window.open(link.attr('href'));

        return false;
    });
} (jQuery));
