if (typeof BegunVAC == 'undefined') {
	BegunVAC = function() {};
}

begun_auto_limit = window.begun_auto_limit || 5;
begun_left = window.begun_left || 0;
begun_width = window.begun_width || 420;
if (begun_width > 420) begun_width = 420;


begun_title_font_size = (typeof begun_title_font_size == "undefined") ? '9pt' : begun_title_font_size;
begun_text_font_size = (typeof begun_text_font_size == "undefined") ? '7pt' : begun_text_font_size;

BegunVAC.BannerInterval = 30000;
BegunVAC.HideInterval = 1500;

BegunVAC.AnimationInterval = 25;


BegunVAC.include = function(file) {
    document.write('<scr'+'ipt type="text/javascript" src="' + file + '"></scr'+'ipt>');
}

BegunVAC.loadJS = function() {
    var oContr = document.getElementsByTagName('head')[0];
    if (!oContr) {
        oContr = document.body;
        if (!oContr) {
            oContr = document;
        }
    }
	var oScript = document.createElement('script');
	oScript.type = 'text/javascript';
	oScript.src = begun_auto_url;
	oContr.appendChild(oScript);
}

BegunVAC.begunAppend = function(params) {
    for (var key in params)
        if (params[key] && params.hasOwnProperty && params.hasOwnProperty(key)) // always check for own property in for .. in ..
            begun_auto_url += '&' + key + '=' + escape(params[key]);
}

BegunVAC.getCSS = function getCSS() {
    var cssTpl = '\
        <style><!-- \
	        .begun_video_block {\
	            z-index:1px;\
	            background: url("http://autocontext.begun.ru/custom/img/bg.gif") right no-repeat;\
	            position: relative;\
	            top:0px;\
	            left:0px;\
	            width:300px;\
	            height:50px;\
	            color: #FFFFFF;\
	            filter:progid:DXImageTransform.Microsoft.Alpha(opacity=80);\
	            -moz-opacity: 0.8;\
	            -khtml-opacity: 0.8;\
	            opacity: 0.8;\
				line-height:normal;\
	        }\
	        .begun_video_title {\
	              filter:progid:DXImageTransform.Microsoft.Alpha(opacity=100);\
	              -moz-opacity: 1;\
	              -khtml-opacity: 1;\
	              opacity: 1;\
				text-align:center;\
				line-height: normal;\
	        }\
	        .begun_video_text { text-align:center; line-height: normal;}\
	        .begun_video_title a { color: #FFFFFF;font-size: '+begun_title_font_size+'; text-decoration: none; font-weight:bold;}\
	        .begun_video_title a:hover { color: #FFFFFF;font-size: '+begun_title_font_size+'; font-weight:bold;}\
	        .begun_video_text a { color: #FFFFFF; font-size: '+begun_text_font_size+';text-decoration: none;}\
	        .begun_video_text a:hover { color: #FFFFFF;font-size: '+begun_text_font_size+'; text-decoration:none;}\
	        .begun_promo { position: absolute; right: 3px; top: 3px;}\
	        .begin_banner_container { display: none; }\
			#begun_advs { height: 13px;	width: 23px; border: none; }\
        //--></style>';
    return cssTpl;
}


BegunVAC.getBannerTpl = function() {
	var bannerTpl = '\
	        <div class="begun_video_block" id="begun_video_block" style="width:0px;">\
	            <div class="begin_banner_container" id="begin_banner_container">\
	                <div class="begun_promo"><a href="http://begun.ru/advertiser/" target="_blank"><img width="23" hegiht="13" id="begun_advs" src="http://leaves.begun.ru/bgl.png"></a></div>\
	                <div class="begun_video_title" id="begun_video_title">Заголовок объявления</div>\
	                <div class="begun_video_text" id="begun_video_text">Текст объявления, Текст объявления, Текст объявления, Текст объявления</div>\
	            </div>\
	        </div>\
	';
	return bannerTpl;
}


BegunVAC.setBegunBanner = function(nPos){
    if (begunBanners[nPos][2]=='' && begunBanners[nPos][6]!='') {
		begunBanners[nPos][2] = begunBanners[nPos][6];
	}
    document.getElementById("begun_video_title").innerHTML = "<a href='"+begunBanners[nPos][2]+"' target='_blank'>"+begunBanners[nPos][0]+"</a>";
    document.getElementById("begun_video_text").innerHTML = "<a href='"+begunBanners[nPos][2]+"' target='_blank'>"+begunBanners[nPos][1]+"</a>";
}

