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);
    });

    $(".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 + 0) + "px");
            sc.fadeIn("fast");
        }
    });

    $("#specialty_chooser li").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 Conditions)")
            status = true;

        if (!status) {
            $(this).parents(".results_providers_top_org").find(".results_providers_top_org_brand ul li").show();
            $(this).html("(Hide Conditions)");
        } 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 Conditions)");
        }
    });

    $(".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 validation events
/////////////////////////////////////
function validate_provider_search_saved() {
        isValid = true;
        errors = "Please provide the following required information: \n\n";

        // validate we have some for the specialty/condition/name search
        if ($('#refine_keyword').val() == '') {
            isValid = false;
            errors += "- Specialty, Condition or Keyword\n";
        }

        if (($('#search_providers6').is(':visible') || $('#providers_search_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) {
return true;
        }
        else {
        alert(errors);
		return false;
		}
    }
//////////////////////////////////////
// end matchmaker validation events
///////////////////////////////////// 


//////////////////////////////////////
// begin matchmaker validation events
/////////////////////////////////////
function validate_provider_search(home) {
        isValid = true;
        errors = "Please provide the following required information: \n\n";

        // validate we have some for the specialty/condition/name search
        if ($('#refine_keyword').val() == '') {
            isValid = false;
            errors += "- Specialty, Condition or Keyword\n";
        }

	// this is to validate the home page only as the home page will not have the searc_ ... visible
	// and therefore the original code will not validate the zip nor the city.
	// the immediate code below will when request comes from the home page.
	// this is a temporary solution and will be modified later.
	if ((home != null) && (home != 'undefined'))
	{
		var zip = (($('#refine_zip').is(':visible')) && (($('#refine_zip').val() != '')));
		var city = (($('#refine_city').is(':visible')) && (($('#refine_city').val() != '')));
		isValid = isValid && (zip||city);
		if (!isValid) errors += "- Zip Code or City and State\n";
	}
	else{	

        if (($('#search_providers6').is(':visible') || $('#providers_search_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) {
return true;
        }
        else {
        alert(errors);
		return false;
		}
    }
//////////////////////////////////////
// end matchmaker validation events
///////////////////////////////////// 




