var VIDEOCLICK;

VideoclickRun();

function VideoclickRun() {
    if (!_rbn.v.ad || _rbn.v.ad.length == 0) {
        setTimeout(VideoclickRun, 100);
        return;
    }
    VIDEOCLICK = new Videoclick(_rbn.v.ad);
    if (_rbn != null && _rbn.v != null) {
        if (_rbn.v.inline_color != null) {
            VIDEOCLICK.w_style.color = _rbn.v.inline_color;
        }
        if (_rbn.v.inline_count_max != null) {
            VIDEOCLICK.params.inline_limit = _rbn.v.inline_count_max;
        }
    }
    VIDEOCLICK.run();
}

function fClick() {
    if (VIDEOCLICK.show_id > -1) {
        VIDEOCLICK.hideBanner(VIDEOCLICK.show_id);
    }
}

function fHitArea() {
    fClick();
}

function Videoclick(ads) {
    this.ads = ads;
    this.params = {
        'use_marked'   : false,
        'inline_limit' : 5,
        'info_url' : 'http://moto.ru/reklama/',
        'show_timeout' : 100,
        'hide_timeout' : 700
    };
    this.skip = { 
        'SCRIPT': 1,'NOSCRIPT': 1,'H1': 1,'H2': 1,'H3': 1,'H4': 1,'H5': 1,'H6': 1,'BIG': 1,'A': 1, 'TH': 1, 'DT': 1,'DL': 1,'DD': 1,'FIELDSET': 1,'TEXTAREA': 1,'SELECT': 1,'LEGEND': 1,'ACRONYM': 1,'ADDRESS': 1,'LABEL': 1
    }
    this.w_bounds = '(^|$|[^0-9a-zA-Zà-ÿÀ-ß_])';
    this.w_style = {
        'cursor'            : 'pointer',
        'color'             : '#009900',
        'textDecoration'    : 'none',
        'borderStyle'       : 'solid',
        'borderWidth'       : '0 0 1px 0'
    };
    this.inline_count = 0;
    this.showTimer = false;
    this.hideTime = false;
    this.show_id = -1;
    
    this.run = function() {
        var n = document.getElementsByTagName("body")[0];
        this.ads.sort(function(){return (Math.round(Math.random())-0.5);});
        this.prepareRegexp();
        this.scanBlocks(n);
        this.setEvents();
    }
    
    this.prepareRegexp = function() {
        //alert(this.ads.length);
        for (var i = 0; i < this.ads.length; i++) {
            this.ads[i].regexp = new RegExp(this.w_bounds + '(' + this.ads[i].keyword + ')' + this.w_bounds, 'ig');
        }
    }
    
    this.scanBlocks = function(n, b) {
        //alert("nodeType="+n.nodeType);
        if (n.nodeType == 3) { 
            if (this.params.use_marked && !b) {
                return;
            }
            if (n.data.replace(/\s+/g, "")) {
                this.scanWords(n);
            }
        } else {
            for (var i = n.firstChild; i != null; i = i.nextSibling) {
                //alert(this.inline_count+"-"+this.params.inline_limit);
                if (this.inline_count == this.params.inline_limit) {
                    break;
                }
                if (this.skip[i.nodeName.toUpperCase()] == 1) {
                    continue;
                }
                
                //alert(i.className);
                if (i.className) {
                    if (i.className.indexOf("_noreachbanner_") != -1) {
                        continue;
                    }
                    if (i.id && i.id.indexOf("y5_direct") != -1) {
                        continue;
                    }
                    if (i.className.indexOf("_reachbanner_") != -1) {
                        this.scanBlocks(i, true);
                        continue;
                    }
                }
                this.scanBlocks(i, b);
            }
        }
    }
    
    this.scanWords = function(n) {
        var i, r;
        //document.write("dl="+n+"\r\n<br>");
        for (i = 0; i < this.ads.length; i++) {
            if (this.ads[i].inlined) {
                continue;
            }
            r = this.ads[i].regexp.exec(n.data);
            //alert(r);
            //var patt = new RegExp(this.ads[i].keyword.toLowerCase());
            //var r = patt.exec(n.data.toLowerCase());
            //alert(n.data.toLowerCase()+"^"+i+" "+this.ads[i].keyword.toLowerCase()+" "+r+"<br>\r\n");

            if (r != null) {
                //this.ads[i].inlined = true;
                //alert(this.ads[i].keyword);
                //document.status(this.inline_count+" == "+this.params.inline_limit+" "+this.ads[i].keyword+"\r\n<br>");
                this.inlineWord(n, i, r.index + r[1].length, r[2].length);
                //alert(n);
                if (this.inline_count == this.params.inline_limit) {
                    break;
                }
            }
        }
    }
    
    this.inlineWord = function(n, i, b, l) {
        var pn = n.parentNode;
        //alert(pn+" "+i+" "+b+" "+l+"\r\n<br>");
        if (pn) {
            var wn = this.makeWordHTML(i, n.data.substr(b, l));
            var bn = b > 0 ? document.createTextNode(n.data.substr(0, b)) : document.createTextNode("");
            var an = (b + l) < n.data.length ? document.createTextNode(n.data.substr(b + l, n.data.length - (b + l))) : document.createTextNode("");
            //document.write(n.index);
            //pn.replace(wn,wn);
            pn.replaceChild(an, n);
            pn.insertBefore(wn, an);
            pn.insertBefore(bn, wn);


            this.inline_count++;
        }
    }
    
    this.makeWordHTML = function(i, w) {
        var n = document.createElement("A");
        n.appendChild(document.createTextNode(w));
        n.href = 'javascript://';
        for (var p in this.w_style) {
            n.style[p] = this.w_style[p];
        }
        this.ads[i].word = n;
        return n;
    }

    this.makeBannerHTML = function(i) {
        var o, u, n;
//alert(this.ads[i].video_url);
if(this.ads[i].video_url!="")
{
//        u = this.ads[i].flash_url + '?atype=1&clickTAG=' + escape(this.ads[i].click_url) + '&fileURL=' + escape(this.ads[i].video_url) + '&infoURL=' + escape(this.params.info_url);
        u = this.ads[i].flash_url + '?&video=' + escape(this.ads[i].video_url) + '&image=' + escape(this.ads[i].pic_url);
        for (var k in this.ads[i].flash_vars) {
    	    u += "&"+k+"="+this.ads[i].flash_vars[k];
        }
        o = '<object id="_vc_flash_' + i + '" width="' + this.ads[i].width + '"  height="' + this.ads[i].height + '" data="' + u + '" type="application/x-shockwave-flash">';
        o += '<param name="movie" value="' + u + '"/>';
        o += '<param name="quality" value="high"/>';
        o += '<param name="wmode" value="transparent"/>';
        o += '<param name="bgcolor" value="#ffdddd"/>';
        o += '<param name="autoplay" value="1"/>';
        o += '<param name="scale" value="exactfit"/>';
        o += '<param name="swliveconnect" value="true"/>';
        o += '<param name="allowscriptaccess" value="always"/>';
        o += '</object>';
        n = document.createElement("DIV");
        n.className = '_noreachbanner_';
        n.innerHTML = o;
        n.style.display = 'none';
        n.style.position = 'absolute';
        n.style.zIndex = 99999;
        n.style.width = this.ads[i].width + 'px';
        n.style.height = this.ads[i].height + 'px';
        return n;
}
else
{
        o='<a href="'+this.ads[i].click_url+'" target=_top><img src="'+this.ads[i].pic_url+'" border=0></a>';
        n = document.createElement("DIV");
        n.className = '_noreachbanner_';
        n.innerHTML = o;
        n.style.display = 'none';
        n.style.position = 'absolute';
        n.style.zIndex = 99999;
        n.style.width = this.ads[i].width + 'px';
        n.style.height = this.ads[i].height + 'px';
        return n;

}
    }
    
    this.showBanner = function(i) {
        this.createBanner(i);
        var b = this.ads[i].banner;
        if (b == null) {
            return;
        }
        var w = this.getPageWidth();
        var h = this.getPageHeight();
        var sy = this.getScrollY();
        var x = this.getPosX(this.ads[i].word);
        var y = this.getPosY(this.ads[i].word);
        var kw = this.ads[i].word.offsetWidth;
        var kh = this.ads[i].word.offsetHeight;
        var bx, by;
        var body = document.getElementsByTagName("body")[0];
        if (x < (w / 2)) {
            bx = x;
        } else {
            bx = x - this.ads[i].width + 30;
        }
        if ((y - sy) < (h / 2)) {
            by = y + kh + 3;
        } else {
            by = y - this.ads[i].height - 3;
        }
        b.style.top = by + 'px';
        b.style.left = bx + 'px';
        b.style.display = 'block';
        if (!this.ads[i].trace) {
            if (this.ads[i].trace_url) {
                this.cacheFile(this.ads[i].trace_url);
            }
            if (this.ads[i].trace_url2 && this.ads[i].trace_url2.length > 0) {
                this.cacheFile(this.ads[i].trace_url2);
            }
            this.ads[i].trace = true;
        }
        if (body) {
            body.appendChild(b);
        }
        this.show_id = i;
    }
    
    this.hideBanner = function(i) {
        var n = this.ads[i].banner;
        if (n) {
            var body = document.getElementsByTagName("body")[0];
            n.style.display = 'none';
            if (body) {
                body.removeChild(n);
            }
            this.show_id = -1;
        }
    }
    
    this.createBanner = function(i) {
        if (this.ads[i].word) {
            this.ads[i].banner = this.makeBannerHTML(i);
            this.ads[i].banner.onmouseover = new Function('VIDEOCLICK.eventBannerMouseOver('+i+')');
            this.ads[i].banner.onmouseout = new Function('VIDEOCLICK.eventBannerMouseOut('+i+')');
        }
    }
    
    this.setEvents = function() {
        for (var i = 0; i < this.ads.length; i++) {
            if (this.ads[i].word) {
                this.ads[i].word.onmouseover = new Function('VIDEOCLICK.eventWordMouseOver('+i+')');
                this.ads[i].word.onmouseout = new Function('VIDEOCLICK.eventWordMouseOut('+i+')');
            }
        }
    }
    
    this.eventWordMouseOver = function(i) {
        this.ads[i].word.style.borderWidth = '0 0 2px 0';
        if (VIDEOCLICK.hideTimer) {
            clearTimeout(VIDEOCLICK.hideTimer);
            VIDEOCLICK.hideTimer = false;
        }
        if (VIDEOCLICK.show_id > -1) {
            VIDEOCLICK.hideBanner(VIDEOCLICK.show_id);
        }
        VIDEOCLICK.showTimer = setTimeout('VIDEOCLICK.showBanner(' + i + ')', VIDEOCLICK.params.show_timeout);
    }

    this.eventWordMouseOut = function(i) {
        this.ads[i].word.style.borderWidth = this.w_style['borderWidth'];
        VIDEOCLICK.hideTimer = setTimeout('VIDEOCLICK.hideBanner(' + i + ')', VIDEOCLICK.params.hide_timeout);
    }

    this.eventBannerMouseOver = function(i) {
        if (VIDEOCLICK.hideTimer) {
            clearTimeout(VIDEOCLICK.hideTimer);
            VIDEOCLICK.hideTimer = false;
        }
    }

    this.eventBannerMouseOut = function(i) {
        VIDEOCLICK.hideTimer = setTimeout('VIDEOCLICK.hideBanner(' + i + ')', VIDEOCLICK.params.hide_timeout);
    }
    
    this.cacheFile = function(u) { 
        (new Image(10, 10)).src = u; 
    }
    
    this.getPosX = function(o) {
        var x = 0;
        if (o.offsetParent) {
            while (o) {
                x += o.offsetLeft;
                o = o.offsetParent;
            }
        } else {
            if (o.x) {
                x = o.x;
            }
        }
        return x;
    }
    
    this.getPosY = function(o) {
        var y = 0;
        if (o.offsetParent) {
            while (o) {
                y += o.offsetTop;
                o = o.offsetParent;
            }
        } else {
            if (o.y) {
                y = o.y;
            }
        }
        return y;
    }
    this.getPageWidth = function() {
        var x = 0;
        if (typeof(window.innerWidth) == 'number') {
            x = window.innerWidth;
        } else if (document.documentElement && document.documentElement.clientWidth) {
            x = document.documentElement.clientWidth;
        } else if (document.body && document.body.clientWidth) {
            x = document.body.clientWidth;
        }
        return x;
    }
    this.getPageHeight = function() {
        var y;
        if (typeof(window.innerHeight) == 'number') {
            y = window.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
            y = document.documentElement.clientHeight;
        } else if (document.body.clientHeight) {
            y = document.body.clientHeight;
        }
        return y;
    }
    this.getScrollX = function() {
        var x = 0;
        if (typeof(window.pageXOffset) == 'number') {
            x = window.pageXOffset;
        } else if (document.body && document.body.scrollLeft) {
            x = document.body.scrollLeft;
        } else if (document.documentElement && document.documentElement.scrollLeft) {
            x = document.documentElement.scrollLeft;
        }
        return x;
    }
    this.getScrollY = function() {
        var y = 0;
        if (typeof(window.pageYOffset) == 'number') {
            y = window.pageYOffset;
        } else if (document.body && document.body.scrollTop) {
            y = document.body.scrollTop;
        } else if (document.documentElement && document.documentElement.scrollTop) {
            y = document.documentElement.scrollTop;
        }
        return y;
    }    
}



