/// <reference path="../intellisense/jquery-1.2.6-vsdoc-cn.js" />

function StrFormat(temp, dataarry) {
    return temp.replace(/\{([\d]+)\}/g, function(s1, s2) { var s = dataarry[s2]; if (typeof (s) != "undefined") { if (s instanceof (Date)) { return s.getTimezoneOffset() } else { return encodeURIComponent(s) } } else { return "" } });
}
(function($) {
    $.fn.DhoverClass = function(className) {
        return $(this).hover(function() { $(this).addClass(className); }, function() { $(this).removeClass(className); });
    }
    function getDulyOffset(target, w, h) {
        var pos = target.offset();
        var height = target.outerHeight();
        var newpos = { left: pos.left, top: pos.top + height - 1 }
        var bw = $(document).width();
        var bh = $(document).height();
        if ((newpos.left + w) >= bw) {
            newpos.left = bw - w - 2;
        }
        if ((newpos.top + h) >= bh && bw > newpos.top) {
            newpos.top = pos.top - h - 2;
        }
        return newpos;
    }

    function returnfalse() { return false; };
    //namespace
    $.ui = $.ui || {};
    $.fn.dropdown = function(opts) {
        var dialog = false;

        if (this[0])
            dialog = new $.ui.dropdown(opts, this[0]);

        return dialog;
    };
    $.ui.dropdown = function(o, elem) {
        var options = $.extend({
            vinputid: false,
            cssClass: "t-form-dropdown",
            containerCssClass: "dropdowncontainer",
            dropwidth: 100,
            dropheight: 200,
            showcolor: false,
            autoheight: true,
            selectedchange: false,
            items: [],
            picker: '<div class="dropIcon" align="middle" src="s.gif" />',
            selecteditem: false,
            isNoItems:false,
            selectedIndex: 0,
            parse: {
                name: "list",
                render: function(parent) {
                    var p = this.target;
                    var ul = $("<ul/>");
                        if (this.items && this.items.length > 0) {
                            $.each(this.items, function() {
                                var item = this;
                                var li = $("<li/>").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); }).html(item.text)
                                .click(function() {
                                    p.SelectedChanged(item); 
                                });
                                //selected style
                                if (options.showcolor) {
                                    li.css("color", item.value);
                                }
                                if (options.selecteditem && options.selecteditem.text == item.text) {
                                    li.addClass("selected");
                                }
                                ul.append(li);
                            });
                        }
                    parent.empty();
                    parent.append(ul);

                },
                items: [],
                setValue: function(item) { },
                target: null
            }
        }, o);
        var me = $(elem).css({"width":options.dropwidth,"cursor":"pointer"}).attr("readonly",true);
        this.wrapper = $(elem).wrap("<div/>").parent().addClass("myui-form-div-dropdown");
        var hidden = false;
        if (options.vinputid) {
            hidden = $("<input type='hidden' />").attr("id", options.vinputid).appendTo(this.wrapper);
        }
        var picker = $("<div />").appendTo(this.wrapper).addClass("dropicon").css("left", me.width());

        this.setSelectedItem = function(selectItem) {
            options.selecteditem = selectItem;
            me.val(selectItem.text);
            if (hidden && selectItem.value) {
                hidden.val(selectItem.value);
            }
        }
        this.changeItems = function(Items) {
            options.items = Items;
        }
        if (!options.selecteditem && options.items && options.items.length > 0) {
            if(options.isNoItems==true)
            {
            }
            else
            {
                options.selecteditem = options.items[options.selectedIndex];
            }
        }
        if (options.selecteditem) {
            me.val(options.selecteditem.text);
            if (hidden && options.selecteditem.value) {
                hidden.val(options.selecteditem.value);

            }
        }
        if (options.dropwidth) {
            //options.dropwidth = me.outerWidth();
            options.dropwidth = (me.outerWidth() > 2 ? me.outerWidth() - 2 : me.outerWidth());
        }
        var d = $("<div/>").addClass("myui-form-dropdown-container")
                           .css({ position: "absolute", "z-index": "999", "overflow": "auto", width: options.dropwidth + 9, display: "none", "border": "solid 1px #B5B8C8" })
                           .click(function(event) { event.stopPropagation(); })
                           .appendTo($("body"));
        if (options.autoheight) {
            d.css("max-height", options.dropheight);
        }
        else {
            d.css("height", options.dropheight);
        }

        me.click(function() {
            var m = this;
            options.parse.items = options.items;
            options.parse.render(d);
            var pos = getDulyOffset(me, options.dropwidth, options.dropheight);
            d.css(pos);
            d.show();
            if ($.browser.msie) {
                if (parseFloat($.browser.version) <= 6) {
                    $(d).bgiframe();
                    var h = d.height();
                    if (h > options.dropheight) {
                        d.height(options.dropheight);
                    }
                }
            }
            $(document).one("click", function(event) { d.hide(); });
            return false;
        });
        $(picker).click(function() {
            var m = this;
            options.parse.items = options.items;
            options.parse.render(d);
            var pos = getDulyOffset(me, options.dropwidth, options.dropheight);
            d.css(pos);
            d.show();
            if ($.browser.msie) {
                if (parseFloat($.browser.version) <= 6) {
                    $(d).bgiframe();
                    var h = d.height();
                    if (h > options.dropheight) {
                        d.height(options.dropheight);
                    }
                }
            }

            $(document).one("click", function(event) { d.hide(); });
            return false;

        });
        me.SelectedChanged = function(t) {
            var b = true;
            me.val(t.text);
            if (hidden && t.value) {
                hidden.val(t.value);
            }

            options.selecteditem = t;
            if (options.selectedchange) {
                b = options.selectedchange.apply(me, [t]);
            }
            d.hide();
        };
        me.Cancel = function() {
            d.hide();
        }
        options.parse.target = me;
    }

})(jQuery);