BegunVAC.openBegunBanner = function () {
    window.oBannerAnTimer = window.setInterval(BegunVAC.incBannerWidth,BegunVAC.AnimationInterval);
}

BegunVAC.incBannerWidth = function () {
    oBanner = document.getElementById("begun_video_block");
    document.getElementById("begun_video_block").style.paddingLeft = "10px";
    document.getElementById("begun_video_block").style.paddingRight = "10px";
    if (oBanner.clientWidth < begun_width) {
        var nCurrentWidth = parseInt(oBanner.style.width);
        var nNewWidth = nCurrentWidth + 10;
        oBanner.style.width = nNewWidth +"px";
    } else {
        window.clearInterval(window.oBannerAnTimer);
        document.getElementById("begin_banner_container").style.display = "block";
		window.ShowTimer = window.setInterval(BegunVAC.closeBegunBanner, BegunVAC.BannerInterval);
    }
}
BegunVAC.closeBegunBanner = function() {
    window.oBannerCloseTimer = window.setInterval(BegunVAC.decBannerWidth, BegunVAC.AnimationInterval);
}
BegunVAC.decBannerWidth = function(){
	window.clearInterval(window.ShowTimer);
    document.getElementById("begin_banner_container").style.display = "none";
    document.getElementById("begun_video_block").style.padding = "0";

    oBanner = document.getElementById("begun_video_block");
    if (oBanner.clientWidth>0) {
        var nCurrentWidth = parseInt(oBanner.style.width);
        var nNewWidth = nCurrentWidth - 10;
        oBanner.style.width = nNewWidth +"px";
    } else {
        window.clearInterval(window.oBannerCloseTimer);
		window.setTimeout(BegunVAC.rotateBanner, BegunVAC.HideInterval);
    }
}

BegunVAC.CurrentIndex = 0;

BegunVAC.rotateBanner = function() {
	if (begunBanners[BegunVAC.CurrentIndex]) {
	    BegunVAC.setBegunBanner(BegunVAC.CurrentIndex);
	    BegunVAC.openBegunBanner();
	    BegunVAC.CurrentIndex++;
	}  else {
        BegunVAC.CurrentIndex = 0;
		BegunVAC.loadJS();
	}
}

BegunVAC.firstRun = true;

function begunAutoRun() {
	//создаем баннер
	if (BegunVAC.firstRun) {
	    document.write(BegunVAC.getCSS());
	    var oSpan = document.createElement("span");
	    oSpan.innerHTML = BegunVAC.getBannerTpl();
	    oBannerTpl = oSpan.getElementsByTagName('div')[0];
		var begunVideo = document.getElementById(begun_video_id);
		if (!begunVideo) {
			return;
		}
		begunVideo.appendChild(oBannerTpl);

        if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
			var oIMG = document.getElementById('begun_advs');
	        if (oIMG.tagName=='IMG') {
	            if (/\.png$/.test(oIMG.src)) {
	                src = oIMG.src;
	                oIMG.src = "http://autocontext.begun.ru/custom/img/blank.gif";
	            }
	        }
		    if (src) oIMG.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
		}

	    var oBanner = document.getElementById("begun_video_block");
	    oBanner.style.top = parseInt(begun_top) + "px";
		if ((typeof begun_horizontal_align != "undefined") && (begun_horizontal_align=="center") ) {
			oBanner.style.left = "auto";
			oBanner.style.margin = "auto";
			begunVideo.style.textAlign = "center";
		} else {
			oBanner.style.left = parseInt(begun_left) + "px";
		}
		if (typeof begun_height != "undefined"){
			oBanner.style.height = parseInt(begun_height)+"px";
		}
		BegunVAC.firstRun = false;
	}
	BegunVAC.rotateBanner();
}


begun_auto_url = (typeof begun_urls != 'undefined' && begun_urls && begun_urls.video_ac_daemon) ? begun_urls.video_ac_daemon : 'http://autocontext.begun.ru/context.jsp?'; 
var params = {
    'pad_id': window.begun_auto_pad,
    'lmt': Date.parse(document.lastModified) / 1000,
    'n': window.begun_auto_limit,
    'begun_utf8': window.begun_utf8,
    'begun_koi8': window.begun_koi8,
    'sense_mode': 'custom',
    'stopwords': window.stopwords || '',
    'ref': document.referrer,
    'real_refer': document.location
}
BegunVAC.begunAppend(params);

begun_auto_url = begun_auto_url.substring(0, 1524).replace(/%[0-9a-fA-F]?$/, '');
BegunVAC.include(begun_auto_url);
