/**
window.begun_utf8
window.begun_koi8
window.begun_auto_pad
window.begun_auto_limit
window.begun_top_id
window.begun_bot_id
window.begunTopB
window.begunBottomB
*/

window.begun_third_position = true;
window.begun_big_limit = true;
window.begun_behav = true;
window.begun_footer_block = 'second';

if (typeof Array.prototype.push == 'undefined'){
    Array.prototype.push = function() {
        var a = arguments,
        b = a.length,
        c = this.length;
        for(var i = 0; i < b; ++i)
        this[c + i] = a[i];
        return this.length;
    }
}

BegunA = new Object();
//feed daemon url
//BegunA.autoUrl          = './context.jsp/context.jsp?';
BegunA.autoUrl      = 'http://autocontext.begun.ru/context.jsp?';
BegunA.lastModTime  = Date.parse(document.lastModified) / 1000;
BegunA.referrerUrl  = document.referrer;
BegunA.TopB         = new Array();
BegunA.BottomB      = new Array();
BegunA.FooterB      = new Array();
BegunA.BehoverB     = new Array();
BegunA.pageUrl      = document.location;
BegunA.pageLocation = document.location;
BegunA.positionBlock= 0;
BegunA.countInsert  = 0;
BegunA.begunBanners = null;
BegunA.begunBehover = null;
BegunA.begunStubs   = null;
BegunA.ppcallArray  = null;
BegunA.ppcallFormArray  = null;
BegunA.ppcallBehov  = null;
BegunA.begunParams  = null;
BegunA.behav        = false;
BegunA.useThird     = false;
BegunA.bigLimit     = false;
BegunA.cssTpl = '\
					<style>\
						ul.begun_adv {\
							font-family: arial,sans-serif;\
							padding: 0;\
							list-style-type:disc;\
						}\
						ul.begun_adv .text {\
							color: #000;\
						}\
						ul.begun_adv .text a {\
							color: #000;\
							text-decoration:none;\
						}\
						ul.begun_adv a.BegunAGreen,\
						ul.begun_adv a.BegunAGreen:active,\
						ul.begun_adv a.BegunAGreen:visited,\
						ul.begun_adv a.BegunAGreen:hover {\
							color: #00803F;\
							text-decoration: none;\
						}\
						span.BegunAGreen {\
							color: green;\
						}\
						ul.begun_adv li {\
							position: relative;\
						}\
					</style>\
';

BegunA.tpls = new Object();
BegunA.tpls['ppcall_title']			= '<a href="javascript:void(0);" onClick="Begun.Ppcall.showEnterForm(#PPi#, this, event, null, \'#link#\', #is_mobile#, \'#directUrl#\');" class="BegunAGreen" title="&#1047;&#1074;&#1086;&#1085;&#1086;&#1082;&#32;&#1073;&#1077;&#1089;&#1087;&#1083;&#1072;&#1090;&#1085;&#1099;&#1081;">&#1047;&#1074;&#1086;&#1085;&#1080;&#1090;&#1100;</a>';
BegunA.tpls['click_source']         = ' onclick=" R(this, \'&bid=#id#&id=#i#\'); return true;" ';
BegunA.tpls['Card_noPPcall']		= ' <span ><span style="white-space: nowrap"><img src="http://se.begun.ru/phone_icon.gif" style="width:12px;height:8px;border:none;" alt="" />&nbsp;<a href="#item_6#" target="_blank" class="BegunAGreen">&#1050;&#1086;&#1085;&#1090;&#1072;&#1082;&#1090;&#1099;</a></span></span>';
BegunA.tpls['Card_PPcall']		= ' <span ><span style="white-space: nowrap"><img src="http://se.begun.ru/phone_icon.gif" style="width:12px;height:8px;border:none;" alt="" />&nbsp;<a href="#item_6#" target="_blank" class="BegunAGreen">&#1050;&#1086;&#1085;&#1090;&#1072;&#1082;&#1090;&#1099;</a></span>&nbsp;<span style="color: #aaa;">&#149;</span>&nbsp;#ppcall_title#</span>';
BegunA.tpls['Url_noPPcall']		= ' <span ><a style="white-space: nowrap" href="#item_2#" target="_blank" #click_source# class="BegunAGreen" >#item_4#</a></span>';
BegunA.tpls['Url_PPcall']		= ' <span ><a style="white-space: nowrap" href="#item_2#" target="_blank" #click_source# class="BegunAGreen">#item_4#</a>&nbsp;<span style="white-space: nowrap"><img src="http://se.begun.ru/phone_icon.gif" style="width:12px;height:8px;border:none;" alt="" />&nbsp;#ppcall_title#</span></span>';
BegunA.tpls['CardUrl_noPPcall']	= ' <span ><a style="white-space: nowrap" href="#item_2#" target="_blank" #click_source# class="BegunAGreen">#item_4#</a>&nbsp;<span style="white-space: nowrap"><img src="http://se.begun.ru/phone_icon.gif" style="width:12px;height:8px;border:none;" alt="" />&nbsp;<a href="#item_6#" target="_blank" class="BegunAGreen" >&#1050;&#1086;&#1085;&#1090;&#1072;&#1082;&#1090;&#1099;</a></span></span>';
BegunA.tpls['CardUrl_PPcall']		= ' <span ><a style="white-space: nowrap" href="#item_2#" target="_blank" #click_source# class="BegunAGreen">#item_4#</a>&nbsp;<span style="white-space: nowrap"><img src="http://se.begun.ru/phone_icon.gif" style="width:12px;height:8px;border:none;" alt="" />&nbsp;<a href="#item_6#" target="_blank" class="BegunAGreen">&#1050;&#1086;&#1085;&#1090;&#1072;&#1082;&#1090;&#1099;</a></span> <span style="color: #aaa;">&#149;</span>&nbsp;#ppcall_title#</span>';
BegunA.tpls['TopBunnersInner']	= '<li>\
									<a class="title" href="#item_2#" target="_blank"  #click_source#>#item_0#</a>\
									<p class="text">\
										<a href="#item_2#" target="_blank" #click_source#>#item_1#</a> &mdash; #show_info#\
									</p>\
								</li>';

