var URL_DEJ_REKLAMU_PHP = 'http://www.bbclick.cz/intext/dej_reklamu.php';
var URL_NASTAVENI_PHP = 'http://www.bbclick.cz/intext/nastaveni.php';
var URL_VYBER_SLOVA_PHP = 'http://www.bbclick.cz/intext/vyber_slova.php';
var BBclick_Title_Color_Default = '00517C';
var BBclick_Text_Color_Default = '000000';
var BBclick_Background_Color_Default = 'E6E6E6';
var BBclick_Border_Color_Default = 'E6E6E6';
var BBclick_URL_Color_Default = '00517C';
var BBclick_inline_text_color_Default = '000000';
var BBclick_inline_text_style_Default = '100'; //underline-weight-italic

var BBclick_Title_Color, BBclick_Text_Color, BBclick_Background_Color, BBclick_Border_Color, BBclick_URL_Color;
BBclick_Title_Color = BBclick_IAB_Zone_Test_Color(BBclick_Title_Color);
BBclick_Text_Color = BBclick_IAB_Zone_Test_Color(BBclick_Text_Color);
BBclick_Background_Color = BBclick_IAB_Zone_Test_Color(BBclick_Background_Color);
BBclick_Border_Color = BBclick_IAB_Zone_Test_Color(BBclick_Border_Color);
BBclick_URL_Color = BBclick_IAB_Zone_Test_Color(BBclick_URL_Color);
var BBclick_Title_Color_Processed = (BBclick_Title_Color) ? BBclick_Title_Color : BBclick_Title_Color_Default;
var BBclick_Text_Color_Processed = (BBclick_Text_Color) ? BBclick_Text_Color : BBclick_Text_Color_Default;
var BBclick_Background_Color_Processed = (BBclick_Background_Color) ? BBclick_Background_Color : BBclick_Background_Color_Default;
var BBclick_Border_Color_Processed = (BBclick_Border_Color) ? BBclick_Border_Color : BBclick_Border_Color_Default;
var BBclick_URL_Color_Processed = (BBclick_URL_Color) ? BBclick_URL_Color : BBclick_URL_Color_Default;
var BBclick_inline_text_color_Processed = (BBclick_inline_text_color) ? BBclick_inline_text_color : BBclick_inline_text_color_Default;
var BBclick_inline_text_style_Processed = (BBclick_inline_text_style) ? BBclick_inline_text_style : BBclick_inline_text_style_Default;

function BBclick_IAB_Zone_Test_Color(color){
    if (typeof(color) != 'string') 
        return false;
    if (!color.match(/^[0-9A-Fa-f]{6}$/) && !color.match(/^[0-9A-Fa-f]{3}$/)) 
        return false;
    return color;
}

BBclick_Title_Color = '';
BBclick_Text_Color = '';
BBclick_Background_Color = '';
BBclick_Border_Color = '';
BBclick_URL_Color = '';

var BBclick_Referrer = null;
try {
    BBclick_Referrer = encodeURIComponent(top.location.href);
} 
catch (ex) {
    BBclick_Referrer = encodeURIComponent(document.referrer);
}

// for safari
if (BBclick_Referrer == 'undefined') {
    BBclick_Referrer = encodeURIComponent(document.referrer);
}
var BBCLICK;
if (!BBCLICK) {
	
    BBCLICK = {};
}

