(function($) {
    $.fn.findADoctor = function(opts) {
        $.fn.findADoctor.defaults = {
            closedWidth: 160,
            openWidth: 410,
            mode: 'sub'
        };
        var opts = $.extend({}, $.fn.findADoctor.defaults, opts);

        return this.each(function(idx) {
            if (opts.mode == "home") {
                var fad = $(this).parent().addClass("dynamic");
                var button = $(this).next("input[type=submit]").hide();
                var select = $(this).hide();

                var ul = $("<ul></ul>");
                var ht = null;
                select.children().each(function(idx) {
                    ul.append('<li class="{code: \'' + $(this).val() + '\'}">' + $(this).text() + '</li>');
                });
                select.replaceWith('<input type="hidden" name="SPECIALTY" value="" />');
                var newSelect = $('<div id="specialty"></div>').append(ul);
                var more = fad.children("p.find-more").remove();

                newSelect.css({ left: fad.position().left + 15, top: (fad.outerHeight(true) + fad.position().top - 50) });
                $("#main").append(newSelect);
                more.css({ position: "absolute", left: newSelect.position().left, top: newSelect.position().top + newSelect.outerHeight(true) + 3 });
                newSelect.after(more);
                newSelect.hover(
		            function() {
		                $(this).addClass("hover");
		            },
		            function() {
		                $(this).removeClass("hover");
		            }
		        ).click(function() {
		            $(this).addClass("opened").css({ height: 200, zIndex: 10000, backgroundImage: "none" });
		            return false;
		        }).mouseleave(function() {
		            if (!$(this).hasClass("opened")) return;
		            ht = window.setTimeout(
		                function() {
		                    newSelect.removeClass("opened").css(
		                        {
		                            height: 25,
		                            backgroundImage: "url('/virtua/images/button-drop-down-go-sprite.png')"
		                        }
		                    ).scrollTop(0);
		                }, 750);
		        }).mouseenter(function() {
		            window.clearTimeout(ht);
		        }).children("ul").children("li").hover(
	                function() {
	                    $(this).addClass("hover");
	                },
    	            function() {
    	                $(this).removeClass("hover");
    	            }
		        ).click(function() {
		            if (!newSelect.hasClass("opened")) return;
		            var data = $(this).metadata();
		            $("input[name=SPECIALTY]").val(data.code);
		            button.click();
		        });
                return;
            }
            var fad = $(this);
            var go = $("a.go", fad);
            if (go.length) fad.addClass("find-a-doctor-with-go");
            $(this).mouseenter(function() {
                if (!$(this).hasClass("open")) $(this).addClass("hover");
                if ($(this).hasClass("open") || $(this).hasClass("show-tab")) return;
                $(this).addClass("show-tab");
                var li = this;
                var arrow = $('<span class="arrow" />').toggle(Expand, Contract);
                $(this).prepend(arrow);
            });
            $(this).mouseleave(function() {
                $(this).removeClass("show-tab").removeClass("hover");
                if ($(this).hasClass("open")) return;
                $(this).children("span.arrow").remove();
            });
            $(this).click(function() {
                if ($(this).hasClass("open")) return;
                $(this).children("span.arrow").click();
                return false;
            });
            $(this).children("a.more").hover(
		        function() {
		            $(this).parent("li").removeClass("hover");
		            return false;
		        },
		        function() {
		            return false;
		        }
		    ).click(function() {
		        location.href = $(this).attr("href");
		        return false;
		    });
            $(this).children(".specialty").hover(
		        function() {
		            $(this).addClass("hover");
		        },
		        function() {
		            $(this).removeClass("hover");
		        }
		    ).click(function() { $(this).toggleClass("dropped") }).find("li").hover(
		        function() {
		            $(this).addClass("hover");
		        },
		        function() {
		            $(this).removeClass("hover");
		        }
		    ).click(function() {
		        var ul = $(this).parent("ul");
		        if ($(this).hasClass("selected")) {
		            // An item is preselected, which means there is also a go button.
		            //newSelect.click();
		            return false;
		        }

		        ul.children("li.selected").remove();
		        var selected = $(this).clone().addClass("selected");
		        ul.prepend(selected);
		        var data = $(this).metadata();
		        SubmitSearch(data.code);
		    });
            go.click(function() {
                var selected = $("ul li.selected");
                var data = selected.metadata();
                if (data && data.code) SubmitSearch(data.code);
                return false;
            });
        });
        function SubmitSearch(code) {
            var form = $('<form style="position:absolute;top:-9999px;left:-9999px" action="https://physicianlocator.virtua.org/navigationweb/locator.aspx" method="post"><input type="hidden" name="SPECIALTY" value="' + code + '" /><input type="hidden" name="Search" value="Search" /><input type="hidden" name="INSURANCE" value="" /><input type="hidden" name="INTEREST" value="" /><input type="hidden" name="LANGUAGE" value="" /><input type="hidden" name="LASTNAME" value="" /><input type="hidden" name="MEDGROUP" value="" /><input type="hidden" name="Sex" value="" /><input type="hidden" name="Zip" value="" /><input type="hidden" name="ZIPRADIUS" value="15" /></form>');
            $("body").append(form);
            form.submit();
        }
        function Expand() {
            var li = $(this).get(0).tagName != "LI" ? $(this).parent("li") : $(this);
            $(li).addClass("open");
            if ($("a.go", li).length) {
                $(li).addClass("find-a-doctor-with-go-open");
            }
            /*$(li).animate({
            width: opts.openWidth
            }, 100, 'linear', function() {
            $(this).removeClass("hover");
            }).children(".specialty").animate({
            width: 230,
            opacity: 1
            }, 100).next("a.go").fadeIn(250);*/
        }
        function Contract() {
            var li = $(this).parent("li");
            li.find(".specialty").removeClass("dropped");
            li.removeClass("open").removeClass("find-a-doctor-with-go-open");
            /*$(li).animate({
            width: opts.closedWidth
            }, 100, 'linear', function() {
            $(this).removeClass("open");
            if (!$(this).hasClass("show-tab")) $(this).children("span.arrow").remove();
            }).children(".specialty").animate({
            width: 0,
            opacity: 0
            }, 100).next("a.go").fadeOut(25);*/
        }
    };
})(jQuery);
