var mailru = mailru || {};
mailru.loader = {
	modvers: {
		receiver: 1,
		proxy: 1,
		api: 5
	},
	_modulePath: 'http://connect.mail.ru/js/',
	_loaded: {},
	_readyCallbacks: {},
	require: function(module, onready){
		if(this._loaded[module]){
			onready();
		} else {
			if(!this._readyCallbacks[module]){
				this._readyCallbacks[module] = [];
				this._readyCallbacks[module].push(onready);
				var modver = (this.modvers[module] && ('?'+ this.modvers[module])) || '';
				document.URL.match(/testmode=1/) && (modver = '');
				this._loaded[module] = document.createElement('script');
				with(document.getElementsByTagName('head')[0].appendChild(this._loaded[module])){
					type = 'text/javascript';
					src = this._modulePath+ module+ '/'+ module+ '.js' +modver;
				}
			} else {
				this._readyCallbacks[module].push(onready);
			} 
						
		}
	},
	onready: function(module){
		if(this._readyCallbacks[module]){
			var cbs = this._readyCallbacks[module];
			for(var i=0; i<cbs.length; i++){
				cbs[i]();
			}
			this._loaded[module] = true;
		}
	}
}
mailru.isIE = /*@cc_on!@*/false;
mailru.isOpera = !!window.opera;
mailru.inited = false;
mailru.intercomType = ( window.postMessage )? 'event' : (((function(){var i,a,o,p,s="Shockwave",f="Flash",t=" 2.0",u=s+" "+f,v=s+f+".",rSW=RegExp("^"+u+" (\\d+)");if((o=navigator.plugins)&&(p=o[u]||o[u+t])&&(a=p.description.match(rSW)))return a[1];else if(!!(window.ActiveXObject))for(i=10;i>0;i--)try{if(!!(new ActiveXObject(v+v+i)))return i}catch(e){}return 0;})() < 10) ? 'hash' : 'flash');

mailru.init = function(onready, private_key, DOMFlashId){
	mailru.loader.require('api', function(){

		mailru.app.init(private_key);
		var e;
		if(DOMFlashId && (e=document.getElementById(DOMFlashId))){
			onready();
			mailru.events.listen('event', function(name, data){
				document.getElementById(DOMFlashId).mailruEvent(name, data);
			});
		}
	})
}
