/// <reference path="jquery-1.3.2.min.js" />

var visibleSelect;

function hook(source) {
    $("#" + source + " li")
    .live("mouseover", function() {
        $(this).addClass('hover');
    })
    .live("mouseout", function() {
        $(this).removeClass('hover')
    })
    .live("click", function(e) {
        itemClick(this, source);
    });

    $("#" + source + " ul").mousedown(function(e) {
        e.stopPropagation();
    });


    if (document.addEventListener) {
        document.addEventListener('mousedown', function() {
            hideList(source);
        }, false)
    }
    else {
        document.attachEvent("onmousedown", function() {
            hideList(source);
        });
    }

    generateList(source);

    $("#" + source).bind('click', function(e) {
        ShowListCallback(e, source);
    });

    $(document).ready(function() {
        var element = $get($('#' + source + ' select').attr('id'));
        if (element._behaviors) {
            for (var loopIndex = 0; loopIndex < element._behaviors.length; loopIndex++) {
                currentBehavior = element._behaviors[loopIndex];
                if (currentBehavior.get_name() == "CascadingDropDownBehavior") {
                    currentBehavior.add_populated(function() {
                        generateList(source);
                    });
                    behaviorID = currentBehavior._id;
                    $('#' + currentBehavior._parentControlID).attr('BehaviorID', behaviorID);

                }
            }
        }
    });
}

function itemClick(i, source) {
    var value;
    $('#' + source + ' ul').hide("fast");

    for (var loopIndex = 0; loopIndex < i.attributes.length; loopIndex++) {
        if (i.attributes[loopIndex].nodeName == 'value') {
            value = i.attributes[loopIndex].value;
            break;
        }
    }


    itemSelected($(i).text(), value, source);

    var itm = $get($('#' + source + ' select').attr('id'));
    if (itm._behaviors) {
        for (var loopIndex = 0; loopIndex < itm._behaviors.length; loopIndex++) {
            currentBehavior = itm._behaviors[loopIndex];
            if (currentBehavior.get_name() == "CascadingDropDownBehavior") {
                currentBehavior.set_SelectedValue($(i).attr('value'), $(i).text());
            }
        }
    }



    var behaviorID = $('#' + source + ' select').attr('BehaviorID');
    if (behaviorID != undefined) {
        if ($find(behaviorID) != null) {
            var currentBehavior = $find(behaviorID);
            // currentBehavior._onChange();
            var parent = $get(currentBehavior.get_ParentControlID());
            var index = parent.selectedIndex;
            parent.selectedIndex = -1;
            currentBehavior._onParentChange(null, false);
            parent.selectedIndex = index;
            currentBehavior._onParentChange(null, false);
        }
    }

}

function itemSelected(text, value, source) {
    $('#' + source + ' .middle').html(text);
    $('#' + source + ' select').val(value);
    $('#' + source + ' select').trigger("change", null);
    /*if ($('#' + source + ' select')[0].onchange)
    $('#' + source + ' select')[0].onchange();*/
}

function ShowListCallback(e, source) {
    var o = $('#' + source + " > ul")
    if (o[0] != visibleSelect)
        o.slideDown(1);

    visibleSelect = null;

}
function hideList(source) {
    $('#' + source + " > ul").each(function() {
        var o = $(this);
        if (o.is(':visible')) {
            visibleSelect = this;
            o.slideUp(1);
        }
    });

}

function generateList(source) {
    $('#' + source + ' ul').html('');
    $('#' + source + ' option').each(function(i, o) {
        appendItems(o, source, o.selected);
    });
}

function appendItems(o, source, selected) {
    var i = $('#' + source + ' ul').append('<li value="' + o.value + '" >' + o.text + '</li>');
    if (selected == true) {
        itemSelected(o.text, o.value, source);
    }
}
