ads_domain = 'advert.ru.redtram.com';
var ads_delimiter = '';
var ak = '';
var id_area = '';

var ads_ref = document.URL;
var ads_patt1=new RegExp("(http://.*?/top100/.*?/)(.+?)(/.*?)");
var ads_patt2=new RegExp("(http://.*?/catalogue/.*?/)(.+?)(/.*?)");
var ads_patt3=new RegExp(/^(http|https):\/\/([\w\.\-:]*)([\/]?)$/ig);
var ads_page='*';

if (ads_ref.search('/top100/') != -1) {
	ads_page = ads_patt1.exec(ads_ref)[2];
} else if (ads_ref.search('/catalogue/') != -1) {
	ads_page = ads_patt2.exec(ads_ref)[2];
} else if (ads_patt3.test(ads_ref)) {
	ads_page = 'main';
}


function stringToRegExp(pattern, flags){
    return new RegExp(
        pattern.replace(/[\[\]\\{}()+*?.$^|]/g, function(m){return '\\'+m;}),
        flags);
}

function RedTramAdvert(id_area, code, page) {
	var advert_type = "banner";
	code = code.replace(/{ESC_SQ}/g,"'");
	if(code.indexOf("{TYPE_ADVERT=MARKET}") != -1) {
		code = code.replace(/{TYPE_ADVERT=MARKET}/g,"");
		advert_type = "market";
	}
	if(code.indexOf("{TYPE_ADVERT=NEWS}") != -1) {
		code = code.replace(/{TYPE_ADVERT=NEWS}/g,"");
		advert_type = "news";
		news_page = code.match(/\{PAGE=([\w\-\d]+)\}/)[1];
		code = code.replace(stringToRegExp('{PAGE=' + news_page + '}', 'g'),"");
		code = code.replace(/{CURRENT_PAGE_ALIAS}/g, page );
		code = code.replace(/{CURRENT_PAGE_NAME}/g, banner_categories[page] );
	}
	if(code.indexOf("{TYPE_ADVERT=CATLINK}") != -1) {
		code = code.replace(/{TYPE_ADVERT=CATLINK}/g,"");
		advert_type = "catlink";
		catlink_page = code.match(/\{PAGE=([\w\-\d]+)\}/)[1];
		code = code.replace(stringToRegExp('{PAGE=' + catlink_page + '}', 'g'),"");
		document.getElementById('catlinksBlock_' + page ).style.display = "block";
	}

	document.write(code);
}

function RedTramLinks(id_area, links_array, page, a, b)
{
	/* not in use now */


	for(i = 0; links_array["links"].length > i ; i++ )
	{
		page = links_array["links"][i]['page'];
		codes = links_array["links"][i]['codes'];
		if(!banner_categories[page])
		{
			banner_categories[page] = "";
		}
		if(!catlink_icons[page])
		{
			catlink_icons[page] = "default";
		}
		document.write('<img height="5" width="10" alt="" src="http://img.ru.redtram.com/px.gif"/><br/><table class="link-menu-box"><tr><td class="link-box-top"><img height="3" width="165" alt="" src="http://img.ru.redtram.com/px.gif"/></td></tr><tr><td class="link-box-s"><img height="12" width="12" class="absmiddle" alt="" src="http://img.ru.redtram.com/icons/ico_cat_' + catlink_icons[page] + '.gif"/>' + banner_categories[page] + '</td><tr><td class="link-box-bottom"><img height="1" width="165" alt="" src="http://img.ru.redtram.com/px.gif"/></td></tr><tr><td>');

		for(c = 0; codes.length > c ; c++ )
		{
			code = eval(codes[c]);
			if(code.indexOf("{TYPE_ADVERT=CATLINK}") != -1) {
				code = code.replace(/{TYPE_ADVERT=CATLINK}/g,"");
				advert_type = "catlink";
				catlink_page = code.match(/\{PAGE=([\w\-\d]+)\}/)[1];
				code = code.replace(stringToRegExp('{PAGE=' + catlink_page + '}', 'g'),"");
//				document.getElementById('catlinksBlock_' + page ).style.display = "block";
			}

			document.write(code);
		}
		document.write('</td></tr><tr><td><img height="3" width="165" alt="" src="http://img.ru.redtram.com/px.gif"/></td></tr></table>');

	}
}