if (!BBCLICK.INTEXT) {
    BBCLICK.INTEXT = {};
    
    BBCLICK.INTEXT.keywords_to_inline = [];
    BBCLICK.INTEXT.keyword_to_id = {};
    BBCLICK.INTEXT.span_id = 'bbclickinline_' + Math.round(Math.random() * 999999999);
    BBCLICK.INTEXT.ad_background = 'http://www.bbclick.cz/images/inline.gif';
	//vytorim si zakladni vzhled odkazu:
    BBCLICK.INTEXT.inline_text_color = '#' + BBclick_inline_text_color_Processed;
	BBCLICK.INTEXT.inline_text_decoration = (BBclick_inline_text_style_Processed.charAt(0) == '1') ? 'underline' : 'none';
	BBCLICK.INTEXT.inline_text_weight = (BBclick_inline_text_style_Processed.charAt(1) == '1') ? 'bold' : 'normal';
	BBCLICK.INTEXT.inline_text_style = (BBclick_inline_text_style_Processed.charAt(2) == '1') ? 'italic' : 'normal';
	
	//obrazek do pozadi odkazu - ZRUSENO
    //BBCLICK.INTEXT.inline_underline = 'http://www.bbclick.cz/images/underline.gif';
    BBCLICK.INTEXT.ad_width = 325;
    BBCLICK.INTEXT.ad_height = 135;
    BBCLICK.INTEXT.supplier = 'www.bbclick.cz';
    BBCLICK.INTEXT.config = {
        'sid': BBclick_ad,
        'open_in_new_window': true,
        'hostname': 'www.bbclick.cz',
        'max_words_to_inline': 20,
        'max_char_for_keywords': 1500,
        'max_char_for_textnodes': 5000,
        'max_adserver_calls': 2,
        'min_char_per_keyword': 3
    };
    BBCLICK.INTEXT.time_limits = {};
    document.write('<span id="' + BBCLICK.INTEXT.span_id + '"></span>');
}
var ab_sUserAgent = navigator.userAgent;
var ab_fAppVersion = parseFloat(navigator.appVersion);
var ab_isKHTML = ab_sUserAgent.indexOf('KHTML') > -1 || ab_sUserAgent.indexOf('Konqueror') > -1 || ab_sUserAgent.indexOf('AppleWebKit') > -1;
var ab_isMoz = ab_sUserAgent.indexOf("Gecko") > -1 && !ab_isKHTML;
var ab_isMinMoz1 =false;
var ab_isMinMoz1_4 = false;
var ab_isMinMoz1_5 = false;
var ab_isOpera = ab_sUserAgent.indexOf('Opera') > -1;
var ab_isIE = ab_sUserAgent.indexOf('compatible') > -1 && ab_sUserAgent.indexOf('MSIE') > -1 && !ab_isOpera;
var ab_isMinIE4 = false;
var ab_isMinIE5 = false;
var ab_isMinIE5_5 = false;
var ab_isMinIE6 = false;

function ab_compareVersions(sVersion1, sVersion2){
    var aVersion1 = sVersion1.split('.');
    var aVersion2 = sVersion2.split('.');
    
    if (aVersion1.length > aVersion2.length) {
        for (var i = 0; i < aVersion1.length - aVersion2.length; i++) {
            aVersion2.push('0');
        }
    }
    else 
        if (aVersion1.length < aVersion2.length) {
            for (vari = 0; i < aVersion2.length - aVersion1.length; i++) {
                aVerison1.push('0');
            }
        }
    
    for (var i = 0; i < aVersion1.length; i++) {
        if (aVersion1[i] < aVersion2[i]) {
            return -1;
        }
        else 
            if (aVersion1[i] > aVersion2[i]) {
                return 1;
            }
    }
    return 0;
}

// this tests for the different versions of mozilla
if (ab_isMoz) {
    var ab_reMoz = new RegExp("rv:(\\d+\\.\\d+(?:\\.\\d+)?)");
    ab_reMoz.test(ab_sUserAgent);
    ab_isMinMoz1 = ab_compareVersions(RegExp["$1"], "1.0") >= 0;
    ab_isMinMoz1_4 = ab_compareVersions(RegExp["$1"], "1.4") >= 0;
    ab_isMinMoz1_5 = ab_compareVersions(RegExp["$1"], "1.5") >= 0;
}

//this tests for the different versions of ie
if (ab_isIE) {
    var ab_re_IE = new RegExp('MSIE (\\d+\\.\\d+)');
    ab_re_IE.test(ab_sUserAgent);
    var ab_fIEVersion = parseFloat(RegExp['$1']);
    
    // these are the minimal version number that will be accepted
    ab_isMinIE4 = ab_fIEVersion >= 4;
    ab_isMinIE5 = ab_fIEVersion >= 5;
    ab_isMinIE5_5 = ab_fIEVersion >= 5.5;
    ab_isMinIE6 = ab_fIEVersion >= 6.0;
}
// stopword list
var BBCLICK;
if (!BBCLICK) {
    BBCLICK = {};
}

if (!BBCLICK.stopword_list) {
    BBCLICK.stopword_list = {
        "aby": 1,
        "ale": 1,
        "kokoti": 1,
        "kokot": 1,
		"kurva": 1,
		"nebo": 1,
		"piča": 1,
        "porno": 1,
        "protoze": 1,
		"sex": 1
    };
}
BBCLICK.INTEXT.is_inlined = null;
BBCLICK.INTEXT.start_time = null;
BBCLICK.INTEXT.keywords_already_inlined = [];
BBCLICK.INTEXT.keywords_ids_already_inlined_str = "";
BBCLICK.INTEXT.text_nodes = [];
BBCLICK.INTEXT.bbclickinline_tags = [];
BBCLICK.INTEXT.tags_to_ignore = {
    'A': 1,
    'H1': 1,
    'H2': 1,
    'H3': 1,
    'H4': 1,
    'H5': 1,
    'H6': 1,
    'TH': 1,
	'EM': 1,
	'STRONG': 1,
    'SCRIPT': 1,
    'NOSCRIPT': 1,
    'SELECT': 1,
    'IFRAME': 1,
    'TITLE': 1,
    'TEXTAREA': 1,
    'DT': 1,
    'DL': 1,
    'DD': 1,
    'FIELDSET': 1,
    'LEGEND': 1,
    'ACRONYM': 1,
    'ADDRESS': 1,
    'LABEL': 1
}
BBCLICK.INTEXT.current_textnode = 0;
BBCLICK.INTEXT.unfiltered_keywords = [];
BBCLICK.INTEXT.filtered_keywords = {};
BBCLICK.INTEXT.filtered_keywords.ab_length = 0;
BBCLICK.INTEXT.ad_url = '';
BBCLICK.INTEXT.inline_ad_timeout_handle = 5000;
BBCLICK.INTEXT.keyword = null;
BBCLICK.INTEXT.div_keyword = null;
BBCLICK.INTEXT.display_ad = false;
BBCLICK.INTEXT.keyword_set_inlined = false;