BegunA.tpls['TopBunners']			= '	<ul class="begun_adv">\
											#TopBunnersInner#\
										</ul>';

BegunA.tpls['BottomBunners']		= '	<ul class="begun_adv" id="BegunBottomBunners">\
                                            <li style="display:inline"><!--AdsNumber0--></li>\
                                            #BottomBunnersInner#\
                                         </ul>\
										#ShowBottomStub#\
										<br />\
										<a href="http://www.begun.ru/advertiser/?r1=Begun&r2=adbegun_rambler" class="text BegunABlue" target="_blank">&#1056;&#1072;&#1079;&#1084;&#1077;&#1089;&#1090;&#1080;&#1090;&#1100; &#1088;&#1077;&#1082;&#1083;&#1072;&#1084;&#1091; &#1079;&#1076;&#1077;&#1089;&#1100;</a>\
                                        <div id="begunSpanBehover" style="margin-top: 50px"><br /></div>';

BegunA.tpls['BehoverBunners']        = '<h3>&#1042;&#1086;&#1079;&#1084;&#1086;&#1078;&#1085;&#1086;&#44;&#32;&#1074;&#1072;&#1084;&#32;&#1090;&#1072;&#1082;&#1078;&#1077;&#32;&#1073;&#1091;&#1076;&#1077;&#1090;&#32;&#1080;&#1085;&#1090;&#1077;&#1088;&#1077;&#1089;&#1085;&#1086;</h3>\
                                        <ul class="begun_adv" id="BegunBehoverBunners">\
                                            #BehoverBunnersInner#\
                                        </ul>';
                                        
BegunA.tpls['BottomBunnersInner']	= ' <li>\
											<a class="title" href="#item_2#" target="_blank" #click_source#>#item_0#</a>\
											<p class="text">\
												<a href="#item_2#" target="_blank" #click_source#>#item_1#</a>\
											</p>\
											<p class="spec">#show_info#</p>\
										</li>\
										<!--AdsNumber#AdsNumber#-->';

BegunA.tpls['SearchBannerForButtomGear']     = '#ShowSearchBanner#'


BegunA.tpls['SearchBanner']         = '#ShowSearchBanner#';

