/*
 * licenses:
 * parseUri 1.2.2 (c) Steven Levithan <stevenlevithan.com>  MIT License
*/

var set_scope = function(fn, scope) {
    return function() {
        fn.apply(scope, arguments);
    }
};

var set_scope_arg = function(fn, scope) {
    return function() {
        var args = [];
        for (var i = 0; i < arguments.length; i++) {
            args.push(arguments[i]);
        }
        args.push(scope);
        fn.apply(this, args);
    }
};

(function($) {


    $.extend({
        pkg: function(ns, obj) {
                ns = ns.split('.');
                var cur = window, i;
                while (i = ns.shift()) {
                        cur = cur[i] || ( cur[i] = {} );
                }
                if (obj) {
                        jQuery.extend(cur, obj);
                }
            }
    });
    $.fn.extend({
        parse_id: function(key) {
            return this.attr('id').replace(key + '_', '');
        },
        parse_class: function(key) {
            var re = new RegExp(key + '_([a-z0-9]+)');
            var m = this.attr('className').match(re);
            if (m && m[0] && m[1]) {
                return m[1];
            } else {
                return null;
            }
        }
    });

    var pareseUri_options = {
        strictMode: false,
        key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
        q:   {
            name:   "queryKey",
            parser: /(?:^|&)([^&=]*)=?([^&]*)/g
        },
        parser: {
            strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
            loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
        }
    };

    $.extend({
        _: function(s) {
            return s
        },
        notify: function(msg, cfg) {
            var config = $.extend({
                pnotify_text: msg,
                pnotify_hide: true,
                pnotify_history: false,
                pnotify_width: "980px",
                pnotify_closer: false,
                pnotify_delay: 2500
            }, cfg);
            $.pnotify(config)
        },
        parseUri: function(str) {
            var o   = pareseUri_options,
                m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
                uri = {},
                i   = 14;

            while (i--) uri[o.key[i]] = m[i] || "";

            uri[o.q.name] = {};
            uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
                if ($1) uri[o.q.name][$1] = $2;
            });

            return uri;
        },
        get_host: function(host) {
           return host.split('.').slice(-2).join('.');
        }
    });

    $(document).ready(function() {
        $('#_lang_select').bind('change', function(event) {
            var form = $('#_lang_form');
            form.find('input[name=lang]').val($(this).val());
            form.submit();
        });
    });
}(jQuery));