;(function($) {

    $.fn.extend({
        multiselectlanguage: function(options) {
            return this.each(function() {
                new $.LanguageMultiselector(this, options);
            });
        }
    });

    $.LanguageMultiselector = function(input, options) {

        options.deleteSrc = '/content/images/common/delete.gif';
        SetVTEmptyState();

        var input = $('select#' + input.id);

        var score = $('select#' + options.score);
        var btn = $('input#' + options.button);
        var div = $('div#' + options.display);

        if (div.length == 0)
            div = $('#' + options.display);

        var selected = $('input#' + options.selected);
        var selections = new Array();
        btn.bind("click", AssignValue);

        if (options.showSelected) {
            SetSelected(options.data);
            DisplaySelected();
        }

        function AreVTsEqual(vt1, vt2) {
            if (vt1 == null | vt2 == null) {
                return false;
            }
            return vt1.ID == vt2.ID;
        }
        function AssignValue(e) {
            selectedAttribute = CreateVT();
            if (selectedAttribute == null | SelectedExists(selectedAttribute) | IsVTEmpty(selectedAttribute)) {
                return;
            }
            var html = div.html();
            div.html(html + DisplayAttribute(selectedAttribute));

            selections.push(selectedAttribute);
            selected.val(Sys.Serialization.JavaScriptSerializer.serialize(selections));

            $(".deleteWrapper").click(HandleRemoveSelection);
            SetVTEmptyState();
        }
        function ClearVT() {
            //$("input#VolunteerTypesTxt").val("");
        }
        function CreateVT() {
            var attribute = new Object;
            for (var i = 0; i < input[0].options.length; ++i) {
                if (input[0].options[i].selected) {
                    for (var j = 0; j < score[0].options.length; ++j) {
                        if (score[0].options[j].selected) {
                            attribute.ID = input[0].options[i].value + ":" + score[0].options[j].value;
                            attribute.Value = input[0].options[i].text + " / " + score[0].options[j].text;
                            break;
                        }
                    }
                    break;
                }
            }

            if (AreVTsEqual(selectedAttribute, attribute))
                return selectedAttribute;
            else
                return attribute;
        }
        function DisplayAutoCompleteVT(row, withHtml) {
            var vt;
            if (withHtml) {
                vt = "<span>{0}</span>";
            } else {
                vt = "{0}";
            }
            return String.format(vt, row.Value);
        }
        function DisplayAttribute(attribute) {
            return String.format("<span class='deleteWrapper' attributeID='{0}'><img class='deleteLnk' alt='Remove Image' title='Remove' src='{1}'/><span class='attribute'>{2}</span></span>",
                        attribute.ID, options.deleteSrc, attribute.Value);
        }
        function DisplaySelected() {
            if (selections == null | selections.length == 0)
                return;

            var html = '';
            div.html(html);

            for (var i = 0; i < selections.length; ++i) {
                var attribute = new Object;
                attribute.ID = selections[i].ID;
                attribute.Value = selections[i].Value;

                html = html + DisplayAttribute(attribute);
            }

            div.html(html);
            $(".deleteWrapper").click(HandleRemoveSelection);
        }
        function DisplayVT(vt) {
            input.val(vt.Value);
        }
        function HandleSelectedVT(data) {
            selectedAttribute = data;
            ClearVT();
            DisplayVT(data);
            $("input#AssignVTBtn").removeAttr("disabled");
        }
        function IsVTEmpty(vt) {
            return vt.Value == "";
        }
        function HandleRemoveSelection(e) {
            if (this != e.currentTarget)
                return;

            $(this).remove();
            var id = $(this).attr('attributeID');

            for (var index = 0; index < selections.length; ++index) {
                if (selections[index].ID == id) {
                    selections.splice(index, 1);
                    if (selections.length == 0)
                        selected.val('');
                    else
                        selected.val(Sys.Serialization.JavaScriptSerializer.serialize(selections));
                }
            }
        }
        function SelectedExists() {
            if (selections == null | selections.length == 0)
                return false;

            for (var i = 0; i < selections.length; ++i) {
                if (AreVTsEqual(selectedAttribute, selections[i]))
                    return true;
            }

            return false;
        }
        function SetSelected(data) {
            if (data == null)
                return;
            for (var i = 0; i < data.length; ++i) {
                var attribute = new Object;
                attribute.ID = data[i].ID;
                attribute.Value = data[i].Value;

                selections.push(attribute);
            }

            selected.val(Sys.Serialization.JavaScriptSerializer.serialize(selections));
        }
        function SetVTEmptyState() {
            selectedAttribute = null;
            ClearVT();
        }
    };

})(jQuery);