BegunA.tpls['SearchBannerSWF']        = '<li>\
                                            <OBJECT  classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" WIDTH="240" HEIGHT="400">\
                                                <PARAM NAME="movie" VALUE="#SOURCE#&link1=#HREF#">\
                                                <PARAM NAME="wmode" VALUE="opaque">\
                                                <PARAM NAME="allowScriptAccess" VALUE="always">\
                                                <PARAM NAME="quality" VALUE="high">\
                                                <EMBED src="#SOURCE#&link1=#HREF#" quality="high" WIDTH="240" HEIGHT="400" TYPE="application/x-shockwave-flash" wmode="opaque"></EMBED>\
                                            </OBJECT>\
                                        </li>';

BegunA.tpls['SearchBannerJS']         = '<li id="begunJsBanner">#begunJsBannerString#</li>';
BegunA.tpls['SearchBannerJScript']         = '<script language="JavaScript" src="#SOURCE#"/>';

BegunA.tpls['SearchBannerIMG']        = ' <a href="#HREF#" target="_blank"><img src="#SOURCE#&redir=1" border="0" width="240" height="400"></a>';

// replace string
BegunA.str_replace = function(search, replace, source) {
    return source.replace(new RegExp(search,'gi'), replace);
}

//old function - quoted strung
BegunA.quoted = function(str) {
    return (str != null) ? '"' + str + '"' : '""';
}

//old function - quoted escape
BegunA.escape = function(str) {
    return escape(str);
}

//function create uri params
BegunA.append = function(param, value) {
    if (typeof(value) =="undefined" || !value){return;}
    BegunA.autoUrl += '&' + param + '=' + BegunA.escape(value);
}

//Init
BegunA.Init = function() {
    if (typeof(window.begun_utf8)!="undefined") {
        BegunA.append('begun_utf8', window.begun_utf8);
    }
    if (typeof(window.begun_koi8)!="undefined") {
        BegunA.append('begun_koi8', window.begun_koi8);
    }
    if (typeof(window.begun_behav) != "undefined" && window.begun_behav) {
        this.behav = true;
        BegunA.append('behav', '1');
    }
    BegunA.append('ref', BegunA.pageUrl);
    BegunA.append('pad_id', window.begun_auto_pad);
    //check pad id
    if (window.begun_auto_pad == 5831331) {
        BegunA.append('n', 14);
    } else {
        BegunA.append('n',window.begun_auto_limit);
    }
    if (screen && screen.width && screen.height) {
        BegunA.append('ut_screen_width', screen.width);
        BegunA.append('ut_screen_height', screen.height);
    }
    this.useThird = (typeof(window.begun_third_position) != "undefined" && window.begun_third_position);
    BegunA.bigLimit = (typeof(window.begun_big_limit) != "undefined" && begun_big_limit);
    BegunA.append('sense_mode', 'custom');
    BegunA.append('rnd', Math.random());
    document.write('<hr style="display:none;" />'+BegunA.cssTpl);
    document.write('<script src="' + BegunA.autoUrl.replace('?&','?') +'"></script>');
}

//check is not a context
BegunA.is_NonContext = function(param) { //SETS [Adult, Law, NonContext]
    var tmp = new String(param);
    if (tmp.indexOf('NonContext')!=-1){return true;}
    return false;
}

//logic for mini-sites
BegunA.logicItem = function(item) {
    if (typeof(item[7])=="undefined"){item[7]='Card, Url';}
    if (item[7]!='Card, Url' && item[7]!='Card' && item[7]!='Url'){item[7]='Card, Url';}

    if (typeof(item[2])=="undefined"){item[2]='';}
    if (typeof(item[6])=="undefined"){item[6]='';}

    if (item[2]=='' && item[6]==''){return item;}
    if (item[2]=='' && item[6]!=''){item[7]='Card';}
    if (item[2]!='' && item[6]==''){item[7]='Url';}
    if (item[2]!='' && item[6]!=''){item[7]=item[7];}
    switch (item[7]) {
        case 'Card':        	{item[2]=item[6]; break;}
        case 'Card, Url':    	{break;}
        case 'Url':				{ break;}
    }
    return item;
}