BBCLICK.append_onload = function(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function(){
            oldonload();
            func();
        }
    }
}

BBCLICK.append_script = function(url, span_id){
    var span_to_append = document.getElementById(span_id);
    var ab_script = document.createElement('script');
    ab_script.src = url;
    ab_script.type = 'text/javascript';
    span_to_append.appendChild(ab_script);
}

BBCLICK.element_in_array = function(element, element_array){
    for (var k = 0; k < element_array.length; k++) 
        if (element_array[k] == element) 
            return true;
    return false;
}

BBCLICK.force_browser_cache_of_image = function(url){
    (new Image(10, 10)).src = url;
}

BBCLICK.find_Pos_X = function(obj){
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    else 
        if (obj.x) 
            curleft += obj.x;
    return curleft;
}

BBCLICK.find_Pos_Y = function(obj){
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    else 
        if (obj.y) 
            curtop += obj.y;
    return curtop;
}

BBCLICK.find_page_width = function(){
    var x;
    if (self.innerWidth) 
        x = self.innerWidth;
    else 
        if (document.documentElement.clientWidth && document.documentElement.clientWidth) 
            x = document.documentElement.clientWidth;
        else 
            if (document.body.clientWidth) 
                x = document.body.clientWidth;
    return x;
}

BBCLICK.find_page_height = function(){
    var y;
    if (self.innerHeight) 
        y = self.innerHeight;
    else 
        if (document.documentElement.clientHeight && document.documentElement.clientHeight) 
            y = document.documentElement.clientHeight;
        else 
            if (document.body.clientHeight) 
                y = document.body.clientHeight;
    return y;
}

BBCLICK.find_scroll_y = function(){
    var y;
    if (self.pageYOffset) 
        y = self.pageYOffset;
    else 
        if (document.documentElement && document.documentElement.scrollTop) 
            y = document.documentElement.scrollTop;
        else 
            if (document.body) 
                y = document.body.scrollTop;
    return y;
}

