var CARHA = {};

CARHA.Navigation = {
    init: function () {
        $("#subnav > li").mouseover(this.show).mouseout(this.hide);
    },
    show: function () {
        var $li = $(this);
        $li.addClass("over").find(">ul").css("min-width", $li.width()).show();
    },
    hide: function () {
        $(this).removeClass("over").find(">ul").hide();
    }
};

CARHA.Finder = {
    init: function () {
        $("#finder > li a").click(this.onFinderClick);
        $(".finder-filter").submit(this.onFinderSubmit);
        $(".parentSelect").change(this.onFinderParentChange);
    },
    onFinderClick: function (e) {
        e.preventDefault();
        var $anchor = $(this),
            $li = $anchor.parent(),
            $section = $anchor.next();

        // toggle selected
        $li.toggleClass("selected").find(".finder-section").toggleClass("hide");
        // close others
        $li.siblings().removeClass("selected").find(".finder-section").addClass("hide");
    },
    onFinderParentChange: function () {
        var $parent = $(this),
                $parentForm = $parent.closest("form"),
                webMethod = $parentForm.find(".webMethod").val(),
                dependentSelectValue = $parentForm.find(".dependentSelectValue").val() || '',
                $dependentSelect = $parentForm.find("#" + $parentForm.find(".dependentSelect").val());

        if ($dependentSelect.length) {

            // clear
            $dependentSelect.find("option:not(.defaultOption)").remove();

            // get values
            if ($parent.val().length > 0) {
                $.ajax({
                    type: "POST",
                    url: "/services.asmx/" + webMethod,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: '{provinceCleanUrl:"' + $parent.val() + '"}',
                    success: function (data) {

                        var obj = data.d,
                            options = '';
                        if (obj) {
                            for (var prop in obj) {
                                if (obj.hasOwnProperty(prop)) {
                                    var selected = (dependentSelectValue === obj[prop].toString() ? ' selected="selected"' : '');
                                    options += '<option value="' + obj[prop] + '"' + selected + '>' + prop.toString() + '</option>';
                                }
                            }
                        }
                        $dependentSelect.focus().attr("disabled", false).append(options);
                    }
                });
            }

        }

    },
    onFinderSubmit: function (e) {
        e.preventDefault();
        var $parentForm = $(this).closest("form"),
            path = $parentForm.attr("action");

        $parentForm.find("select").each(function () {
            var pathSegment = $(this).val();
            if (pathSegment.length > 0) {
                path += '/' + pathSegment;
            }
        });

        window.location = path;
    }
}

CARHA.MailingList = {
    init: function () {

        // input defaults
        var $frm = $("#join-frm"),
            $input = $("#join-email")
                .focus(function () {
                    if (this.value === this.defaultValue) {
                        this.value = '';
                    }
                })
                .blur(function () {
                    if (this.value === '') {
                        this.value = this.defaultValue;
                    }
                });

        // join
        $frm.submit(function (e) {
            e.preventDefault();

            // send to ws
            $.ajax({
                type: "POST",
                url: "/services.asmx/JoinMailingList",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: '{emailAddress:"' + $input.val() + '"}',
                error: function () {
                    $("#join-fail").text("There was a problem joining our mailing list.").removeClass("hide");
                },
                success: function (data) {
                    if (data.d) {
                        $("#join-success").removeClass("hide");
                        $("#join-fail").addClass("hide");
                        $frm.addClass("hide");
                    } else {
                        $("#join-fail").removeClass("hide");
                    }
                }
            });

        });

    }
};

CARHA.FAQ = {
    init: function () {

        var $content = $("#content"),
            $questions = $(".faq-question", $content);

        if ($questions) {
            $(".faq-answer", $content).addClass("hide");
            $questions.click(this.onFAQexpand);
        }
    },
    onFAQexpand: function (e) {
        e.preventDefault();
        $(this).next(".faq-answer").toggleClass("hide");
    }
};

$(function () {
    CARHA.Navigation.init();
    CARHA.Finder.init();
    CARHA.MailingList.init();
    CARHA.FAQ.init();
    //leave this commented out for now. Try later once we have real content
    //$("#ft-nav").masonry();
});