//auto init text on demon loaded
BegunA.onDaemonLoaded = function(begunBanners, begunStubs, ppcallArray, begunParams, begunBehaviourBanners, ppcallBehov, ppcallFormArray) {
    if ((!document.getElementById('begunSpanTop')  && !document.getElementById('begunSpanBottom')) || (!begunBanners && !begunBehaviourBanners))
        return false;

    BegunA.begunBanners = begunBanners;
    BegunA.begunBehover = begunBehaviourBanners;
    BegunA.begunStubs   = begunStubs;
    BegunA.ppcallArray  = ppcallArray;
	BegunA.ppcallFormArray = ppcallFormArray;
    BegunA.begunParams  = begunParams;
    BegunA.ppcallBehov  = ppcallBehov;
    BegunA.allocateBanners();

	//include auto_ppcall.js if ppcall commercial has been found
	if (typeof(BegunA.ppcallArray) != "undefined" && typeof(BegunA.ppcallArray.length) != "undefined") {
		if (BegunA.ppcallArray.toString().indexOf(1) !== -1) {
			document.write('<scr' + 'ipt src="http://autocontext.begun.ru/auto_ppcall.js" type="text/javascript"></scr' + 'ipt>');
		}
	}
	if (typeof(BegunA.ppcallBehov) != "undefined" && typeof(BegunA.ppcallBehov.length) != "undefined") {
		if (BegunA.ppcallBehov.toString().indexOf(1) !== -1) {
			document.write('<scr' + 'ipt src="http://autocontext.begun.ru/auto_ppcall.js" type="text/javascript"></scr' + 'ipt>');
		}
	}

    if (document.getElementById('begunSpanTop') ) {
        BegunA.ShowTop( BegunA.TopB );
    }
    if (document.getElementById('begunSpanBottom') ) {
        this.ShowBottom();
    }
    var bSpan = document.getElementById('begunSpanBehover');
    if (bSpan) {
        this.ShowBehover(bSpan);
    }
    if (document.getElementById('begunFooterBottom') ) {
        BegunA.ShowFooter( BegunA.FooterB );
    }

    window.begunTopB =  BegunA.TopB;
    window.begunBottomB  = BegunA.BottomB;
    window.begunFooterB  = BegunA.FooterB;

    document.write('<img src="http://counter.begun.ru/blockcounter.gif?pad_id=5831331&top=' + BegunA.TopB.length + '&side=0&bottom=' + BegunA.BottomB.length  + '&footer=' + BegunA.FooterB.length  + '&behaver=' + BegunA.BehoverB.length + '&rnd=' + Math.random() + '">');
    return true;
}

//auto init image on demon loaded
BegunA.onDaemonLoadedImage = function(begunGraphBanners) {
    BegunA.begunGraphBanners = begunGraphBanners;
    BegunA.loadSearchBanner(BegunA.begunGraphBanners);
    return true;
}

BegunA.prepeaUrl = function(key, bCollection) {
    if (typeof(bCollection[key][4]) == "undefined"){
        bCollection[key][4]='';
    }
    return;
}

//add to TopArray
BegunA.pushOnTop = function(key) {
    var topLimit = 2;
    if (this.useThird) {
        topLimit = 3;
    }
    if (this.TopB.length >= topLimit ) {
        return false;
    } else if (this.useThird && this.TopB.length == 2 && this.begunBanners[key][3] <= 0.3) {
        return false;
    } else if (this.is_NonContext(this.begunBanners[key][8])) {
        return false;
    }
    this.prepeaUrl(key, this.begunBanners);
    this.begunBanners[key][2] += '&misc2=256';
    if (typeof(window.begun_bold_header) != "undefined" && window.begun_bold_header) {
        this.begunBanners[key][0] = this.begunBanners[key][0];
    }
    this.TopB.push(key);
    this.countInsert++;
    return true;
}

//add to BottomArray
BegunA.pushOnBottom = function(key) {
    var countBlock = this.BottomB.length;
    if (this.bigLimit) {
        if (this.countInsert >= 12 || countBlock >= 10 || ( countBlock >= 5 && this.begunBanners[key][3] <= 0.25 ) ) {
            return false;
        }
    } else if (countBlock >= 5) {
        return false;
    }
    this.prepeaUrl(key, this.begunBanners);
    if (!this.TopB.length) {
        this.begunBanners[key][2] += '&misc2=768';
    } else {
        this.begunBanners[key][2] += '&misc2=512';
    }
    this.BottomB.push(key);
    this.countInsert++;
    return true;
}