function RedTramLink(id_area, code, page)
{
		if(code.indexOf("{TYPE_ADVERT=CATLINK}") != -1) {
			code = code.replace(/{TYPE_ADVERT=CATLINK}/g,"");
		}
		else
		{
			return false;
		}

		catlink_area_div = document.getElementById('catlinksBlock_' + id_area);
//		code = code.replace(/{ESC_SQ}/g,"'");
		catlink_category_alias = code.match(/\{CATEGORY_ALIAS=([\w\-\d]+)\}/)[1];
		code = code.replace(stringToRegExp('{CATEGORY_ALIAS=' + catlink_category_alias + '}', 'g'),"");
		catlink_category_name = code.match(/\{CATEGORY_NAME=([\w\-\d\s\і\ї\є\`\'А-Яа-я]+)\}/)[1];
		code = code.replace(stringToRegExp('{CATEGORY_NAME=' + catlink_category_name + '}', 'g'),"");
		
		/* if category block not exist - create it */
		if(!document.getElementById('catlinksCatBlock_' + catlink_category_alias) )
		{
			if(!catlink_icons[catlink_category_alias])
			{
				catlink_icons[catlink_category_alias] = "default";
			}

			category_block_html = '<img height="5" width="10" alt="" src="http://img.ru.redtram.com/px.gif"/><br/><table class="link-menu-box"><tr><td class="link-box-top"><img height="3" width="165" alt="" src="http://img.ru.redtram.com/px.gif"/></td></tr><tr><td class="link-box-s"><img height="12" width="12" class="absmiddle" alt="" src="http://img.ru.redtram.com/icons/ico_cat_' + catlink_icons[catlink_category_alias] + '.gif"/>' + catlink_category_name + '</td><tr><td class="link-box-bottom"><img height="1" width="165" alt="" src="http://img.ru.redtram.com/px.gif"/></td></tr><tr><td id="catlinksCatBlock_' + catlink_category_alias + '">' + 
									'</td></tr><tr><td><img height="3" width="165" alt="" src="http://img.ru.redtram.com/px.gif"/></td></tr></table>';
			catlink_area_div.innerHTML = catlink_area_div.innerHTML + category_block_html;
		}
		document.getElementById('catlinksCatBlock_' + catlink_category_alias).innerHTML = document.getElementById('catlinksCatBlock_' + catlink_category_alias).innerHTML + code;
}

/*functions to support iframe scheme */
	function adv_create_area(name, frame_style, area_id)
	{
		var adv_pattern=new RegExp("(http://.+?)\\?");
		if (document.URL.search('\\?') != -1)
		{
		    adv_url = adv_pattern.exec(document.URL)[1];
		}else{
			adv_url = document.URL;
		}
		document.write('<ifr'+'ame id="' + name + '" scrolling="no" src="' + adv_url + '?aea=' + area_id + '" frameborder="no" style="border: none; overflow: hidden; width: 100%; height: 1px; ' + frame_style + '"><'+'/ifr'+'ame>');
		adv_fix_area_height(name);
	}
	function adv_fix_area_height(name)
	{
		if( document.getElementById(name).contentWindow.adv_frame_loaded && parseInt(document.getElementById(name).style.height) < 2 )
		{
			document.getElementById(name).style.height = document.getElementById(name).contentWindow.document.getElementById('body').offsetHeight + "px";
			
			if( document.getElementById(name).contentWindow.document.getElementById('body').offsetHeight < 1 )
			{
				setTimeout("adv_fix_area_height('" + name + "');", 1000);
			}
		}else
		{
			if(parseInt(document.getElementById(name).style.height) < 2 ){
				setTimeout("adv_fix_area_height('" + name + "');", 1000);
			}
		}
	}