BBCLICK.INTEXT.inline_keyword = function(keyword, keyword_regex, node_array_index){
    BBCLICK.INTEXT.keywords_already_inlined.push(keyword);
    BBCLICK.INTEXT.keyword_set_inlined = true;
    
    // add keyword_id to keyword string
    var keyword_id_str = '' + BBCLICK.INTEXT.keyword_to_id[keyword];
    BBCLICK.INTEXT.config.max_char_for_keywords -= (keyword_id_str.length + 1);
    if (BBCLICK.INTEXT.keywords_ids_already_inlined_str.length > 0) {
        BBCLICK.INTEXT.keywords_ids_already_inlined_str += ',';
    }
    BBCLICK.INTEXT.keywords_ids_already_inlined_str += keyword_id_str;
    
    // inline the keyword
    var node_to_modify = BBCLICK.INTEXT.text_nodes[node_array_index];
    var broken_apart_text = null;
    var pre_text = null;
    var post_text = '';
    var inline_text = null;
    var inline_tag = null;
    var pre_broken_node_text = node_to_modify.data;
    var parent_node = node_to_modify.parentNode;
    
    //calculate the original keyword
    var start_id = node_to_modify.data.toLowerCase().indexOf(keyword.toLowerCase());
    var end_id = start_id + keyword.length;
    var orig_keyword = node_to_modify.data.slice(start_id, end_id);
    
    broken_apart_text = node_to_modify.data.split(keyword_regex);
    
    if (broken_apart_text.length == 0) {
        // this happens when the keyword is the only word in the node
        pre_text = document.createTextNode('');
        post_text = document.createTextNode('');
    }
    else 
        if (broken_apart_text.length == 1) {
            // happens when the keyword is either at the beginning or the end of the node
            if (pre_broken_node_text.toLowerCase().indexOf(keyword.toLowerCase()) == 0) {
                // word to inline is at the beginning of the node
                pre_text = '';
                post_text = broken_apart_text[0];
            }
            else {
                // word to inline is at the end of the node
                pre_text = broken_apart_text[0];
                post_text = '';
            }
            
            pre_text = document.createTextNode(pre_text);
            post_text = document.createTextNode(post_text);
        }
        else {
            if (broken_apart_text.length > 2) {
                for (var i = 1; i < (broken_apart_text.length - 1); i++) 
                    post_text += broken_apart_text[i] + keyword;
                post_text += broken_apart_text[(broken_apart_text.length - 1)];
            }
            else 
                post_text = broken_apart_text[1];
            
            pre_text = document.createTextNode(broken_apart_text[0]);
            post_text = document.createTextNode(post_text);
        }
    
    // create the inlined keyword tag
    if (!document.getElementById('BBclickInlineAd_' + keyword)) {
        inline_tag = document.createElement('a');
        inline_tag.target = (BBCLICK.INTEXT.config.open_in_new_window) ? '_blank' : '_top';
        inline_tag.display = "inline";
        inline_tag.id = 'BBclickInlineAd_' + keyword;
        inline_tag.keyword = keyword;
        inline_tag.name = 'BBclickInlineAd_' + keyword;
        inline_tag.style.cursor = 'pointer';
        
		inline_tag.style.color = BBCLICK.INTEXT.inline_text_color;
        inline_tag.style.textDecoration = BBCLICK.INTEXT.inline_text_decoration;
		inline_tag.style.fontWeight = BBCLICK.INTEXT.inline_text_weight;
		inline_tag.style.fontStyle = BBCLICK.INTEXT.inline_text_style;
		
        inline_tag.onmouseover = BBCLICK.INTEXT.prep_and_show_inline_ad;
        inline_tag.onmouseout = BBCLICK.INTEXT.hide_inline_ad_timer;
        inline_tag.style.background = "url('" + BBCLICK.INTEXT.inline_underline + "') bottom repeat-x";
        inline_tag.style.marginBottom = "-2px";
        inline_tag.style.paddingBottom = "2px";
        inline_tag.innerHTML = orig_keyword;
        parent_node.replaceChild(post_text, node_to_modify);
        parent_node.insertBefore(inline_tag, post_text);
        parent_node.insertBefore(pre_text, inline_tag);
        BBCLICK.INTEXT.text_nodes = [];
        BBCLICK.INTEXT.populate_text_nodes();
        
    }
}

