﻿

var textbooks = {
    searchLiteral: "Enter Course Name or Course Number",

    // Clears the search input field.  Called by dropdowns
    clearSearch: function()
    {
        $("#searchfield").val("");
    },

    // Checks whether search field has the default value, and if it does, it clears it.
    validateSearch: function()
    {
        if ($("#searchfield").val() == textbooks.searchLiteral)
            textbooks.clearSearch();
    },

    // Clears or sets the default value depending on whether we are stepping in or out of the input field.
    toggleSearchField: function(event)
    {
        // Stepping into
        if (event.type == "focus")
        {
            if ($("#searchfield").val() == textbooks.searchLiteral)
            {
                $("#searchfield").val("");
                $("#searchfield").removeClass("default-value");
            }
        }
        else  // Stepping out (blur)
        {
            if ($("#searchfield").val() == "")
            {
                $("#searchfield").val(textbooks.searchLiteral);
                $("#searchfield").addClass("default-value");
            }
        }
    },

    // This is called only for IE browsers.  This makes sure that when we step out of the dropdown, we set it to its default width.
    selectBlur: function()
    {
        //if ($.browser.version <= 6.0 && event.type != "change")
        //    $(this).attr("selectedIndex", $(this).attr("selectedIndex"));
        $(this).css("width", "");
    },

    // This is called only for IE browsers.  IE browsers chop off any text in the dropdown list that is longer than the
    // dropdown itself.  This is a very simple fix for this. 
    // This makes sure that when we step into the dropdown, we set its width to auto so it can resize.
    selectFocus: function()
    {
        $(this).css("width", "auto");
        if ($.browser.version <= 6.0)
            $(this).focus();
    }
};



$(document).ready(function()
{
    // Make sure to set the defalt value and default-value style to the search field if it's empty or has the default value when loading the
    // page.
    if ($("#searchfield").val() == "" || $("#searchfield").val() == textbooks.searchLiteral)
    {
        $("#searchfield").val(textbooks.searchLiteral);
        $("#searchfield").addClass("default-value");
    }
    $("#searchfield").bind("focus", textbooks.toggleSearchField);   // Set the focus event for the search field.
    $("#searchfield").bind("blur", textbooks.toggleSearchField);    // Set the blur event for the search field.

    // Set the event for when submitting the form and when clicking on the pagination links.
    // This event will check the search field to clear it if it only has the default value
    // in it.
    $("form").bind("submit", textbooks.validateSearch);
    $("#contentTable a").click(textbooks.validateSearch);

    // bind events for dropdowns.  This will clear the search field.
    $("select").bind("change", textbooks.clearSearch);

    // If browser is IE, make sure the text in the dropdown list is not longer than the dropdown itself.
    // If it is, set the events for mousedown, blur, and change to set the width of the dropdown dinamically.
    // If it is IE6, add a bit more by setting the position absolute and the parent position relative.
    if ($.browser.msie)
    {
        $("select").each(function()
        {
            var w = $(this).width();
            $(this).css("width", "auto");   // Set the width to auto to check whether this width is greater than the regular width.
            if (w < $(this).width())
            {
                if ($.browser.version <= 6.0)
                {
                    $(this).css("position", "absolute");
                    $(this).parent().css("position", "relative");
                }

                $(this).bind("mousedown", textbooks.selectFocus);
                $(this).bind("blur", textbooks.selectBlur);
                $(this).bind("change", textbooks.selectBlur);
            }

            $(this).css("width", "");   // Set the width back to its original sate.
        });
    }
});

