﻿var flag_my_select_active = false;
var flag_my_select_selected = false;
$(document).ready(function () {
    /* Begin Drop Down Lists */
    cleanDDLs();

    $(".myddl").each(function () { mydllinit($(this)); });
});
/* Set default values for all drop down lists */
function cleanDDLs() {
    $(".myddl").find(".myddl-option-list:first").each(function(){
        var currentval = $(this).parent().parent().parent().find(".phidden:first").val();
        $(this).find("li input[type='hidden']").each(function(){
            tmpval = $(this).val();
            txt = "";
            if (tmpval == currentval) {
                $(this).parent().addClass("lihover");
                $(this).parent().find("span:first").each(function(){
                    txt = $(this).html();
                });
                $(this).parent().parent().parent().parent().parent().find(".selected-text span").each(function(){
                    $(this).html(txt);
                    $(this).parent().attr({title: txt});
                });
            } else {
                $(this).parent().removeClass("lihover");
            }
        });
    });
}
function mydllinit(list) {
    list.hover(function () {
        if ($(this).attr("class").indexOf("disabled") != -1) return false;
    }, function () {
        if ($(this).attr("class").indexOf("disabled") != -1) return false;
        $(this).find(".myddl-option-list:first").hide();
        $(this).removeClass("mfocused");
    });
    // Open/close list
    list.click(function () {
        if ($(this).attr("class").indexOf("disabled") != -1) return false;
        myddl = $(this);
        var currentval = myddl.find(".phidden:first").val();
        $(myddl).find(".myddl-option-list:first").each(function () {
            $(this).find("li input[type='hidden']").each(function () {
                tmpval = $(this).val();
                if (tmpval == currentval) $(this).parent().addClass("lihover");
                else $(this).parent().removeClass("lihover");
            });
            dd = $(this).css("display");
            if (dd == "none" && flag_my_select_active == false && flag_my_select_selected == false) {
                $(this).css("display", "block");
                myddl.addClass("mfocused");
                flag_my_select_active = true;
            } else if (dd == "none" && flag_my_select_active == false && flag_my_select_selected == true) {
                $(this).css("display", "none");
                myddl.removeClass("mfocused");
                flag_my_select_active = true;
                flag_my_select_selected = false;
            } else if (dd == "none" && flag_my_select_active == true) {
                $(".myddl").find(".myddl-option-list").each(function () {
                    $(this).css("display", "none");
                    myddl.removeClass("mfocused");
                });
                $(this).css("display", "block");
                myddl.addClass("mfocused");
            } else if (dd == "block" && flag_my_select_active == true) {
                $(this).css("display", "none");
                myddl.removeClass("mfocused");
                flag_my_select_active = false;
            }
        });
    });
    /* Option select */
    list.find(".myddl-option-list li").hover(onOptionHover, onOptionUnhover).click(onOptionClick);
    /* End Drop Down Lists */
}
function mydllgetval(list) {
    return list.find(".selected-text input[type='hidden']").val();
}
function mydllsetval(list, val) {
    var li = list.find("input[value='" + val + "']").parent()[0];
    var text = list.find("input[value='" + val + "']").prev().html();
    list.find(".selected-text input[type='hidden']").each(function () {
        $(this).val(val);
    });
    list.find(".selected-text span").each(function () {
        $(this).html(text);
        $(this).parent().attr({ title: text });
    });
    $(li).addClass("lihover");
    $(li).parent().find("li").each(function () {
        $(this).css("display", "block");
    });
    $(li).parent().parent().parent().find(".myddl-option-list").each(function () {
        $(this).css("display", "none");
    });
    flag_my_select_selected = true;
    flag_my_select_active = true;
}
function mydllsettext(list, text) {
    var labels = list.find(".myddl-option-list span");
    var count = labels.length;
    for (var index = 0; index < count; index++) {
        var label = $(labels[index]);
        if (label.text() != text) {
            continue;
        }

        mydllsetval(list, label.next().val());
        return;
    }
}
function onOptionHover() 
{ 
    $(this).parent().find("li").removeClass("lihover");
    $(this).addClass("lihover");
}
function onOptionUnhover() 
{ 
    $(this).removeClass("lihover");
}
function onOptionClick(){
    val = "";
    txt = "";
    $(this).find("input[type='hidden']").each(function(){
        val = $(this).val();
    });
    $(this).find("span").each(function(){
        txt = $(this).html();
    });
    $(this).parent().parent().parent().parent().find(".selected-text input[type='hidden']").each(function(){
        $(this).val(val);
    });
    $(this).parent().parent().parent().parent().find(".selected-text span").each(function(){
        $(this).html(txt);
        $(this).parent().attr({title: txt});
    });
    $(this).addClass("lihover");
    $(this).parent().find("li").each(function(){
        $(this).css("display", "block");
    });
    $(this).parent().parent().parent().find(".myddl-option-list").each(function(){
        $(this).css("display", "none");
    });
    flag_my_select_selected = true;
    flag_my_select_active = false;
}