//add to FooterArray
BegunA.pushOnFooter = function(key) {
    if (this.FooterB.length >= 2 || this.begunBanners[key][3] < 0.10 ) {
        return false;
    } else if ( this.is_NonContext(this.begunBanners[key][8]) ) {
        return false;
    }
    this.prepeaUrl(key, this.begunBanners);
    this.begunBanners[key][2] += '&misc2=1024';
    this.FooterB.push(key);
    this.countInsert++;
    return true;
}

BegunA.clone = function(o) {
    if (!o || 'object' !== typeof o)  {
        return o;
    }
    var c = 'function'=== typeof o.pop ? [] : {};
    var p, v;
    for(p in o) {
        if (o.hasOwnProperty(p)) {
            v = o[p];
            if (v && 'object' === typeof v) {
                c[p] = this.clone(v);
            } else {
                c[p] = v;
            }
        }
    }
    return c;
}

//duplicate to FooterArray
BegunA.pushOnFooterEx = function(key) {
    if ( this.is_NonContext(this.begunBanners[key][8]) ) {
        return false;
    }
    //this.prepeaUrl(key, this.begunBanners);
    this.begunBanners[key + 'ex'] = this.clone(this.begunBanners[key]);
    this.begunBanners[key + 'ex'][2] = this.begunBanners[key + 'ex'][2].replace('misc2=256', 'misc2=1024');
    this.FooterB.push(key + 'ex');
    this.countInsert++;
    return true;
}
                           
//add to BehoverArray
BegunA.fillBehoverBanner = function() {
    if (this.countInsert > 3 || !this.begunBehover) {
        return false;
    }
    var countBlock = 0;
    for (key in this.begunBehover) {
        if (this.bigLimit) {
            if (this.countInsert >= 12 || countBlock >= 10) {
                break;
            }
        } else if (countBlock >= 5) {
            break;
        }
		if (this.begunBehover[key][3] <= 0.25) {
			continue;
		}
        this.prepeaUrl(key, this.begunBehover);
        this.begunBehover[key][2] += '&misc2=1280';
        this.BehoverB.push(key);
        this.countInsert++;
        countBlock++;
    }
}

//fill banners array to parse
BegunA.allocateBanners = function() {
    if (typeof(BegunA.begunBanners) == "undefined" && typeof(BegunA.begunBehover) == "undefined" )
        return;
    
    for(key in this.begunBanners) {
        if (this.pushOnTop(key)) {
            continue;
        }
        if (typeof(window.begun_footer_block) != "undefined" && window.begun_footer_block && document.getElementById('begunFooterBottom')) {
            if (window.begun_footer_block == 'first') {
                if (this.pushOnFooter(key)) 
                    continue;
                if (this.pushOnBottom(key)) 
                    continue;
            } else if (window.begun_footer_block == 'second') {
                if (this.pushOnBottom(key))
                    continue;
                if (this.pushOnFooter(key)) 
                    continue;
            }
        } else {
            if (this.pushOnBottom(key))
                continue;
        }
    }

    //put 2 banners from top to footer
    if (!this.FooterB.length){
        if (this.TopB[0]) this.pushOnFooterEx(this.TopB[0]);
        if (this.TopB[1]) this.pushOnFooterEx(this.TopB[1]);
    }

	if (this.behav) {
        this.fillBehoverBanner();
    }
}

//create top banner
BegunA.ShowTop = function( bann ) {
	var tpl 				= BegunA.tpls['TopBunners'];
	var TopBunnersInner 	= '';
    this.positionBlock = 0;
    for (key in bann) {
        var bKey = bann[key];
        TopBunnersInner+=BegunA.TopParseItem(this.begunBanners[bKey], bKey, this.ppcallArray);
    }
    tpl = BegunA.str_replace('#TopBunnersInner#', TopBunnersInner, tpl);
    tpl = BegunA.str_replace("[\t]{1,}", '', tpl);
    document.getElementById('begunSpanTop').innerHTML = tpl;
}

//create footer banner
BegunA.ShowFooter = function( bann ) {
    var tpl = BegunA.tpls['TopBunners'];
    var TopFooterInner = '';
    this.positionBlock = 0;
    for (key in bann) {
        var bKey = bann[key];
        TopFooterInner += BegunA.FooterParseItem(this.begunBanners[bKey], bKey, this.ppcallArray);
    }
    tpl = BegunA.str_replace('#TopBunnersInner#', TopFooterInner, tpl);
    tpl = BegunA.str_replace("[\t]{1,}", '', tpl);
    document.getElementById('begunFooterBottom').innerHTML = tpl;
}

