(function($) {
    if (!$) { return; }

    $.fn.identify = function(prefix) {
        var i = 1;
        prefix = prefix || 'id_by_jquery_';
        return this.each(function() {
            if ($(this).attr('id')) { return; }
            while($('#' + prefix + i).length > 0)  { i++; }
            $(this).attr('id', prefix + i);
        });
    };

    var getPopupFunc = function(e) {
        var m = $(e).attr('class').match(/popup(\d+)x(\d+)/);
        if (m) {
            var l = e.href + (e.href.match(/\?/) ? "&" : "?") + "popup=true";
            var n = $(e).identify().attr('id');

            var options = [
                'scrollbars=' + ($(e).attr('class').match(/popupNoScroll/) ? 'no' : 'yes'),
                'width=' + m[1],
                'height=' + m[2]
            ];
            return function(evt) {
                var w = window.open(l, n, options.join(","));
                w.popupBacklinkTarget = window;
                evt.preventDefault();
                w.focus();
            };
        }
    };
    var localeCompareSupported = typeof ''.localeCompare != 'undefined';
    var mySort = function(a, b) {
        var s1 = (typeof a == 'object' && a.tagName) ?  $(a).html() : "" + a;
        var s2 = (typeof b == 'object' && b.tagName) ?  $(b).html() : "" + b;
        if (localeCompareSupported) {
            return s1.localeCompare(s2);
        }
        if (s1 == s2) {
            return 0;
        }
        return ([s1, s2].sort())[0] == a ? -1 : 1;
    };
    $(function() {
       $('a.popup').each(function() {
           var f = getPopupFunc(this);
           if (f) {
              $(this).click(f);
          }
       });
       $('a.popupBacklink').click(function(evt) {
           if (typeof popupBacklinkTarget != 'undefined') {
               popupBacklinkTarget.location.href = this.href;
               evt.preventDefault();
               window.close();
           }
       });
       $('select.autoSortOptions').each(function() {
           var f = $(this).find('option[value=""]').get(0);
           var s = $(this).find('option[value!=""]').each(function() {$(this).remove();}).sort(mySort);
           if (f) { this.appendChild(f); }
           for (var i = 0; i < s.length; i++) {
               this.appendChild(s[i]);
           }
       });
    });

    $.COL = $.COL ? $.COL : {};

    $.COL.jsonp = (function() {
        var id = 0;
        return function(url, callback, nameOverride) {
            var script = document.createElement("script");
            if (arguments.length < 4) {
                if (typeof callback != 'function') { callback = function() {}; }
                var name = nameOverride ? nameOverride : "__ColGCJSONP_" + (id++);
                window[name] = function(data) {
                    callback(data);
                    window[name] = undefined;
                };
                script.setAttribute("src", url + (url.indexOf("?") > 0 ? "&" : "?") + "callback=" + name);
                script.setAttribute("type", 'text/javascript');
            } else {
                script.setAttribute("src", url);
            }
            (document.getElementsByTagName("head")[0]).appendChild(script);
        };
    })();

})(jQuery);