﻿$(document).ready(function() {
    $(".dropdownList").each(function(i) {
        $(this).mouseenter(function() {
            $(this).addClass("dropdownList-hovered");
            $(this).find(".arrow").addClass("arrow-hovered");
            $(this).find(".arrowsmall").addClass("arrow-hovered");
            $(this).stopTime("hide-dropdown");
        });

        $(this).mouseleave(function() {
            $(this).removeClass("dropdownList-hovered");
            $(this).find(".arrow").removeClass("arrow-hovered");
            $(this).find(".arrowsmall").removeClass("arrow-hovered");

            $(this).oneTime(500, "hide-dropdown", function() {
                if ($(this).find(".options").is(":visible")) {
                    $(this).find(".options").hide();
                }
            });
        });

        $(this).click(function() {
            $(this).find(".options").toggle();
            $(this).stopTime("hide-dropdown");
        });

        $(this).find(".options div").mouseenter(function() {
            $(this).parent().addClass("hovered");
        });

        $(this).find(".options div").mouseleave(function() {
            $(this).parent().removeClass("hovered");
        });

        $(this).find(".options div").each(function(i) {
            $(this).css("background-image", "url(/images/flag_mid/" + $(this).attr("id") + ".gif)");
        });

        $(this).find(".options div").click(function() {
            var value = $(this).attr("id");
            var oldValue = $(this).parents(".dropdownList").find("input:first").attr("value");

            if (value != oldValue) {
                $(this).parents(".dropdownList").find("input:first").attr("value", value);
                if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 || parseInt(jQuery.browser.version) == 7) {
                    $(this).parents(".dropdownList").find(".image>img").css("position", "relative");
                    $(this).parents(".dropdownList").find(".image>img").css("top", "-3px");
                }

                var image = "<img src='/images/flag_big/" + value + ".gif' />";
                $(this).parents(".dropdownList").find(".display div").eq(0).html(image);
                $(this).parents(".dropdownList").find(".display div").eq(1).html($(this).html());
                $(this).parents(".dropdownList").trigger("changed");
            }
        });
    });
});