//standart item parsed
BegunA.AnyItemParse = function(item, tpl, tplClick, tplInfo) {
    tpl = BegunA.str_replace('#click_source#', tplClick, tpl);
    tpl = BegunA.str_replace('#show_info#', tplInfo, tpl);
    tpl = BegunA.str_replace('#item_0#', item[0], tpl);
    tpl = BegunA.str_replace('#item_1#', item[1], tpl);
    tpl = BegunA.str_replace('#item_2#', item[2], tpl);
    tpl = BegunA.str_replace("[\t]{1,}", '', tpl);
    return tpl;
}

//parse one top banner
BegunA.TopParseItem = function(item, i, pCollection) {
    item = BegunA.logicItem(item);
    var clk_source 	= BegunA.setClickProc(++this.positionBlock, window.begun_top_id, BegunA.tpls['click_source']);
    var tpl 		= BegunA.tpls['TopBunnersInner'];
    var show_info 	= BegunA.ParseBannerInfo(item, i, pCollection, window.begun_top_id);
    return this.AnyItemParse(item, tpl, clk_source, show_info);
}

//parse bottom banner
BegunA.BottomParseItem = function(item, i, pCollection, clkVar) {
    item = BegunA.logicItem(item);
    var clk_source  = BegunA.setClickProc(++this.positionBlock, clkVar, BegunA.tpls['click_source']);
    var tpl         = BegunA.tpls['BottomBunnersInner'];
    var show_info   = BegunA.ParseBannerInfo(item, i, pCollection, clkVar);
    return this.AnyItemParse(item, tpl, clk_source, show_info);
}

//parse one top banner
BegunA.FooterParseItem = function(item, i, pCollection) {
    item = BegunA.logicItem(item);
    var clk_source  = BegunA.setClickProc(++this.positionBlock, window.begun_foot_id, BegunA.tpls['click_source']);
    var tpl         = BegunA.tpls['TopBunnersInner'];
    var show_info   = BegunA.ParseBannerInfo(item, i, pCollection, window.begun_foot_id);
    return this.AnyItemParse(item, tpl, clk_source, show_info);
}

BegunA.setClickProc = function(i, blockId, tpl) {
    if (typeof blockId == "undefined") {
        return '';
    }
    tpl = BegunA.str_replace('#id#', blockId, tpl);
    tpl = BegunA.str_replace('#i#', parseInt(i), tpl);
    tpl = BegunA.str_replace("[\t]{1,}", '', tpl);
    return tpl;
}

//parse ppcall
BegunA.getPPCallTitle = function(item, i, pCollection) {
	var tpl = BegunA.tpls['ppcall_title'];
	var url = new String(item[2]);
	var is_mobile = Number(BegunA.begunParams && BegunA.begunParams.is_mobile);
	url = url.substr(url.indexOf('url=')+4 );
	tpl = BegunA.str_replace('#PPurl#', url, tpl);
	tpl = BegunA.str_replace('#PPi#', i, tpl);
	tpl = BegunA.str_replace('#is_mobile#',is_mobile, tpl);
	var directUrl = (pCollection && pCollection[i] == 1?item[2]:'');
	tpl = BegunA.str_replace('#directUrl#', directUrl, tpl);
	if (pCollection === this.ppcallArray) {
		if (this.ppcallFormArray && this.ppcallFormArray[i]) {
			tpl = BegunA.str_replace('#link#', this.ppcallFormArray[i], tpl);
		}
	} else {
		if (window.ppcallBehaviourFormArray && window.ppcallBehaviourFormArray[i]) {
			tpl = BegunA.str_replace('#link#', window.ppcallBehaviourFormArray[i], tpl);
		}
	}

	tpl = BegunA.str_replace("[\t]{1,}", '', tpl);
    return tpl;
}

