﻿jQuery.fn.extend({
    fire: function(evttype) {
        el = this.get(0);
        if (document.createEvent) {
            var evt = document.createEvent('HTMLEvents');
            evt.initEvent(evttype, false, false);
            el.dispatchEvent(evt);
        } else if (document.createEventObject) {
            el.fireEvent('on' + evttype);
        }
        return this;
    }
});

(function($) {
    $.fn.gc_customDropDown = function(options) {
        jQuery(this).attr("gc_init", "true");

        var lb = jQuery(this).find(".dropdown_value");
        var submn = jQuery(this).find(".submenu");
        var ddl = jQuery(submn).find("ul");
        var ddlControll = jQuery(this);
        var myParent = jQuery(this).parent(".cstm_dropdown"); //.DropDownListParent");
        var dropDownListParent = jQuery(myParent).find("select");

        if (dropDownListParent.length == 0) {
            jQuery(this).css({ display: "none" });
        }


        jQuery(ddl).empty();
        jQuery(this).css({ width: jQuery(dropDownListParent).outerWidth() + 'px' });

        jQuery(document).click(
            function() {
                jQuery(ddlControll).attr('class', 'dropdown');
                jQuery(ddlControll).css('z-index', '1');
            }
        );




        // Synchronize with real dropdown controll
        jQuery(dropDownListParent).children().each(
            function() {
                var listItem = jQuery(this);
                if (jQuery(this).attr("selected")) {
                    jQuery(lb).text(jQuery(this).attr("text"));
                }
                jQuery(ddl).append("<li><a href=\"#\">" + jQuery(listItem).attr("text") + "</a></li>");

                jQuery(jQuery(ddl).find("a").get(jQuery(this).attr("index"))).click(
                        function(e) {

                            jQuery(lb).text(jQuery(listItem).attr("text"));
                            jQuery(listItem).attr("selected", "true");
                            jQuery(dropDownListParent).val(jQuery(listItem).attr("value"));


                            jQuery(ddlControll).attr('class', 'dropdown');
                            jQuery(ddlControll).css('z-index', '1');
                            if ($.browser.msie) {
                                jQuery(dropDownListParent).fire("change");
                            } else {
                                jQuery(dropDownListParent).trigger("change");
                            }
                            return false;
                        }

                 );
            }
        );



        jQuery(ddl).css(
            { height: ((jQuery(ddl).children().length * 28 > 300) ? 300 : jQuery(ddl).children().length * 28) + 'px' }

        );

        // Hide real dropdown controll
        //jQuery(dropDownListParent).show();
        jQuery(lb).click(
                function() {
                    if (jQuery(submn).is(':visible')) {
                        jQuery(ddlControll).attr('class', 'dropdown');
                        jQuery(ddlControll).css('z-index', 1);

                    } else {
                        selectIndexMax++;
                        
                        jQuery(ddlControll).css('z-index', selectIndexMax + 1);
                        jQuery(ddlControll).attr("class", "dropdown opened");
                        jQuery(ddl).find("a").get(jQuery(dropDownListParent).attr("selectedIndex")).focus();

                    }
                    return false;
                }
            );

    }
})
(jQuery);
var selectIndexMax = 99000;
function gc_initAjaxDropDowns() {
    jQuery(".ajax_dropdown").each(
                function(i) {

                    if (jQuery(this).attr("gc_init") != "true" && jQuery(this).find("select").length > 0) {
                       // alert("ajax_dropdown");
                        jQuery(this).gc_customDropDown();
                    } else if (jQuery(this).find("select").length == 0) {
                        jQuery(this).css({ display: "none" });
                    }
                    
                }
            );
}

jQuery(document).ready(function() {
    jQuery(".dropdown").each(
                function(i) {

                    if (jQuery(this).attr("gc_init") != "true") {
                       // alert("dropdown");
                        jQuery(this).gc_customDropDown();
                    }
                }
            );
});
    