BBCLICK.INTEXT.create_inline_div = function(){
    if (!document.getElementById('bbclick_inline_div')) {
    
        //outermost div
        var inline = document.createElement('ispan');
        inline.className = 'nobbclickinline';
        inline.id = 'bbclick_inline_div';
        inline.onmouseover = BBCLICK.INTEXT.reset_display_inline_ad;
        inline.onmouseout = BBCLICK.INTEXT.hide_inline_ad_timer;
        var isty = inline.style;
        isty.display = 'none';
        isty.position = 'absolute';
        isty.zIndex = '32767';
        isty.width = BBCLICK.INTEXT.ad_width + 'px';
        isty.height = BBCLICK.INTEXT.ad_height + 'px';
        isty.overflow = "hidden";
        isty.backgroundImage = 'url(' + BBCLICK.INTEXT.ad_background + ')';
        isty.backgroundPosition = 'top';
        isty.backgroundRepeat = 'no-repeat';
        isty.opacity = .95;
        isty.filter = 'alpha(opacity=95)';
        
        isty.textAlign = 'left';
        document.getElementsByTagName('body')[0].appendChild(inline);
        
        //create & append link for logo
        var linkBack = document.createElement('ispan');
        var lbsty = linkBack.style;
        lbsty.display = "inline";
        lbsty.whiteSpace = "nowrap";
        lbsty.position = "absolute";
        lbsty.top = "9px";
        lbsty.right = "14px";
        lbsty.hieght = "14px";
        lbsty.width = "40px";
        lbsty.cursor = "pointer";
        lbsty.fontSize = "14px";
        lbsty.lineHeight = "14px";
        linkBack.innerHTML = '&nbsp;';
        linkBack.onclick = function(){
            window.open('http://' + BBCLICK.INTEXT.supplier)
        }
        inline.appendChild(linkBack);
        
        //create & append link for 'About this advertisement'
        var about = document.createElement('ispan');
        var absty = about.style;
        absty.fontSize = "11px";
        absty.display = "inline";
        absty.fontWeight = "bold";
        absty.color = "#FFF";
        absty.position = "absolute";
        absty.fontFamily = "Arial,Helvetica,sans-serif";
        absty.top = "8px";
        absty.left = "10px";
        absty.cursor = "pointer";
        about.onclick = function(){
            //ZRUSENO
			//window.open('http://' + BBCLICK.INTEXT.supplier + '/mb/about_inline.php?sid=' + BBCLICK.INTEXT.config.sid, 'AboutInline', 'menubar=no,location=no,resizeable=no,scrollbars=no,status=no,left=100,top=100,height=300,width=300');
        }
        about.appendChild(document.createTextNode("Reklama BBclick"));
        inline.appendChild(about);
        
        //create & append wrapper (for padding purposes)
        var w = document.createElement('ispan');
        w.style.display = 'block';
        w.style.width = (BBCLICK.INTEXT.ad_width - 34) + 'px';
        inline.appendChild(w);
        
        //create & append anchor
        var a = document.createElement('a');
        a.id = 'bbclick-inline-target-url';
        var asty = a.style;
        asty.padding = '0';
        asty.margin = '0';
        asty.border = 'none';
        asty.color = "#000";
        asty.fontWeight = 'normal';
        asty.textDecoration = 'none';
        asty.background = 'transparent';
        asty.cursor = 'pointer';
        asty.position = 'relative';
        asty.top = '33px';
        asty.left = '24px';
        w.appendChild(a);
        
        //create & append ad_headline
        var head = document.createElement('ispan');
        head.id = "bbclick-inline-headline";
        var hsty = head.style;
        hsty.fontSize = "17px";
        hsty.fontFamily = "Arial,Helvetica,sans-serif";
        hsty.fontWeight = "bold";
        hsty.color = "#" + BBclick_Title_Color_Processed;
        hsty.textDecoration = "underline";
        hsty.whiteSpace = "nowrap";
        a.appendChild(head);
        a.appendChild(document.createElement('br'));
        
        //create and append ad_text
        var text = document.createElement('ispan');
        text.id = "bbclick-inline-adtext";
        var tsty = text.style;
        tsty.fontSize = "13px";
        tsty.lineHeight = "15px";
        tsty.fontFamily = "Verdana,Arial,Helvetica,sans-serif";
        a.appendChild(text);
        a.appendChild(document.createElement('br'));
        
        //create and append display url
        var url = document.createElement('ispan');
        url.id = "bbclick-inline-display-url";
        urlsty = url.style;
        urlsty.fontSize = "11px";
        urlsty.lineHeight = "18px";
        urlsty.fontFamily = "Verdana,Arial,Helvetica,sans-serif";
        urlsty.textDecoration = "underline";
        urlsty.color = "#008000";
        a.appendChild(url);
        
        //create and append "your ad here" link
        var yah = document.createElement('ispan');
        yah.id = "bbclick-inline-your-ad-here";
        var ysty = yah.style;
        ysty.display = "inline";
        ysty.position = "absolute";
        ysty.bottom = "13px";
        ysty.right = "15px";
        ysty.fontFamily = "Verdana,Arial,Helvetica,sans-serif";
        ysty.fontSize = "11px";
        ysty.cursor = "pointer";
        ysty.color = "#" + BBclick_URL_Color_Processed;
        ysty.textDecoration = 'underline';
        yah.onclick = function(){
            window.open("http://www.bbclick.cz/?opid=" + BBCLICK.INTEXT.config.sid);
        }
        yah.appendChild(document.createTextNode('Přidat reklamu'));
        inline.appendChild(yah);
    }
}

BBCLICK.INTEXT.prep_and_show_inline_ad = function(){
    BBCLICK.INTEXT.keyword = this.keyword;
    
    if (BBCLICK.INTEXT.div_keyword != BBCLICK.INTEXT.keyword) {
        BBCLICK.INTEXT.display_ad = true;
        var page_width = BBCLICK.find_page_width();
        var page_height = BBCLICK.find_page_height();
        var scroll_y = BBCLICK.find_scroll_y();
        var div_left = BBCLICK.find_Pos_X(this);
        var div_top = BBCLICK.find_Pos_Y(this);
        
        var bbclick_inline_div = document.getElementById('bbclick_inline_div');
        bbclick_inline_div.style.display = 'none';
        
        // find the x value
        if ((div_left + BBCLICK.INTEXT.ad_width) > page_width + 30) 
            bbclick_inline_div.style.left = (div_left - (div_left + BBCLICK.INTEXT.ad_width - page_width) - 30) + 'px';
        else 
            bbclick_inline_div.style.left = div_left + 'px';
        
        // find the y value
        if ((div_top - BBCLICK.INTEXT.ad_height) < 0) 
            bbclick_inline_div.style.top = (div_top + 20) + 'px';
        else 
            if ((div_top - BBCLICK.INTEXT.ad_height) < scroll_y) 
                bbclick_inline_div.style.top = (div_top + 20) + 'px';
            else 
                bbclick_inline_div.style.top = (div_top - BBCLICK.INTEXT.ad_height) + 'px';
        BBCLICK.INTEXT.display_inline_ad();
    }
    else 
        BBCLICK.INTEXT.reset_display_inline_ad();
}