//parse bottom banner info & logging
BegunA.ParseBannerInfo = function(item, i, pCollection, blockId) {
    var ppcall = false;
    if (pCollection && pCollection[i]) {
        ppcall=true;
    }
	if (ppcall) {
		if (typeof BegunA.ppcallsShown === "undefined") {
			BegunA.ppcallsShown = 1;
		} else {
			BegunA.ppcallsShown++;
		}
	}
    var tpl = '';
    if (item[7]=='Card' && ppcall==false) {
    	tpl = BegunA.tpls['Card_noPPcall'];
    }
    else if (item[7]=='Card' && ppcall==true) {
		tpl = BegunA.tpls['Card_PPcall'];
    }
    else if (item[7]=='Url' && ppcall==false) {
    	tpl = BegunA.tpls['Url_noPPcall'];
    }
    else if (item[7]=='Url' && ppcall==true) {
		tpl = BegunA.tpls['Url_PPcall'];
    }
    else if (item[7]=='Card, Url' && ppcall==false) {
		tpl = BegunA.tpls['CardUrl_noPPcall'];
    }
    else if (item[7]=='Card, Url' && ppcall==true) {
		tpl = BegunA.tpls['CardUrl_PPcall'];
    }
    var ppcall_title = BegunA.getPPCallTitle(item, i, pCollection);
	var clk_source = BegunA.setClickProc(this.positionBlock, blockId, BegunA.tpls['click_source']);
	tpl = BegunA.str_replace('#item_2#', item[2], tpl);
    tpl = BegunA.str_replace('#item_4#', item[4], tpl);
    tpl = BegunA.str_replace('#item_6#', item[6], tpl);
	tpl = BegunA.str_replace('#click_source#', clk_source, tpl);
    tpl = BegunA.str_replace('#ppcall_title#', ppcall_title, tpl);
    tpl = BegunA.str_replace("[\t]{1,}", '', tpl);
    return tpl;
}

//create bottom banner
BegunA.ShowBottom = function() {
	var tpl = BegunA.tpls['BottomBunners'];
	var BottomBunnersInner = '';
	var ShowBottomStub = BegunA.ShowBottomStub();
    var bannerPos = 0;
    this.positionBlock = 0;
    for (key in this.BottomB) {
        var bKey = parseInt(this.BottomB[key]);
        BottomBunnersInner+= BegunA.str_replace('#AdsNumber#', ++bannerPos, BegunA.BottomParseItem(this.begunBanners[bKey], bKey, this.ppcallArray, window.begun_bot_id));
    }
    tpl = BegunA.str_replace('#BottomBunnersInner#', BottomBunnersInner, tpl);
	tpl = BegunA.str_replace('#ShowBottomStub#', ShowBottomStub, tpl);
	tpl = BegunA.str_replace("[\t]{1,}", '', tpl);
    document.getElementById('begunSpanBottom').innerHTML = tpl;
}

BegunA.ShowBehover = function(bSpan) {
    if (this.BehoverB.length <= 0) {
        return '';
    }
    var tpl = BegunA.tpls['BehoverBunners'];
    var BehoverInner = '';
    this.positionBlock = 0;
    for (key in this.BehoverB) {
        var bKey = parseInt(this.BehoverB[key]);
        BehoverInner+= BegunA.str_replace('#AdsNumber#', 'B' + (bKey + 1), BegunA.BottomParseItem(this.begunBehover[bKey], bKey, this.ppcallBehov, window.begun_extra_id));
    }
    tpl = this.str_replace('#BehoverBunnersInner#', BehoverInner, tpl);
    tpl = this.str_replace("[\t]{1,}", '', tpl);
    bSpan.innerHTML = tpl;
}

//create tumbls
BegunA.ShowBottomStub = function() {
    if (typeof(BegunA.begunStubs)=="undefined") {
        return '';
    }
    if (typeof(BegunA.begunStubs[0])=="undefined") {
        return '';
    }
    if (typeof(BegunA.begunStubs[0]['href'])=="undefined") {
        return '';
    }
    if (typeof(BegunA.begunStubs[0]['text'])=="undefined") {
        return '';
    }
    return '<a href="'+BegunA.begunStubs[0]['href']+'" class="text" target="_blank">'+BegunA.begunStubs[0]['text']+'</a>';
}

