var options_status = false;

$(document).ready(function() {
    //Based on code from http://snipplr.com/view/20519/keyup-event-with-delay/
    var timeout = undefined;
    var orig;
    $("#refine_location").keyup(function(e) {
        var code = e.keyCode;
        if (timeout != undefined) {
            clearTimeout(timeout);
        }
        timeout = setTimeout(function() {
            timeout = undefined;
            //do it
            var box = $("#refine_location");
            var div = $(".taxonomy");
            if (box.val().length > 2) {
                //handle the keyboard navigation
                if ((code >= 37 && code <= 40) || code == 13) {
                    var curr = div.find("li.selected");
                    if (curr.length < 1) {
                        if (code == 40 && div.find("li.specialty").length > 0) {
                            //go down into the div
                            var next = div.find("li:first-child");
                            next.addClass('selected');
                            box.val(next.text());
                        }
                    } else {
                        if (code == 38) { //up
                            var prev = curr.prev();
                            if (prev.length > 0) {
                                curr.removeClass("selected");
                                prev.addClass("selected");
                                box.val(prev.text());
                            } else {
                                curr.removeClass("selected");
                                box.val(orig);
                            }
                        } else if (code == 40) { //down
                            var next = curr.next();
                            if (next.length > 0) {
                                curr.removeClass("selected");
                                next.addClass("selected");
                                box.val(next.text());
                            }
                        } else if (code == 13) {
                            div.slideUp('fast');
                        }
                    }
                }
                else {
                    orig = box.val();
                    div.animate({ width: "205px" }, 100).html('<img src="/Content/images/taxonomy_loading.gif" alt="loading" align="middle" />Loading Locations...').slideDown('fast');
                    $.post("/providers/lookuplocation/" + box.val(), function(data) {
                        div.html(data);
                    });
                }
            } else {
                div.slideUp('fast');
            }
        }, 50);
    });

    $("#provider_search_tabs").click(function(e) {
        var tgt = $(e.target);
        if (tgt.closest(".taxonomy").length == 0) {
            $(".taxonomy").slideUp('fast');
        }
        if (tgt.attr("id") != "chooseSpecialty" && tgt.closest("#specialty_chooser").length == 0) {
            $("#specialty_chooser").fadeOut("fast");
        }
    });

    $(".taxonomy li.specialty").live("mouseover mouseout", function(event) {
        if (event.type == "mouseover") {
            $(this).addClass("selected");
            $(this).siblings().removeClass("selected");
        } else {
            $(this).removeClass("selected");
        }
    });

    $(".taxonomy li.specialty").live("click", function() {
        $('.taxonomy li').removeClass('selected');
        var ths = $(this);
        ths.addClass('selected');
        $("#refine_location").val(ths.text());
        ths.closest('ul').slideUp('fast');
    });

    $("#chooseSpecialty").click(function() {
        var sc = $("#specialty_chooser");
        if (sc.is(":visible")) {
            sc.fadeOut("fast");
        } else {
            sc.css("left", ($("#chooseSpecialty").prev().position().left + 45) + "px");
            sc.fadeIn("fast");
        }
    });

    $("#specialty_chooser li:not(.empty)").click(function() {
        $("#refine_keyword").val($(this).text());
        $("#specialty_chooser").fadeOut("fast");
    });

    $("#correctedProvider").click(function() {
        $("#refine_keyword").val($(this).text());
        $(".provider_search").click();
    });

    $("#correctedLibrary").click(function() {
        $("#condition").val($(this).text());
        $("#contentSearchSubmit").click();
    });

    // Splash Variations
    $(".splash_providers_featured_locations_more").click(function() {
        var status = false;

        if ($(this).html() == "(Hide Locations)")
            status = true;

        if (!status) {
            $(this).parents(".splash_providers_featured_locations").find(".splash_providers_featured_locations_list ul li").show();
            $(this).parents(".results_providers_top_locations").find(".results_providers_top_locations_list ul li").show();
            $(this).html("(Hide Locations)");
        } else {
            $(this).parents(".splash_providers_featured_locations").find(".splash_providers_featured_locations_list ul li").hide();
            $(this).parents(".splash_providers_featured_locations").find(".splash_providers_featured_locations_list ul li:first-child").show();

            $(this).parents(".results_providers_top_locations").find(".results_providers_top_locations_list ul li").hide();
            $(this).parents(".results_providers_top_locations").find(".results_providers_top_locations_list ul li:first-child").show();

            $(this).html("(More Locations)");
        }
    });

    $(".splash_providers_featured_specialties_more").click(function() {
        var status = false;

        if ($(this).html() == "(Hide Specialties)")
            status = true;

        if (!status) {
            $(this).parents(".results_providers_top_org").find(".results_providers_top_org_brand ul li").show();
            $(this).html("(Hide Specialties)");
        } else {
            $(this).parents(".results_providers_top_org").find(".results_providers_top_org_brand ul li").hide();
            $(this).parents(".results_providers_top_org").find(".results_providers_top_org_brand ul li:first-child").show();

            $(this).html("(More Specialties)");
        }
    });

    $(".splash_providers_featured_conditions_more").click(function() {
        var status = false;

        if ($(this).html() == "(Hide)")
            status = true;

        if (!status) {
            $(this).parents(".results_providers_top_org").find(".results_providers_top_conditions_list ul li").show();
            $(this).html("(Hide)");
        } else {
            $(this).parents(".results_providers_top_org").find(".results_providers_top_conditions_list ul li").hide();
            $(this).parents(".results_providers_top_org").find(".results_providers_top_conditions_list ul li:nth-child(-n+3)").show();
            $(this).html("(Show All)");
        }
    });

    $(".splash_providers_featured_philo_more").click(function() {
        var open = false;
        if ($(this).html() == "(Hide)")
            open = true;

        if (!open) {
            $(this).closest(".splash_providers_featured_philo_label").next().find('.ellipsis').text(''); // featured providers
            $(this).closest(".results_providers_top_philo").find(".results_providers_top_philo_msg .ellipsis").text(''); // matchmaker results

            $(this).parents(".splash_providers_featured_philo").find(".splash_providers_featured_philo_msg span").show(); // featured providers
            $(this).closest(".results_providers_top_philo").find(".results_providers_top_philo_msg span").not('.ellipsis').show(); // matchmaker results
            $(this).html("(Hide)");
        } else {
            $(this).closest(".splash_providers_featured_philo_label").next().find('.ellipsis').text('...'); // featured providers
            $(this).closest(".results_providers_top_philo").find(".results_providers_top_philo_msg .ellipsis").text('...'); // matchmaker results
            $(this).parents(".splash_providers_featured_philo").find(".splash_providers_featured_philo_msg span").not('.ellipsis').hide(); // featured providers
            $(this).closest(".results_providers_top_philo").find(".results_providers_top_philo_msg span").not('.ellipsis').hide(); // matchmaker results
            $(this).html("(Show All)");
        }
    });

    $(".results_events_top_about_more").click(function() {
        var open = false;
        if ($(this).html() == "(Hide)")
            open = true;

        if (open) {
            aboutDiv = $(this).closest(".splash_events_featured_about");
            if (aboutDiv.length == 0) aboutDiv = $(this).closest(".results_events_top_about");
            if (aboutDiv.length == 0) return false;

            // get message div
            msgDiv = aboutDiv.find('.splash_events_featured_about_msg');
            if (msgDiv.length == 0) msgDiv = aboutDiv.find('.results_events_top_about_msg');

            if (msgDiv.length == 0) return false;

            msgDiv.find('.fulldesc').hide();
            msgDiv.find('.ellipsis').text('...');
            $(this).html("(Show All)");
        } else {
            aboutDiv = $(this).closest(".splash_calendar_featured_about");
            if (aboutDiv.length == 0) aboutDiv = $(this).closest(".results_events_top_about");
            if (aboutDiv.length == 0) return false;

            // get message div
            msgDiv = aboutDiv.find('.splash_events_featured_about_msg');
            if (msgDiv.length == 0) msgDiv = aboutDiv.find('.results_events_top_about_msg');

            if (msgDiv.length == 0) return false;

            msgDiv.find('.fulldesc').show();
            msgDiv.find('.ellipsis').text('');
            $(this).html("(Hide)");
        }
    });


    // Results Variations

    $(".results_providers_top_locations_more").click(function() {
        var status = false;

        if ($(this).html() == "(Hide Locations)")
            status = true;

        if (!status) {
            $(this).parents(".results_providers_top_locations").find(".results_providers_top_locations_list a").css("display", "block");
            $(this).html("(Hide Locations)");
        } else {
            $(this).parents(".results_providers_top_locations").find(".results_providers_top_locations_list a").css("display", "none");
            $(this).parents(".results_providers_top_locations").find(".results_providers_top_locations_list a:first-child").css("display", "block");
            $(this).html("(More Locations)");
        }
    });

    $(".results_providers_top_philo_more").click(function() {
        var status = false;

        if ($(this).html() == "(Hide)")
            status = true;

        if (!status) {
            $(this).parents(".results_providers_top_philo").find(".results_providers_top_philo_msg span").show();
            $(this).html("(Hide)");
        } else {
            $(this).parents(".results_providers_top_philo").find(".results_providers_top_philo_msg span").hide();
            $(this).html("(Show All)");
        }
    });

    //////////////////////////////////////
    // begin matchmaker events
    /////////////////////////////////////
    $('.provider_search').click(function() {
        isValid = true;
        errors = "Please provide the following required information: \n\n";

        //correct any textboxes so they don't have bad values
        $("input[type=text]").each(function() { $(this).val($(this).val().replace(/[^\w. -/(),\u2013]/g, "")); });

        // validate we have some for the specialty/condition/name search
        if ($('#tab_content_keyword').is(':visible') && $('#refine_keyword').val().trim() == '') {
            isValid = false;
            errors += "Specialty, Condition or Keyword.\n";
        }

        if ($('#tab_content_location').is(':visible') && ($('#refine_location').val().trim() == '' && ($('#location_zip').val() == '' && $('#location_city').val() == ''))) {
            isValid = false;
            errors += "Clinic/Location Name, ZIP Code, or City and State.\n";
        }

        if ($('#tab_content_name').is(':visible') && $('#refine_firstname').val().trim() == '' && $('#refine_lastname').val().trim() == '') {
            isValid = false;
            errors += "First or Last Name.\n";
        }

        if (($('#search_providers6').is(':visible') || $('#tab_content_keyword').is(':visible')) && ($('#refine_zip').val() == '' || $('#refine_zip').val() == 'e.g. 55436') && (($('#refine_city').val() == '' || $('#refine_city').val() == 'City') || $('#refine_state').val() == '')) {
            isValid = false;
            errors += "ZIP Code or City and State.\n";
        }

        if (isValid) {
            // find which search we are doing: specialty, condition or name, and blank out the unneeded fields
            selected1 = $('#search_providers1 .search_box_field_toggle a.toggle_highlight').attr('id');
            switch (selected1) {
                case "search_box_toggle_keyword":
                    $('#refine_location').val('');
                    $('#refine_firstname').val('');
                    $('#refine_lastname').val('');
                    break;
                case "search_box_toggle_location":
                    $('#refine_keyword').val('');
                    $('#refine_firstname').val('');
                    $('#refine_lastname').val('');
                    break;
                case "search_box_toggle_name":
                    $('#refine_keyword').val('');
                    $('#refine_location').val('');
                    break;
            }

            // find which search we are doing: zip or city, and blank out the unneeded fields
            selected2 = $('#search_providers2 .search_box_field_toggle a.toggle_highlight').attr('id');
            switch (selected2) {
                case "search_box_toggle_zip":
                    $('#refine_city').val('');
                    $('#refine_state').val('');
                    break;
                case "search_box_toggle_city":
                    $('#refine_zip').val('');
                    break;
            }

            $('#frmRefineMatch').submit();
            $("#searchMessage").dialog("open");
        }
        else alert(errors);
    });

    //////////////////////////////////////
    // end matchmaker events
    /////////////////////////////////////


    //////////////////////////////////////
    // beghin content search events
    /////////////////////////////////////

    // indicate if we a doing a specialty or condition search
    $('#contentSearchSubmit').click(function() {
        if ($('#condition').val() == '' && $("#alpha").val() == '') {
            alert('Please enter a procedure, condition, or keyword.');
            return false;
        }
        else {
            $('#contentSearch').submit();
            $("#searchMessage").dialog("open");
        }
    });

    // set the initial search result to highlighted
    $('#results_library_top3 .results_library_top_item').first().addClass('results_library_top_item_highlight');

    // set up the mouseover to highlight content search results
    $('#results_library_top3 .results_library_top_item').mouseover(
        function() {
            $(this).siblings().removeClass('results_library_top_item_highlight');
            $(this).addClass('results_library_top_item_highlight');
        }
    );


    //////////////////////////////////////
    // end content search events
    /////////////////////////////////////
});
