﻿/* JavaScript code for the CustomSelect component */

function DropDown(el) {
    this.dd = $(el);
    this.placeholder = this.dd.children('span');
    this.opts = this.dd.find('.dd-select > li');
    //this.value = '';
    this.text = '';
    this.index = -1;
    this.initEvents();
    this.selValId = '';

    // Set default value
    var sel = this.dd.attr('data-sel-val');
    if (this.dd.attr('data-sel-val') == null) {
        this.dd.attr('data-sel-val', '');
        if (this.dd.attr('data-sel-val') != null) {
            this.placeholder.text(this.dd.attr('data-placeholder'));
        };
    } else {
        this.text = this.dd.find('.dd-select > li[data-item-val="' + sel + '"]').text();
        if (this.text == null || this.text == '') {
            this.text = this.dd.attr('data-placeholder');
        };
        this.placeholder.text(this.text);
    };

    this.dd.on('ddChange', function (event) {
        // alert('ddChange!');
    });
}

DropDown.prototype = {
    initEvents: function () {
        var obj = this;

        obj.dd.on('click', function (event) {
            $(this).toggleClass('active');
            return false;
        });

        obj.opts.on('click', function () {
            var opt = $(this);
            obj.text = opt.text();
            obj.index = opt.index();
            obj.placeholder.text(obj.text);

            // Check for value change
            var di = opt.attr('data-item-val');
            if (di != obj.getValue()) {
                obj.dd.attr('data-sel-val', di);
                obj.dd.trigger("ddChange");
            };

            // Set the selected value if hidden field is present
            if (obj.selValId != '') {
                $('#' + obj.selValId).val(di);
            };

            // Activate link if specified
            if (opt.find('a[href*="/"]:first').length) {
                var href = opt.find('a:first').attr('href');
                location.href = href;
            };
        });
    },
    getValue: function () {
        return this.dd.attr('data-sel-val');
    },
    getText: function () {
        return this.text;
    },
    getIndex: function () {
        return this.index;
    },
    setValue: function (val) {
        var c = this.dd.attr('data-sel-val');

        // Check for value change
        if (c != val) {
            this.dd.attr('data-sel-val', val);

            if (this.dd.attr('data-sel-val') == null) {
                this.dd.attr('data-sel-val', '');
                if (this.dd.attr('data-sel-val') != null) {
                    this.placeholder.text(this.dd.attr('data-placeholder'));
                };
            } else {
                this.text = this.dd.find('.dd-select > li[data-item-val="' + sel + '"]').text();
                if (this.text == null || this.text == '') {
                    this.text = this.dd.attr('data-placeholder');
                };
                this.placeholder.text(this.text);
            };

            this.dd.trigger("ddChange");
        };
    }
}


