// fix badly broken shift as imported from some versions of prototype.js	
try {
	[].shift();
}
catch (e) {
    Array.prototype.shift = function() {
        var result = this[0];
        for (var i = 0; i < this.length - 1; i++)
            this[i] = this[i + 1];
        if (this.length>0) {
            this.length--;
        }
        return result;
    }
}

djConfig = {
	scopeMap: [['dojo', 'toksta']],
	baseUrl:  'http://embed.toksta.com/toksta/'
};

var TOKSTA = {
	VERSION : '3306', // will be replaced with content from file named VERSION by Makefile
	baseUrl : 'http://embed.toksta.com/',
	require: function(libraryName) {
		// inserting via DOM fails in Safari 2.0, so brute force approach
		document.write('<script type="text/javascript" charset="utf-8" src="'+libraryName+'"></script>');
	},
	load: function() {
		var includes = {
			main: 
			[
				'http://o.aolcdn.com/dojo/1.2/dojo/dojo.xd.js',
				'ads/chatbar.js.var',
				'im/lang/chatbar/text.js.var',
				'toksta_im.js',
				'toksta_app_config.js',
				'toksta_bar_init.js'
			],
			controller:
			[
				'http://o.aolcdn.com/dojo/1.2/dojo/dojo.xd.js',
				'toksta_im.js',
				'toksta_controller.js'
			],
			frame:
			[
				'http://o.aolcdn.com/dojo/1.2/dojo/dojo.xd.js',
				'toksta_im.js',
				'toksta_frame.js'
			]
		};
		var scripts = document.getElementsByTagName("script");
		var selfSrc = '';
		for (var i=0; i<scripts.length; ++i) {
			if (scripts.item(i).src && scripts.item(i).src.match(/\/toksta.js/)) {
				selfSrc = scripts.item(i).src;
				selfSrc = selfSrc.substring(selfSrc.indexOf("toksta.js"));
				break;
			}
		}
		if (selfSrc.indexOf("controller") != -1)
			includes = includes.controller;
		else if (selfSrc.indexOf("frame") != -1)
			includes = includes.frame;
		else
			includes = includes.main;
		for (var i=0; i<includes.length; i++)
			if (includes[i].indexOf('http://') == 0)
				this.require(includes[i]);
		else
			this.require(this.baseUrl+includes[i]+'?'+this.VERSION);
	}

};
TOKSTA.load();