BBCLICK.INTEXT.display_inline_ad = function(){
    if (BBCLICK.INTEXT.keyword != BBCLICK.INTEXT.div_keyword) {
        BBCLICK.INTEXT.div_keyword = BBCLICK.INTEXT.keyword;
        var bbclick_inline_div = document.getElementById('bbclick_inline_div');
        // reset the div inbetween keywords
        BBCLICK.INTEXT.populate_inline_div('Načítám...', '', '', '');
        BBCLICK.INTEXT.load_inline_ad();
        bbclick_inline_div.style.display = 'block';
    }
}

BBCLICK.INTEXT.load_inline_ad = function(){
    var query_string = '?sid=' + BBCLICK.INTEXT.config.sid + '&keyword=' + BBCLICK.INTEXT.keyword_to_id[BBCLICK.INTEXT.keyword];
    var url = URL_DEJ_REKLAMU_PHP + query_string + '&random=' + Math.round(Math.random() * 99999);
    BBCLICK.append_script(url, BBCLICK.INTEXT.span_id);
}

BBCLICK.INTEXT.populate_and_display_inline_div = function(ad_headline, ad_text, ad_url, display_url){
    BBCLICK.INTEXT.populate_inline_div(ad_headline, ad_text, ad_url, display_url);
    var keyword_span_id = 'BBclickInlineAd_' + BBCLICK.INTEXT.keyword;
    BBCLICK.INTEXT.ad_url = ad_url;
    document.getElementById(keyword_span_id).href = ad_url;
}

BBCLICK.INTEXT.populate_inline_div = function(ad_headline, ad_text, ad_url, display_url){
    var a = document.getElementById('bbclick-inline-target-url');
    a.href = ad_url;
    a.target = (BBCLICK.INTEXT.config.open_in_new_window) ? '_blank' : '_top';
    var head = document.getElementById('bbclick-inline-headline');
    head.innerHTML = unescape(decodeURIComponent(ad_headline));
    var text = document.getElementById('bbclick-inline-adtext');
    text.innerHTML = unescape(decodeURIComponent(ad_text));
    var url = document.getElementById('bbclick-inline-display-url');
    if (display_url && display_url != '') {
        url.innerHTML = unescape(decodeURIComponent(display_url));
        url.href = ad_url;
        url.target = (BBCLICK.INTEXT.config.open_in_new_window) ? '_blank' : '_top';
    }
    else {
        url.innerHTML = '';
        url.href = '';
        url.target = '';
    }
}

BBCLICK.INTEXT.hide_inline_ad_timer = function(){
    BBCLICK.INTEXT.display_ad = false;
    BBCLICK.INTEXT.inline_ad_timeout_handle = window.setTimeout(BBCLICK.INTEXT.hide_inline_ad, 1000);
}

BBCLICK.INTEXT.hide_inline_ad = function(){
    if (BBCLICK.INTEXT.display_ad == false) {
        var keyword_span_id = 'BBclickInlineAd_' + BBCLICK.INTEXT.keyword;
        document.getElementById(keyword_span_id).onclick = '';
        BBCLICK.INTEXT.div_keyword = null;
        document.getElementById('bbclick_inline_div').style.display = 'none';
    }
}

BBCLICK.INTEXT.reset_display_inline_ad = function(){
    BBCLICK.INTEXT.display_ad = true;
    window.clearTimeout(BBCLICK.INTEXT.inline_ad_timeout_handle);
}

BBCLICK.INTEXT.reset_text_nodes = function(){
    BBCLICK.INTEXT.text_nodes = "";
    BBCLICK.INTEXT.text_nodes = [];
}

BBCLICK.INTEXT.populate_text_nodes = function(){
    if (BBCLICK.INTEXT.text_nodes.length == 0) {
		if (BBclick_element_to_scan) {
			var nodes = getElementsByClass(BBclick_element_to_scan);
			for(var i = 0; nodes.length > i; i++) {
				BBCLICK.INTEXT.scan_nodes(nodes[i], true);	
			}
		}
        else {
			BBCLICK.INTEXT.scan_nodes(document.getElementsByTagName('body')[0], true);
		}
        if (BBCLICK.INTEXT.bbclickinline_tags.length > 0) {
            BBCLICK.INTEXT.reset_text_nodes();
            var length = BBCLICK.INTEXT.bbclickinline_tags.length;
            for (var i = 0; i < length; i++) 
                BBCLICK.INTEXT.scan_nodes(BBCLICK.INTEXT.bbclickinline_tags[i], false);
        }
    }
}

