﻿Type.registerNamespace('Framework.Controls');

Framework.Controls.DropDownListCssExtender = function(element) {
    Framework.Controls.DropDownListCssExtender.initializeBase(this, [element]);
    this.el = element;
}
Framework.Controls.DropDownListCssExtender.prototype = {
    initialize: function() {
        Framework.Controls.DropDownListCssExtender.callBaseMethod(this, 'initialize');
        this._createCss();
    },
    dispose: function() {
        Framework.Controls.DropDownListCssExtender.callBaseMethod(this, 'dispose');
    },
    _createCss: function() {
        // Wrap select in div container
        var div = document.createElement("div");
        var classname = "";
        if (this.el.className) {
            classname = this.el.className;
            div.className = classname;
        }
        var elClone = this.el.cloneNode(true);
        div.appendChild(elClone);
        this.el.parentNode.replaceChild(div, this.el);
        if (this.el.options.length <= 0) return;
        
        // IE doesn't clone selectedIndex attribute
        elClone.selectedIndex = this.el.selectedIndex;

        // Find the select box
        div = $(div);
        if (this.el.selectedIndex <= 0) {
            div.children().before('<div class="select-text"></div>').each(
                function() { $(this).prev().html(this.options[0].innerHTML); }
            );
        } else {
            // Restore selected value
            div.children().before('<div class="select-text"></div>').each(
                function() {
                    $(this).prev().html(this.options[this.selectedIndex].innerHTML);
                }
            );
        }

        // Store the parent object
        var parentTextObj = div.children().prev();

        // As we click on the options
        div.children().click(function() {
            // Set the value of the html
            parentTextObj.html(this.options[this.selectedIndex].innerHTML);
        })
    }
}
Framework.Controls.DropDownListCssExtender.registerClass('Framework.Controls.DropDownListCssExtender', AjaxControlToolkit.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();