//create search banner
BegunA.ShowSearchBanner = function(bann) {
    var tpl                 = BegunA.tpls['SearchBanner'];
    var TopBunnersInner     = '';
    TopBunnersInner+=BegunA.ParseSearchBanner(bann);
    if ( TopBunnersInner ) {
    	
        tpl = BegunA.str_replace('#ShowSearchBanner#', TopBunnersInner, tpl);
        tpl = BegunA.str_replace("[\t]{1,}", '', tpl);

        bannerPosition = BegunA.maxImageBannerPosition();
		if(bann.price && bann.price < 28.57142) {
			bannerPosition = BegunA.BottomB.length;
		}
        tpl = BegunA.str_replace('#ShowSearchBanner#',tpl, BegunA.tpls['SearchBannerForButtomGear']);
        var src = document.getElementById('begunSpanBottom').innerHTML;
        window.setTimeout(function(){
            document.getElementById('begunSpanBottom').innerHTML = (typeof begunJsBannerString != 'undefined') ? BegunA.str_replace('<!--AdsNumber'+bannerPosition+'-->', BegunA.str_replace("#begunJsBannerString#", begunJsBannerString,tpl), src) : BegunA.str_replace('<!--AdsNumber'+bannerPosition+'-->',tpl,src); 
         }, 500); // timeout is FF3 hack
    }
}

BegunA.maxImageBannerPosition = function () {
    var wwidth=(window.innerWidth)?window.innerWidth:((document.documentElement)?document.documentElement.offsetWidth:null);
    var wheight=(window.innerHeight)?window.innerHeight:((document.documentElement)?document.documentElement.offsetHeight:null);
    var iRes = 0;
    if (wheight < 700) iRes = 1;
    else if (wheight < 820) iRes = 2;
    else if (wheight < 900) iRes = 3;
    else if (wheight < 1000) iRes = 4;
    if (BegunA.BottomB.length < iRes)
        iRes = BegunA.BottomB.length;
    return iRes;
}

BegunA.loadSearchBanner = function(item) {
    if (document.getElementById("BegunBottomBunners") == null){
        var tpl = BegunA.tpls['BottomBunners'];
        tpl = BegunA.str_replace('#BottomBunnersInner#','', tpl);
	    tpl = BegunA.str_replace('#ShowBottomStub#','',tpl);
	    tpl = BegunA.str_replace("[\t]{1,}", '', tpl);
	    var bspanb = document.getElementById('begunSpanBottom');
        if (typeof(bspanb) == 'undefined' || !bspanb) {
            return false;
        }
        bspanb.innerHTML = tpl;
    }
    window.begunBannerClickURL = item.url ? item.url : '';
    BegunA.ShowSearchBanner(item);
    return true;
}

//parse search banner
BegunA.ParseSearchBanner = function(item) {
    var swf_graph = ( "swf" == item.mime ) || ( "application/x-shockwave-flash" == item.mime );
    var js_graph = ( "js" == item.mime ) || ( "application/x-javascript" == item.mime );
    var other_graph = (typeof(item.mime) != 'undefined' && item.mime != '' );
    if ( swf_graph ) {
        tpl = BegunA.tpls['SearchBannerSWF'];
    }
    else if (js_graph) {
        tpl = BegunA.tpls['SearchBannerJScript'];
    }
    else if (other_graph) {
        tpl = BegunA.tpls['SearchBannerIMG'];
    }
    window.begunBannerClickURL = item.url;
    tpl = BegunA.str_replace('#HREF#', item.url, tpl);
    tpl = BegunA.str_replace('#SOURCE#', item.source, tpl);
    if (js_graph){
    	document.write(tpl);
    	tpl = BegunA.tpls['SearchBannerJS'];
    }
    return tpl;
}

//called from htmlbanner feed
function begunDraw() {
    if (document.getElementById('begunSpanSearch') ) {
        BegunA.ShowSearchBanner( BegunA.begunGraphBanners );
    }
}

//called text banner
function begunAutoRun() {
    BegunA.onDaemonLoaded(window.begunBanners, window.begunStubs, window.ppcallArray, window.begunParams, window.begunBehaviourBanners, window.ppcallBehaviourArray, window.ppcallFormArray);
	if (typeof BegunA.ppcallsShown !== "undefined") {
		var ppcallsLogInfo = "[{\"pad_id\":" + window.begun_auto_pad + ",\"blocks\":[{\"id\":null,\"ppcall_count\":" + BegunA.ppcallsShown + "}]}]";
		(new Image()).src = "http://autocontext.begun.ru/ppcallcounter?data=" + encodeURIComponent(ppcallsLogInfo) + "&log_ppcall_visibility=1";
	}
}

//called image banner
function begunImageRun() {
    BegunA.onDaemonLoadedImage(window.begunGraphBanners);
}

BegunA.Init();