BBCLICK.INTEXT.scan_nodes = function(node, log_classname){
    // log the bbclickinline classes
    if (log_classname == true && BBCLICK.INTEXT.tags_to_ignore[node.nodeName.toUpperCase()] != 1) 
        if (node.className && node.className.toLowerCase() == 'bbclickinline') 
            BBCLICK.INTEXT.bbclickinline_tags.push(node);
    
    if (node.nodeType == 3) {
        // remove all empty text nodes
        if (node.data.replace(/\s+/g, '')) 
            BBCLICK.INTEXT.text_nodes.push(node);
    }
    else {
        for (var next_node = node.firstChild; next_node != null; next_node = next_node.nextSibling) {
            var scan_node = true;
            if (BBCLICK.INTEXT.tags_to_ignore[next_node.nodeName.toUpperCase()]!= null && BBCLICK.INTEXT.tags_to_ignore[next_node.nodeName.toUpperCase()] == 1) 
                scan_node = false;
            if (next_node.className && (next_node.className.toLowerCase()).indexOf('nobbclickinline') != -1) 
                scan_node = false;
            if (scan_node) 
                BBCLICK.INTEXT.scan_nodes(next_node, log_classname);
        }
    }
}

BBCLICK.INTEXT.find_keyword = function(keyword){
    var keyword_regex = new RegExp("\\b" + keyword + "\\b", 'i');
    for (var i = 0; i < BBCLICK.INTEXT.text_nodes.length; i++) {
        if (BBCLICK.INTEXT.text_nodes[i].data.search(keyword_regex) > -1) {
            BBCLICK.INTEXT.inline_keyword(keyword, keyword_regex, i);
            break;
        }
    }
}

BBCLICK.INTEXT.get_next_keyword = function(){
    var keyword = "";
    // remove any empty keyword sets until a non-empty set is found, or the array is empty
    while (BBCLICK.INTEXT.keywords_to_inline.length > 0 && BBCLICK.INTEXT.keywords_to_inline[0].length == 0) 
        BBCLICK.INTEXT.keywords_to_inline.shift();
    
    if (BBCLICK.INTEXT.keywords_to_inline.length > 0) 
        keyword = BBCLICK.INTEXT.keywords_to_inline[0][0];
    
    // remove keyword from keyword set
    BBCLICK.INTEXT.remove_keyword_from_list();
    return keyword;
}

BBCLICK.INTEXT.remove_keyword_from_list = function(){
    // keyword was found, remove the keyword set
    if (BBCLICK.INTEXT.keyword_set_inlined == true) {
        BBCLICK.INTEXT.keyword_set_inlined = false;
        BBCLICK.INTEXT.keywords_to_inline.shift();
    }
    else {
        // keyword was not found, remove keyword from list
        // if keyword is only keyword is set, remove set
        if (BBCLICK.INTEXT.keywords_to_inline[0].length == 1) {
            BBCLICK.INTEXT.keywords_to_inline.shift();
        }
        else {
            BBCLICK.INTEXT.keywords_to_inline[0].shift();
        }
    }
}

BBCLICK.INTEXT.find_and_inline_keywords = function(){
    while (BBCLICK.INTEXT.keywords_to_inline.length > 0 && BBCLICK.INTEXT.keywords_already_inlined.length < BBCLICK.INTEXT.config.max_words_to_inline) {
        var keyword_to_find = BBCLICK.INTEXT.get_next_keyword();
        if (keyword_to_find) {
            BBCLICK.INTEXT.find_keyword(keyword_to_find);
        }
    }
}

BBCLICK.INTEXT.check_scanning_status = function(){
    var continue_scan = true;
    if (BBCLICK.INTEXT.keywords_already_inlined.length >= BBCLICK.INTEXT.config.max_words_to_inline) 
        continue_scan = false; // already inlined enough words
    if (BBCLICK.INTEXT.text_nodes.length < 1) 
        continue_scan = false; // no nodes to scan/inline
    if (BBCLICK.INTEXT.text_nodes.length == BBCLICK.INTEXT.current_textnode) 
        continue_scan = false; // reached the last node already
    if (BBCLICK.INTEXT.unfiltered_keywords.length > 0 && BBCLICK.INTEXT.filtered_keywords.ab_length != 0) 
        continue_scan = false; // filtered_keyword_array is full
    return continue_scan;
}

BBCLICK.INTEXT.build_unfiltered_keyword_array = function(){
    if (BBCLICK.INTEXT.unfiltered_keywords.length == 0) {
        var unfiltered_keyword_str = BBCLICK.INTEXT.text_nodes[BBCLICK.INTEXT.current_textnode].data;
        if (unfiltered_keyword_str > BBCLICK.INTEXT.max_char_for_textnodes) 
            unfiltered_keyword_str = unfiltered_keyword_str.slice(0, BBCLICK.INTEXT.max_char_for_textnodes);
        unfiltered_keyword_str = unfiltered_keyword_str.replace(/\s+/g, ' ');
        BBCLICK.INTEXT.unfiltered_keywords = unfiltered_keyword_str.split(' ');
    }
}

