var MyBegun = {
	ba: null,
	bj: null,
	r: false,
	
	pid: 0,
	bid: 0,
	mid: 0,
	kwd: '',
	num: 50,
	
	svs: ['pid','bid','mid'],//,'num'
	
	rs: [],
	
	sfc: 0,
	sfl: 10,
	
	p: function (ps){
		if(typeof(ps) != 'object') return;
		for(var i in MyBegun.svs){
			var n = MyBegun.svs[i];
			if(typeof(ps[n]) != 'undefined')
				MyBegun[n] = ps[n];
		};
	},
	
	gp: function (){
		var ps = {};
		for(var i in MyBegun.svs){
			var n = MyBegun.svs[i];
			if(typeof(MyBegun[n]) != 'undefined')
				ps[n] = MyBegun[n];
		};
		return ps;
	},
	
	i: function (ps){
		if(!MyBegun.bj || typeof(Begun)!="object"){
			MyBegun.bj = hajsu('http://ochecho.ru/autocontext2.js');
			add_timer(0.1, 'if(typeof(Begun)!="undefined"){MyBegun.r=true; MyBegun.ba=Begun.Autocontext; rm_timer(timer_id);}');
		};
		MyBegun.p(ps);
	},
	
	gf: function (misc_id){
		var bs = begunAds['banners']['autocontext'];
		var misc_id = typeof(misc_id) == 'undefined' ? 0 : new Number(misc_id);
		//alert('i am feed! '+bs);
		if(typeof(bs) != 'object') return;
		var use_oct = typeof(MyBegun.ba.Strings.js.banner_onclick) == 'string';
		for(var i in bs){
			bs[i]['onclick_tag'] = use_oct ? ' onclick="'+this.ba.Strings.js.banner_onclick+'"' : '';
			bs[i]['url'] = MyBegun.ba.addMisc2URL(misc_id, bs[i]['url']);
		};
		return bs;
	},
	
	ag: function (n){
		var n = typeof(n) != 'undefined' ? new Boolean(n) : false;
		begun_auto_pad = n ? null : new Number(MyBegun.pid);
		begun_block_id = n ? null : new Number(MyBegun.bid);
		begun_self_keywords = n ? null : new _s(MyBegun.kwd);
	},
	
	rl: function (a){
		var s = '';
		for(var i=0; i<a.length; i++)
			s += (i?',':'') + jse(a[i]);
		if(typeof(a[4]) == 'undefined'){
			var ri = MyBegun.rs.length;
			MyBegun.rs[ri] = MyBegun.gp();
			s += ','+ri;
		}
		var ev = 'MyBegun.s('+s+');';
		//alert(ev);
		add_timer(0.1, ev, false);
		return false;
	},
	
	s: function (kwd, block_tpl_id, item_tpl_id, show_div_id, ri){
		if(!MyBegun.r || !MyBegun.ba){
			return MyBegun.rl(arguments);
		};
		
		if(typeof(ri) != 'undefined')
			MyBegun.p(MyBegun.rs[ri]);
		
		MyBegun.kwd = kwd;
		MyBegun.ag();
		var b = MyBegun.ba;
		b.Pads.init();
		
		var sfi = MyBegun.sfc % MyBegun.sfl;
		MyBegun.sfc++;
		eval('__MBsf'+sfi+" = function(){ var bs=MyBegun.gf("+jse(MyBegun.mid)+"); var idt="+jse(block_tpl_id)+",idti="+jse(item_tpl_id)+",t=null,ti=null; if(!(t=gih(idt)))t=idt; if(!(ti=gih(idti)))ti=idti; if(!bs)return; t.tr({items:ti.tra(bs)})._h('"+show_div_id+"'); };");
		
		b.requestParams.pad_id = window.begun_auto_pad;
		b.requestParams.begun_self_keywords = window.begun_self_keywords;
		b.requestParams.jscall = '__MBsf'+sfi;
		if(typeof(begun_banner_filter) == 'object')
			b.requestParams.banner_filter = begun_banner_filter;
		b.prepareRequestParams();
		//b.requestParams.n = MyBegun.num;
		//b.requestParams.rq = MyBegun.sfc;
		var fu = b.Strings.urls.daemon + Begun.Utils.toQuery(b.requestParams);
		//alert(fu);
		hajsu(fu);
	},
	
_:null};


var pMyBegun = {
	bids: [
		[162788634,1],
		[162788654,2],
		[162788668,3],
		[162788903,4],
		[162222989,5],
		[162666691,7],
		[162788930,9],
		[162788934,12],
		[162788950,20],
		[162667161,100],
	],
	
	bid: 162222989,
	pid: 161601634,
	mid: 0,
	
	pi: function (ps){
		if(typeof(ps) == 'undefined')
			var ps = {};
		for(var i in MyBegun.svs){
			var n = MyBegun.svs[i];
			if(typeof(ps[n]) != 'undefined')
				pMyBegun[n] = ps[n];
		};
		//alert(pMyBegun.bid);
		MyBegun.i(pMyBegun);
	},
	
	i: function (ps){
		if(typeof(ps) != 'object' || typeof(ps.num) == 'undefined'){
			pMyBegun.pi(ps);
			return;
		};
		var n = new Number(ps.num);
		var bs = pMyBegun.bids;
		var li = 0;
		for(var i=0; i<bs.length; i++){
			var b = bs[i];
			if(b[1] == n){
				ps.bid = b[0];
				//alert(n+' equals '+b[1]);
				return pMyBegun.pi(ps);
			}else if(b[1] > n){
				//alert(n+' more than '+b[1]);
				var ln = b[li][1];
				ps.bid = n-ln > b[1]-n ? b[0] : bs[li][0];
				//alert(n+' selected '+ps.bid);
				return pMyBegun.pi(ps);
			}else{
				li = new Number(i);
			};
		};
		ps.bid = n > bs[li][1] ? bs[li][0] : bs[0][0];
		pMyBegun.pi(ps);
	},
	
_:null};