BBCLICK.INTEXT.keyword_in_stopword_list = function(keyword){
    return (BBCLICK.stopword_list[keyword]) ? true : false;
}

BBCLICK.INTEXT.build_filtered_keyword_array = function(){
    while (BBCLICK.INTEXT.unfiltered_keywords.length > 0) {
        if (BBCLICK.INTEXT.filtered_keywords.ab_length + BBCLICK.INTEXT.unfiltered_keywords[0].length > BBCLICK.INTEXT.config.max_char_for_keywords) 
            break;
        
        var keep_keyword = true;
        var keyword = BBCLICK.INTEXT.unfiltered_keywords.shift();
        
        if (keyword.length < BBCLICK.INTEXT.config.min_char_per_keyword) 
            keep_keyword = false;
        else 
            if (keyword.match(/\W/)) 
                keep_keyword = false;
            else 
                if (BBCLICK.element_in_array(keyword, BBCLICK.INTEXT.keywords_already_inlined)) 
                    keep_keyword = false;
                else 
                    if (BBCLICK.INTEXT.keyword_in_stopword_list(keyword)) 
                        keep_keyword = false;
        
        if (keep_keyword == true) {
            if (!BBCLICK.INTEXT.filtered_keywords[keyword]) {
                BBCLICK.INTEXT.filtered_keywords.ab_length = BBCLICK.INTEXT.filtered_keywords.ab_length + keyword.length;
                BBCLICK.INTEXT.filtered_keywords[keyword] = 1;
            }
        }
    }
}

BBCLICK.INTEXT.update_current_textnode = function(){
    if (BBCLICK.INTEXT.unfiltered_keywords.length == 0) 
        BBCLICK.INTEXT.current_textnode = BBCLICK.INTEXT.current_textnode + 1;
}

BBCLICK.INTEXT.scan = function(){
    var continue_scanning = BBCLICK.INTEXT.check_scanning_status();
    
    while (continue_scanning == true) {
        BBCLICK.INTEXT.build_unfiltered_keyword_array();
        BBCLICK.INTEXT.build_filtered_keyword_array();
        BBCLICK.INTEXT.update_current_textnode();
        continue_scanning = BBCLICK.INTEXT.check_scanning_status();
    }
}

BBCLICK.INTEXT.converg_filtered_keywords_toString = function(){
    var str = "";
    for (keyword in BBCLICK.INTEXT.filtered_keywords) {
        if (keyword == 'ab_length') 
            continue;
        if (str) 
            str += ",";
        str += keyword;
    }
    return str;
}

BBCLICK.INTEXT.send_keyword = function(){
    if (BBCLICK.INTEXT.filtered_keywords.ab_length > 0 && BBCLICK.INTEXT.config.max_adserver_calls > 0) {
        BBCLICK.INTEXT.config.max_adserver_calls = BBCLICK.INTEXT.config.max_adserver_calls - 1;
        var appended_keyword_str = BBCLICK.INTEXT.converg_filtered_keywords_toString();
        BBCLICK.INTEXT.filtered_keywords = {};
        BBCLICK.INTEXT.filtered_keywords.ab_length = 0;
        var url = URL_VYBER_SLOVA_PHP + '?sid=' + BBCLICK.INTEXT.config.sid +
        '&keyword_ids=' +
        BBCLICK.INTEXT.keywords_ids_already_inlined_str +
        '&keywords=' +
        appended_keyword_str;
        BBCLICK.append_script(url, BBCLICK.INTEXT.span_id);
    }
}

BBCLICK.INTEXT.inline = function(){
    BBCLICK.INTEXT.create_inline_div();
    BBCLICK.INTEXT.populate_text_nodes();
    BBCLICK.INTEXT.find_and_inline_keywords();
    BBCLICK.INTEXT.scan();
    BBCLICK.INTEXT.send_keyword();
}

BBCLICK.INTEXT.get_url_based_keywords = function(){
    var url = URL_NASTAVENI_PHP + '?sid=' + BBCLICK.INTEXT.config.sid;
    BBCLICK.append_script(url, BBCLICK.INTEXT.span_id);
}

if (ab_isMinMoz1_5 || ab_isMinIE6) {
    if (!BBCLICK.INTEXT.is_inlined) {
        BBCLICK.INTEXT.is_inlined = true;
        // force all images into the browser cache
        BBCLICK.force_browser_cache_of_image(BBCLICK.INTEXT.ad_background);
        BBCLICK.append_onload(BBCLICK.INTEXT.get_url_based_keywords);
    }
}


//OTHER FUNCTIONS
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

